rufus-scheduler 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +2 -1
- data/lib/rufus/otime.rb +98 -59
- data/test/time_1_test.rb +20 -3
- metadata +1 -1
data/CHANGELOG.txt
CHANGED
data/lib/rufus/otime.rb
CHANGED
@@ -132,17 +132,14 @@ module Rufus
|
|
132
132
|
index = index + 1
|
133
133
|
|
134
134
|
if index >= string.length
|
135
|
-
if number.length > 0
|
136
|
-
result = result + (Float(number) / 1000.0)
|
137
|
-
end
|
135
|
+
result = result + (Float(number) / 1000.0) if number.length > 0
|
138
136
|
break
|
139
137
|
end
|
140
138
|
|
141
139
|
c = string[index, 1]
|
142
140
|
|
143
|
-
#
|
144
|
-
|
145
|
-
if is_digit?(c)
|
141
|
+
#if is_digit?(c)
|
142
|
+
if (c >= "0" and c <= "9")
|
146
143
|
number = number + c
|
147
144
|
next
|
148
145
|
end
|
@@ -161,8 +158,14 @@ module Rufus
|
|
161
158
|
result
|
162
159
|
end
|
163
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
|
164
167
|
#
|
165
|
-
#
|
168
|
+
# (probably better served by a regex)
|
166
169
|
#
|
167
170
|
def Rufus.is_digit? (c)
|
168
171
|
|
@@ -201,14 +204,6 @@ module Rufus
|
|
201
204
|
|
202
205
|
rescue Exception => e
|
203
206
|
|
204
|
-
#puts
|
205
|
-
#puts OpenWFE::exception_to_s(e)
|
206
|
-
#puts
|
207
|
-
#puts \
|
208
|
-
# "\n Date.new() problem. Params :"+
|
209
|
-
# "\n....y:#{time.year} M:#{time.month} d:#{time.day} "+
|
210
|
-
# "h:#{time.hour} m:#{time.min} s:#{s} o:#{o}"
|
211
|
-
|
212
207
|
DateTime.new(
|
213
208
|
time.year,
|
214
209
|
time.month,
|
@@ -239,11 +234,11 @@ module Rufus
|
|
239
234
|
#
|
240
235
|
# Turns a number of seconds into a a time string
|
241
236
|
#
|
242
|
-
# Rufus.
|
243
|
-
# Rufus.
|
244
|
-
# Rufus.
|
245
|
-
# Rufus.
|
246
|
-
# Rufus.
|
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"
|
247
242
|
#
|
248
243
|
# It goes from seconds to the year. Months are not counted (as they
|
249
244
|
# are of variable length). Weeks are counted.
|
@@ -253,70 +248,114 @@ module Rufus
|
|
253
248
|
#
|
254
249
|
# Rufus.to_time_string 30 * 24 * 3600 + 1, true # => "1M1s"
|
255
250
|
#
|
251
|
+
# (to_time_string is an alias for to_duration_string)
|
252
|
+
#
|
256
253
|
# If a Float value is passed, milliseconds will be displayed without
|
257
254
|
# 'marker'
|
258
255
|
#
|
259
|
-
# Rufus.
|
260
|
-
# Rufus.
|
261
|
-
# Rufus.
|
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"
|
262
259
|
#
|
263
260
|
# (this behaviour mirrors the one found for parse_time_string()).
|
264
261
|
#
|
265
|
-
|
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={})
|
266
270
|
|
267
271
|
return '0s' if seconds <= 0
|
268
272
|
|
269
|
-
|
273
|
+
h = to_duration_hash seconds, options
|
270
274
|
|
271
|
-
|
272
|
-
|
273
|
-
|
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
|
274
280
|
end
|
275
281
|
|
276
|
-
|
282
|
+
ms = h[:ms]
|
283
|
+
s << ms.to_s if ms
|
277
284
|
|
278
|
-
s
|
285
|
+
s
|
286
|
+
end
|
279
287
|
|
280
|
-
|
281
|
-
|
288
|
+
class << self
|
289
|
+
alias_method :to_time_string, :to_duration_string
|
290
|
+
end
|
282
291
|
|
283
|
-
|
284
|
-
|
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 = {}
|
285
315
|
|
286
|
-
|
287
|
-
|
316
|
+
if seconds.is_a?(Float)
|
317
|
+
h[:ms] = (seconds % 1 * 1000).to_i
|
318
|
+
seconds = seconds.to_i
|
319
|
+
end
|
288
320
|
|
289
|
-
|
321
|
+
if options[:drop_seconds]
|
322
|
+
h.delete :ms
|
323
|
+
seconds = (seconds - seconds % 60)
|
324
|
+
end
|
290
325
|
|
291
|
-
|
292
|
-
}[1]
|
326
|
+
durations = options[:months] ? DURATIONS2M : DURATIONS2
|
293
327
|
|
294
|
-
|
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
|
295
337
|
end
|
296
338
|
|
297
339
|
protected
|
298
340
|
|
299
|
-
|
300
|
-
"y"
|
301
|
-
"M"
|
302
|
-
"w"
|
303
|
-
"d"
|
304
|
-
"h"
|
305
|
-
"m"
|
306
|
-
"s"
|
307
|
-
}
|
308
|
-
|
309
|
-
DURATIONS2w = [
|
310
|
-
[ "y", DURATIONS["y"] ],
|
311
|
-
[ "M", DURATIONS["M"] ],
|
312
|
-
[ "w", DURATIONS["w"] ],
|
313
|
-
[ "d", DURATIONS["d"] ],
|
314
|
-
[ "h", DURATIONS["h"] ],
|
315
|
-
[ "m", DURATIONS["m"] ],
|
316
|
-
[ "s", DURATIONS["s"] ]
|
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 ]
|
317
349
|
]
|
318
|
-
DURATIONS2 =
|
350
|
+
DURATIONS2 = DURATIONS2M.dup
|
319
351
|
DURATIONS2.delete_at 1
|
320
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 }
|
359
|
+
|
321
360
|
end
|
322
361
|
|
data/test/time_1_test.rb
CHANGED
@@ -36,23 +36,40 @@ class Time1Test < Test::Unit::TestCase
|
|
36
36
|
|
37
37
|
def test_1
|
38
38
|
|
39
|
-
tts 30 * 24 * 3600 + 1, "1M1s", true
|
39
|
+
tts 30 * 24 * 3600 + 1, "1M1s", { :months => true }
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_2
|
43
43
|
|
44
44
|
tts 0.120 + 30 * 24 * 3600 + 1, "4w2d1s120"
|
45
45
|
tts 0.130, "130"
|
46
|
+
tts 61.127, "1m", { :drop_seconds => true }
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_3
|
50
|
+
|
51
|
+
tdh 0, {}
|
52
|
+
tdh 0.128, { :ms => 128 }
|
53
|
+
tdh 60.127, { :m => 1, :ms => 127 }
|
54
|
+
tdh 61.127, { :m => 1, :s => 1, :ms => 127 }
|
55
|
+
tdh 61.127, { :m => 1 }, { :drop_seconds => true }
|
46
56
|
end
|
47
57
|
|
48
58
|
protected
|
49
59
|
|
50
|
-
def tts (seconds, time_string,
|
60
|
+
def tts (seconds, time_string, options={})
|
51
61
|
|
52
62
|
assert_equal(
|
53
63
|
time_string,
|
54
|
-
Rufus::to_time_string(seconds,
|
64
|
+
Rufus::to_time_string(seconds, options),
|
55
65
|
"#{seconds} seconds did not map to '#{time_string}'")
|
56
66
|
end
|
57
67
|
|
68
|
+
def tdh (seconds, time_hash, options={})
|
69
|
+
|
70
|
+
assert_equal(
|
71
|
+
time_hash,
|
72
|
+
Rufus::to_duration_hash(seconds, options))
|
73
|
+
end
|
74
|
+
|
58
75
|
end
|