rubysl-date 1.0.1 → 2.0.3
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.
- 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
@@ -0,0 +1,301 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by racc 1.4.5
|
4
|
+
# from racc grammer file "parser.ry".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser'
|
8
|
+
|
9
|
+
|
10
|
+
class Date
|
11
|
+
|
12
|
+
class Delta
|
13
|
+
|
14
|
+
class Parser < Racc::Parser
|
15
|
+
|
16
|
+
module_eval <<'..end parser.ry modeval..id43bff5dec9', 'parser.ry', 42
|
17
|
+
|
18
|
+
def lookup(str)
|
19
|
+
t = str.downcase
|
20
|
+
k = UNITS4KEY[t]
|
21
|
+
return [:UNIT, k] if k
|
22
|
+
return [:AND, nil] if t == 'and'
|
23
|
+
return [:UNKNOWNWORD, nil]
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse(str)
|
27
|
+
@q = []
|
28
|
+
until str.empty?
|
29
|
+
case str
|
30
|
+
when /\A\s+/
|
31
|
+
when /\AP(\d+y)?(\d+m)?(\d+d)?t?(\d+h)?(\d+m)?(\d+s)?(\d+w)?/i
|
32
|
+
y, m, d, h, min, s, w =
|
33
|
+
[$1, $2, $3, $4, $5, $6, $7].collect{|x| x.to_i}
|
34
|
+
y *= UNITS4KEY['years']
|
35
|
+
m *= UNITS4KEY['months']
|
36
|
+
d *= UNITS4KEY['days']
|
37
|
+
h *= UNITS4KEY['hours']
|
38
|
+
min *= UNITS4KEY['minutes']
|
39
|
+
s *= UNITS4KEY['seconds']
|
40
|
+
w *= UNITS4KEY['weeks']
|
41
|
+
@q.push [:DURATION, y + m + d + h + min + s + w]
|
42
|
+
when /\A\d+/
|
43
|
+
@q.push [:DIGITS, $&.to_i]
|
44
|
+
when /\A[a-z]+/i
|
45
|
+
@q.push lookup($&)
|
46
|
+
when /\A.|\n/
|
47
|
+
@q.push [$&, $&]
|
48
|
+
end
|
49
|
+
str = $'
|
50
|
+
end
|
51
|
+
@q.push [false, false]
|
52
|
+
do_parse
|
53
|
+
end
|
54
|
+
|
55
|
+
def next_token
|
56
|
+
@q.shift
|
57
|
+
end
|
58
|
+
|
59
|
+
..end parser.ry modeval..id43bff5dec9
|
60
|
+
|
61
|
+
##### racc 1.4.5 generates ###
|
62
|
+
|
63
|
+
racc_reduce_table = [
|
64
|
+
0, 0, :racc_error,
|
65
|
+
1, 16, :_reduce_none,
|
66
|
+
1, 17, :_reduce_none,
|
67
|
+
1, 17, :_reduce_none,
|
68
|
+
3, 17, :_reduce_4,
|
69
|
+
3, 17, :_reduce_5,
|
70
|
+
3, 17, :_reduce_6,
|
71
|
+
3, 17, :_reduce_7,
|
72
|
+
3, 17, :_reduce_8,
|
73
|
+
3, 17, :_reduce_9,
|
74
|
+
3, 17, :_reduce_10,
|
75
|
+
2, 17, :_reduce_11,
|
76
|
+
2, 17, :_reduce_12,
|
77
|
+
3, 17, :_reduce_13,
|
78
|
+
2, 18, :_reduce_14,
|
79
|
+
0, 20, :_reduce_15,
|
80
|
+
1, 20, :_reduce_none,
|
81
|
+
1, 19, :_reduce_none ]
|
82
|
+
|
83
|
+
racc_reduce_n = 18
|
84
|
+
|
85
|
+
racc_shift_n = 32
|
86
|
+
|
87
|
+
racc_action_table = [
|
88
|
+
13, 14, 15, 16, 17, 18, 19, 4, 27, 23,
|
89
|
+
8, 9, 1, 4, 25, 2, 8, 9, 1, 4,
|
90
|
+
24, 2, 8, 9, 1, 4, 21, 2, 8, 9,
|
91
|
+
1, 4, 11, 2, 8, 9, 1, 4, 26, 2,
|
92
|
+
8, 9, 1, 4, nil, 2, 8, 9, 1, 4,
|
93
|
+
nil, 2, 8, 9, 1, nil, nil, 2, 13, 14,
|
94
|
+
15, 16, 17, 18, 19, 13, 14, 15, 13, 14,
|
95
|
+
15, 13, 14, 15, 13, 14, 15 ]
|
96
|
+
|
97
|
+
racc_action_check = [
|
98
|
+
10, 10, 10, 10, 10, 10, 10, 17, 15, 10,
|
99
|
+
17, 17, 17, 18, 13, 17, 18, 18, 18, 4,
|
100
|
+
11, 18, 4, 4, 4, 1, 9, 4, 1, 1,
|
101
|
+
1, 8, 3, 1, 8, 8, 8, 19, 14, 8,
|
102
|
+
19, 19, 19, 0, nil, 19, 0, 0, 0, 16,
|
103
|
+
nil, 0, 16, 16, 16, nil, nil, 16, 5, 5,
|
104
|
+
5, 5, 5, 5, 5, 30, 30, 30, 28, 28,
|
105
|
+
28, 29, 29, 29, 31, 31, 31 ]
|
106
|
+
|
107
|
+
racc_action_pointer = [
|
108
|
+
37, 19, nil, 32, 13, 55, nil, nil, 25, 13,
|
109
|
+
-3, 20, nil, 4, 28, -2, 43, 1, 7, 31,
|
110
|
+
nil, nil, nil, nil, nil, nil, nil, nil, 65, 68,
|
111
|
+
62, 71 ]
|
112
|
+
|
113
|
+
racc_action_default = [
|
114
|
+
-18, -18, -17, -18, -18, -1, -2, -3, -18, -15,
|
115
|
+
-18, -18, -12, -18, -18, -18, -18, -18, -18, -18,
|
116
|
+
-11, -16, -14, -13, 32, -10, -8, -9, -4, -5,
|
117
|
+
-6, -7 ]
|
118
|
+
|
119
|
+
racc_goto_table = [
|
120
|
+
5, 10, 3, 22, 12, nil, nil, nil, 20, nil,
|
121
|
+
nil, nil, nil, nil, nil, nil, 28, 29, 30, 31 ]
|
122
|
+
|
123
|
+
racc_goto_check = [
|
124
|
+
2, 2, 1, 5, 2, nil, nil, nil, 2, nil,
|
125
|
+
nil, nil, nil, nil, nil, nil, 2, 2, 2, 2 ]
|
126
|
+
|
127
|
+
racc_goto_pointer = [
|
128
|
+
nil, 2, 0, nil, nil, -6 ]
|
129
|
+
|
130
|
+
racc_goto_default = [
|
131
|
+
nil, nil, nil, 6, 7, nil ]
|
132
|
+
|
133
|
+
racc_token_table = {
|
134
|
+
false => 0,
|
135
|
+
Object.new => 1,
|
136
|
+
:UNARY => 2,
|
137
|
+
"^" => 3,
|
138
|
+
"*" => 4,
|
139
|
+
"/" => 5,
|
140
|
+
"+" => 6,
|
141
|
+
"," => 7,
|
142
|
+
:AND => 8,
|
143
|
+
"-" => 9,
|
144
|
+
:DIGITS => 10,
|
145
|
+
"(" => 11,
|
146
|
+
")" => 12,
|
147
|
+
:UNIT => 13,
|
148
|
+
:DURATION => 14 }
|
149
|
+
|
150
|
+
racc_use_result_var = true
|
151
|
+
|
152
|
+
racc_nt_base = 15
|
153
|
+
|
154
|
+
Racc_arg = [
|
155
|
+
racc_action_table,
|
156
|
+
racc_action_check,
|
157
|
+
racc_action_default,
|
158
|
+
racc_action_pointer,
|
159
|
+
racc_goto_table,
|
160
|
+
racc_goto_check,
|
161
|
+
racc_goto_default,
|
162
|
+
racc_goto_pointer,
|
163
|
+
racc_nt_base,
|
164
|
+
racc_reduce_table,
|
165
|
+
racc_token_table,
|
166
|
+
racc_shift_n,
|
167
|
+
racc_reduce_n,
|
168
|
+
racc_use_result_var ]
|
169
|
+
|
170
|
+
Racc_token_to_s_table = [
|
171
|
+
'$end',
|
172
|
+
'error',
|
173
|
+
'UNARY',
|
174
|
+
'"^"',
|
175
|
+
'"*"',
|
176
|
+
'"/"',
|
177
|
+
'"+"',
|
178
|
+
'","',
|
179
|
+
'AND',
|
180
|
+
'"-"',
|
181
|
+
'DIGITS',
|
182
|
+
'"("',
|
183
|
+
'")"',
|
184
|
+
'UNIT',
|
185
|
+
'DURATION',
|
186
|
+
'$start',
|
187
|
+
'stmt',
|
188
|
+
'expr',
|
189
|
+
'time',
|
190
|
+
'iso',
|
191
|
+
'unit']
|
192
|
+
|
193
|
+
Racc_debug_parser = false
|
194
|
+
|
195
|
+
##### racc system variables end #####
|
196
|
+
|
197
|
+
# reduce 0 omitted
|
198
|
+
|
199
|
+
# reduce 1 omitted
|
200
|
+
|
201
|
+
# reduce 2 omitted
|
202
|
+
|
203
|
+
# reduce 3 omitted
|
204
|
+
|
205
|
+
module_eval <<'.,.,', 'parser.ry', 18
|
206
|
+
def _reduce_4( val, _values, result )
|
207
|
+
result += val[2]
|
208
|
+
result
|
209
|
+
end
|
210
|
+
.,.,
|
211
|
+
|
212
|
+
module_eval <<'.,.,', 'parser.ry', 19
|
213
|
+
def _reduce_5( val, _values, result )
|
214
|
+
result += val[2]
|
215
|
+
result
|
216
|
+
end
|
217
|
+
.,.,
|
218
|
+
|
219
|
+
module_eval <<'.,.,', 'parser.ry', 20
|
220
|
+
def _reduce_6( val, _values, result )
|
221
|
+
result += val[2]
|
222
|
+
result
|
223
|
+
end
|
224
|
+
.,.,
|
225
|
+
|
226
|
+
module_eval <<'.,.,', 'parser.ry', 21
|
227
|
+
def _reduce_7( val, _values, result )
|
228
|
+
result -= val[2]
|
229
|
+
result
|
230
|
+
end
|
231
|
+
.,.,
|
232
|
+
|
233
|
+
module_eval <<'.,.,', 'parser.ry', 22
|
234
|
+
def _reduce_8( val, _values, result )
|
235
|
+
result *= val[2]
|
236
|
+
result
|
237
|
+
end
|
238
|
+
.,.,
|
239
|
+
|
240
|
+
module_eval <<'.,.,', 'parser.ry', 23
|
241
|
+
def _reduce_9( val, _values, result )
|
242
|
+
result /= val[2]
|
243
|
+
result
|
244
|
+
end
|
245
|
+
.,.,
|
246
|
+
|
247
|
+
module_eval <<'.,.,', 'parser.ry', 24
|
248
|
+
def _reduce_10( val, _values, result )
|
249
|
+
result **= val[2]
|
250
|
+
result
|
251
|
+
end
|
252
|
+
.,.,
|
253
|
+
|
254
|
+
module_eval <<'.,.,', 'parser.ry', 25
|
255
|
+
def _reduce_11( val, _values, result )
|
256
|
+
result = -val[1]
|
257
|
+
result
|
258
|
+
end
|
259
|
+
.,.,
|
260
|
+
|
261
|
+
module_eval <<'.,.,', 'parser.ry', 26
|
262
|
+
def _reduce_12( val, _values, result )
|
263
|
+
result = +val[1]
|
264
|
+
result
|
265
|
+
end
|
266
|
+
.,.,
|
267
|
+
|
268
|
+
module_eval <<'.,.,', 'parser.ry', 27
|
269
|
+
def _reduce_13( val, _values, result )
|
270
|
+
result = val[1]
|
271
|
+
result
|
272
|
+
end
|
273
|
+
.,.,
|
274
|
+
|
275
|
+
module_eval <<'.,.,', 'parser.ry', 30
|
276
|
+
def _reduce_14( val, _values, result )
|
277
|
+
result = val[0] * val[1]
|
278
|
+
result
|
279
|
+
end
|
280
|
+
.,.,
|
281
|
+
|
282
|
+
module_eval <<'.,.,', 'parser.ry', 33
|
283
|
+
def _reduce_15( val, _values, result )
|
284
|
+
result = 1
|
285
|
+
result
|
286
|
+
end
|
287
|
+
.,.,
|
288
|
+
|
289
|
+
# reduce 16 omitted
|
290
|
+
|
291
|
+
# reduce 17 omitted
|
292
|
+
|
293
|
+
def _reduce_none( val, _values, result )
|
294
|
+
result
|
295
|
+
end
|
296
|
+
|
297
|
+
end # class Parser
|
298
|
+
|
299
|
+
end # class Delta
|
300
|
+
|
301
|
+
end # class Date
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# parser.ry: Written by Tadayoshi Funaba 2006,2008,2009 -*- ruby -*-
|
2
|
+
|
3
|
+
class Date::Delta::Parser
|
4
|
+
|
5
|
+
prechigh
|
6
|
+
nonassoc UNARY
|
7
|
+
left '^'
|
8
|
+
left '*' '/'
|
9
|
+
left '+' ',' AND '-'
|
10
|
+
preclow
|
11
|
+
|
12
|
+
rule
|
13
|
+
|
14
|
+
stmt : expr
|
15
|
+
;
|
16
|
+
|
17
|
+
expr : time
|
18
|
+
| iso
|
19
|
+
| expr '+' expr {result += val[2]}
|
20
|
+
| expr ',' expr {result += val[2]}
|
21
|
+
| expr AND expr {result += val[2]}
|
22
|
+
| expr '-' expr {result -= val[2]}
|
23
|
+
| expr '*' DIGITS {result *= val[2]}
|
24
|
+
| expr '/' DIGITS {result /= val[2]}
|
25
|
+
| expr '^' DIGITS {result **= val[2]}
|
26
|
+
| '-' expr =UNARY {result = -val[1]}
|
27
|
+
| '+' expr =UNARY {result = +val[1]}
|
28
|
+
| '(' expr ')' {result = val[1]}
|
29
|
+
;
|
30
|
+
|
31
|
+
time : DIGITS unit {result = val[0] * val[1]}
|
32
|
+
;
|
33
|
+
|
34
|
+
unit : {result = 1} | UNIT
|
35
|
+
;
|
36
|
+
|
37
|
+
iso : DURATION
|
38
|
+
;
|
39
|
+
|
40
|
+
---- header ----
|
41
|
+
---- inner ----
|
42
|
+
|
43
|
+
def lookup(str)
|
44
|
+
t = str.downcase
|
45
|
+
k = UNITS4KEY[t]
|
46
|
+
return [:UNIT, k] if k
|
47
|
+
return [:AND, nil] if t == 'and'
|
48
|
+
return [:UNKNOWNWORD, nil]
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse(str)
|
52
|
+
@q = []
|
53
|
+
until str.empty?
|
54
|
+
case str
|
55
|
+
when /\A\s+/
|
56
|
+
when /\AP(\d+y)?(\d+m)?(\d+d)?t?(\d+h)?(\d+m)?(\d+s)?(\d+w)?/i
|
57
|
+
y, m, d, h, min, s, w =
|
58
|
+
[$1, $2, $3, $4, $5, $6, $7].collect{|x| x.to_i}
|
59
|
+
y *= UNITS4KEY['years']
|
60
|
+
m *= UNITS4KEY['months']
|
61
|
+
d *= UNITS4KEY['days']
|
62
|
+
h *= UNITS4KEY['hours']
|
63
|
+
min *= UNITS4KEY['minutes']
|
64
|
+
s *= UNITS4KEY['seconds']
|
65
|
+
w *= UNITS4KEY['weeks']
|
66
|
+
@q.push [:DURATION, y + m + d + h + min + s + w]
|
67
|
+
when /\A\d+/
|
68
|
+
@q.push [:DIGITS, $&.to_i]
|
69
|
+
when /\A[a-z]+/i
|
70
|
+
@q.push lookup($&)
|
71
|
+
when /\A.|\n/
|
72
|
+
@q.push [$&, $&]
|
73
|
+
end
|
74
|
+
str = $'
|
75
|
+
end
|
76
|
+
@q.push [false, false]
|
77
|
+
do_parse
|
78
|
+
end
|
79
|
+
|
80
|
+
def next_token
|
81
|
+
@q.shift
|
82
|
+
end
|
83
|
+
|
84
|
+
---- footer ----
|
data/lib/date/format.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# format.rb: Written by Tadayoshi Funaba 1999-
|
1
|
+
# format.rb: Written by Tadayoshi Funaba 1999-2009
|
2
2
|
# $Id: format.rb,v 2.43 2008-01-17 20:16:31+09 tadf Exp $
|
3
3
|
|
4
|
-
require 'rational'
|
5
|
-
|
6
4
|
class Date
|
7
5
|
|
8
6
|
module Format # :nodoc:
|
@@ -178,7 +176,7 @@ class Date
|
|
178
176
|
end
|
179
177
|
|
180
178
|
def emit_w(e, w, f) # :nodoc:
|
181
|
-
f[:w] =
|
179
|
+
f[:w] = [f[:w], w].compact.max
|
182
180
|
emit(e, f)
|
183
181
|
end
|
184
182
|
|
@@ -223,26 +221,26 @@ class Date
|
|
223
221
|
end
|
224
222
|
|
225
223
|
private :emit, :emit_w, :emit_n, :emit_sn, :emit_z,
|
226
|
-
|
224
|
+
:emit_a, :emit_ad, :emit_au
|
227
225
|
|
228
226
|
def strftime(fmt='%F')
|
229
|
-
fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
|
227
|
+
fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
|
230
228
|
f = {}
|
231
|
-
|
229
|
+
m = $&
|
232
230
|
s, w, c = $1, $2, $3
|
233
231
|
if s
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
232
|
+
s.scan(/./) do |k|
|
233
|
+
case k
|
234
|
+
when '-'; f[:p] = '-'
|
235
|
+
when '_'; f[:p] = "\s"
|
236
|
+
when '0'; f[:p] = '0'
|
237
|
+
when '^'; f[:u] = true
|
238
|
+
when '#'; f[:x] = true
|
239
|
+
end
|
240
|
+
end
|
243
241
|
end
|
244
242
|
if w
|
245
|
-
|
243
|
+
f[:w] = w.to_i
|
246
244
|
end
|
247
245
|
case c
|
248
246
|
when 'A'; emit_ad(DAYNAMES[wday], 0, f)
|
@@ -255,11 +253,11 @@ class Date
|
|
255
253
|
when 'd', 'Od'; emit_n(mday, 2, f)
|
256
254
|
when 'e', 'Oe'; emit_a(mday, 2, f)
|
257
255
|
when 'F'
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
256
|
+
if m == '%F'
|
257
|
+
format('%.4d-%02d-%02d', year, mon, mday) # 4p
|
258
|
+
else
|
259
|
+
emit_a(strftime('%Y-%m-%d'), 0, f)
|
260
|
+
end
|
263
261
|
when 'G'; emit_sn(cwyear, 4, f)
|
264
262
|
when 'g'; emit_n(cwyear % 100, 2, f)
|
265
263
|
when 'H', 'OH'; emit_n(hour, 2, f)
|
@@ -268,33 +266,39 @@ class Date
|
|
268
266
|
when 'j'; emit_n(yday, 3, f)
|
269
267
|
when 'k'; emit_a(hour, 2, f)
|
270
268
|
when 'L'
|
271
|
-
|
269
|
+
f[:p] = nil
|
270
|
+
w = f[:w] || 3
|
271
|
+
u = 10**w
|
272
|
+
emit_n((sec_fraction * u).floor, w, f)
|
272
273
|
when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
|
273
274
|
when 'M', 'OM'; emit_n(min, 2, f)
|
274
275
|
when 'm', 'Om'; emit_n(mon, 2, f)
|
275
276
|
when 'N'
|
276
|
-
|
277
|
-
|
277
|
+
f[:p] = nil
|
278
|
+
w = f[:w] || 9
|
279
|
+
u = 10**w
|
280
|
+
emit_n((sec_fraction * u).floor, w, f)
|
281
|
+
when 'n'; emit_a("\n", 0, f)
|
278
282
|
when 'P'; emit_ad(strftime('%p').downcase, 0, f)
|
279
283
|
when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
|
280
284
|
when 'Q'
|
281
|
-
|
282
|
-
|
285
|
+
s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
|
286
|
+
emit_sn(s, 1, f)
|
283
287
|
when 'R'; emit_a(strftime('%H:%M'), 0, f)
|
284
288
|
when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
|
285
289
|
when 'S', 'OS'; emit_n(sec, 2, f)
|
286
290
|
when 's'
|
287
|
-
|
288
|
-
|
291
|
+
s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
|
292
|
+
emit_sn(s, 1, f)
|
289
293
|
when 'T'
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
when 't'; "\t"
|
294
|
+
if m == '%T'
|
295
|
+
format('%02d:%02d:%02d', hour, min, sec) # 4p
|
296
|
+
else
|
297
|
+
emit_a(strftime('%H:%M:%S'), 0, f)
|
298
|
+
end
|
299
|
+
when 't'; emit_a("\t", 0, f)
|
296
300
|
when 'U', 'W', 'OU', 'OW'
|
297
|
-
|
301
|
+
emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
|
298
302
|
when 'u', 'Ou'; emit_n(cwday, 1, f)
|
299
303
|
when 'V', 'OV'; emit_n(cweek, 2, f)
|
300
304
|
when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
|
@@ -305,55 +309,40 @@ class Date
|
|
305
309
|
when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
|
306
310
|
when 'Z'; emit_au(strftime('%:z'), 0, f)
|
307
311
|
when /\A(:{0,3})z/
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
312
|
+
t = $1.size
|
313
|
+
sign = if offset < 0 then -1 else +1 end
|
314
|
+
fr = offset.abs
|
315
|
+
ss = fr.div(SECONDS_IN_DAY) # 4p
|
316
|
+
hh, ss = ss.divmod(3600)
|
317
|
+
mm, ss = ss.divmod(60)
|
318
|
+
if t == 3
|
319
|
+
if ss.nonzero? then t = 2
|
320
|
+
elsif mm.nonzero? then t = 1
|
321
|
+
else t = -1
|
322
|
+
end
|
323
|
+
end
|
324
|
+
case t
|
325
|
+
when -1
|
326
|
+
tail = []
|
327
|
+
sep = ''
|
328
|
+
when 0
|
329
|
+
f[:w] -= 2 if f[:w]
|
330
|
+
tail = ['%02d' % mm]
|
331
|
+
sep = ''
|
332
|
+
when 1
|
333
|
+
f[:w] -= 3 if f[:w]
|
334
|
+
tail = ['%02d' % mm]
|
335
|
+
sep = ':'
|
336
|
+
when 2
|
337
|
+
f[:w] -= 6 if f[:w]
|
338
|
+
tail = ['%02d' % mm, '%02d' % ss]
|
339
|
+
sep = ':'
|
340
|
+
end
|
341
|
+
([emit_z(sign * hh, 2, f)] + tail).join(sep)
|
338
342
|
when '%'; emit_a('%', 0, f)
|
339
343
|
when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
|
340
|
-
when '1'
|
341
|
-
if $VERBOSE
|
342
|
-
warn("warning: strftime: %1 is deprecated; forget this")
|
343
|
-
end
|
344
|
-
emit_n(jd, 1, f)
|
345
|
-
when '2'
|
346
|
-
if $VERBOSE
|
347
|
-
warn("warning: strftime: %2 is deprecated; use '%Y-%j'")
|
348
|
-
end
|
349
|
-
emit_a(strftime('%Y-%j'), 0, f)
|
350
|
-
when '3'
|
351
|
-
if $VERBOSE
|
352
|
-
warn("warning: strftime: %3 is deprecated; use '%F'")
|
353
|
-
end
|
354
|
-
emit_a(strftime('%F'), 0, f)
|
355
344
|
else
|
356
|
-
|
345
|
+
m
|
357
346
|
end
|
358
347
|
end
|
359
348
|
end
|
@@ -364,40 +353,44 @@ class Date
|
|
364
353
|
|
365
354
|
alias_method :ctime, :asctime
|
366
355
|
|
367
|
-
=begin
|
368
356
|
def iso8601() strftime('%F') end
|
369
357
|
|
370
358
|
def rfc3339() iso8601 end
|
371
359
|
|
360
|
+
def xmlschema() iso8601 end # :nodoc:
|
361
|
+
|
372
362
|
def rfc2822() strftime('%a, %-d %b %Y %T %z') end
|
373
363
|
|
374
364
|
alias_method :rfc822, :rfc2822
|
375
365
|
|
366
|
+
def httpdate() new_offset(0).strftime('%a, %d %b %Y %T GMT') end # :nodoc:
|
367
|
+
|
376
368
|
def jisx0301
|
377
369
|
if jd < 2405160
|
378
370
|
iso8601
|
379
371
|
else
|
380
372
|
case jd
|
381
373
|
when 2405160...2419614
|
382
|
-
|
374
|
+
g = 'M%02d' % (year - 1867)
|
383
375
|
when 2419614...2424875
|
384
|
-
|
376
|
+
g = 'T%02d' % (year - 1911)
|
385
377
|
when 2424875...2447535
|
386
|
-
|
378
|
+
g = 'S%02d' % (year - 1925)
|
387
379
|
else
|
388
|
-
|
380
|
+
g = 'H%02d' % (year - 1988)
|
389
381
|
end
|
390
382
|
g + strftime('.%m.%d')
|
391
383
|
end
|
392
384
|
end
|
393
385
|
|
386
|
+
=begin
|
394
387
|
def beat(n=0)
|
395
388
|
i, f = (new_offset(HOURS_IN_DAY).day_fraction * 1000).divmod(1)
|
396
389
|
('@%03d' % i) +
|
397
390
|
if n < 1
|
398
|
-
|
391
|
+
''
|
399
392
|
else
|
400
|
-
|
393
|
+
'.%0*d' % [n, (f / Rational(1, 10**n)).round]
|
401
394
|
end
|
402
395
|
end
|
403
396
|
=end
|
@@ -412,198 +405,181 @@ class Date
|
|
412
405
|
fmt.scan(/%([EO]?(?::{1,3}z|.))|(.)/m) do |s, c|
|
413
406
|
a = $&
|
414
407
|
if s
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
408
|
+
case s
|
409
|
+
when 'A', 'a'
|
410
|
+
return unless str.sub!(/\A(#{Format::DAYS.keys.join('|')})/io, '') ||
|
411
|
+
str.sub!(/\A(#{Format::ABBR_DAYS.keys.join('|')})/io, '')
|
412
|
+
val = Format::DAYS[$1.downcase] || Format::ABBR_DAYS[$1.downcase]
|
413
|
+
return unless val
|
414
|
+
e[:wday] = val
|
415
|
+
when 'B', 'b', 'h'
|
416
|
+
return unless str.sub!(/\A(#{Format::MONTHS.keys.join('|')})/io, '') ||
|
417
|
+
str.sub!(/\A(#{Format::ABBR_MONTHS.keys.join('|')})/io, '')
|
418
|
+
val = Format::MONTHS[$1.downcase] || Format::ABBR_MONTHS[$1.downcase]
|
419
|
+
return unless val
|
420
|
+
e.mon = val
|
421
|
+
when 'C', 'EC'
|
422
|
+
return unless str.sub!(if num_pattern?($')
|
423
|
+
then /\A([-+]?\d{1,2})/
|
424
|
+
else /\A([-+]?\d{1,})/
|
425
|
+
end, '')
|
426
|
+
val = $1.to_i
|
427
|
+
e._cent = val
|
428
|
+
when 'c', 'Ec'
|
429
|
+
return unless _strptime_i(str, '%a %b %e %H:%M:%S %Y', e)
|
430
|
+
when 'D'
|
431
|
+
return unless _strptime_i(str, '%m/%d/%y', e)
|
432
|
+
when 'd', 'e', 'Od', 'Oe'
|
433
|
+
return unless str.sub!(/\A( \d|\d{1,2})/, '')
|
434
|
+
val = $1.to_i
|
435
|
+
return unless (1..31) === val
|
436
|
+
e.mday = val
|
437
|
+
when 'F'
|
438
|
+
return unless _strptime_i(str, '%Y-%m-%d', e)
|
439
|
+
when 'G'
|
440
|
+
return unless str.sub!(if num_pattern?($')
|
441
|
+
then /\A([-+]?\d{1,4})/
|
442
|
+
else /\A([-+]?\d{1,})/
|
443
|
+
end, '')
|
444
|
+
val = $1.to_i
|
445
|
+
e[:cwyear] = val
|
446
|
+
when 'g'
|
447
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
448
|
+
val = $1.to_i
|
449
|
+
return unless (0..99) === val
|
450
|
+
e[:cwyear] = val
|
451
|
+
e._cent ||= if val >= 69 then 19 else 20 end
|
452
|
+
when 'H', 'k', 'OH'
|
453
|
+
return unless str.sub!(/\A( \d|\d{1,2})/, '')
|
454
|
+
val = $1.to_i
|
455
|
+
return unless (0..24) === val
|
456
|
+
e.hour = val
|
457
|
+
when 'I', 'l', 'OI'
|
458
|
+
return unless str.sub!(/\A( \d|\d{1,2})/, '')
|
459
|
+
val = $1.to_i
|
460
|
+
return unless (1..12) === val
|
461
|
+
e.hour = val
|
462
|
+
when 'j'
|
463
|
+
return unless str.sub!(/\A(\d{1,3})/, '')
|
464
|
+
val = $1.to_i
|
465
|
+
return unless (1..366) === val
|
466
|
+
e[:yday] = val
|
467
|
+
when 'L'
|
468
|
+
return unless str.sub!(if num_pattern?($')
|
469
|
+
then /\A([-+]?\d{1,3})/
|
470
|
+
else /\A([-+]?\d{1,})/
|
471
|
+
end, '')
|
479
472
|
# val = Rational($1.to_i, 10**3)
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
473
|
+
val = Rational($1.to_i, 10**$1.size)
|
474
|
+
e.sec_fraction = val
|
475
|
+
when 'M', 'OM'
|
476
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
477
|
+
val = $1.to_i
|
478
|
+
return unless (0..59) === val
|
479
|
+
e.min = val
|
480
|
+
when 'm', 'Om'
|
481
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
482
|
+
val = $1.to_i
|
483
|
+
return unless (1..12) === val
|
484
|
+
e.mon = val
|
485
|
+
when 'N'
|
486
|
+
return unless str.sub!(if num_pattern?($')
|
487
|
+
then /\A([-+]?\d{1,9})/
|
488
|
+
else /\A([-+]?\d{1,})/
|
489
|
+
end, '')
|
497
490
|
# val = Rational($1.to_i, 10**9)
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
end
|
584
|
-
return unless str.sub!(/\A(\d+)/, '')
|
585
|
-
val = $1.to_i
|
586
|
-
e[:jd] = val
|
587
|
-
when '2'
|
588
|
-
if $VERBOSE
|
589
|
-
warn("warning: strptime: %2 is deprecated; use '%Y-%j'")
|
590
|
-
end
|
591
|
-
return unless _strptime_i(str, '%Y-%j', e)
|
592
|
-
when '3'
|
593
|
-
if $VERBOSE
|
594
|
-
warn("warning: strptime: %3 is deprecated; use '%F'")
|
595
|
-
end
|
596
|
-
return unless _strptime_i(str, '%F', e)
|
597
|
-
else
|
598
|
-
return unless str.sub!(Regexp.new('\\A' + Regexp.quote(a)), '')
|
599
|
-
end
|
491
|
+
val = Rational($1.to_i, 10**$1.size)
|
492
|
+
e.sec_fraction = val
|
493
|
+
when 'n', 't'
|
494
|
+
return unless _strptime_i(str, "\s", e)
|
495
|
+
when 'P', 'p'
|
496
|
+
return unless str.sub!(/\A([ap])(?:m\b|\.m\.)/i, '')
|
497
|
+
e._merid = if $1.downcase == 'a' then 0 else 12 end
|
498
|
+
when 'Q'
|
499
|
+
return unless str.sub!(/\A(-?\d{1,})/, '')
|
500
|
+
val = Rational($1.to_i, 10**3)
|
501
|
+
e[:seconds] = val
|
502
|
+
when 'R'
|
503
|
+
return unless _strptime_i(str, '%H:%M', e)
|
504
|
+
when 'r'
|
505
|
+
return unless _strptime_i(str, '%I:%M:%S %p', e)
|
506
|
+
when 'S', 'OS'
|
507
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
508
|
+
val = $1.to_i
|
509
|
+
return unless (0..60) === val
|
510
|
+
e.sec = val
|
511
|
+
when 's'
|
512
|
+
return unless str.sub!(/\A(-?\d{1,})/, '')
|
513
|
+
val = $1.to_i
|
514
|
+
e[:seconds] = val
|
515
|
+
when 'T'
|
516
|
+
return unless _strptime_i(str, '%H:%M:%S', e)
|
517
|
+
when 'U', 'W', 'OU', 'OW'
|
518
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
519
|
+
val = $1.to_i
|
520
|
+
return unless (0..53) === val
|
521
|
+
if s[-1,1] == 'U'
|
522
|
+
e[:wnum0] = val
|
523
|
+
else
|
524
|
+
e[:wnum1] = val
|
525
|
+
end
|
526
|
+
when 'u', 'Ou'
|
527
|
+
return unless str.sub!(/\A(\d{1})/, '')
|
528
|
+
val = $1.to_i
|
529
|
+
return unless (1..7) === val
|
530
|
+
e[:cwday] = val
|
531
|
+
when 'V', 'OV'
|
532
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
533
|
+
val = $1.to_i
|
534
|
+
return unless (1..53) === val
|
535
|
+
e[:cweek] = val
|
536
|
+
when 'v'
|
537
|
+
return unless _strptime_i(str, '%e-%b-%Y', e)
|
538
|
+
when 'w'
|
539
|
+
return unless str.sub!(/\A(\d{1})/, '')
|
540
|
+
val = $1.to_i
|
541
|
+
return unless (0..6) === val
|
542
|
+
e[:wday] = val
|
543
|
+
when 'X', 'EX'
|
544
|
+
return unless _strptime_i(str, '%H:%M:%S', e)
|
545
|
+
when 'x', 'Ex'
|
546
|
+
return unless _strptime_i(str, '%m/%d/%y', e)
|
547
|
+
when 'Y', 'EY'
|
548
|
+
return unless str.sub!(if num_pattern?($')
|
549
|
+
then /\A([-+]?\d{1,4})/
|
550
|
+
else /\A([-+]?\d{1,})/
|
551
|
+
end, '')
|
552
|
+
val = $1.to_i
|
553
|
+
e.year = val
|
554
|
+
when 'y', 'Ey', 'Oy'
|
555
|
+
return unless str.sub!(/\A(\d{1,2})/, '')
|
556
|
+
val = $1.to_i
|
557
|
+
return unless (0..99) === val
|
558
|
+
e.year = val
|
559
|
+
e._cent ||= if val >= 69 then 19 else 20 end
|
560
|
+
when 'Z', /\A:{0,3}z/
|
561
|
+
return unless str.sub!(/\A((?:gmt|utc?)?[-+]\d+(?:[,.:]\d+(?::\d+)?)?
|
562
|
+
|[[:alpha:].\s]+(?:standard|daylight)\s+time\b
|
563
|
+
|[[:alpha:]]+(?:\s+dst)?\b
|
564
|
+
)/ix, '')
|
565
|
+
val = $1
|
566
|
+
e.zone = val
|
567
|
+
offset = zone_to_diff(val)
|
568
|
+
e.offset = offset
|
569
|
+
when '%'
|
570
|
+
return unless str.sub!(/\A%/, '')
|
571
|
+
when '+'
|
572
|
+
return unless _strptime_i(str, '%a %b %e %H:%M:%S %Z %Y', e)
|
573
|
+
else
|
574
|
+
return unless str.sub!(Regexp.new('\\A' + Regexp.quote(a)), '')
|
575
|
+
end
|
600
576
|
else
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
577
|
+
case c
|
578
|
+
when /\A\s/
|
579
|
+
str.sub!(/\A\s+/, '')
|
580
|
+
else
|
581
|
+
return unless str.sub!(Regexp.new('\\A' + Regexp.quote(a)), '')
|
582
|
+
end
|
607
583
|
end
|
608
584
|
end
|
609
585
|
end
|
@@ -613,22 +589,21 @@ class Date
|
|
613
589
|
def self._strptime(str, fmt='%F')
|
614
590
|
str = str.dup
|
615
591
|
e = Format::Bag.new
|
616
|
-
|
617
592
|
return unless _strptime_i(str, fmt, e)
|
618
593
|
|
619
594
|
if e._cent
|
620
595
|
if e[:cwyear]
|
621
|
-
|
596
|
+
e[:cwyear] += e._cent * 100
|
622
597
|
end
|
623
598
|
if e.year
|
624
|
-
|
599
|
+
e. year += e._cent * 100
|
625
600
|
end
|
626
601
|
end
|
627
602
|
|
628
603
|
if e._merid
|
629
604
|
if e.hour
|
630
|
-
|
631
|
-
|
605
|
+
e.hour %= 12
|
606
|
+
e.hour += e._merid
|
632
607
|
end
|
633
608
|
end
|
634
609
|
|
@@ -650,42 +625,42 @@ class Date
|
|
650
625
|
|
651
626
|
if y == nil
|
652
627
|
if d && d.size > 2
|
653
|
-
|
654
|
-
|
628
|
+
y = d
|
629
|
+
d = nil
|
655
630
|
end
|
656
631
|
if d && d[0,1] == "'"
|
657
|
-
|
658
|
-
|
632
|
+
y = d
|
633
|
+
d = nil
|
659
634
|
end
|
660
635
|
end
|
661
636
|
|
662
637
|
if y
|
663
638
|
y.scan(/(\d+)(.+)?/)
|
664
639
|
if $2
|
665
|
-
|
640
|
+
y, d = d, $1
|
666
641
|
end
|
667
642
|
end
|
668
643
|
|
669
644
|
if m
|
670
645
|
if m[0,1] == "'" || m.size > 2
|
671
|
-
|
646
|
+
y, m, d = m, d, y # us -> be
|
672
647
|
end
|
673
648
|
end
|
674
649
|
|
675
650
|
if d
|
676
651
|
if d[0,1] == "'" || d.size > 2
|
677
|
-
|
652
|
+
y, d = d, y
|
678
653
|
end
|
679
654
|
end
|
680
655
|
|
681
656
|
if y
|
682
657
|
y =~ /([-+])?(\d+)/
|
683
658
|
if $1 || $2.size > 2
|
684
|
-
|
659
|
+
c = false
|
685
660
|
end
|
686
661
|
iy = $&.to_i
|
687
662
|
if bc
|
688
|
-
|
663
|
+
iy = -iy + 1
|
689
664
|
end
|
690
665
|
e.year = iy
|
691
666
|
end
|
@@ -709,12 +684,12 @@ class Date
|
|
709
684
|
private_class_method :s3e
|
710
685
|
|
711
686
|
def self._parse_day(str, e) # :nodoc:
|
712
|
-
if str.sub!(/\b(#{Format::ABBR_DAYS.keys.join('|')})[^-\d\s]*/
|
687
|
+
if str.sub!(/\b(#{Format::ABBR_DAYS.keys.join('|')})[^-\d\s]*/io, ' ')
|
713
688
|
e[:wday] = Format::ABBR_DAYS[$1.downcase]
|
714
689
|
true
|
715
690
|
=begin
|
716
|
-
elsif str.sub!(/\b(?!\dth)(su|mo|tu|we|th|fr|sa)\b/
|
717
|
-
e
|
691
|
+
elsif str.sub!(/\b(?!\dth)(su|mo|tu|we|th|fr|sa)\b/i, ' ')
|
692
|
+
e.wday = %w(su mo tu we th fr sa).index($1.downcase)
|
718
693
|
true
|
719
694
|
=end
|
720
695
|
end
|
@@ -722,45 +697,45 @@ class Date
|
|
722
697
|
|
723
698
|
def self._parse_time(str, e) # :nodoc:
|
724
699
|
if str.sub!(
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
700
|
+
/(
|
701
|
+
(?:
|
702
|
+
\d+\s*:\s*\d+
|
703
|
+
(?:
|
704
|
+
\s*:\s*\d+(?:[,.]\d*)?
|
705
|
+
)?
|
706
|
+
|
|
707
|
+
\d+\s*h(?:\s*\d+m?(?:\s*\d+s?)?)?
|
708
|
+
)
|
709
|
+
(?:
|
710
|
+
\s*
|
711
|
+
[ap](?:m\b|\.m\.)
|
712
|
+
)?
|
713
|
+
|
|
714
|
+
\d+\s*[ap](?:m\b|\.m\.)
|
715
|
+
)
|
716
|
+
(?:
|
717
|
+
\s*
|
718
|
+
(
|
719
|
+
(?:gmt|utc?)?[-+]\d+(?:[,.:]\d+(?::\d+)?)?
|
720
|
+
|
|
721
|
+
[[:alpha:].\s]+(?:standard|daylight)\stime\b
|
722
|
+
|
|
723
|
+
[[:alpha:]]+(?:\sdst)?\b
|
724
|
+
)
|
725
|
+
)?
|
726
|
+
/ix,
|
727
|
+
' ')
|
753
728
|
|
754
729
|
t = $1
|
755
730
|
e.zone = $2 if $2
|
756
731
|
|
757
732
|
t =~ /\A(\d+)h?
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
733
|
+
(?:\s*:?\s*(\d+)m?
|
734
|
+
(?:
|
735
|
+
\s*:?\s*(\d+)(?:[,.](\d+))?s?
|
736
|
+
)?
|
737
|
+
)?
|
738
|
+
(?:\s*([ap])(?:m\b|\.m\.))?/ix
|
764
739
|
|
765
740
|
e.hour = $1.to_i
|
766
741
|
e.min = $2.to_i if $2
|
@@ -768,10 +743,10 @@ class Date
|
|
768
743
|
e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4
|
769
744
|
|
770
745
|
if $5
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
746
|
+
e.hour %= 12
|
747
|
+
if $5.downcase == 'p'
|
748
|
+
e.hour += 12
|
749
|
+
end
|
775
750
|
end
|
776
751
|
true
|
777
752
|
end
|
@@ -796,85 +771,85 @@ class Date
|
|
796
771
|
|
797
772
|
def self._parse_eu(str, e) # :nodoc:
|
798
773
|
if str.sub!(
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
774
|
+
/'?(\d+)[^-\d\s]*
|
775
|
+
\s*
|
776
|
+
(#{Format::ABBR_MONTHS.keys.join('|')})[^-\d\s']*
|
777
|
+
(?:
|
778
|
+
\s*
|
779
|
+
(c(?:e|\.e\.)|b(?:ce|\.c\.e\.)|a(?:d|\.d\.)|b(?:c|\.c\.))?
|
780
|
+
\s*
|
781
|
+
('?-?\d+(?:(?:st|nd|rd|th)\b)?)
|
782
|
+
)?
|
783
|
+
/iox,
|
784
|
+
' ') # '
|
810
785
|
s3e(e, $4, Format::ABBR_MONTHS[$2.downcase], $1,
|
811
|
-
|
786
|
+
$3 && $3[0,1].downcase == 'b')
|
812
787
|
true
|
813
788
|
end
|
814
789
|
end
|
815
790
|
|
816
791
|
def self._parse_us(str, e) # :nodoc:
|
817
792
|
if str.sub!(
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
793
|
+
/\b(#{Format::ABBR_MONTHS.keys.join('|')})[^-\d\s']*
|
794
|
+
\s*
|
795
|
+
('?\d+)[^-\d\s']*
|
796
|
+
(?:
|
797
|
+
\s*
|
798
|
+
(c(?:e|\.e\.)|b(?:ce|\.c\.e\.)|a(?:d|\.d\.)|b(?:c|\.c\.))?
|
799
|
+
\s*
|
800
|
+
('?-?\d+)
|
801
|
+
)?
|
802
|
+
/iox,
|
803
|
+
' ') # '
|
829
804
|
s3e(e, $4, Format::ABBR_MONTHS[$1.downcase], $2,
|
830
|
-
|
805
|
+
$3 && $3[0,1].downcase == 'b')
|
831
806
|
true
|
832
807
|
end
|
833
808
|
end
|
834
809
|
|
835
810
|
def self._parse_iso(str, e) # :nodoc:
|
836
|
-
if str.sub!(/('?[-+]?\d+)-(\d+)-('?-?\d+)
|
811
|
+
if str.sub!(/('?[-+]?\d+)-(\d+)-('?-?\d+)/, ' ')
|
837
812
|
s3e(e, $1, $2, $3)
|
838
813
|
true
|
839
814
|
end
|
840
815
|
end
|
841
816
|
|
842
817
|
def self._parse_iso2(str, e) # :nodoc:
|
843
|
-
if str.sub!(/\b(\d{2}|\d{4})?-?w(\d{2})(?:-?(\d))?\b/
|
818
|
+
if str.sub!(/\b(\d{2}|\d{4})?-?w(\d{2})(?:-?(\d))?\b/i, ' ')
|
844
819
|
e[:cwyear] = $1.to_i if $1
|
845
820
|
e[:cweek] = $2.to_i
|
846
821
|
e[:cwday] = $3.to_i if $3
|
847
822
|
true
|
848
|
-
elsif str.sub!(/-w-(\d)\b/
|
823
|
+
elsif str.sub!(/-w-(\d)\b/i, ' ')
|
849
824
|
e[:cwday] = $1.to_i
|
850
825
|
true
|
851
|
-
elsif str.sub!(/--(\d{2})?-(\d{2})\b
|
826
|
+
elsif str.sub!(/--(\d{2})?-(\d{2})\b/, ' ')
|
852
827
|
e.mon = $1.to_i if $1
|
853
828
|
e.mday = $2.to_i
|
854
829
|
true
|
855
|
-
elsif str.sub!(/--(\d{2})(\d{2})?\b
|
830
|
+
elsif str.sub!(/--(\d{2})(\d{2})?\b/, ' ')
|
856
831
|
e.mon = $1.to_i
|
857
832
|
e.mday = $2.to_i if $2
|
858
833
|
true
|
859
|
-
elsif /[,.](\d{2}|\d{4})-\d{3}\b/
|
860
|
-
|
834
|
+
elsif /[,.](\d{2}|\d{4})-\d{3}\b/ !~ str &&
|
835
|
+
str.sub!(/\b(\d{2}|\d{4})-(\d{3})\b/, ' ')
|
861
836
|
e.year = $1.to_i
|
862
837
|
e[:yday] = $2.to_i
|
863
838
|
true
|
864
|
-
elsif /\d-\d{3}\b/
|
865
|
-
|
839
|
+
elsif /\d-\d{3}\b/ !~ str &&
|
840
|
+
str.sub!(/\b-(\d{3})\b/, ' ')
|
866
841
|
e[:yday] = $1.to_i
|
867
842
|
true
|
868
843
|
end
|
869
844
|
end
|
870
845
|
|
871
846
|
def self._parse_jis(str, e) # :nodoc:
|
872
|
-
if str.sub!(/\b([mtsh])(\d+)\.(\d+)\.(\d+)/
|
847
|
+
if str.sub!(/\b([mtsh])(\d+)\.(\d+)\.(\d+)/i, ' ')
|
873
848
|
era = { 'm'=>1867,
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
849
|
+
't'=>1911,
|
850
|
+
's'=>1925,
|
851
|
+
'h'=>1988
|
852
|
+
}[$1.downcase]
|
878
853
|
e.year = $2.to_i + era
|
879
854
|
e.mon = $3.to_i
|
880
855
|
e.mday = $4.to_i
|
@@ -884,46 +859,46 @@ class Date
|
|
884
859
|
|
885
860
|
def self._parse_vms(str, e) # :nodoc:
|
886
861
|
if str.sub!(/('?-?\d+)-(#{Format::ABBR_MONTHS.keys.join('|')})[^-]*
|
887
|
-
|
862
|
+
-('?-?\d+)/iox, ' ')
|
888
863
|
s3e(e, $3, Format::ABBR_MONTHS[$2.downcase], $1)
|
889
864
|
true
|
890
865
|
elsif str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})[^-]*
|
891
|
-
|
866
|
+
-('?-?\d+)(?:-('?-?\d+))?/iox, ' ')
|
892
867
|
s3e(e, $3, Format::ABBR_MONTHS[$1.downcase], $2)
|
893
868
|
true
|
894
869
|
end
|
895
870
|
end
|
896
871
|
|
897
872
|
def self._parse_sla(str, e) # :nodoc:
|
898
|
-
if str.sub!(%r|('?-?\d+)/\s*('?\d+)(?:\D\s*('?-?\d+))
|
899
|
-
s3e(e, $
|
873
|
+
if str.sub!(%r|('?-?\d+)/\s*('?\d+)(?:\D\s*('?-?\d+))?|, ' ') # '
|
874
|
+
s3e(e, $1, $2, $3)
|
900
875
|
true
|
901
876
|
end
|
902
877
|
end
|
903
878
|
|
904
879
|
def self._parse_dot(str, e) # :nodoc:
|
905
|
-
if str.sub!(%r|('?-?\d+)\.\s*('?\d+)\.\s*('?-?\d+)
|
880
|
+
if str.sub!(%r|('?-?\d+)\.\s*('?\d+)\.\s*('?-?\d+)|, ' ') # '
|
906
881
|
s3e(e, $1, $2, $3)
|
907
882
|
true
|
908
883
|
end
|
909
884
|
end
|
910
885
|
|
911
886
|
def self._parse_year(str, e) # :nodoc:
|
912
|
-
if str.sub!(/'(\d+)\b
|
887
|
+
if str.sub!(/'(\d+)\b/, ' ')
|
913
888
|
e.year = $1.to_i
|
914
889
|
true
|
915
890
|
end
|
916
891
|
end
|
917
892
|
|
918
893
|
def self._parse_mon(str, e) # :nodoc:
|
919
|
-
if str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})\S*/
|
894
|
+
if str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})\S*/io, ' ')
|
920
895
|
e.mon = Format::ABBR_MONTHS[$1.downcase]
|
921
896
|
true
|
922
897
|
end
|
923
898
|
end
|
924
899
|
|
925
900
|
def self._parse_mday(str, e) # :nodoc:
|
926
|
-
if str.sub!(/(\d+)(st|nd|rd|th)\b/
|
901
|
+
if str.sub!(/(\d+)(st|nd|rd|th)\b/i, ' ')
|
927
902
|
e.mday = $1.to_i
|
928
903
|
true
|
929
904
|
end
|
@@ -931,150 +906,150 @@ class Date
|
|
931
906
|
|
932
907
|
def self._parse_ddd(str, e) # :nodoc:
|
933
908
|
if str.sub!(
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
909
|
+
/([-+]?)(\d{2,14})
|
910
|
+
(?:
|
911
|
+
\s*
|
912
|
+
t?
|
913
|
+
\s*
|
914
|
+
(\d{2,6})?(?:[,.](\d*))?
|
915
|
+
)?
|
916
|
+
(?:
|
917
|
+
\s*
|
918
|
+
(
|
919
|
+
z\b
|
920
|
+
|
|
921
|
+
[-+]\d{1,4}\b
|
922
|
+
|
|
923
|
+
\[[-+]?\d[^\]]*\]
|
924
|
+
)
|
925
|
+
)?
|
926
|
+
/ix,
|
927
|
+
' ')
|
953
928
|
case $2.size
|
954
929
|
when 2
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
930
|
+
if $3.nil? && $4
|
931
|
+
e.sec = $2[-2, 2].to_i
|
932
|
+
else
|
933
|
+
e.mday = $2[ 0, 2].to_i
|
934
|
+
end
|
960
935
|
when 4
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
936
|
+
if $3.nil? && $4
|
937
|
+
e.sec = $2[-2, 2].to_i
|
938
|
+
e.min = $2[-4, 2].to_i
|
939
|
+
else
|
940
|
+
e.mon = $2[ 0, 2].to_i
|
941
|
+
e.mday = $2[ 2, 2].to_i
|
942
|
+
end
|
968
943
|
when 6
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
944
|
+
if $3.nil? && $4
|
945
|
+
e.sec = $2[-2, 2].to_i
|
946
|
+
e.min = $2[-4, 2].to_i
|
947
|
+
e.hour = $2[-6, 2].to_i
|
948
|
+
else
|
949
|
+
e.year = ($1 + $2[ 0, 2]).to_i
|
950
|
+
e.mon = $2[ 2, 2].to_i
|
951
|
+
e.mday = $2[ 4, 2].to_i
|
952
|
+
end
|
978
953
|
when 8, 10, 12, 14
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
954
|
+
if $3.nil? && $4
|
955
|
+
e.sec = $2[-2, 2].to_i
|
956
|
+
e.min = $2[-4, 2].to_i
|
957
|
+
e.hour = $2[-6, 2].to_i
|
958
|
+
e.mday = $2[-8, 2].to_i
|
959
|
+
if $2.size >= 10
|
960
|
+
e.mon = $2[-10, 2].to_i
|
961
|
+
end
|
962
|
+
if $2.size == 12
|
963
|
+
e.year = ($1 + $2[-12, 2]).to_i
|
964
|
+
end
|
965
|
+
if $2.size == 14
|
966
|
+
e.year = ($1 + $2[-14, 4]).to_i
|
967
|
+
e._comp = false
|
968
|
+
end
|
969
|
+
else
|
970
|
+
e.year = ($1 + $2[ 0, 4]).to_i
|
971
|
+
e.mon = $2[ 4, 2].to_i
|
972
|
+
e.mday = $2[ 6, 2].to_i
|
973
|
+
e.hour = $2[ 8, 2].to_i if $2.size >= 10
|
974
|
+
e.min = $2[10, 2].to_i if $2.size >= 12
|
975
|
+
e.sec = $2[12, 2].to_i if $2.size >= 14
|
976
|
+
e._comp = false
|
977
|
+
end
|
1003
978
|
when 3
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
979
|
+
if $3.nil? && $4
|
980
|
+
e.sec = $2[-2, 2].to_i
|
981
|
+
e.min = $2[-3, 1].to_i
|
982
|
+
else
|
983
|
+
e[:yday] = $2[ 0, 3].to_i
|
984
|
+
end
|
1010
985
|
when 5
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
986
|
+
if $3.nil? && $4
|
987
|
+
e.sec = $2[-2, 2].to_i
|
988
|
+
e.min = $2[-4, 2].to_i
|
989
|
+
e.hour = $2[-5, 1].to_i
|
990
|
+
else
|
991
|
+
e.year = ($1 + $2[ 0, 2]).to_i
|
992
|
+
e[:yday] = $2[ 2, 3].to_i
|
993
|
+
end
|
1019
994
|
when 7
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
995
|
+
if $3.nil? && $4
|
996
|
+
e.sec = $2[-2, 2].to_i
|
997
|
+
e.min = $2[-4, 2].to_i
|
998
|
+
e.hour = $2[-6, 2].to_i
|
999
|
+
e.mday = $2[-7, 1].to_i
|
1000
|
+
else
|
1001
|
+
e.year = ($1 + $2[ 0, 4]).to_i
|
1002
|
+
e[:yday] = $2[ 4, 3].to_i
|
1003
|
+
end
|
1029
1004
|
end
|
1030
1005
|
if $3
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1006
|
+
if $4
|
1007
|
+
case $3.size
|
1008
|
+
when 2, 4, 6
|
1009
|
+
e.sec = $3[-2, 2].to_i
|
1010
|
+
e.min = $3[-4, 2].to_i if $3.size >= 4
|
1011
|
+
e.hour = $3[-6, 2].to_i if $3.size >= 6
|
1012
|
+
end
|
1013
|
+
else
|
1014
|
+
case $3.size
|
1015
|
+
when 2, 4, 6
|
1016
|
+
e.hour = $3[ 0, 2].to_i
|
1017
|
+
e.min = $3[ 2, 2].to_i if $3.size >= 4
|
1018
|
+
e.sec = $3[ 4, 2].to_i if $3.size >= 6
|
1019
|
+
end
|
1020
|
+
end
|
1046
1021
|
end
|
1047
1022
|
if $4
|
1048
|
-
|
1023
|
+
e.sec_fraction = Rational($4.to_i, 10**$4.size)
|
1049
1024
|
end
|
1050
1025
|
if $5
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1026
|
+
e.zone = $5
|
1027
|
+
if e.zone[0,1] == '['
|
1028
|
+
o, n, = e.zone[1..-2].split(':')
|
1029
|
+
e.zone = n || o
|
1030
|
+
if /\A\d/ =~ o
|
1031
|
+
o = format('+%s', o)
|
1032
|
+
end
|
1033
|
+
e.offset = zone_to_diff(o)
|
1034
|
+
end
|
1060
1035
|
end
|
1061
1036
|
true
|
1062
1037
|
end
|
1063
1038
|
end
|
1064
1039
|
|
1065
1040
|
private_class_method :_parse_day, :_parse_time, # :_parse_beat,
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1041
|
+
:_parse_eu, :_parse_us, :_parse_iso, :_parse_iso2,
|
1042
|
+
:_parse_jis, :_parse_vms, :_parse_sla, :_parse_dot,
|
1043
|
+
:_parse_year, :_parse_mon, :_parse_mday, :_parse_ddd
|
1069
1044
|
|
1070
|
-
def self._parse(str, comp=
|
1045
|
+
def self._parse(str, comp=true)
|
1071
1046
|
str = str.dup
|
1072
1047
|
|
1073
1048
|
e = Format::Bag.new
|
1074
1049
|
|
1075
1050
|
e._comp = comp
|
1076
1051
|
|
1077
|
-
str.gsub!(/[^-+',.\/:@[:alnum:]\[\]
|
1052
|
+
str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ')
|
1078
1053
|
|
1079
1054
|
_parse_time(str, e) # || _parse_beat(str, e)
|
1080
1055
|
_parse_day(str, e)
|
@@ -1092,82 +1067,228 @@ class Date
|
|
1092
1067
|
_parse_mday(str, e) ||
|
1093
1068
|
_parse_ddd(str, e)
|
1094
1069
|
|
1095
|
-
if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/
|
1070
|
+
if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/i, ' ')
|
1096
1071
|
if e.year
|
1097
|
-
|
1072
|
+
e.year = -e.year + 1
|
1098
1073
|
end
|
1099
1074
|
end
|
1100
1075
|
|
1101
|
-
if str.sub!(/\A\s*(\d{1,2})\s*\z
|
1076
|
+
if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ')
|
1102
1077
|
if e.hour && !e.mday
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1078
|
+
v = $1.to_i
|
1079
|
+
if (1..31) === v
|
1080
|
+
e.mday = v
|
1081
|
+
end
|
1107
1082
|
end
|
1108
1083
|
if e.mday && !e.hour
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1084
|
+
v = $1.to_i
|
1085
|
+
if (0..24) === v
|
1086
|
+
e.hour = v
|
1087
|
+
end
|
1113
1088
|
end
|
1114
1089
|
end
|
1115
1090
|
|
1116
1091
|
if e._comp
|
1117
1092
|
if e[:cwyear]
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1093
|
+
if e[:cwyear] >= 0 && e[:cwyear] <= 99
|
1094
|
+
e[:cwyear] += if e[:cwyear] >= 69
|
1095
|
+
then 1900 else 2000 end
|
1096
|
+
end
|
1122
1097
|
end
|
1123
1098
|
if e.year
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1099
|
+
if e.year >= 0 && e.year <= 99
|
1100
|
+
e.year += if e.year >= 69
|
1101
|
+
then 1900 else 2000 end
|
1102
|
+
end
|
1128
1103
|
end
|
1129
1104
|
end
|
1130
1105
|
|
1131
1106
|
e.offset ||= zone_to_diff(e.zone) if e.zone
|
1132
1107
|
|
1133
|
-
return e if return_bag
|
1134
1108
|
e.to_hash
|
1135
1109
|
end
|
1136
1110
|
|
1137
|
-
def self.
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1111
|
+
def self._iso8601(str) # :nodoc:
|
1112
|
+
if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
|
1113
|
+
([-+]?\d{2,})?-\d{3}|
|
1114
|
+
(\d{2}|\d{4})?-w\d{2}-\d|
|
1115
|
+
-w-\d)
|
1116
|
+
(t
|
1117
|
+
\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
|
1118
|
+
(z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
|
1119
|
+
_parse(str)
|
1120
|
+
elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}|
|
1121
|
+
([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}|
|
1122
|
+
(\d{2}|\d{4})?w\d{2}\d)
|
1123
|
+
(t?
|
1124
|
+
\d{2}\d{2}(\d{2}([,.]\d+)?)?
|
1125
|
+
(z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
|
1126
|
+
_parse(str)
|
1127
|
+
elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
|
1128
|
+
(z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
|
1129
|
+
_parse(str)
|
1130
|
+
elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)?
|
1131
|
+
(z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
|
1132
|
+
_parse(str)
|
1133
|
+
end
|
1134
|
+
end
|
1135
|
+
|
1136
|
+
def self._rfc3339(str) # :nodoc:
|
1137
|
+
if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway
|
1138
|
+
(t|\s)
|
1139
|
+
\d{2}:\d{2}:\d{2}(\.\d+)?
|
1140
|
+
(z|[-+]\d{2}:\d{2})\s*\z/ix =~ str
|
1141
|
+
_parse(str)
|
1142
|
+
end
|
1143
|
+
end
|
1144
|
+
|
1145
|
+
def self._xmlschema(str) # :nodoc:
|
1146
|
+
if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)?
|
1147
|
+
(?:t
|
1148
|
+
(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?
|
1149
|
+
(z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
|
1150
|
+
e = Format::Bag.new
|
1151
|
+
e.year = $1.to_i
|
1152
|
+
e.mon = $2.to_i if $2
|
1153
|
+
e.mday = $3.to_i if $3
|
1154
|
+
e.hour = $4.to_i if $4
|
1155
|
+
e.min = $5.to_i if $5
|
1156
|
+
e.sec = $6.to_i if $6
|
1157
|
+
e.sec_fraction = Rational($7.to_i, 10**$7.size) if $7
|
1158
|
+
if $8
|
1159
|
+
e.zone = $8
|
1160
|
+
e.offset = zone_to_diff($8)
|
1161
|
+
end
|
1162
|
+
e.to_hash
|
1163
|
+
elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
|
1164
|
+
(z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
|
1165
|
+
e = Format::Bag.new
|
1166
|
+
e.hour = $1.to_i if $1
|
1167
|
+
e.min = $2.to_i if $2
|
1168
|
+
e.sec = $3.to_i if $3
|
1169
|
+
e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4
|
1170
|
+
if $5
|
1171
|
+
e.zone = $5
|
1172
|
+
e.offset = zone_to_diff($5)
|
1173
|
+
end
|
1174
|
+
e.to_hash
|
1175
|
+
elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2}))
|
1176
|
+
(z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
|
1177
|
+
e = Format::Bag.new
|
1178
|
+
e.mon = $1.to_i if $1
|
1179
|
+
e.mday = $2.to_i if $2
|
1180
|
+
e.mday = $3.to_i if $3
|
1181
|
+
if $4
|
1182
|
+
e.zone = $4
|
1183
|
+
e.offset = zone_to_diff($4)
|
1184
|
+
end
|
1185
|
+
e.to_hash
|
1186
|
+
end
|
1187
|
+
end
|
1188
|
+
|
1189
|
+
def self._rfc2822(str) # :nodoc:
|
1190
|
+
if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
|
1191
|
+
\d{1,2}\s+
|
1192
|
+
(?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
|
1193
|
+
-?(\d{2,})\s+ # allow minus, anyway
|
1194
|
+
\d{2}:\d{2}(:\d{2})?\s*
|
1195
|
+
(?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/iox =~ str
|
1196
|
+
e = _parse(str, false)
|
1197
|
+
if $1.size < 4
|
1198
|
+
if e[:year] < 50
|
1199
|
+
e[:year] += 2000
|
1200
|
+
elsif e[:year] < 1000
|
1201
|
+
e[:year] += 1900
|
1202
|
+
end
|
1203
|
+
end
|
1204
|
+
e
|
1205
|
+
end
|
1206
|
+
end
|
1207
|
+
|
1208
|
+
class << self; alias_method :_rfc822, :_rfc2822 end
|
1209
|
+
|
1210
|
+
def self._httpdate(str) # :nodoc:
|
1211
|
+
if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
|
1212
|
+
\d{2}\s+
|
1213
|
+
(#{Format::ABBR_MONTHS.keys.join('|')})\s+
|
1214
|
+
-?\d{4}\s+ # allow minus, anyway
|
1215
|
+
\d{2}:\d{2}:\d{2}\s+
|
1216
|
+
gmt\s*\z/iox =~ str
|
1217
|
+
_rfc2822(str)
|
1218
|
+
elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
|
1219
|
+
\d{2}\s*-\s*
|
1220
|
+
(#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
|
1221
|
+
\d{2}\s+
|
1222
|
+
\d{2}:\d{2}:\d{2}\s+
|
1223
|
+
gmt\s*\z/iox =~ str
|
1224
|
+
_parse(str)
|
1225
|
+
elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
|
1226
|
+
(#{Format::ABBR_MONTHS.keys.join('|')})\s+
|
1227
|
+
\d{1,2}\s+
|
1228
|
+
\d{2}:\d{2}:\d{2}\s+
|
1229
|
+
\d{4}\s*\z/iox =~ str
|
1230
|
+
_parse(str)
|
1231
|
+
end
|
1232
|
+
end
|
1233
|
+
|
1234
|
+
def self._jisx0301(str) # :nodoc:
|
1235
|
+
if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2}
|
1236
|
+
(t
|
1237
|
+
(\d{2}:\d{2}(:\d{2}([,.]\d*)?)?
|
1238
|
+
(z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/ix =~ str
|
1239
|
+
if /\A\s*\d/ =~ str
|
1240
|
+
_parse(str.sub(/\A\s*(\d)/, 'h\1'))
|
1241
|
+
else
|
1242
|
+
_parse(str)
|
1243
|
+
end
|
1141
1244
|
else
|
1142
|
-
|
1245
|
+
_iso8601(str)
|
1143
1246
|
end
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1247
|
+
end
|
1248
|
+
|
1249
|
+
t = Module.new do
|
1250
|
+
|
1251
|
+
private
|
1252
|
+
|
1253
|
+
def zone_to_diff(zone) # :nodoc:
|
1254
|
+
zone = zone.downcase
|
1255
|
+
if zone.sub!(/\s+(standard|daylight)\s+time\z/, '')
|
1256
|
+
dst = $1 == 'daylight'
|
1154
1257
|
else
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1258
|
+
dst = zone.sub!(/\s+dst\z/, '')
|
1259
|
+
end
|
1260
|
+
if Format::ZONES.include?(zone)
|
1261
|
+
offset = Format::ZONES[zone]
|
1262
|
+
offset += 3600 if dst
|
1263
|
+
elsif zone.sub!(/\A(?:gmt|utc?)?([-+])/, '')
|
1264
|
+
sign = $1
|
1265
|
+
if zone.include?(':')
|
1266
|
+
hour, min, sec, = zone.split(':')
|
1267
|
+
elsif zone.include?(',') || zone.include?('.')
|
1268
|
+
hour, fr, = zone.split(/[,.]/)
|
1269
|
+
min = Rational(fr.to_i, 10**fr.size) * 60
|
1270
|
+
else
|
1271
|
+
case zone.size
|
1272
|
+
when 3
|
1273
|
+
hour = zone[0,1]
|
1274
|
+
min = zone[1,2]
|
1275
|
+
else
|
1276
|
+
hour = zone[0,2]
|
1277
|
+
min = zone[2,2]
|
1278
|
+
sec = zone[4,2]
|
1279
|
+
end
|
1280
|
+
end
|
1281
|
+
offset = hour.to_i * 3600 + min.to_i * 60 + sec.to_i
|
1282
|
+
offset *= -1 if sign == '-'
|
1164
1283
|
end
|
1165
|
-
offset
|
1166
|
-
offset *= -1 if sign == '-'
|
1284
|
+
offset
|
1167
1285
|
end
|
1168
|
-
|
1286
|
+
|
1169
1287
|
end
|
1170
1288
|
|
1289
|
+
extend t
|
1290
|
+
include t
|
1291
|
+
|
1171
1292
|
end
|
1172
1293
|
|
1173
1294
|
class DateTime < Date
|
@@ -1180,15 +1301,14 @@ class DateTime < Date
|
|
1180
1301
|
super(str, fmt)
|
1181
1302
|
end
|
1182
1303
|
|
1183
|
-
=begin
|
1184
1304
|
def iso8601_timediv(n) # :nodoc:
|
1185
1305
|
strftime('T%T' +
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1306
|
+
if n < 1
|
1307
|
+
''
|
1308
|
+
else
|
1309
|
+
'.%0*d' % [n, (sec_fraction / Rational(1, 10**n)).round]
|
1310
|
+
end +
|
1311
|
+
'%:z')
|
1192
1312
|
end
|
1193
1313
|
|
1194
1314
|
private :iso8601_timediv
|
@@ -1199,9 +1319,10 @@ class DateTime < Date
|
|
1199
1319
|
|
1200
1320
|
def rfc3339(n=0) iso8601(n) end
|
1201
1321
|
|
1322
|
+
def xmlschema(n=0) iso8601(n) end # :nodoc:
|
1323
|
+
|
1202
1324
|
def jisx0301(n=0)
|
1203
1325
|
super() + iso8601_timediv(n)
|
1204
1326
|
end
|
1205
|
-
=end
|
1206
1327
|
|
1207
1328
|
end
|