units-time 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd753e92ae05a7b8124067b1530096616392cb33
4
- data.tar.gz: 16ded8e6519cdc289e8ea6f452a7e985571ac602
3
+ metadata.gz: 76fe6741c76332996673e18c6d299f1fa85dc014
4
+ data.tar.gz: b7c77366854058091dd6223196cfdb63950269b9
5
5
  SHA512:
6
- metadata.gz: d2e4132b9194102097f6ebcfbf1c78145f6467965c19df49d30b61808b5732a168c38e00af35a371af8357d34bc730568be7bf628c87f075d276851098d4ea65
7
- data.tar.gz: 0c3f079769f959f77f58913f79396a87bbbed7032b5fe435933f7ac6e3c3fc715fd91352412d3afca32362d56025f745d72052f7b0cdb75ffd8120650bce5113
6
+ metadata.gz: 074f4a4717ef00dd8023f4daa8dbe1760aa7caaf6dc056bf825cccd2c2c2dde311ca8d5e4761e888da96b8750f6d0c5f37644a2c4fb9ad2992f64c78f25d4ee4
7
+ data.tar.gz: df29e7ca34b97c40740e8e6fb450224c46f1a9cf1a6f92d6b4b483a6f15b2c65ebd6852f196e9f67c4ed64223910ac6ac0be69400447c87ead00d3ee24ec2cd8
data/Manifest.txt CHANGED
@@ -4,6 +4,8 @@ Manifest.txt
4
4
  README.md
5
5
  Rakefile
6
6
  lib/units-time.rb
7
+ lib/units-time/timedelta.rb
8
+ lib/units-time/timestamp.rb
7
9
  lib/units-time/version.rb
8
10
  test/helper.rb
