datet 0.0.1 → 0.0.2
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.
- data/VERSION +1 -1
- data/datet.gemspec +1 -1
- data/lib/datet.rb +128 -81
- data/spec/datet_spec.rb +17 -10
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/datet.gemspec
CHANGED
data/lib/datet.rb
CHANGED
@@ -44,74 +44,114 @@ class Datet
|
|
44
44
|
@@days_lcase[key[0, 3]] = val
|
45
45
|
end
|
46
46
|
|
47
|
+
#Thanks to ActiveSupport: http://rubydoc.info/docs/rails/2.3.8/ActiveSupport/CoreExtensions/Time/Calculations
|
48
|
+
@@days_in_months = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
47
49
|
|
48
50
|
#Initializes the object. Default is the current time. A time-object can be given.
|
49
|
-
def initialize(
|
50
|
-
if
|
51
|
-
self.update_from_time(
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
#Check larger hour than allowed.
|
80
|
-
if args[2] and args[2] >= 24
|
81
|
-
hours_left = args[2] + 1
|
82
|
-
args[2] = 0
|
83
|
-
end
|
84
|
-
|
85
|
-
#Check larger minute than allowed.
|
86
|
-
if args[3] and args[3] >= 60
|
87
|
-
mins_left = args[3] + 1
|
88
|
-
args[3] = 0
|
89
|
-
end
|
90
|
-
|
91
|
-
#Check larger secs than allowed.
|
92
|
-
if args[4] and args[4] >= 60
|
93
|
-
secs_left = args[4] + 1
|
94
|
-
args[4] = 0
|
95
|
-
end
|
96
|
-
|
97
|
-
#Check larger usecs than allowed.
|
98
|
-
if args[5] and args[5] >= 60
|
99
|
-
usecs_left = args[5] + 1
|
100
|
-
args[5] = 0
|
101
|
-
end
|
102
|
-
|
103
|
-
#Generate new stamp.
|
104
|
-
time = Time.new(*([time] | args))
|
105
|
-
self.update_from_time(time)
|
106
|
-
|
107
|
-
self.mins + mins_left if mins_left > 0
|
108
|
-
self.hours + hours_left if hours_left > 0
|
109
|
-
self.days + days_left if days_left > 0
|
110
|
-
self.months + months_left if months_left > 0
|
111
|
-
self.secs + secs_left if secs_left > 0
|
112
|
-
self.usecs + usecs_left if usecs_left > 0
|
51
|
+
def initialize(*args)
|
52
|
+
if args.length == 1 and args.first.is_a?(Time)
|
53
|
+
self.update_from_time(args.first)
|
54
|
+
return nil
|
55
|
+
elsif args.empty?
|
56
|
+
tnow = Time.now
|
57
|
+
args = [tnow.year, tnow.month, tnow.day, tnow.hour, tnow.min, tnow.sec, tnow.usec]
|
58
|
+
end
|
59
|
+
|
60
|
+
days_left = 0
|
61
|
+
months_left = 0
|
62
|
+
hours_left = 0
|
63
|
+
mins_left = 0
|
64
|
+
secs_left = 0
|
65
|
+
usecs_left = 0
|
66
|
+
|
67
|
+
#Check larger month the allowed.
|
68
|
+
if args[1] and args[1] > 12
|
69
|
+
months_left = args[1] - 12
|
70
|
+
args[1] = 12
|
71
|
+
end
|
72
|
+
|
73
|
+
#Check larger date than allowed.
|
74
|
+
if args[1]
|
75
|
+
dim = Datet.days_in_month(args[0], args[1])
|
76
|
+
if args[2] and args[2] > dim
|
77
|
+
days_left = args[2] - dim
|
78
|
+
args[2] = dim if days_left > 0
|
113
79
|
end
|
114
80
|
end
|
81
|
+
|
82
|
+
#Check larger hour than allowed.
|
83
|
+
if args[3] and args[3] >= 24
|
84
|
+
hours_left = args[3] + 1
|
85
|
+
args[3] = 0
|
86
|
+
end
|
87
|
+
|
88
|
+
#Check larger minute than allowed.
|
89
|
+
if args[4] and args[4] >= 60
|
90
|
+
mins_left = args[4] + 1
|
91
|
+
args[4] = 0
|
92
|
+
end
|
93
|
+
|
94
|
+
#Check larger secs than allowed.
|
95
|
+
if args[5] and args[5] >= 60
|
96
|
+
secs_left = args[5] + 1
|
97
|
+
args[5] = 0
|
98
|
+
end
|
99
|
+
|
100
|
+
#Check larger usecs than allowed.
|
101
|
+
if args[6] and args[6] >= 1000000
|
102
|
+
usecs_left = args[6] + 1
|
103
|
+
args[6] = 0
|
104
|
+
end
|
105
|
+
|
106
|
+
#Generate new stamp.
|
107
|
+
if args[0]
|
108
|
+
@t_year = args[0]
|
109
|
+
else
|
110
|
+
@t_year = Time.now.year
|
111
|
+
end
|
112
|
+
|
113
|
+
if args[1]
|
114
|
+
@t_month = args[1]
|
115
|
+
else
|
116
|
+
@t_month = 1
|
117
|
+
end
|
118
|
+
|
119
|
+
if args[2]
|
120
|
+
@t_day = args[2]
|
121
|
+
else
|
122
|
+
@t_day = 1
|
123
|
+
end
|
124
|
+
|
125
|
+
if args[3]
|
126
|
+
@t_hour = args[3]
|
127
|
+
else
|
128
|
+
@t_hour = 0
|
129
|
+
end
|
130
|
+
|
131
|
+
if args[4]
|
132
|
+
@t_min = args[4]
|
133
|
+
else
|
134
|
+
@t_min = 0
|
135
|
+
end
|
136
|
+
|
137
|
+
if args[5]
|
138
|
+
@t_sec = args[5]
|
139
|
+
else
|
140
|
+
@t_sec = 0
|
141
|
+
end
|
142
|
+
|
143
|
+
if args[6]
|
144
|
+
@t_usec = args[6]
|
145
|
+
else
|
146
|
+
@t_usec = 0
|
147
|
+
end
|
148
|
+
|
149
|
+
self.add_mins(mins_left) if mins_left > 0
|
150
|
+
self.add_hours(hours_left) if hours_left > 0
|
151
|
+
self.add_days(days_left) if days_left > 0
|
152
|
+
self.add_months(months_left) if months_left > 0
|
153
|
+
self.add_secs(secs_left) if secs_left > 0
|
154
|
+
self.add_usecs(usecs_left) if usecs_left > 0
|
115
155
|
end
|
116
156
|
|
117
157
|
#Updates the current variables to the given time.
|
@@ -129,7 +169,7 @@ class Datet
|
|
129
169
|
nil
|
130
170
|
end
|
131
171
|
|
132
|
-
#Returns a new Time-object based on the data of the Datet-object.
|
172
|
+
#Returns a new 'Time'-object based on the data of the 'Datet'-object.
|
133
173
|
def time
|
134
174
|
return Time.new(@t_year, @t_month, @t_day, @t_hour, @t_min, @t_sec)
|
135
175
|
end
|
@@ -166,19 +206,19 @@ class Datet
|
|
166
206
|
end
|
167
207
|
end
|
168
208
|
|
169
|
-
#Add a given amount of seconds to the object.
|
209
|
+
#Add a given amount of micro-seconds to the object.
|
170
210
|
def add_usecs(usecs = 1)
|
171
211
|
usecs = usecs.to_i
|
172
212
|
cur_usecs = @t_usec
|
173
213
|
next_usec = cur_usecs + usecs
|
174
214
|
|
175
|
-
if next_usec >=
|
215
|
+
if next_usec >= 1000000
|
176
216
|
@t_usec = 0
|
177
217
|
self.add_secs(1)
|
178
|
-
usecs_left = (usecs - 1) - (
|
218
|
+
usecs_left = (usecs - 1) - (1000000 - cur_usecs)
|
179
219
|
self.add_usecs(usecs_left) if usecs_left > 0
|
180
220
|
elsif next_usec < 0
|
181
|
-
@t_usec =
|
221
|
+
@t_usec = 99999
|
182
222
|
self.add_secs(-1)
|
183
223
|
usecs_left = usecs + cur_usecs + 1
|
184
224
|
self.add_usecs(usecs_left) if usecs_left > 0
|
@@ -199,7 +239,7 @@ class Datet
|
|
199
239
|
@t_sec = 0
|
200
240
|
self.add_mins(1)
|
201
241
|
secs_left = (secs - 1) - (60 - cur_secs)
|
202
|
-
|
242
|
+
self.add_secs(secs_left) if secs_left > 0
|
203
243
|
elsif next_sec < 0
|
204
244
|
@t_sec = 59
|
205
245
|
self.add_mins(-1)
|
@@ -367,11 +407,14 @@ class Datet
|
|
367
407
|
# datet = Datet.new
|
368
408
|
# print "There are #{datet.days_in_month} days in the current month."
|
369
409
|
def days_in_month
|
370
|
-
return
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
410
|
+
return Datet.days_in_month(@t_year, @t_month)
|
411
|
+
end
|
412
|
+
|
413
|
+
#Class-method for days in month.
|
414
|
+
def self.days_in_month(year, month)
|
415
|
+
raise "Invalid month: '#{month}'." if month.to_i <= 0
|
416
|
+
return 29 if month == 2 and Datet.gregorian_leap?(year)
|
417
|
+
return @@days_in_months[month]
|
375
418
|
end
|
376
419
|
|
377
420
|
#Returns the day in the week. Monday being 1 and sunday being 6.
|
@@ -795,15 +838,14 @@ class Datet
|
|
795
838
|
if match = timestr_t.match(/^(\d+)\/(\d+) (\d+)/)
|
796
839
|
#MySQL date format
|
797
840
|
timestr = timestr.gsub(match[0], "")
|
798
|
-
date = match[1]
|
799
|
-
month = match[2]
|
800
|
-
year = match[3]
|
841
|
+
date = match[1].to_i
|
842
|
+
month = match[2].to_i
|
843
|
+
year = match[3].to_i
|
801
844
|
|
802
845
|
if match = timestr.match(/\s*(\d+):(\d+)/)
|
803
846
|
#MySQL datetime format
|
804
|
-
|
805
|
-
|
806
|
-
minute = match[2]
|
847
|
+
hour = match[1].to_i
|
848
|
+
minute = match[2].to_i
|
807
849
|
end
|
808
850
|
|
809
851
|
return Datet.new(year, month, date, hour, minute)
|
@@ -956,6 +998,11 @@ class Datet
|
|
956
998
|
return self.time.to_s
|
957
999
|
end
|
958
1000
|
|
1001
|
+
#Returns arguments in an array.
|
1002
|
+
def to_a
|
1003
|
+
return [@t_year, @t_month, @t_day, @t_hour, @t_min, @t_sec, @t_usec]
|
1004
|
+
end
|
1005
|
+
|
959
1006
|
#Returns the HTTP-date that can be used in headers and such.
|
960
1007
|
#===Examples
|
961
1008
|
# datet.httpdate #=> "Mon, 17 Jun 1985 08:00:00 GMT"
|
data/spec/datet_spec.rb
CHANGED
@@ -2,10 +2,16 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Datet" do
|
4
4
|
it "should have the same 'to_i' as normal time" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
time = Time.now
|
6
|
+
datet = Datet.new
|
7
|
+
|
8
|
+
test_methods = [:year, :month, :day, :hour, :min, :sec, :to_i]
|
9
|
+
test_methods.each do |method|
|
10
|
+
tc = time.__send__(method)
|
11
|
+
dc = datet.__send__(method)
|
12
|
+
|
13
|
+
raise "Expected '#{method}'-calls to be the same but they werent: #{tc} vs #{dc} (now: #{Time.now.__send__(method)})" if tc != dc
|
14
|
+
end
|
9
15
|
end
|
10
16
|
|
11
17
|
it "should be able to make ago-strings" do
|
@@ -35,11 +41,12 @@ describe "Datet" do
|
|
35
41
|
|
36
42
|
#From "knjrbfw_spec.rb".
|
37
43
|
it "should be able to parse various date formats." do
|
38
|
-
date = Datet.in("Wed, 13 Jul 2011 16:08:51 GMT")
|
39
|
-
date = Datet.in("2011-07-09 00:00:00 UTC")
|
40
|
-
date = Datet.in("1985-06-17 01:00:00")
|
41
|
-
date = Datet.in("1985-06-17")
|
42
|
-
date = Datet.in("17/06 1985")
|
44
|
+
date = Datet.in("Wed, 13 Jul 2011 16:08:51 GMT").time
|
45
|
+
date = Datet.in("2011-07-09 00:00:00 UTC").time
|
46
|
+
date = Datet.in("1985-06-17 01:00:00").time
|
47
|
+
date = Datet.in("1985-06-17").time
|
48
|
+
date = Datet.in("17/06 1985").time
|
49
|
+
date = Datet.in("2012-06-06").time
|
43
50
|
|
44
51
|
raise "Couldnt register type 1 nullstamp." if !Datet.is_nullstamp?("0000-00-00")
|
45
52
|
raise "Couldnt register type 2 nullstamp." if !Datet.is_nullstamp?("0000-00-00 00:00:00")
|
@@ -98,7 +105,7 @@ describe "Datet" do
|
|
98
105
|
datet = Datet.new(1985, 6, 17, 28, 68, 68)
|
99
106
|
raise "Expected dbstr to be '1985-06-18 05:09:08' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:08"
|
100
107
|
|
101
|
-
datet = Datet.new(1985, 6, 17, 28, 68, 68,
|
108
|
+
datet = Datet.new(1985, 6, 17, 28, 68, 68, 1000008)
|
102
109
|
raise "Expected dbstr to be '1985-06-18 05:09:09' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:09"
|
103
110
|
end
|
104
111
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: datet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
104
|
requirements:
|
105
105
|
- - ">="
|
106
106
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
107
|
+
hash: 2787627911254732399
|
108
108
|
segments:
|
109
109
|
- 0
|
110
110
|
version: "0"
|