units-time 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/units-time.rb +14 -28
- data/lib/units-time/timedelta.rb +25 -0
- data/lib/units-time/timestamp.rb +1 -0
- data/lib/units-time/version.rb +2 -2
- data/test/test_units_time.rb +15 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f4434428e744835697e7787669aa746664a0273
|
4
|
+
data.tar.gz: 7e1d04423f3135839ebc5098b0eb9afc147bbeb4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54d9206a1d4ff9ccd803867fe7b00bae3e448addc93c57089d9437a36d6bc02ade415786e843fcb65d8c9b7110e3f625ae443d370328cff72d5e0ccbff1160cf
|
7
|
+
data.tar.gz: 852713aa82ad2ab0d9fa55c02b69b6ada32e4193097b57234bbc0b4819225cdbc86d68679e007cb7aea07267e42d4608010a52fba4b5ce53f945b3067cc6a545
|
data/lib/units-time.rb
CHANGED
@@ -21,37 +21,23 @@ require 'units-time/timedelta'
|
|
21
21
|
# - you CAN only compare (<=>) timedelta to timedelta but NOT timestamp !!
|
22
22
|
|
23
23
|
|
24
|
-
### "global" converter functions use like
|
25
|
-
## Timestamp(0)
|
26
|
-
|
27
|
-
def
|
28
|
-
|
24
|
+
### "global" Kernel converter functions use like
|
25
|
+
## Timestamp(0) or Timedelta(0)
|
26
|
+
module Kernel
|
27
|
+
def Timestamp(arg) Timestamp.new( arg ); end
|
28
|
+
def Timedelta(arg) Timedelta.new( arg ); end
|
29
|
+
end
|
29
30
|
|
30
31
|
|
31
32
|
module TimeUnits
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS # 7 days * 24 * 60 * 60
|
41
|
-
FORTNIGHT_IN_SECONDS = 14 * DAY_IN_SECONDS # 14 days * 24 * 60 * 60
|
42
|
-
|
43
|
-
#####
|
44
|
-
# note: for year use 365 days for now and NOT 365.25 (1/4)
|
45
|
-
# - why? why not? discuss
|
46
|
-
YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS # 365 days * 24 * 60 * 60
|
47
|
-
|
48
|
-
def second() Timedelta.new( self ); end
|
49
|
-
def minute() Timedelta.new( self * 60 ); end
|
50
|
-
def hour() Timedelta.new( self * HOUR_IN_SECONDS ); end
|
51
|
-
def day() Timedelta.new( self * DAY_IN_SECONDS ); end
|
52
|
-
def week() Timedelta.new( self * WEEK_IN_SECONDS ); end
|
53
|
-
def fortnight() Timedelta.new( self * FORTNIGHT_IN_SECONDS ); end
|
54
|
-
def year() Timedelta.new( self * YEAR_IN_SECONDS ); end
|
33
|
+
|
34
|
+
def second() Timedelta.seconds( self ); end
|
35
|
+
def minute() Timedelta.minutes( self ); end
|
36
|
+
def hour() Timedelta.hours( self ); end
|
37
|
+
def day() Timedelta.days( self ); end
|
38
|
+
def week() Timedelta.weeks( self ); end
|
39
|
+
def fortnight() Timedelta.weeks( self * 2 ); end
|
40
|
+
def year() Timedelta.years( self ); end
|
55
41
|
|
56
42
|
|
57
43
|
########################################################
|
data/lib/units-time/timedelta.rb
CHANGED
@@ -3,6 +3,29 @@
|
|
3
3
|
|
4
4
|
class Timedelta
|
5
5
|
|
6
|
+
######################################
|
7
|
+
## note: there's NO month (for now)!!!
|
8
|
+
## why? month might be 28,29,30,31 days
|
9
|
+
## use days e.g. 30.days or 31.days etc.
|
10
|
+
|
11
|
+
HOUR_IN_SECONDS = 60 * 60 # 60 minutes * 60 seconds
|
12
|
+
DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS # 24 hours * 60 * 60
|
13
|
+
WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS # 7 days * 24 * 60 * 60
|
14
|
+
## FORTNIGHT_IN_SECONDS = 14 * DAY_IN_SECONDS # 14 days * 24 * 60 * 60
|
15
|
+
|
16
|
+
#####
|
17
|
+
# note: for year use 365 days for now and NOT 365.25 (1/4)
|
18
|
+
# - why? why not? discuss
|
19
|
+
YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS # 365 days * 24 * 60 * 60
|
20
|
+
|
21
|
+
def self.seconds( seconds ) new( seconds ); end
|
22
|
+
def self.minutes( minutes ) new( minutes * 60 ); end
|
23
|
+
def self.hours( hours ) new( hours * HOUR_IN_SECONDS ); end
|
24
|
+
def self.days( days ) new( days * DAY_IN_SECONDS ); end
|
25
|
+
def self.weeks( weeks ) new( weeks * WEEK_IN_SECONDS ); end
|
26
|
+
def self.years( years ) new( years * YEAR_IN_SECONDS ); end
|
27
|
+
|
28
|
+
|
6
29
|
attr_reader :seconds
|
7
30
|
|
8
31
|
def initialize( seconds=0 )
|
@@ -81,6 +104,8 @@ class Timedelta
|
|
81
104
|
end
|
82
105
|
|
83
106
|
|
107
|
+
def zero?() self == self.class.zero; end ## note: compares values (e.g. 0==0) - not object_id (or frozen) etc.
|
108
|
+
|
84
109
|
## todo/fix: always freeze by default (timedelta is immutable) - why? why not?
|
85
110
|
def self.zero() @@zero ||= new(0).freeze; end
|
86
111
|
|
data/lib/units-time/timestamp.rb
CHANGED
@@ -51,6 +51,7 @@ class Timestamp
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
+
def zero?() self == self.class.zero; end ## note: compares values (e.g. 0==0) - not object_id (or frozen) etc.
|
54
55
|
|
55
56
|
## todo/fix: always freeze by default (timestamp is immutable) - why? why not?
|
56
57
|
def self.zero() @@zero ||= new(0).freeze; end
|
data/lib/units-time/version.rb
CHANGED
data/test/test_units_time.rb
CHANGED
@@ -112,4 +112,19 @@ def test_conv
|
|
112
112
|
assert_equal 0.secs, Timedelta(0)
|
113
113
|
end
|
114
114
|
|
115
|
+
def test_zero
|
116
|
+
assert_equal true, Timestamp(0).zero?
|
117
|
+
assert_equal true, Timedelta(0).zero?
|
118
|
+
|
119
|
+
assert_equal true, Timestamp.zero.zero?
|
120
|
+
assert_equal true, Timedelta.zero.zero?
|
121
|
+
|
122
|
+
assert_equal true, Timestamp.new(0).zero?
|
123
|
+
assert_equal true, Timedelta.new(0).zero?
|
124
|
+
|
125
|
+
assert_equal false, Timestamp.new(1).zero?
|
126
|
+
assert_equal false, Timestamp.new.zero?
|
127
|
+
assert_equal false, Timedelta.new(1).zero?
|
128
|
+
end
|
129
|
+
|
115
130
|
end # class TestUnitsTime
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: units-time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|