9
11
  test/test_units_time.rb
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ class Timedelta
5
+
6
+ attr_reader :seconds
7
+
8
+ def initialize( seconds=0 )
9
+ @seconds = seconds
10
+ end
11
+
12
+
13
+ def ==( other )
14
+ if other.is_a?( self.class )
15
+ @seconds == other.seconds
16
+ else
17
+ false
18
+ end
19
+ end
20
+ alias_method :eql?, :==
21
+
22
+ def *( other )
23
+ if other.is_a?( Integer )
24
+ self.class.new( @seconds * other )
25
+ else
26
+ raise TypeError.new( "[Timedelta] mul(tiplication) - wrong type >#{other.inspect}< #{other.class.name} - integer number expected" )
27
+ end
28
+ end
29
+
30
+ def +( other )
31
+ if other.is_a?( self.class )
32
+ self.class.new( @seconds + other.seconds )
33
+ else
34
+ raise TypeError.new( "[Timedelta] add(ition) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
35
+ end
36
+ end
37
+
38
+ def -( other )
39
+ if other.is_a?( self.class )
40
+ self.class.new( @seconds - other.seconds )
41
+ else
42
+ raise TypeError.new( "[Timedelta] sub(straction) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
43
+ end
44
+ end
45
+
46
+
47
+ ### for details on coerce
48
+ ## see https://blog.dnsimple.com/2017/01/ruby-coercion-protocols-part-2/
49
+ ## and http://wiki.c2.com/?RubyCoerce
50
+ ##
51
+ ## note: just switch left and right will NOT work with subtraction
52
+ ## e.g. 2+3.seconds == 3.seconds+2 BUT
53
+ ## 2-3.seconds != 3.seconds-2 !!!!!
54
+
55
+ ## note: safe integer used for coerce dispatch
56
+ class SafeInteger
57
+ def initialize( value )
58
+ @value = value
59
+ end
60
+ ## it's safe to swap left and right for multiplication (it's communicative)
61
+ def *( other ) other.*( @value ); end
62
+ end
63
+
64
+ def coerce( other )
65
+ if other.is_a?( Integer )
66
+ [SafeInteger.new(other), self]
67
+ else
68
+ raise TypeError.new( "[Timedelta] coerce - wrong type >#{other.inspect}< #{other.class.name} - Integer number expected" )
69
+ end
70
+ end
71
+
72
+
73
+ include Comparable
74
+
75
+ def <=>( other )
76
+ if other.is_a?( self.class )
77
+ @seconds <=> other.seconds
78
+ else
79
+ raise TypeError.new( "[Timedelta] <=> - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
80
+ end
81
+ end
82
+
83
+
84
+ ## todo/fix: always freeze by default (timedelta is immutable) - why? why not?
85
+ def self.zero() @@zero ||= new(0).freeze; end
86
+
87
+ alias_method :to_i, :seconds ## add to integer conversion - why? why not?
88
+ alias_method :to_int, :seconds
89
+ ## note: same as:
90
+ ## def to_i() @seconds; end ## add to integer conversion - why? why not?
91
+ ## def to_int() @seconds; end
92
+ end # class Timedelta
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ class Timestamp
5
+
6
+ def self.now() new( Time.now.to_i ); end
7
+
8
+ attr_reader :seconds
9
+
10
+ def initialize( seconds=Time.now.to_i )
11
+ @seconds = seconds ## seconds since Jan 1st, 1970 (unix epoch time)
12
+ end
13
+
14
+ def ==( other )
15
+ if other.is_a?( self.class )
16
+ @seconds == other.seconds
17
+ else
18
+ false
19
+ end
20
+ end
21
+ alias_method :eql?, :==
22
+
23
+ def +( other )
24
+ if other.is_a?( Timedelta )
25
+ self.class.new( @seconds + other.seconds )
26
+ else
27
+ raise TypeError.new( "[Timestamp] add(ition) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
28
+ end
29
+ end
30
+
31
+ def -( other )
32
+ if other.is_a?( Timedelta )
33
+ self.class.new( @seconds - other.seconds )
34
+ else
35
+ raise TypeError.new( "[Timestamp] sub(straction) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
36
+ end
37
+ end
38
+
39
+
40
+ include Comparable
41
+
42
+ def <=>( other )
43
+ if other.is_a?( self.class )
44
+ @seconds <=> other.seconds
45
+ else
46
+ raise TypeError.new( "[Timestamp] <=> - wrong type >#{other.inspect}< #{other.class.name} - Timestamp expected" )
47
+ end
48
+ end
49
+
50
+
51
+ ## todo/fix: always freeze by default (timestamp is immutable) - why? why not?
52
+ def self.zero() @@zero ||= new(0).freeze; end
53
+
54
+ alias_method :to_i, :seconds ## add to integer conversion - why? why not?
55
+ alias_method :to_int, :seconds
56
+ end ## class Timestamp
@@ -4,7 +4,7 @@ module UnitsTime
4
4
 
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
- PATCH = 0
7
+ PATCH = 1
8
8
  VERSION = [MAJOR,MINOR,PATCH].join('.')
9
9
 
10
10
  def self.version
data/lib/units-time.rb CHANGED
@@ -7,6 +7,8 @@ require 'time'
7
7
 
8
8
  ## our own code
9
9
  require 'units-time/version' # note: let version always go first
10
+ require 'units-time/timestamp'
11
+ require 'units-time/timedelta'
10
12
 
11
13
 
12
14
 
@@ -18,155 +20,6 @@ require 'units-time/version' # note: let version always go first
18
20
  # - you CAN only compare (<=>) timestamp to timestamp but NOT timedelta !!
19
21
  # - you CAN only compare (<=>) timedelta to timedelta but NOT timestamp !!
20
22
 
21
- class Timestamp
22
-
23
- def self.now() new( Time.now.to_i ); end
24
-
25
- attr_reader :seconds
26
-
27
- def initialize( seconds=Time.now.to_i )
28
- @seconds = seconds ## seconds since Jan 1st, 1970 (unix epoch time)
29
- end
30
-
31
- def ==( other )
32
- if other.is_a?( self.class )
33
- @seconds == other.seconds
34
- else
35
- ## note: allow for now integer comparision too - why? why not?
36
- if other.is_a?( Integer )
37
- @seconds == other
38
- else
39
- false
40
- end
41
- end
42
- end
43
- alias_method :eql?, :==
44
-
45
- def +( other )
46
- if other.is_a?( Timedelta )
47
- self.class.new( @seconds + other.seconds )
48
- else
49
- ## fix/todo: use TypeError!!!!
50
- raise ArgumentError.new( "[Timestamp] add(ition) - wrong type #{other.inspect} - Timedelta expected" )
51
- end
52
- end
53
-
54
- def -( other )
55
- if other.is_a?( Timedelta )
56
- self.class.new( @seconds - other.seconds )
57
- else
58
- ## fix/todo: use TypeError!!!!
59
- raise ArgumentError.new( "[Timestamp] sub(straction) - wrong type #{other.inspect} - Timedelta expected" )
60
- end
61
- end
62
-
63
-
64
- include Comparable
65
-
66
- def <=>( other )
67
- if other.is_a?( self.class )
68
- @seconds <=> other.seconds
69
- else
70
- ## fix/todo: use TypeError!!!!
71
- raise ArgumentError.new( "[Timestamp] <=> - wrong type #{other.inspect} - Timestamp expected" )
72
- end
73
- end
74
-
75
-
76
- ## todo/fix: always freeze by default (timestamp is immutable) - why? why not?
77
- def self.zero() @@zero ||= new(0).freeze; end
78
-
79
- alias_method :to_i, :seconds ## add to integer conversion - why? why not?
80
- alias_method :to_int, :seconds
81
- end ## class Timestamp
82
-
83
-
84
-
85
- class Timedelta
86
-
87
- attr_reader :seconds
88
-
89
- def initialize( seconds=0 )
90
- @seconds = seconds
91
- end
92
-
93
-
94
- def ==( other )
95
- if other.is_a?( self.class )
96
- @seconds == other.seconds
97
- else
98
- ## note: allow for now integer comparision too - why? why not?
99
- if other.is_a?( Integer )
100
- @seconds == other
101
- else
102
- false
103
- end
104
- end
105
- end
106
- alias_method :eql?, :==
107
-
108
- def *( other )
109
- if other.is_a?( Integer )
110
- self.class.new( @seconds * other )
111
- else
112
- ## fix/todo: use TypeError!!!!
113
- raise ArgumentError.new( "[Timedelta] mul(tiplication) - wrong type #{other.inspect} - integer number expected" )
114
- end
115
- end
116
-
117
- def +( other )
118
- if other.is_a?( self.class )
119
- self.class.new( @seconds + other.seconds )
120
- else
121
- ## fix/todo: use TypeError!!!!
122
- raise ArgumentError.new( "[Timedelta] add(ition) - wrong type #{other.inspect} - Timedelta expected" )
123
- end
124
- end
125
-
126
- def -( other )
127
- if other.is_a?( self.class )
128
- self.class.new( @seconds - other.seconds )
129
- else
130
- ## fix/todo: use TypeError!!!!
131
- raise ArgumentError.new( "[Timedelta] sub(straction) - wrong type #{other.inspect} - Timedelta expected" )
132
- end
133
- end
134
-
135
-
136
- ### for details on coerce
137
- ## see https://blog.dnsimple.com/2017/01/ruby-coercion-protocols-part-2/
138
- def coerce( other )
139
- if other.is_a?( Integer )
140
- [self, other] ## allow communitative - switch left and right e.g. 365*24.hours to 24.hours*365
141
- else
142
- ## fix/todo: use TypeError!!!!
143
- raise ArgumentError.new( "[Timedelta] coerce - wrong type #{other.inspect} - Integer number expected" )
144
- end
145
- end
146
-
147
-
148
- include Comparable
149
-
150
- def <=>( other )
151
- if other.is_a?( self.class )
152
- @seconds <=> other.seconds
153
- else
154
- ## fix/todo: use TypeError!!!!
155
- raise ArgumentError.new( "[Timedelta] <=> - wrong type #{other.inspect} - Timedelta expected" )
156
- end
157
- end
158
-
159
-
160
- ## todo/fix: always freeze by default (timedelta is immutable) - why? why not?
161
- def self.zero() @@zero ||= new(0).freeze; end
162
-
163
- alias_method :to_i, :seconds ## add to integer conversion - why? why not?
164
- alias_method :to_int, :seconds
165
- ## note: same as:
166
- ## def to_i() @seconds; end ## add to integer conversion - why? why not?
167
- ## def to_int() @seconds; end
168
- end # class Timedelta
169
-
170
23
 
171
24
  ### "global" converter functions use like
172
25
  ## Timestamp(0) or Timedelta(0)
@@ -13,7 +13,7 @@ class TestUnitsTime < MiniTest::Test
13
13
  def test_timstamp
14
14
  now = Timestamp.now
15
15
 
16
- assert_equal now.to_i, now
16
+ assert_equal Timestamp.new( now.to_i ), now
17
17
 
18
18
  assert_equal now+31_536_000.secs, now+1.year
19
19
  assert_equal now+1_209_600.secs, now+1.fortnight
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: units-time
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -55,6 +55,8 @@ files:
55
55
  - README.md
56
56
  - Rakefile
57
57
  - lib/units-time.rb
58
+ - lib/units-time/timedelta.rb
59
+ - lib/units-time/timestamp.rb
58
60
  - lib/units-time/version.rb
59
61
  - test/helper.rb
60
62
  - test/test_units_time.rb