l43_time 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/l43/time/format_time.rb +78 -0
- data/lib/l43/time/to_time.rb +3 -8
- data/lib/l43/time/tools/map_ok.rb +16 -0
- data/lib/l43/time/version.rb +1 -1
- data/lib/l43/time.rb +2 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c9041781cb4167c91b0f711d24ebfaaaed3fcca6eb5ed8d8c9436f1914801e10
|
|
4
|
+
data.tar.gz: 0e29a1ac404e894bd7edf2921ca5c9436e5e7bf27d13ba8b18d26bcd2b59413a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: de3af7a6b133365b8dcc1fd0169bcab618e167ef9a6702a284e7d4280cf6059e3fa3f7aba21993452ea53e02a98b7a0d2f6237ab3d96f0e742fd0d82a249b467
|
|
7
|
+
data.tar.gz: be1d2b7bc136b6bd97335b2133ff9628625c8c99bb5d1369de27bdda344a155dc67cbe242d654571db372b562aa18de4916d5e1777b20b993e9deb65b0eaad36
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'tools/map_ok'
|
|
4
|
+
module L43
|
|
5
|
+
module Time
|
|
6
|
+
module FormatTime
|
|
7
|
+
include Tools::MapOk
|
|
8
|
+
|
|
9
|
+
module Formatter extend self
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def _date(time) = time.strftime("%Y%m%d")
|
|
13
|
+
def _iso(time) = time.iso8601[0..18]
|
|
14
|
+
def _long(time) = time.strftime("%Y%m%d%H%M%S")
|
|
15
|
+
def _time(time) = time.strftime("%H%M%S")
|
|
16
|
+
def _decimal_seconds(time) = time.to_i.to_s
|
|
17
|
+
def _decimal_milliseconds(time) = _format(time, factor: 1000, base: 10)
|
|
18
|
+
def _decimal_microseconds(time) = _format(time, factor: 1_000_000, base: 10)
|
|
19
|
+
|
|
20
|
+
def _hex_seconds(time) = time.to_i.to_s(16)
|
|
21
|
+
def _hex_milliseconds(time) = _format(time, factor: 1000, base: 16)
|
|
22
|
+
def _hex_microseconds(time) = _format(time, factor: 1_000_000, base: 16)
|
|
23
|
+
|
|
24
|
+
def _base36_seconds(time) = time.to_i.to_s(36)
|
|
25
|
+
def _base36_milliseconds(time) = _format(time, factor: 1000, base: 36)
|
|
26
|
+
def _base36_microseconds(time) = _format(time, factor: 1_000_000, base: 36)
|
|
27
|
+
|
|
28
|
+
def _format(time, factor:, base:) = (time.to_f * factor).to_i.to_s(base)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
LegalFormats = {
|
|
32
|
+
ds: :_decimal_seconds,
|
|
33
|
+
dms: :_decimal_milliseconds,
|
|
34
|
+
dmcs: :_decimal_microseconds,
|
|
35
|
+
dμs: :_decimal_microseconds,
|
|
36
|
+
|
|
37
|
+
xs: :_hex_seconds,
|
|
38
|
+
xms: :_hex_milliseconds,
|
|
39
|
+
xmcs: :_hex_microseconds,
|
|
40
|
+
xμs: :_hex_microseconds,
|
|
41
|
+
|
|
42
|
+
zs: :_base36_seconds,
|
|
43
|
+
zms: :_base36_milliseconds,
|
|
44
|
+
zmcs: :_base36_microseconds,
|
|
45
|
+
zμs: :_base36_microseconds,
|
|
46
|
+
|
|
47
|
+
date: :_date,
|
|
48
|
+
long: :_long,
|
|
49
|
+
iso: :_iso,
|
|
50
|
+
time: :_time,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
def format_time!(fmt, time: nil)
|
|
54
|
+
case format_time(fmt, time:)
|
|
55
|
+
in :ok, result
|
|
56
|
+
result
|
|
57
|
+
in :error, msg
|
|
58
|
+
raise BadFormat, msg
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def format_time(fmt, time: nil)
|
|
63
|
+
time ||= ::Time.now
|
|
64
|
+
case map_ok(exception: KeyError) {
|
|
65
|
+
Formatter.send LegalFormats.fetch(fmt.to_sym), time
|
|
66
|
+
}
|
|
67
|
+
in :error, _
|
|
68
|
+
[:error, ":#{fmt} is not in one of the supported formats: #{LegalFormats.keys.inspect}"]
|
|
69
|
+
in result
|
|
70
|
+
result
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
data/lib/l43/time/to_time.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'parser'
|
|
4
|
+
require_relative 'tools/map_ok'
|
|
4
5
|
module L43
|
|
5
6
|
module Time
|
|
6
7
|
module Duration
|
|
@@ -8,6 +9,8 @@ module L43
|
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
module ToTime extend self
|
|
12
|
+
include Tools::MapOk
|
|
13
|
+
|
|
11
14
|
T = ::Time
|
|
12
15
|
def str_to_time(str)
|
|
13
16
|
case str
|
|
@@ -66,19 +69,11 @@ module L43
|
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
def symbolic_time(str)
|
|
69
|
-
# TODO: Use &"sym...".to_sym
|
|
70
72
|
map_ok(exception: KeyError) {
|
|
71
73
|
send SymbolicTimes.fetch(str)
|
|
72
74
|
}
|
|
73
75
|
end
|
|
74
76
|
|
|
75
|
-
def map_ok(exception: RuntimeError, &blk)
|
|
76
|
-
[:ok, blk.()]
|
|
77
|
-
rescue exception => e
|
|
78
|
-
[:error, e.message]
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
|
|
82
77
|
end
|
|
83
78
|
end
|
|
84
79
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module L43
|
|
4
|
+
module Time
|
|
5
|
+
module Tools
|
|
6
|
+
module MapOk
|
|
7
|
+
def map_ok(exception: RuntimeError, &blk)
|
|
8
|
+
[:ok, blk.()]
|
|
9
|
+
rescue exception => e
|
|
10
|
+
[:error, e.message]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
data/lib/l43/time/version.rb
CHANGED
data/lib/l43/time.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'time'
|
|
4
4
|
|
|
5
5
|
require_relative 'time/delta'
|
|
6
|
+
require_relative 'time/format_time'
|
|
6
7
|
require_relative 'time/time_range'
|
|
7
8
|
require_relative 'time/to_time'
|
|
8
9
|
require_relative 'time/version'
|
|
@@ -10,6 +11,7 @@ module L43
|
|
|
10
11
|
module Time
|
|
11
12
|
|
|
12
13
|
extend Delta
|
|
14
|
+
include FormatTime
|
|
13
15
|
include ToTime
|
|
14
16
|
include TimeRange
|
|
15
17
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: l43_time
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robert Dober
|
|
@@ -65,10 +65,12 @@ files:
|
|
|
65
65
|
- lib/l43/time/consts.rb
|
|
66
66
|
- lib/l43/time/delta.rb
|
|
67
67
|
- lib/l43/time/errors.rb
|
|
68
|
+
- lib/l43/time/format_time.rb
|
|
68
69
|
- lib/l43/time/parser.rb
|
|
69
70
|
- lib/l43/time/range.rb
|
|
70
71
|
- lib/l43/time/time_range.rb
|
|
71
72
|
- lib/l43/time/to_time.rb
|
|
73
|
+
- lib/l43/time/tools/map_ok.rb
|
|
72
74
|
- lib/l43/time/tools/scanner.rb
|
|
73
75
|
- lib/l43/time/ts.rb
|
|
74
76
|
- lib/l43/time/version.rb
|