evt-clock 0.4.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3774995830bf251f1db6d3579490b50349ee2707
4
+ data.tar.gz: 881f4843ee93399302a4c8eb3d8aa09c9b85ffc4
5
+ SHA512:
6
+ metadata.gz: 05a468f10b8e34e7d10b49d6e9f8fe26a689ceeb33dc4231fbf97b16956fee2e550eb0abacf75949031c40a2237b83f7fa641cced1ad9f6b6ad0285ab053ff63
7
+ data.tar.gz: db2e35f806c8393b2e87f5c3a598e7ef391537727c24db0910500c68e3dfd14aca6d5e237295a123a3b2f288f322a044d1652e8bd4bed2fbc779e215d2592171
@@ -0,0 +1,11 @@
1
+ require 'time'
2
+ require 'tzinfo'
3
+ require 'ostruct'
4
+ require 'naught'
5
+
6
+ require 'clock/clock'
7
+ require 'clock/substitute'
8
+ require 'clock/utc'
9
+ require 'clock/local'
10
+ require 'clock/localized'
11
+ require 'clock/localized/timezone'
@@ -0,0 +1,141 @@
1
+ module Clock
2
+ class Error < RuntimeError; end
3
+
4
+ def self.included(cls)
5
+ cls.extend Now
6
+ cls.extend Canonize
7
+ cls.extend SystemTime
8
+ cls.extend ISO8601
9
+ cls.extend Parse
10
+ cls.extend ElapsedMilliseconds
11
+ cls.extend Timestamp
12
+ cls.extend Configure
13
+ end
14
+
15
+ def now(time=nil)
16
+ time || self.class.now(system_time: system_time)
17
+ end
18
+
19
+ def canonize(time)
20
+ self.class.canonize(time, system_time)
21
+ end
22
+
23
+ def system_time
24
+ self.class.system_time
25
+ end
26
+
27
+ def iso8601(time=nil, precision: nil)
28
+ time ||= now
29
+ self.class.iso8601 time, precision: precision, system_time: system_time
30
+ end
31
+
32
+ def parse(str)
33
+ time = self.class.parse(str, system_time: system_time)
34
+ end
35
+
36
+ def timestamp(time=nil)
37
+ self.class.timestamp time
38
+ end
39
+
40
+ def self.local(time)
41
+ time.getlocal
42
+ end
43
+
44
+ def self.localized(time, identifier)
45
+ clock = Localized.build identifier
46
+ clock.canonize time
47
+ end
48
+
49
+ def self.utc(time)
50
+ time.utc
51
+ end
52
+
53
+ def elapsed_milliseconds(start_time, end_time)
54
+ self.class.elapsed_milliseconds(start_time, end_time)
55
+ end
56
+
57
+ module Now
58
+ def now(time=nil, system_time: nil)
59
+ system_time ||= self.system_time
60
+ time ||= system_time.now
61
+ canonize(time, system_time)
62
+ end
63
+ end
64
+
65
+ module Canonize
66
+ def canonize(time, system_time)
67
+ time
68
+ end
69
+ end
70
+
71
+ module SystemTime
72
+ extend self
73
+ def system_time
74
+ Time
75
+ end
76
+ end
77
+
78
+ module ISO8601
79
+ extend self
80
+ def iso8601(time=nil, precision: nil, system_time: nil)
81
+ precision ||= self.precision
82
+ time ||= now(system_time: system_time)
83
+ time.iso8601(precision)
84
+ end
85
+
86
+ def precision
87
+ Defaults.precision
88
+ end
89
+
90
+ module Defaults
91
+ def self.precision
92
+ 3
93
+ end
94
+ end
95
+ end
96
+
97
+ module Parse
98
+ extend self
99
+ def parse(str, system_time: nil)
100
+ system_time ||= self.system_time
101
+ time = Time.parse str
102
+ canonize(time, system_time)
103
+ end
104
+ end
105
+
106
+ module ElapsedMilliseconds
107
+ extend self
108
+ def elapsed_milliseconds(start_time, end_time=nil, system_time: nil)
109
+ end_time ||= now(system_time: system_time)
110
+
111
+ start_time = parse(start_time) if start_time.is_a? String
112
+ end_time = parse(end_time) if end_time.is_a? String
113
+
114
+ ((end_time - start_time) * 1000).round
115
+ end
116
+ end
117
+
118
+ module Timestamp
119
+ extend self
120
+ def timestamp(time=nil, system_time: nil)
121
+ time ||= now(system_time: system_time)
122
+ time.to_f
123
+ end
124
+ end
125
+
126
+ module Configure
127
+ def configure(receiver)
128
+ instance = new
129
+ receiver.clock = instance
130
+ instance
131
+ end
132
+ end
133
+
134
+ extend Now
135
+ extend Canonize
136
+ extend SystemTime
137
+ extend ISO8601
138
+ extend Parse
139
+ extend ElapsedMilliseconds
140
+ extend Timestamp
141
+ end
@@ -0,0 +1,2 @@
1
+ require 'clock/controls/time'
2
+ require 'clock/controls/time/offset'
@@ -0,0 +1,28 @@
1
+ module Clock
2
+ module Controls
3
+ module Time
4
+ def self.example(time=nil, precision: nil)
5
+ time ||= Raw.example
6
+
7
+ ISO8601.example(time, precision: precision)
8
+ end
9
+
10
+ def self.reference
11
+ ::Time.utc 2000
12
+ end
13
+
14
+ module Raw
15
+ def self.example
16
+ Time.reference
17
+ end
18
+ end
19
+
20
+ module ISO8601
21
+ def self.example(time=nil, precision: nil)
22
+ time ||= Raw.example
23
+ UTC.iso8601(time, precision: precision)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,34 @@
1
+ module Clock
2
+ module Controls
3
+ module Time
4
+ module Offset
5
+ def self.example(amount=nil, precision: nil, time: nil)
6
+ offset_time = Raw.example amount, precision: precision, time: time
7
+
8
+ ISO8601.example offset_time, precision: precision
9
+ end
10
+
11
+ module Raw
12
+ def self.example(amount=nil, precision: nil, time: nil)
13
+ amount ||= 1
14
+ time ||= Time::Raw.example
15
+
16
+ scale = scale(precision: precision)
17
+
18
+ offset = amount * scale
19
+
20
+ time + offset
21
+ end
22
+
23
+ def self.scale(precision: nil)
24
+ precision ||= Clock::ISO8601::Defaults.precision
25
+
26
+ scale = 10 ** precision
27
+
28
+ Rational(1, scale)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ module Clock
2
+ class Local
3
+ include Clock
4
+
5
+ def self.canonize(time, *)
6
+ Clock.local(time)
7
+ end
8
+
9
+ module Substitute
10
+ def self.build
11
+ Clock::Substitute.new
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ module Clock
2
+ class Localized
3
+ include Clock
4
+
5
+ attr_reader :timezone
6
+
7
+ def initialize(timezone)
8
+ @timezone = timezone
9
+ end
10
+
11
+ def self.build(timezone_identifier=nil, utc_reference: nil)
12
+ timezone = Timezone.build timezone_identifier, utc_reference: utc_reference
13
+ new(timezone)
14
+ end
15
+
16
+ def self.configure(receiver, timezone_identifier=nil)
17
+ instance = build(timezone_identifier)
18
+ receiver.clock = instance
19
+ instance
20
+ end
21
+
22
+ def system_time
23
+ timezone
24
+ end
25
+
26
+ def self.canonize(time, system_time)
27
+ time = time.utc
28
+ system_time.utc_to_local time
29
+ end
30
+
31
+ def self.iso8601(time, precision: nil, system_time: nil)
32
+ time ||= now time, system_time: system_time
33
+ utc_time = time.utc
34
+ local_time = system_time.utc_to_local utc_time
35
+ local_time.iso8601 precision
36
+ end
37
+
38
+ module Defaults
39
+ def self.timezone_identifier(timezone_identifier)
40
+ env_identifier = ENV['TIMEZONE']
41
+
42
+ if env_identifier
43
+ timezone_identifier = env_identifier
44
+ end
45
+
46
+ unless timezone_identifier
47
+ timezone_identifier = 'America/Mexico_City'
48
+ end
49
+
50
+ timezone_identifier
51
+ end
52
+ end
53
+
54
+ module Substitute
55
+ def self.build
56
+ Clock::Substitute.new
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,60 @@
1
+ module Clock
2
+ class Localized
3
+ class Timezone
4
+ attr_reader :tzinfo_timezone
5
+ attr_reader :utc_reference
6
+
7
+ def initialize(tzinfo_timezone, utc_reference)
8
+ @tzinfo_timezone = tzinfo_timezone
9
+ @utc_reference = utc_reference
10
+ end
11
+
12
+ def self.build(timezone_identifier=nil, utc_reference: nil)
13
+ utc_reference ||= Time.now.utc
14
+
15
+ timezone_identifier = Defaults.timezone_identifier(timezone_identifier)
16
+ tzinfo_timezone = TZInfo::Timezone.get(timezone_identifier)
17
+
18
+ new tzinfo_timezone, utc_reference
19
+ end
20
+
21
+ def period
22
+ tzinfo_timezone.period_for_utc utc_reference
23
+ end
24
+
25
+ def now(tzinfo_time=nil)
26
+ tzinfo_time ||= tzinfo_timezone.now
27
+ convert tzinfo_time
28
+ end
29
+
30
+ def utc_to_local(utc_time)
31
+ tzinfo_time = tzinfo_timezone.utc_to_local utc_time
32
+ convert tzinfo_time
33
+ end
34
+
35
+ def convert(tzinfo_time)
36
+ seconds = tzinfo_time.sec + tzinfo_time.subsec
37
+
38
+ Time.new(
39
+ tzinfo_time.year,
40
+ tzinfo_time.month,
41
+ tzinfo_time.day,
42
+ tzinfo_time.hour,
43
+ tzinfo_time.min,
44
+ seconds,
45
+ offset
46
+ )
47
+ end
48
+
49
+ def offset
50
+ total_offset = period.offset.utc_total_offset
51
+
52
+ hours, seconds = total_offset.abs.divmod 3600
53
+ minutes = seconds / 60
54
+
55
+ sign = total_offset > 0 ? '+' : '-'
56
+ offset = "#{sign}#{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,31 @@
1
+ module Clock
2
+ class Substitute
3
+ include Clock
4
+
5
+ attr_writer :system_time
6
+
7
+ def system_time
8
+ @system_time ||= NullTime.build
9
+ end
10
+
11
+ def now=(val)
12
+ system_time = OpenStruct.new
13
+ system_time.now = val
14
+ self.system_time = system_time
15
+ system_time
16
+ end
17
+
18
+ def iso8601(*args)
19
+ if system_time.is_a? OpenStruct
20
+ return super(*args)
21
+ end
22
+ nil
23
+ end
24
+
25
+ class NullTime < Naught.build
26
+ def self.build
27
+ new
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ module Clock
2
+ class UTC
3
+ include Clock
4
+
5
+ def self.canonize(time, *)
6
+ Clock.utc(time)
7
+ end
8
+
9
+ def coerce(time)
10
+ self.class.shift(time)
11
+ end
12
+
13
+ def self.coerce(time)
14
+ if time.is_a? String
15
+ time = Time.parse(time)
16
+ end
17
+
18
+ offset = time.gmt_offset
19
+
20
+ utc_time = time.getutc
21
+
22
+ utc_time = utc_time + offset
23
+
24
+ now(utc_time)
25
+ end
26
+
27
+ module Substitute
28
+ def self.build
29
+ Clock::Substitute.new
30
+ end
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evt-clock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0.2
5
+ platform: ruby
6
+ authors:
7
+ - The Eventide Project
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tzinfo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: naught
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ntl-test_bench
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: " "
56
+ email: opensource@eventide-project.org
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/clock.rb
62
+ - lib/clock/clock.rb
63
+ - lib/clock/controls.rb
64
+ - lib/clock/controls/time.rb
65
+ - lib/clock/controls/time/offset.rb
66
+ - lib/clock/local.rb
67
+ - lib/clock/localized.rb
68
+ - lib/clock/localized/timezone.rb
69
+ - lib/clock/substitute.rb
70
+ - lib/clock/utc.rb
71
+ homepage: https://github.com/eventide-project/clock
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.3.3
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.5.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Clock interface with support for dependency configuration for real and null
95
+ object implementations
96
+ test_files: []