rufus-scheduler 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +8 -0
- data/CREDITS.txt +1 -0
- data/README.txt +7 -2
- data/lib/rufus/otime.rb +302 -302
- data/lib/rufus/scheduler.rb +1129 -1111
- data/test/cron_test.rb +81 -81
- data/test/cronline_test.rb +29 -23
- data/test/dev.rb +28 -0
- data/test/scheduler_0_test.rb +228 -228
- data/test/scheduler_1_test.rb +49 -54
- data/test/scheduler_2_test.rb +69 -69
- data/test/scheduler_3_test.rb +36 -36
- data/test/scheduler_4_test.rb +55 -55
- data/test/scheduler_5_test.rb +42 -42
- data/test/scheduler_6_test.rb +28 -26
- data/test/scheduler_name_test.rb +54 -0
- data/test/test.rb +2 -0
- data/test/time_0_test.rb +51 -51
- data/test/time_1_test.rb +44 -44
- metadata +5 -3
data/CHANGELOG.txt
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
= rufus-scheduler CHANGELOG.txt
|
3
3
|
|
4
4
|
|
5
|
+
== rufus-scheduler - 1.0.6 not yet released
|
6
|
+
|
7
|
+
- bug #20476 : no cron jobs on sundays (wday == 0). Fixed.
|
8
|
+
(thanks to Manfred Usselmann)
|
9
|
+
- todo #19619 : added :thread_name options to Scheduler.new
|
10
|
+
- source moved to http://github.com/jmettraux/rufus-scheduler
|
11
|
+
|
12
|
+
|
5
13
|
== rufus-scheduler - 1.0.5 released 2008/03/17
|
6
14
|
|
7
15
|
- bug #18363 : constrained precision to 0.0 < p <= 1.0 - s188
|
data/CREDITS.txt
CHANGED
data/README.txt
CHANGED
@@ -54,9 +54,9 @@ http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
|
|
54
54
|
|
55
55
|
== source
|
56
56
|
|
57
|
-
http://
|
57
|
+
http://github.com/jmettraux/rufus-scheduler
|
58
58
|
|
59
|
-
|
59
|
+
git clone git://github.com/jmettraux/rufus-scheduler.git
|
60
60
|
|
61
61
|
|
62
62
|
== author
|
@@ -65,6 +65,11 @@ John Mettraux, jmettraux@gmail.com
|
|
65
65
|
http://jmettraux.wordpress.com
|
66
66
|
|
67
67
|
|
68
|
+
== the rest of Rufus
|
69
|
+
|
70
|
+
http://rufus.rubyforge.org
|
71
|
+
|
72
|
+
|
68
73
|
== license
|
69
74
|
|
70
75
|
MIT
|
data/lib/rufus/otime.rb
CHANGED
@@ -8,10 +8,10 @@
|
|
8
8
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
9
|
# copies of the Software, and to permit persons to whom the Software is
|
10
10
|
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# The above copyright notice and this permission notice shall be included in
|
13
13
|
# all copies or substantial portions of the Software.
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
16
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
17
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -34,328 +34,328 @@ require 'date'
|
|
34
34
|
|
35
35
|
module Rufus
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
#
|
40
|
-
# Returns the current time as an ISO date string
|
41
|
-
#
|
42
|
-
def Rufus.now
|
43
|
-
|
44
|
-
to_iso8601_date(Time.new())
|
45
|
-
end
|
46
|
-
|
47
|
-
#
|
48
|
-
# As the name implies.
|
49
|
-
#
|
50
|
-
def Rufus.to_iso8601_date (date)
|
51
|
-
|
52
|
-
if date.kind_of? Float
|
53
|
-
date = to_datetime(Time.at(date))
|
54
|
-
elsif date.kind_of? Time
|
55
|
-
date = to_datetime(date)
|
56
|
-
elsif not date.kind_of? Date
|
57
|
-
date = DateTime.parse(date)
|
58
|
-
end
|
59
|
-
|
60
|
-
s = date.to_s # this is costly
|
61
|
-
s[10] = " "
|
62
|
-
|
63
|
-
s
|
64
|
-
end
|
65
|
-
|
66
|
-
#
|
67
|
-
# the old method we used to generate our ISO datetime strings
|
68
|
-
#
|
69
|
-
def Rufus.time_to_iso8601_date (time)
|
70
|
-
|
71
|
-
s = time.getutc().strftime(TIME_FORMAT)
|
72
|
-
o = time.utc_offset / 3600
|
73
|
-
o = o.to_s + "00"
|
74
|
-
o = "0" + o if o.length < 4
|
75
|
-
o = "+" + o unless o[0..1] == '-'
|
37
|
+
#TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
76
38
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
# Returns a Ruby time
|
82
|
-
#
|
83
|
-
def Rufus.to_ruby_time (iso_date)
|
84
|
-
|
85
|
-
DateTime.parse(iso_date)
|
86
|
-
end
|
87
|
-
|
88
|
-
#def Rufus.parse_date (date)
|
89
|
-
#end
|
39
|
+
#
|
40
|
+
# Returns the current time as an ISO date string
|
41
|
+
#
|
42
|
+
def Rufus.now
|
90
43
|
|
91
|
-
|
92
|
-
|
93
|
-
#
|
94
|
-
def Rufus.current_time_millis
|
44
|
+
to_iso8601_date(Time.new())
|
45
|
+
end
|
95
46
|
|
96
|
-
|
97
|
-
|
47
|
+
#
|
48
|
+
# As the name implies.
|
49
|
+
#
|
50
|
+
def Rufus.to_iso8601_date (date)
|
98
51
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
# d -> day
|
106
|
-
# h -> hour
|
107
|
-
# m -> minute
|
108
|
-
# s -> second
|
109
|
-
# M -> month
|
110
|
-
# y -> year
|
111
|
-
# 'nada' -> millisecond
|
112
|
-
#
|
113
|
-
# Some examples :
|
114
|
-
#
|
115
|
-
# Rufus.parse_time_string "500" # => 0.5
|
116
|
-
# Rufus.parse_time_string "1000" # => 1.0
|
117
|
-
# Rufus.parse_time_string "1h" # => 3600.0
|
118
|
-
# Rufus.parse_time_string "1h10s" # => 3610.0
|
119
|
-
# Rufus.parse_time_string "1w2d" # => 777600.0
|
120
|
-
#
|
121
|
-
def Rufus.parse_time_string (string)
|
122
|
-
|
123
|
-
string = string.strip
|
124
|
-
|
125
|
-
index = -1
|
126
|
-
result = 0.0
|
127
|
-
|
128
|
-
number = ""
|
129
|
-
|
130
|
-
loop do
|
131
|
-
|
132
|
-
index = index + 1
|
133
|
-
|
134
|
-
if index >= string.length
|
135
|
-
result = result + (Float(number) / 1000.0) if number.length > 0
|
136
|
-
break
|
137
|
-
end
|
138
|
-
|
139
|
-
c = string[index, 1]
|
140
|
-
|
141
|
-
#if is_digit?(c)
|
142
|
-
if (c >= "0" and c <= "9")
|
143
|
-
number = number + c
|
144
|
-
next
|
145
|
-
end
|
146
|
-
|
147
|
-
value = Integer(number)
|
148
|
-
number = ""
|
149
|
-
|
150
|
-
multiplier = DURATIONS[c]
|
151
|
-
|
152
|
-
raise "unknown time char '#{c}'" \
|
153
|
-
if not multiplier
|
154
|
-
|
155
|
-
result = result + (value * multiplier)
|
156
|
-
end
|
157
|
-
|
158
|
-
result
|
52
|
+
if date.kind_of? Float
|
53
|
+
date = to_datetime(Time.at(date))
|
54
|
+
elsif date.kind_of? Time
|
55
|
+
date = to_datetime(date)
|
56
|
+
elsif not date.kind_of? Date
|
57
|
+
date = DateTime.parse(date)
|
159
58
|
end
|
160
59
|
|
161
|
-
|
162
|
-
|
60
|
+
s = date.to_s # this is costly
|
61
|
+
s[10] = " "
|
62
|
+
|
63
|
+
s
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# the old method we used to generate our ISO datetime strings
|
68
|
+
#
|
69
|
+
def Rufus.time_to_iso8601_date (time)
|
70
|
+
|
71
|
+
s = time.getutc().strftime(TIME_FORMAT)
|
72
|
+
o = time.utc_offset / 3600
|
73
|
+
o = o.to_s + "00"
|
74
|
+
o = "0" + o if o.length < 4
|
75
|
+
o = "+" + o unless o[0..1] == '-'
|
76
|
+
|
77
|
+
s + " " + o.to_s
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Returns a Ruby time
|
82
|
+
#
|
83
|
+
def Rufus.to_ruby_time (iso_date)
|
84
|
+
|
85
|
+
DateTime.parse(iso_date)
|
86
|
+
end
|
87
|
+
|
88
|
+
#def Rufus.parse_date (date)
|
89
|
+
#end
|
90
|
+
|
91
|
+
#
|
92
|
+
# equivalent to java.lang.System.currentTimeMillis()
|
93
|
+
#
|
94
|
+
def Rufus.current_time_millis
|
95
|
+
|
96
|
+
(Time.new.to_f * 1000).to_i
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Turns a string like '1m10s' into a float like '70.0', more formally,
|
101
|
+
# turns a time duration expressed as a string into a Float instance
|
102
|
+
# (millisecond count).
|
103
|
+
#
|
104
|
+
# w -> week
|
105
|
+
# d -> day
|
106
|
+
# h -> hour
|
107
|
+
# m -> minute
|
108
|
+
# s -> second
|
109
|
+
# M -> month
|
110
|
+
# y -> year
|
111
|
+
# 'nada' -> millisecond
|
112
|
+
#
|
113
|
+
# Some examples :
|
114
|
+
#
|
115
|
+
# Rufus.parse_time_string "500" # => 0.5
|
116
|
+
# Rufus.parse_time_string "1000" # => 1.0
|
117
|
+
# Rufus.parse_time_string "1h" # => 3600.0
|
118
|
+
# Rufus.parse_time_string "1h10s" # => 3610.0
|
119
|
+
# Rufus.parse_time_string "1w2d" # => 777600.0
|
120
|
+
#
|
121
|
+
def Rufus.parse_time_string (string)
|
122
|
+
|
123
|
+
string = string.strip
|
124
|
+
|
125
|
+
index = -1
|
126
|
+
result = 0.0
|
127
|
+
|
128
|
+
number = ""
|
129
|
+
|
130
|
+
loop do
|
131
|
+
|
132
|
+
index = index + 1
|
133
|
+
|
134
|
+
if index >= string.length
|
135
|
+
result = result + (Float(number) / 1000.0) if number.length > 0
|
136
|
+
break
|
137
|
+
end
|
138
|
+
|
139
|
+
c = string[index, 1]
|
140
|
+
|
141
|
+
#if is_digit?(c)
|
142
|
+
if (c >= "0" and c <= "9")
|
143
|
+
number = number + c
|
144
|
+
next
|
145
|
+
end
|
146
|
+
|
147
|
+
value = Integer(number)
|
148
|
+
number = ""
|
149
|
+
|
150
|
+
multiplier = DURATIONS[c]
|
151
|
+
|
152
|
+
raise "unknown time char '#{c}'" \
|
153
|
+
if not multiplier
|
154
|
+
|
155
|
+
result = result + (value * multiplier)
|
163
156
|
end
|
164
157
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
158
|
+
result
|
159
|
+
end
|
160
|
+
|
161
|
+
class << self
|
162
|
+
alias_method :parse_duration_string, :parse_time_string
|
163
|
+
end
|
164
|
+
|
165
|
+
#
|
166
|
+
# Returns true if the character c is a digit
|
167
|
+
#
|
168
|
+
# (probably better served by a regex)
|
169
|
+
#
|
170
|
+
def Rufus.is_digit? (c)
|
171
|
+
|
172
|
+
return false if not c.kind_of?(String)
|
173
|
+
return false if c.length > 1
|
174
|
+
(c >= "0" and c <= "9")
|
175
|
+
end
|
176
|
+
|
177
|
+
#
|
178
|
+
# conversion methods between Date[Time] and Time
|
179
|
+
|
180
|
+
#
|
181
|
+
# Ruby Cookbook 1st edition p.111
|
182
|
+
# http://www.oreilly.com/catalog/rubyckbk/
|
183
|
+
# a must
|
184
|
+
#
|
185
|
+
|
186
|
+
#
|
187
|
+
# converts a Time instance to a DateTime one
|
188
|
+
#
|
189
|
+
def Rufus.to_datetime (time)
|
190
|
+
|
191
|
+
s = time.sec + Rational(time.usec, 10**6)
|
192
|
+
o = Rational(time.utc_offset, 3600 * 24)
|
193
|
+
|
194
|
+
begin
|
195
|
+
|
196
|
+
DateTime.new(
|
197
|
+
time.year,
|
198
|
+
time.month,
|
199
|
+
time.day,
|
200
|
+
time.hour,
|
201
|
+
time.min,
|
202
|
+
s,
|
203
|
+
o)
|
204
|
+
|
205
|
+
rescue Exception => e
|
206
|
+
|
207
|
+
DateTime.new(
|
208
|
+
time.year,
|
209
|
+
time.month,
|
210
|
+
time.day,
|
211
|
+
time.hour,
|
212
|
+
time.min,
|
213
|
+
time.sec,
|
214
|
+
time.utc_offset)
|
175
215
|
end
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
+
end
|
217
|
+
|
218
|
+
def Rufus.to_gm_time (dtime)
|
219
|
+
|
220
|
+
to_ttime(dtime.new_offset, :gm)
|
221
|
+
end
|
222
|
+
|
223
|
+
def Rufus.to_local_time (dtime)
|
224
|
+
|
225
|
+
to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
|
226
|
+
end
|
227
|
+
|
228
|
+
def Rufus.to_ttime (d, method)
|
229
|
+
|
230
|
+
usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i
|
231
|
+
Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec)
|
232
|
+
end
|
233
|
+
|
234
|
+
#
|
235
|
+
# Turns a number of seconds into a a time string
|
236
|
+
#
|
237
|
+
# Rufus.to_duration_string 0 # => '0s'
|
238
|
+
# Rufus.to_duration_string 60 # => '1m'
|
239
|
+
# Rufus.to_duration_string 3661 # => '1h1m1s'
|
240
|
+
# Rufus.to_duration_string 7 * 24 * 3600 # => '1w'
|
241
|
+
# Rufus.to_duration_string 30 * 24 * 3600 + 1 # => "4w2d1s"
|
242
|
+
#
|
243
|
+
# It goes from seconds to the year. Months are not counted (as they
|
244
|
+
# are of variable length). Weeks are counted.
|
245
|
+
#
|
246
|
+
# For 30 days months to be counted, the second parameter of this
|
247
|
+
# method can be set to true.
|
248
|
+
#
|
249
|
+
# Rufus.to_time_string 30 * 24 * 3600 + 1, true # => "1M1s"
|
250
|
+
#
|
251
|
+
# (to_time_string is an alias for to_duration_string)
|
252
|
+
#
|
253
|
+
# If a Float value is passed, milliseconds will be displayed without
|
254
|
+
# 'marker'
|
255
|
+
#
|
256
|
+
# Rufus.to_duration_string 0.051 # =>"51"
|
257
|
+
# Rufus.to_duration_string 7.051 # =>"7s51"
|
258
|
+
# Rufus.to_duration_string 0.120 + 30 * 24 * 3600 + 1 # =>"4w2d1s120"
|
259
|
+
#
|
260
|
+
# (this behaviour mirrors the one found for parse_time_string()).
|
261
|
+
#
|
262
|
+
# Options are :
|
263
|
+
#
|
264
|
+
# * :months, if set to true, months (M) of 30 days will be taken into
|
265
|
+
# account when building up the result
|
266
|
+
# * :drop_seconds, if set to true, seconds and milliseconds will be trimmed
|
267
|
+
# from the result
|
268
|
+
#
|
269
|
+
def Rufus.to_duration_string (seconds, options={})
|
270
|
+
|
271
|
+
return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0
|
272
|
+
|
273
|
+
h = to_duration_hash seconds, options
|
274
|
+
|
275
|
+
s = DU_KEYS.inject("") do |r, key|
|
276
|
+
count = h[key]
|
277
|
+
count = nil if count == 0
|
278
|
+
r << "#{count}#{key}" if count
|
279
|
+
r
|
216
280
|
end
|
217
281
|
|
218
|
-
|
219
|
-
|
220
|
-
|
282
|
+
ms = h[:ms]
|
283
|
+
s << ms.to_s if ms
|
284
|
+
|
285
|
+
s
|
286
|
+
end
|
287
|
+
|
288
|
+
class << self
|
289
|
+
alias_method :to_time_string, :to_duration_string
|
290
|
+
end
|
291
|
+
|
292
|
+
#
|
293
|
+
# Turns a number of seconds (integer or Float) into a hash like in :
|
294
|
+
#
|
295
|
+
# Rufus.to_duration_hash 0.051
|
296
|
+
# # => { :ms => "51" }
|
297
|
+
# Rufus.to_duration_hash 7.051
|
298
|
+
# # => { :s => 7, :ms => "51" }
|
299
|
+
# Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
|
300
|
+
# # => { :w => 4, :d => 2, :s => 1, :ms => "120" }
|
301
|
+
#
|
302
|
+
# This method is used by to_duration_string (to_time_string) behind
|
303
|
+
# the scene.
|
304
|
+
#
|
305
|
+
# Options are :
|
306
|
+
#
|
307
|
+
# * :months, if set to true, months (M) of 30 days will be taken into
|
308
|
+
# account when building up the result
|
309
|
+
# * :drop_seconds, if set to true, seconds and milliseconds will be trimmed
|
310
|
+
# from the result
|
311
|
+
#
|
312
|
+
def Rufus.to_duration_hash (seconds, options={})
|
313
|
+
|
314
|
+
h = {}
|
315
|
+
|
316
|
+
if seconds.is_a?(Float)
|
317
|
+
h[:ms] = (seconds % 1 * 1000).to_i
|
318
|
+
seconds = seconds.to_i
|
221
319
|
end
|
222
320
|
|
223
|
-
|
224
|
-
|
225
|
-
|
321
|
+
if options[:drop_seconds]
|
322
|
+
h.delete :ms
|
323
|
+
seconds = (seconds - seconds % 60)
|
226
324
|
end
|
227
325
|
|
228
|
-
|
326
|
+
durations = options[:months] ? DURATIONS2M : DURATIONS2
|
229
327
|
|
230
|
-
|
231
|
-
Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec)
|
232
|
-
end
|
328
|
+
durations.each do |key, duration|
|
233
329
|
|
234
|
-
|
235
|
-
|
236
|
-
#
|
237
|
-
# Rufus.to_duration_string 0 # => '0s'
|
238
|
-
# Rufus.to_duration_string 60 # => '1m'
|
239
|
-
# Rufus.to_duration_string 3661 # => '1h1m1s'
|
240
|
-
# Rufus.to_duration_string 7 * 24 * 3600 # => '1w'
|
241
|
-
# Rufus.to_duration_string 30 * 24 * 3600 + 1 # => "4w2d1s"
|
242
|
-
#
|
243
|
-
# It goes from seconds to the year. Months are not counted (as they
|
244
|
-
# are of variable length). Weeks are counted.
|
245
|
-
#
|
246
|
-
# For 30 days months to be counted, the second parameter of this
|
247
|
-
# method can be set to true.
|
248
|
-
#
|
249
|
-
# Rufus.to_time_string 30 * 24 * 3600 + 1, true # => "1M1s"
|
250
|
-
#
|
251
|
-
# (to_time_string is an alias for to_duration_string)
|
252
|
-
#
|
253
|
-
# If a Float value is passed, milliseconds will be displayed without
|
254
|
-
# 'marker'
|
255
|
-
#
|
256
|
-
# Rufus.to_duration_string 0.051 # =>"51"
|
257
|
-
# Rufus.to_duration_string 7.051 # =>"7s51"
|
258
|
-
# Rufus.to_duration_string 0.120 + 30 * 24 * 3600 + 1 # =>"4w2d1s120"
|
259
|
-
#
|
260
|
-
# (this behaviour mirrors the one found for parse_time_string()).
|
261
|
-
#
|
262
|
-
# Options are :
|
263
|
-
#
|
264
|
-
# * :months, if set to true, months (M) of 30 days will be taken into
|
265
|
-
# account when building up the result
|
266
|
-
# * :drop_seconds, if set to true, seconds and milliseconds will be trimmed
|
267
|
-
# from the result
|
268
|
-
#
|
269
|
-
def Rufus.to_duration_string (seconds, options={})
|
270
|
-
|
271
|
-
return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0
|
272
|
-
|
273
|
-
h = to_duration_hash seconds, options
|
274
|
-
|
275
|
-
s = DU_KEYS.inject("") do |r, key|
|
276
|
-
count = h[key]
|
277
|
-
count = nil if count == 0
|
278
|
-
r << "#{count}#{key}" if count
|
279
|
-
r
|
280
|
-
end
|
281
|
-
|
282
|
-
ms = h[:ms]
|
283
|
-
s << ms.to_s if ms
|
284
|
-
|
285
|
-
s
|
286
|
-
end
|
330
|
+
count = seconds / duration
|
331
|
+
seconds = seconds % duration
|
287
332
|
|
288
|
-
|
289
|
-
alias_method :to_time_string, :to_duration_string
|
333
|
+
h[key.to_sym] = count if count > 0
|
290
334
|
end
|
291
335
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
def Rufus.to_duration_hash (seconds, options={})
|
313
|
-
|
314
|
-
h = {}
|
315
|
-
|
316
|
-
if seconds.is_a?(Float)
|
317
|
-
h[:ms] = (seconds % 1 * 1000).to_i
|
318
|
-
seconds = seconds.to_i
|
319
|
-
end
|
320
|
-
|
321
|
-
if options[:drop_seconds]
|
322
|
-
h.delete :ms
|
323
|
-
seconds = (seconds - seconds % 60)
|
324
|
-
end
|
325
|
-
|
326
|
-
durations = options[:months] ? DURATIONS2M : DURATIONS2
|
327
|
-
|
328
|
-
durations.each do |key, duration|
|
329
|
-
|
330
|
-
count = seconds / duration
|
331
|
-
seconds = seconds % duration
|
332
|
-
|
333
|
-
h[key.to_sym] = count if count > 0
|
334
|
-
end
|
335
|
-
|
336
|
-
h
|
336
|
+
h
|
337
|
+
end
|
338
|
+
|
339
|
+
protected
|
340
|
+
|
341
|
+
DURATIONS2M = [
|
342
|
+
[ "y", 365 * 24 * 3600 ],
|
343
|
+
[ "M", 30 * 24 * 3600 ],
|
344
|
+
[ "w", 7 * 24 * 3600 ],
|
345
|
+
[ "d", 24 * 3600 ],
|
346
|
+
[ "h", 3600 ],
|
347
|
+
[ "m", 60 ],
|
348
|
+
[ "s", 1 ]
|
349
|
+
]
|
350
|
+
DURATIONS2 = DURATIONS2M.dup
|
351
|
+
DURATIONS2.delete_at 1
|
352
|
+
|
353
|
+
DURATIONS = DURATIONS2M.inject({}) do |r, (k, v)|
|
354
|
+
r[k] = v
|
355
|
+
r
|
337
356
|
end
|
338
357
|
|
339
|
-
|
340
|
-
|
341
|
-
DURATIONS2M = [
|
342
|
-
[ "y", 365 * 24 * 3600 ],
|
343
|
-
[ "M", 30 * 24 * 3600 ],
|
344
|
-
[ "w", 7 * 24 * 3600 ],
|
345
|
-
[ "d", 24 * 3600 ],
|
346
|
-
[ "h", 3600 ],
|
347
|
-
[ "m", 60 ],
|
348
|
-
[ "s", 1 ]
|
349
|
-
]
|
350
|
-
DURATIONS2 = DURATIONS2M.dup
|
351
|
-
DURATIONS2.delete_at 1
|
352
|
-
|
353
|
-
DURATIONS = DURATIONS2M.inject({}) do |r, (k, v)|
|
354
|
-
r[k] = v
|
355
|
-
r
|
356
|
-
end
|
357
|
-
|
358
|
-
DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym }
|
358
|
+
DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym }
|
359
359
|
|
360
360
|
end
|
361
361
|
|