rubysl-date 1.0.1 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -5
- data/lib/date/delta.rb +431 -0
- data/lib/date/delta/parser.rb +301 -0
- data/lib/date/delta/parser.ry +84 -0
- data/lib/date/format.rb +672 -551
- data/lib/rubysl/date/date.rb +653 -568
- data/lib/rubysl/date/version.rb +1 -1
- data/rubysl-date.gemspec +3 -0
- data/spec/date/relationship_spec.rb +1 -1
- metadata +36 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef338a6f69e34ebe197850c7e8f26b5a31b5e9df
|
4
|
+
data.tar.gz: f832b4ac193923f9ae808e3d59443a3098843aad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69b4b3c1a906551fff012aa639c82ed7e8178f77fd389678e6cab6ca883796b576ab5e2e4f2ae8ba3dbf973a6338fd94128a83671c875bc6b1878a4f9b874b88
|
7
|
+
data.tar.gz: 90cb9b3a4ec7f461abd20fb40bccf45cb70040bbcbf4dc44d6abd0dfc07d578fb17a8dfa70b37c343f50ede0eb452673c28d0b02f3ab6a91c7181bb210c2c022
|
data/.travis.yml
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
language: ruby
|
2
|
+
before_install:
|
3
|
+
- rvm use $RVM --install --binary --fuzzy
|
4
|
+
- gem update --system
|
5
|
+
- gem --version
|
6
|
+
- gem install rubysl-bundler
|
2
7
|
env:
|
3
|
-
- RUBYLIB=lib
|
4
|
-
script: bundle exec mspec
|
5
|
-
rvm:
|
6
|
-
- 1.8.7
|
7
|
-
- rbx-nightly-18mode
|
8
|
+
- RVM=rbx-nightly-d21 RUBYLIB=lib
|
9
|
+
script: bundle exec mspec spec
|
data/lib/date/delta.rb
ADDED
@@ -0,0 +1,431 @@
|
|
1
|
+
# delta.rb: Written by Tadayoshi Funaba 2004-2009
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'date/delta/parser'
|
5
|
+
|
6
|
+
class Date
|
7
|
+
|
8
|
+
class Delta
|
9
|
+
|
10
|
+
include Comparable
|
11
|
+
|
12
|
+
UNIT_PREFIXES = {
|
13
|
+
'yotta' => Rational(10**24),
|
14
|
+
'zetta' => Rational(10**21),
|
15
|
+
'exa' => Rational(10**18),
|
16
|
+
'peta' => Rational(10**15),
|
17
|
+
'tera' => Rational(10**12),
|
18
|
+
'giga' => Rational(10**9),
|
19
|
+
'mega' => Rational(10**6),
|
20
|
+
'kilo' => Rational(10**3),
|
21
|
+
'hecto' => Rational(10**2),
|
22
|
+
'deca' => Rational(10**1),
|
23
|
+
'deka' => Rational(10**1),
|
24
|
+
'deci' => Rational(1, 10**1),
|
25
|
+
'centi' => Rational(1, 10**2),
|
26
|
+
'milli' => Rational(1, 10**3),
|
27
|
+
'decimilli' => Rational(1, 10**4),
|
28
|
+
'centimilli' => Rational(1, 10**5),
|
29
|
+
'micro' => Rational(1, 10**6),
|
30
|
+
'nano' => Rational(1, 10**9),
|
31
|
+
'millimicro' => Rational(1, 10**9),
|
32
|
+
'pico' => Rational(1, 10**12),
|
33
|
+
'micromicro' => Rational(1, 10**12),
|
34
|
+
'femto' => Rational(1, 10**15),
|
35
|
+
'atto' => Rational(1, 10**18),
|
36
|
+
'zepto' => Rational(1, 10**21),
|
37
|
+
'yocto' => Rational(1, 10**24)
|
38
|
+
}
|
39
|
+
|
40
|
+
IUNITS = {
|
41
|
+
'year' => Complex(0, 12),
|
42
|
+
'month' => Complex(0, 1)
|
43
|
+
}
|
44
|
+
|
45
|
+
RUNITS = {
|
46
|
+
'day' => Rational(1),
|
47
|
+
'week' => Rational(7),
|
48
|
+
'sennight' => Rational(7),
|
49
|
+
'fortnight' => Rational(14),
|
50
|
+
'hour' => Rational(1, 24),
|
51
|
+
'minute' => Rational(1, 1440),
|
52
|
+
'second' => Rational(1, 86400)
|
53
|
+
}
|
54
|
+
|
55
|
+
UNIT_PREFIXES.each do |k, v|
|
56
|
+
RUNITS[k + 'second'] = v * RUNITS['second']
|
57
|
+
end
|
58
|
+
|
59
|
+
remove_const :UNIT_PREFIXES
|
60
|
+
|
61
|
+
UNITS = {}
|
62
|
+
|
63
|
+
IUNITS.each do |k, v|
|
64
|
+
UNITS[k] = v
|
65
|
+
end
|
66
|
+
|
67
|
+
RUNITS.each do |k, v|
|
68
|
+
UNITS[k] = v
|
69
|
+
end
|
70
|
+
|
71
|
+
UNITS4KEY = {}
|
72
|
+
|
73
|
+
UNITS.each do |k, v|
|
74
|
+
UNITS4KEY[k] = UNITS4KEY[k + 's'] = v
|
75
|
+
end
|
76
|
+
|
77
|
+
UNITS4KEY['y'] = UNITS4KEY['years']
|
78
|
+
UNITS4KEY['yr'] = UNITS4KEY['years']
|
79
|
+
UNITS4KEY['yrs'] = UNITS4KEY['years']
|
80
|
+
UNITS4KEY['m'] = UNITS4KEY['months']
|
81
|
+
UNITS4KEY['mo'] = UNITS4KEY['months']
|
82
|
+
UNITS4KEY['mon'] = UNITS4KEY['months']
|
83
|
+
UNITS4KEY['mnth'] = UNITS4KEY['months']
|
84
|
+
UNITS4KEY['mnths'] = UNITS4KEY['months']
|
85
|
+
UNITS4KEY['w'] = UNITS4KEY['weeks']
|
86
|
+
UNITS4KEY['wk'] = UNITS4KEY['weeks']
|
87
|
+
UNITS4KEY['d'] = UNITS4KEY['days']
|
88
|
+
UNITS4KEY['dy'] = UNITS4KEY['days']
|
89
|
+
UNITS4KEY['dys'] = UNITS4KEY['days']
|
90
|
+
UNITS4KEY['h'] = UNITS4KEY['hours']
|
91
|
+
UNITS4KEY['hr'] = UNITS4KEY['hours']
|
92
|
+
UNITS4KEY['hrs'] = UNITS4KEY['hours']
|
93
|
+
UNITS4KEY['min'] = UNITS4KEY['minutes']
|
94
|
+
UNITS4KEY['mins'] = UNITS4KEY['minutes']
|
95
|
+
UNITS4KEY['s'] = UNITS4KEY['seconds']
|
96
|
+
UNITS4KEY['sec'] = UNITS4KEY['seconds']
|
97
|
+
UNITS4KEY['secs'] = UNITS4KEY['seconds']
|
98
|
+
UNITS4KEY['ms'] = UNITS4KEY['milliseconds']
|
99
|
+
UNITS4KEY['msec'] = UNITS4KEY['milliseconds']
|
100
|
+
UNITS4KEY['msecs'] = UNITS4KEY['milliseconds']
|
101
|
+
UNITS4KEY['milli'] = UNITS4KEY['milliseconds']
|
102
|
+
UNITS4KEY['us'] = UNITS4KEY['microseconds']
|
103
|
+
UNITS4KEY['usec'] = UNITS4KEY['microseconds']
|
104
|
+
UNITS4KEY['usecs'] = UNITS4KEY['microseconds']
|
105
|
+
UNITS4KEY['micro'] = UNITS4KEY['microseconds']
|
106
|
+
UNITS4KEY['ns'] = UNITS4KEY['nanoseconds']
|
107
|
+
UNITS4KEY['nsec'] = UNITS4KEY['nanoseconds']
|
108
|
+
UNITS4KEY['nsecs'] = UNITS4KEY['nanoseconds']
|
109
|
+
UNITS4KEY['nano'] = UNITS4KEY['nanoseconds']
|
110
|
+
|
111
|
+
def self.delta_to_dhms(delta)
|
112
|
+
fr = delta.imag.abs
|
113
|
+
y, fr = fr.divmod(12)
|
114
|
+
m, fr = fr.divmod(1)
|
115
|
+
|
116
|
+
if delta.imag < 0
|
117
|
+
y = -y
|
118
|
+
m = -m
|
119
|
+
end
|
120
|
+
|
121
|
+
fr = delta.real.abs
|
122
|
+
ss, fr = fr.divmod(SECONDS_IN_DAY) # 4p
|
123
|
+
d, ss = ss.divmod(86400)
|
124
|
+
h, ss = ss.divmod(3600)
|
125
|
+
min, s = ss.divmod(60)
|
126
|
+
|
127
|
+
if delta.real < 0
|
128
|
+
d = -d
|
129
|
+
h = -h
|
130
|
+
min = -min
|
131
|
+
s = -s
|
132
|
+
end
|
133
|
+
|
134
|
+
return y, m, d, h, min, s, fr
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.dhms_to_delta(y, m, d, h, min, s, fr)
|
138
|
+
fr = 0 if fr == 0
|
139
|
+
Complex(0, y.to_i * 12 + m.to_i) +
|
140
|
+
Rational(d * 86400 + h * 3600 + min * 60 + (s + fr), 86400) # 4p
|
141
|
+
end
|
142
|
+
|
143
|
+
def initialize(delta)
|
144
|
+
@delta = delta
|
145
|
+
@__ca__ = {}
|
146
|
+
end
|
147
|
+
|
148
|
+
class << self; alias_method :new!, :new end
|
149
|
+
|
150
|
+
def self.new(arg=0, h=0, min=0, s=0)
|
151
|
+
if Hash === arg
|
152
|
+
d = Complex(0)
|
153
|
+
arg.each do |k, v|
|
154
|
+
k = k.to_s.downcase
|
155
|
+
unless UNITS4KEY[k]
|
156
|
+
raise ArgumentError, "unknown keyword #{k}"
|
157
|
+
end
|
158
|
+
d += v * UNITS4KEY[k]
|
159
|
+
end
|
160
|
+
else
|
161
|
+
d = dhms_to_delta(0, 0, arg, h, min, s, 0)
|
162
|
+
end
|
163
|
+
new!(d)
|
164
|
+
end
|
165
|
+
|
166
|
+
UNITS.each_key do |k|
|
167
|
+
module_eval <<-"end;"
|
168
|
+
def self.#{k}s(n=1)
|
169
|
+
new(:d=>n * UNITS['#{k}'])
|
170
|
+
end
|
171
|
+
end;
|
172
|
+
end
|
173
|
+
|
174
|
+
class << self; alias_method :mins, :minutes end
|
175
|
+
class << self; alias_method :secs, :seconds end
|
176
|
+
|
177
|
+
def self.parse(str)
|
178
|
+
d = begin (@@pa ||= Parser.new).parse(str)
|
179
|
+
rescue Racc::ParseError
|
180
|
+
raise ArgumentError, 'syntax error'
|
181
|
+
end
|
182
|
+
new!(d)
|
183
|
+
end
|
184
|
+
|
185
|
+
def self.diff(d1, d2) new(d1.ajd - d2.ajd) end
|
186
|
+
|
187
|
+
class << self
|
188
|
+
|
189
|
+
def once(*ids) # :nodoc: -- restricted
|
190
|
+
for id in ids
|
191
|
+
module_eval <<-"end;"
|
192
|
+
alias_method :__#{id.object_id}__, :#{id.to_s}
|
193
|
+
private :__#{id.object_id}__
|
194
|
+
def #{id.to_s}(*args)
|
195
|
+
@__ca__[#{id.object_id}] ||= __#{id.object_id}__(*args)
|
196
|
+
end
|
197
|
+
end;
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
private :once
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
def dhms() self.class.delta_to_dhms(@delta) end
|
206
|
+
|
207
|
+
once :dhms
|
208
|
+
|
209
|
+
def delta() @delta end
|
210
|
+
|
211
|
+
protected :delta
|
212
|
+
|
213
|
+
def years() dhms[0] end
|
214
|
+
def months() dhms[1] end
|
215
|
+
def days() dhms[2] end
|
216
|
+
def hours() dhms[3] end
|
217
|
+
def minutes() dhms[4] end
|
218
|
+
def seconds() dhms[5] end
|
219
|
+
def second_fractions() dhms[6] end
|
220
|
+
|
221
|
+
alias_method :mins, :minutes
|
222
|
+
alias_method :secs, :seconds
|
223
|
+
alias_method :sec_fractions, :second_fractions
|
224
|
+
|
225
|
+
RUNITS.each_key do |k|
|
226
|
+
module_eval <<-"end;"
|
227
|
+
def in_#{k}s(u=1)
|
228
|
+
if @delta.imag != 0
|
229
|
+
raise ArgumentError, "#{k}: #{self} has month"
|
230
|
+
end
|
231
|
+
@delta.real / (u * RUNITS['#{k}'])
|
232
|
+
end
|
233
|
+
end;
|
234
|
+
end # <<dummy
|
235
|
+
|
236
|
+
alias_method :in_mins, :in_minutes
|
237
|
+
alias_method :in_secs, :in_seconds
|
238
|
+
|
239
|
+
def zero?() @delta.zero? end
|
240
|
+
def nonzero?() unless zero? then self end end
|
241
|
+
|
242
|
+
def integer? () @delta.imag == 0 && @delta.real.integer? end
|
243
|
+
|
244
|
+
def -@ () self.class.new!(-@delta) end
|
245
|
+
def +@ () self.class.new!(+@delta) end
|
246
|
+
|
247
|
+
def dx_addsub(m, n)
|
248
|
+
case n
|
249
|
+
when Numeric; return self.class.new!(@delta.__send__(m, n))
|
250
|
+
when Delta; return self.class.new!(@delta.__send__(m, n.delta))
|
251
|
+
else
|
252
|
+
l, r = n.coerce(self)
|
253
|
+
return l.__send__(m, r)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
private :dx_addsub
|
258
|
+
|
259
|
+
def + (n) dx_addsub(:+, n) end
|
260
|
+
def - (n) dx_addsub(:-, n) end
|
261
|
+
|
262
|
+
def dx_muldiv(m, n)
|
263
|
+
case n
|
264
|
+
when Numeric
|
265
|
+
return self.class.new!(@delta.__send__(m, n))
|
266
|
+
else
|
267
|
+
l, r = n.coerce(self)
|
268
|
+
return l.__send__(m, r)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
private :dx_muldiv
|
273
|
+
|
274
|
+
def * (n) dx_muldiv(:*, n) end
|
275
|
+
def / (n) dx_muldiv(:/, n) end
|
276
|
+
|
277
|
+
def dx_conv1(m, n)
|
278
|
+
if @delta.imag != 0
|
279
|
+
raise ArgumentError, "#{m}: #{self} has month"
|
280
|
+
end
|
281
|
+
case n
|
282
|
+
when Numeric
|
283
|
+
return self.class.new!(Complex(@delta.real.__send__(m, n), 0))
|
284
|
+
else
|
285
|
+
l, r = n.coerce(self)
|
286
|
+
return l.__send__(m, r)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
private :dx_conv1
|
291
|
+
|
292
|
+
def % (n) dx_conv1(:%, n) end
|
293
|
+
|
294
|
+
def div(n) dx_conv1(:div, n) end
|
295
|
+
def modulo(n) dx_conv1(:modulo, n) end
|
296
|
+
def divmod(n) [div(n), modulo(n)] end
|
297
|
+
|
298
|
+
def quotient(n)
|
299
|
+
if @delta.imag != 0
|
300
|
+
raise ArgumentError, "quotient: #{self} has month"
|
301
|
+
end
|
302
|
+
case n
|
303
|
+
when Numeric
|
304
|
+
return self.class.new!(Complex((@delta.real / n).truncate))
|
305
|
+
else
|
306
|
+
l, r = n.coerce(self)
|
307
|
+
return l.__send__(m, r)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
def remainder(n) dx_conv1(:remainder, n) end
|
312
|
+
def quotrem(n) [quotient(n), remainder(n)] end
|
313
|
+
|
314
|
+
def ** (n) dx_conv1(:**, n) end
|
315
|
+
def quo(n) dx_muldiv(:quo, n) end
|
316
|
+
|
317
|
+
def <=> (other)
|
318
|
+
if @delta.imag != 0
|
319
|
+
raise ArgumentError, "<=>: #{self} has month"
|
320
|
+
end
|
321
|
+
case other
|
322
|
+
when Numeric; return @delta.real <=> other
|
323
|
+
when Delta; return @delta.real <=> other.delta.real
|
324
|
+
else
|
325
|
+
begin
|
326
|
+
l, r = other.coerce(self)
|
327
|
+
return l <=> r
|
328
|
+
rescue NoMethodError
|
329
|
+
end
|
330
|
+
end
|
331
|
+
nil
|
332
|
+
end
|
333
|
+
|
334
|
+
def == (other)
|
335
|
+
case other
|
336
|
+
when Numeric; return @delta == other
|
337
|
+
when Delta; return @delta == other
|
338
|
+
else
|
339
|
+
begin
|
340
|
+
l, r = other.coerce(self)
|
341
|
+
return l == r
|
342
|
+
rescue NoMethodError
|
343
|
+
end
|
344
|
+
end
|
345
|
+
nil
|
346
|
+
end
|
347
|
+
|
348
|
+
def coerce(other)
|
349
|
+
case other
|
350
|
+
when Numeric; return other, @delta
|
351
|
+
else
|
352
|
+
super
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
def eql? (other) Delta === other && self == other end
|
357
|
+
def hash() @delta.hash end
|
358
|
+
|
359
|
+
def dx_conv0(m)
|
360
|
+
if @delta.imag != 0
|
361
|
+
raise ArgumentError, "#{m}: #{self} has month"
|
362
|
+
end
|
363
|
+
@delta.real.__send__(m)
|
364
|
+
end
|
365
|
+
|
366
|
+
private :dx_conv0
|
367
|
+
|
368
|
+
def abs() dx_conv0(:abs) end
|
369
|
+
|
370
|
+
def ceil() dx_conv0(:ceil) end
|
371
|
+
def floor() dx_conv0(:floor) end
|
372
|
+
def round() dx_conv0(:round) end
|
373
|
+
def truncate() dx_conv0(:truncate) end
|
374
|
+
|
375
|
+
def to_i() dx_conv0(:to_i) end
|
376
|
+
def to_f() dx_conv0(:to_f) end
|
377
|
+
def to_r() dx_conv0(:to_r) end
|
378
|
+
def to_c() @delta end
|
379
|
+
|
380
|
+
alias_method :to_int, :to_i
|
381
|
+
|
382
|
+
def inspect() format('#<%s: %s (%s)>', self.class, to_s, @delta) end
|
383
|
+
|
384
|
+
def to_s
|
385
|
+
format(%(%s(%dd %.02d:%02d'%02d"%03d)%s(%dy %dm)), # '
|
386
|
+
if @delta.real < 0 then '-' else '+' end,
|
387
|
+
days.abs, hours.abs, mins.abs, secs.abs, sec_fractions.abs * 1000,
|
388
|
+
if @delta.imag < 0 then '-' else '+' end,
|
389
|
+
years.abs, months.abs)
|
390
|
+
end
|
391
|
+
|
392
|
+
def marshal_dump() @delta end
|
393
|
+
|
394
|
+
def marshal_load(a)
|
395
|
+
@delta = a
|
396
|
+
@__ca__ = {}
|
397
|
+
end
|
398
|
+
|
399
|
+
end
|
400
|
+
|
401
|
+
end
|
402
|
+
|
403
|
+
vsave = $VERBOSE
|
404
|
+
$VERBOSE = false
|
405
|
+
|
406
|
+
class Date
|
407
|
+
|
408
|
+
def + (n)
|
409
|
+
case n
|
410
|
+
when Numeric; return self.class.new!(@ajd + n, @of, @sg)
|
411
|
+
when Delta
|
412
|
+
d = n.__send__(:delta)
|
413
|
+
return (self >> d.imag) + d.real
|
414
|
+
end
|
415
|
+
raise TypeError, 'expected numeric'
|
416
|
+
end
|
417
|
+
|
418
|
+
def - (x)
|
419
|
+
case x
|
420
|
+
when Numeric; return self.class.new!(@ajd - x, @of, @sg)
|
421
|
+
when Date; return @ajd - x.ajd
|
422
|
+
when Delta
|
423
|
+
d = x.__send__(:delta)
|
424
|
+
return (self << d.imag) - d.real
|
425
|
+
end
|
426
|
+
raise TypeError, 'expected numeric'
|
427
|
+
end
|
428
|
+
|
429
|
+
end
|
430
|
+
|
431
|
+
$VERBOSE = vsave
|