rubysl-time 1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7d3e6821ee1fe3334182e7a064308c72b9dfacb
4
+ data.tar.gz: d68ac18351fda6e3e29cb32368fb34d7b01a8916
5
+ SHA512:
6
+ metadata.gz: fd375cb1994ca2c22f784bf3b886e62d74efde8589816017a38bb03d42ab11c1926ba50020b457e69adfb6e66ba5d880aad341b00ab7a9164953d5111547afc2
7
+ data.tar.gz: 48a1bb0c79d29e72ab7c11d848faa4ff6afcc8717a15766818bf03af684af870c094813233f278c4df651f9e2f3d4c62181a69598cd207aefbb98ba6fd4040e9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
5
+ rvm:
6
+ - 1.8.7
7
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-time.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rubysl::Time
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-time'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-time
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "rubysl/time/version"
2
+ require "rubysl/time/time"
@@ -0,0 +1,829 @@
1
+
2
+ #
3
+ # == Introduction
4
+ #
5
+ # This library extends the Time class:
6
+ # * conversion between date string and time object.
7
+ # * date-time defined by RFC 2822
8
+ # * HTTP-date defined by RFC 2616
9
+ # * dateTime defined by XML Schema Part 2: Datatypes (ISO 8601)
10
+ # * various formats handled by Date._parse (string to time only)
11
+ #
12
+ # == Design Issues
13
+ #
14
+ # === Specialized interface
15
+ #
16
+ # This library provides methods dedicated to special purposes:
17
+ # * RFC 2822, RFC 2616 and XML Schema.
18
+ # * They makes usual life easier.
19
+ #
20
+ # === Doesn't depend on strftime
21
+ #
22
+ # This library doesn't use +strftime+. Especially #rfc2822 doesn't depend
23
+ # on +strftime+ because:
24
+ #
25
+ # * %a and %b are locale sensitive
26
+ #
27
+ # Since they are locale sensitive, they may be replaced to
28
+ # invalid weekday/month name in some locales.
29
+ # Since ruby-1.6 doesn't invoke setlocale by default,
30
+ # the problem doesn't arise until some external library invokes setlocale.
31
+ # Ruby/GTK is the example of such library.
32
+ #
33
+ # * %z is not portable
34
+ #
35
+ # %z is required to generate zone in date-time of RFC 2822
36
+ # but it is not portable.
37
+ #
38
+ # == Revision Information
39
+ #
40
+ # $Id$
41
+ #
42
+
43
+ require 'parsedate'
44
+
45
+ #
46
+ # Implements the extensions to the Time class that are described in the
47
+ # documentation for the time.rb library.
48
+ #
49
+ class Time
50
+ class << Time
51
+
52
+ ZoneOffset = {
53
+ 'UTC' => 0,
54
+ # ISO 8601
55
+ 'Z' => 0,
56
+ # RFC 822
57
+ 'UT' => 0, 'GMT' => 0,
58
+ 'EST' => -5, 'EDT' => -4,
59
+ 'CST' => -6, 'CDT' => -5,
60
+ 'MST' => -7, 'MDT' => -6,
61
+ 'PST' => -8, 'PDT' => -7,
62
+ # Following definition of military zones is original one.
63
+ # See RFC 1123 and RFC 2822 for the error in RFC 822.
64
+ 'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
65
+ 'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12,
66
+ 'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
67
+ 'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12,
68
+ }
69
+ def zone_offset(zone, year=self.now.year)
70
+ off = nil
71
+ zone = zone.upcase
72
+ if /\A([+-])(\d\d):?(\d\d)\z/ =~ zone
73
+ off = ($1 == '-' ? -1 : 1) * ($2.to_i * 60 + $3.to_i) * 60
74
+ elsif /\A[+-]\d\d\z/ =~ zone
75
+ off = zone.to_i * 3600
76
+ elsif ZoneOffset.include?(zone)
77
+ off = ZoneOffset[zone] * 3600
78
+ elsif ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false)
79
+ off = t.utc_offset
80
+ elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false)
81
+ off = t.utc_offset
82
+ end
83
+ off
84
+ end
85
+
86
+ def zone_utc?(zone)
87
+ # * +0000 means localtime. [RFC 2822]
88
+ # * GMT is a localtime abbreviation in Europe/London, etc.
89
+ if /\A(?:-00:00|-0000|-00|UTC|Z|UT)\z/i =~ zone
90
+ true
91
+ else
92
+ false
93
+ end
94
+ end
95
+ private :zone_utc?
96
+
97
+ LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
98
+ CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
99
+ def month_days(y, m)
100
+ if ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)
101
+ LeapYearMonthDays[m-1]
102
+ else
103
+ CommonYearMonthDays[m-1]
104
+ end
105
+ end
106
+ private :month_days
107
+
108
+ def apply_offset(year, mon, day, hour, min, sec, off)
109
+ if off < 0
110
+ off = -off
111
+ off, o = off.divmod(60)
112
+ if o != 0 then sec += o; o, sec = sec.divmod(60); off += o end
113
+ off, o = off.divmod(60)
114
+ if o != 0 then min += o; o, min = min.divmod(60); off += o end
115
+ off, o = off.divmod(24)
116
+ if o != 0 then hour += o; o, hour = hour.divmod(24); off += o end
117
+ if off != 0
118
+ day += off
119
+ if month_days(year, mon) < day
120
+ mon += 1
121
+ if 12 < mon
122
+ mon = 1
123
+ year += 1
124
+ end
125
+ day = 1
126
+ end
127
+ end
128
+ elsif 0 < off
129
+ off, o = off.divmod(60)
130
+ if o != 0 then sec -= o; o, sec = sec.divmod(60); off -= o end
131
+ off, o = off.divmod(60)
132
+ if o != 0 then min -= o; o, min = min.divmod(60); off -= o end
133
+ off, o = off.divmod(24)
134
+ if o != 0 then hour -= o; o, hour = hour.divmod(24); off -= o end
135
+ if off != 0 then
136
+ day -= off
137
+ if day < 1
138
+ mon -= 1
139
+ if mon < 1
140
+ year -= 1
141
+ mon = 12
142
+ end
143
+ day = month_days(year, mon)
144
+ end
145
+ end
146
+ end
147
+ return year, mon, day, hour, min, sec
148
+ end
149
+ private :apply_offset
150
+
151
+ def make_time(year, mon, day, hour, min, sec, sec_fraction, zone, now)
152
+ usec = nil
153
+ usec = (sec_fraction * 1000000).to_i if sec_fraction
154
+ if now
155
+ begin
156
+ break if year; year = now.year
157
+ break if mon; mon = now.mon
158
+ break if day; day = now.day
159
+ break if hour; hour = now.hour
160
+ break if min; min = now.min
161
+ break if sec; sec = now.sec
162
+ break if sec_fraction; usec = now.tv_usec
163
+ end until true
164
+ end
165
+
166
+ year ||= 1970
167
+ mon ||= 1
168
+ day ||= 1
169
+ hour ||= 0
170
+ min ||= 0
171
+ sec ||= 0
172
+ usec ||= 0
173
+
174
+ off = nil
175
+ off = zone_offset(zone, year) if zone
176
+
177
+ if off
178
+ year, mon, day, hour, min, sec =
179
+ apply_offset(year, mon, day, hour, min, sec, off)
180
+ t = self.utc(year, mon, day, hour, min, sec, usec)
181
+ t.localtime if !zone_utc?(zone)
182
+ t
183
+ else
184
+ self.local(year, mon, day, hour, min, sec, usec)
185
+ end
186
+ end
187
+ private :make_time
188
+
189
+ #
190
+ # Parses +date+ using Date._parse and converts it to a Time object.
191
+ #
192
+ # If a block is given, the year described in +date+ is converted by the
193
+ # block. For example:
194
+ #
195
+ # Time.parse(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
196
+ #
197
+ # If the upper components of the given time are broken or missing, they are
198
+ # supplied with those of +now+. For the lower components, the minimum
199
+ # values (1 or 0) are assumed if broken or missing. For example:
200
+ #
201
+ # # Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
202
+ # # your timezone is GMT:
203
+ # Time.parse("16:30") #=> Thu Nov 29 16:30:00 GMT 2001
204
+ # Time.parse("7/23") #=> Mon Jul 23 00:00:00 GMT 2001
205
+ # Time.parse("Aug 31") #=> Fri Aug 31 00:00:00 GMT 2001
206
+ #
207
+ # Since there are numerous conflicts among locally defined timezone
208
+ # abbreviations all over the world, this method is not made to
209
+ # understand all of them. For example, the abbreviation "CST" is
210
+ # used variously as:
211
+ #
212
+ # -06:00 in America/Chicago,
213
+ # -05:00 in America/Havana,
214
+ # +08:00 in Asia/Harbin,
215
+ # +09:30 in Australia/Darwin,
216
+ # +10:30 in Australia/Adelaide,
217
+ # etc.
218
+ #
219
+ # Based on the fact, this method only understands the timezone
220
+ # abbreviations described in RFC 822 and the system timezone, in the
221
+ # order named. (i.e. a definition in RFC 822 overrides the system
222
+ # timezone definition.) The system timezone is taken from
223
+ # <tt>Time.local(year, 1, 1).zone</tt> and
224
+ # <tt>Time.local(year, 7, 1).zone</tt>.
225
+ # If the extracted timezone abbreviation does not match any of them,
226
+ # it is ignored and the given time is regarded as a local time.
227
+ #
228
+ # ArgumentError is raised if Date._parse cannot extract information from
229
+ # +date+ or Time class cannot represent specified date.
230
+ #
231
+ # This method can be used as fail-safe for other parsing methods as:
232
+ #
233
+ # Time.rfc2822(date) rescue Time.parse(date)
234
+ # Time.httpdate(date) rescue Time.parse(date)
235
+ # Time.xmlschema(date) rescue Time.parse(date)
236
+ #
237
+ # A failure for Time.parse should be checked, though.
238
+ #
239
+ def parse(date, now=self.now)
240
+ d = Date._parse(date, false, true)
241
+ year = d.year
242
+ year = yield(year) if year && block_given?
243
+ make_time(year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction, d.zone, now)
244
+ end
245
+
246
+ MonthValue = {
247
+ 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
248
+ 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
249
+ }
250
+
251
+ #
252
+ # Parses +date+ as date-time defined by RFC 2822 and converts it to a Time
253
+ # object. The format is identical to the date format defined by RFC 822 and
254
+ # updated by RFC 1123.
255
+ #
256
+ # ArgumentError is raised if +date+ is not compliant with RFC 2822
257
+ # or Time class cannot represent specified date.
258
+ #
259
+ # See #rfc2822 for more information on this format.
260
+ #
261
+ def rfc2822(date)
262
+ if /\A\s*
263
+ (?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?
264
+ (\d{1,2})\s+
265
+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+
266
+ (\d{2,})\s+
267
+ (\d{2})\s*
268
+ :\s*(\d{2})\s*
269
+ (?::\s*(\d{2}))?\s+
270
+ ([+-]\d{4}|
271
+ UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date
272
+ # Since RFC 2822 permit comments, the regexp has no right anchor.
273
+ day = $1.to_i
274
+ mon = MonthValue[$2.upcase]
275
+ year = $3.to_i
276
+ hour = $4.to_i
277
+ min = $5.to_i
278
+ sec = $6 ? $6.to_i : 0
279
+ zone = $7
280
+
281
+ # following year completion is compliant with RFC 2822.
282
+ year = if year < 50
283
+ 2000 + year
284
+ elsif year < 1000
285
+ 1900 + year
286
+ else
287
+ year
288
+ end
289
+
290
+ year, mon, day, hour, min, sec =
291
+ apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
292
+ t = self.utc(year, mon, day, hour, min, sec)
293
+ t.localtime if !zone_utc?(zone)
294
+ t
295
+ else
296
+ raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
297
+ end
298
+ end
299
+ alias rfc822 rfc2822
300
+
301
+ #
302
+ # Parses +date+ as HTTP-date defined by RFC 2616 and converts it to a Time
303
+ # object.
304
+ #
305
+ # ArgumentError is raised if +date+ is not compliant with RFC 2616 or Time
306
+ # class cannot represent specified date.
307
+ #
308
+ # See #httpdate for more information on this format.
309
+ #
310
+ def httpdate(date)
311
+ if /\A\s*
312
+ (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\x20
313
+ (\d{2})\x20
314
+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
315
+ (\d{4})\x20
316
+ (\d{2}):(\d{2}):(\d{2})\x20
317
+ GMT
318
+ \s*\z/ix =~ date
319
+ self.rfc2822(date)
320
+ elsif /\A\s*
321
+ (?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday),\x20
322
+ (\d\d)-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d\d)\x20
323
+ (\d\d):(\d\d):(\d\d)\x20
324
+ GMT
325
+ \s*\z/ix =~ date
326
+ self.parse(date)
327
+ elsif /\A\s*
328
+ (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\x20
329
+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
330
+ (\d\d|\x20\d)\x20
331
+ (\d\d):(\d\d):(\d\d)\x20
332
+ (\d{4})
333
+ \s*\z/ix =~ date
334
+ self.utc($6.to_i, MonthValue[$1.upcase], $2.to_i,
335
+ $3.to_i, $4.to_i, $5.to_i)
336
+ else
337
+ raise ArgumentError.new("not RFC 2616 compliant date: #{date.inspect}")
338
+ end
339
+ end
340
+
341
+ #
342
+ # Parses +date+ as dateTime defined by XML Schema and converts it to a Time
343
+ # object. The format is restricted version of the format defined by ISO
344
+ # 8601.
345
+ #
346
+ # ArgumentError is raised if +date+ is not compliant with the format or Time
347
+ # class cannot represent specified date.
348
+ #
349
+ # See #xmlschema for more information on this format.
350
+ #
351
+ def xmlschema(date)
352
+ if /\A\s*
353
+ (-?\d+)-(\d\d)-(\d\d)
354
+ T
355
+ (\d\d):(\d\d):(\d\d)
356
+ (\.\d*)?
357
+ (Z|[+-]\d\d:\d\d)?
358
+ \s*\z/ix =~ date
359
+ year = $1.to_i
360
+ mon = $2.to_i
361
+ day = $3.to_i
362
+ hour = $4.to_i
363
+ min = $5.to_i
364
+ sec = $6.to_i
365
+ usec = 0
366
+ usec = ($7[1..-1] + '000000')[0,6].to_i if $7
367
+ if $8
368
+ zone = $8
369
+ year, mon, day, hour, min, sec =
370
+ apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
371
+ self.utc(year, mon, day, hour, min, sec, usec)
372
+ else
373
+ self.local(year, mon, day, hour, min, sec, usec)
374
+ end
375
+ else
376
+ raise ArgumentError.new("invalid date: #{date.inspect}")
377
+ end
378
+ end
379
+ alias iso8601 xmlschema
380
+ end # class << self
381
+
382
+ #
383
+ # Returns a string which represents the time as date-time defined by RFC 2822:
384
+ #
385
+ # day-of-week, DD month-name CCYY hh:mm:ss zone
386
+ #
387
+ # where zone is [+-]hhmm.
388
+ #
389
+ # If +self+ is a UTC time, -0000 is used as zone.
390
+ #
391
+ def rfc2822
392
+ sprintf('%s, %02d %s %d %02d:%02d:%02d ',
393
+ RFC2822_DAY_NAME[wday],
394
+ day, RFC2822_MONTH_NAME[mon-1], year,
395
+ hour, min, sec) +
396
+ if utc?
397
+ '-0000'
398
+ else
399
+ off = utc_offset
400
+ sign = off < 0 ? '-' : '+'
401
+ sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))
402
+ end
403
+ end
404
+ alias rfc822 rfc2822
405
+
406
+ RFC2822_DAY_NAME = [
407
+ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
408
+ ]
409
+ RFC2822_MONTH_NAME = [
410
+ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
411
+ 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
412
+ ]
413
+
414
+ #
415
+ # Returns a string which represents the time as rfc1123-date of HTTP-date
416
+ # defined by RFC 2616:
417
+ #
418
+ # day-of-week, DD month-name CCYY hh:mm:ss GMT
419
+ #
420
+ # Note that the result is always UTC (GMT).
421
+ #
422
+ def httpdate
423
+ t = dup.utc
424
+ # sprintf('%s, %02d %s %d %02d:%02d:%02d GMT',
425
+ # RFC2822_DAY_NAME[t.wday],
426
+ # t.day, RFC2822_MONTH_NAME[t.mon-1], t.year,
427
+ # t.hour, t.min, t.sec)
428
+
429
+ day = t.day
430
+ if day < 10
431
+ day = "0#{day}"
432
+ else
433
+ day = day.to_s
434
+ end
435
+
436
+ hour = t.hour
437
+ if hour < 10
438
+ hour = "0#{hour}"
439
+ else
440
+ hour = hour.to_s
441
+ end
442
+
443
+ min = t.min
444
+ if min < 10
445
+ min = "0#{min}"
446
+ else
447
+ min = min.to_s
448
+ end
449
+
450
+ sec = t.sec
451
+ if sec < 10
452
+ sec = "0#{sec}"
453
+ else
454
+ sec = sec.to_s
455
+ end
456
+
457
+ "#{RFC2822_DAY_NAME[t.wday]}, #{day} #{RFC2822_MONTH_NAME[t.mon-1]} #{t.year} #{hour}:#{min}:#{sec} GMT"
458
+ end
459
+
460
+ #
461
+ # Returns a string which represents the time as dateTime defined by XML
462
+ # Schema:
463
+ #
464
+ # CCYY-MM-DDThh:mm:ssTZD
465
+ # CCYY-MM-DDThh:mm:ss.sssTZD
466
+ #
467
+ # where TZD is Z or [+-]hh:mm.
468
+ #
469
+ # If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
470
+ #
471
+ # +fractional_seconds+ specifies a number of digits of fractional seconds.
472
+ # Its default value is 0.
473
+ #
474
+ def xmlschema(fraction_digits=0)
475
+ sprintf('%d-%02d-%02dT%02d:%02d:%02d',
476
+ year, mon, day, hour, min, sec) +
477
+ if fraction_digits == 0
478
+ ''
479
+ elsif fraction_digits <= 6
480
+ '.' + sprintf('%06d', usec)[0, fraction_digits]
481
+ else
482
+ '.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
483
+ end +
484
+ if utc?
485
+ 'Z'
486
+ else
487
+ off = utc_offset
488
+ sign = off < 0 ? '-' : '+'
489
+ sprintf('%s%02d:%02d', sign, *(off.abs / 60).divmod(60))
490
+ end
491
+ end
492
+ alias iso8601 xmlschema
493
+ end
494
+
495
+ if __FILE__ == $0
496
+ require 'test/unit'
497
+
498
+ class TimeExtentionTest < Test::Unit::TestCase # :nodoc:
499
+ def test_rfc822
500
+ assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600,
501
+ Time.rfc2822("26 Aug 76 14:30 EDT"))
502
+ assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600,
503
+ Time.rfc2822("27 Aug 76 09:32 PDT"))
504
+ end
505
+
506
+ def test_rfc2822
507
+ assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
508
+ Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600"))
509
+ assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600,
510
+ Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200"))
511
+ assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600,
512
+ Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600"))
513
+ assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600,
514
+ Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600"))
515
+ assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600,
516
+ Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800"))
517
+ begin
518
+ Time.at(-1)
519
+ rescue ArgumentError
520
+ # ignore
521
+ else
522
+ assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60,
523
+ Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330"))
524
+ assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60,
525
+ Time.rfc2822(" Thu,
526
+ 13
527
+ Feb
528
+ 1969
529
+ 23:32
530
+ -0330 (Newfoundland Time)"))
531
+ end
532
+ assert_equal(Time.utc(1997, 11, 21, 9, 55, 6),
533
+ Time.rfc2822("21 Nov 97 09:55:06 GMT"))
534
+ assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
535
+ Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600"))
536
+ assert_raise(ArgumentError) {
537
+ # inner comment is not supported.
538
+ Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600")
539
+ }
540
+ end
541
+
542
+ def test_rfc2616
543
+ t = Time.utc(1994, 11, 6, 8, 49, 37)
544
+ assert_equal(t, Time.httpdate("Sun, 06 Nov 1994 08:49:37 GMT"))
545
+ assert_equal(t, Time.httpdate("Sunday, 06-Nov-94 08:49:37 GMT"))
546
+ assert_equal(t, Time.httpdate("Sun Nov 6 08:49:37 1994"))
547
+ assert_equal(Time.utc(1995, 11, 15, 6, 25, 24),
548
+ Time.httpdate("Wed, 15 Nov 1995 06:25:24 GMT"))
549
+ assert_equal(Time.utc(1995, 11, 15, 4, 58, 8),
550
+ Time.httpdate("Wed, 15 Nov 1995 04:58:08 GMT"))
551
+ assert_equal(Time.utc(1994, 11, 15, 8, 12, 31),
552
+ Time.httpdate("Tue, 15 Nov 1994 08:12:31 GMT"))
553
+ assert_equal(Time.utc(1994, 12, 1, 16, 0, 0),
554
+ Time.httpdate("Thu, 01 Dec 1994 16:00:00 GMT"))
555
+ assert_equal(Time.utc(1994, 10, 29, 19, 43, 31),
556
+ Time.httpdate("Sat, 29 Oct 1994 19:43:31 GMT"))
557
+ assert_equal(Time.utc(1994, 11, 15, 12, 45, 26),
558
+ Time.httpdate("Tue, 15 Nov 1994 12:45:26 GMT"))
559
+ assert_equal(Time.utc(1999, 12, 31, 23, 59, 59),
560
+ Time.httpdate("Fri, 31 Dec 1999 23:59:59 GMT"))
561
+ end
562
+
563
+ def test_rfc3339
564
+ t = Time.utc(1985, 4, 12, 23, 20, 50, 520000)
565
+ s = "1985-04-12T23:20:50.52Z"
566
+ assert_equal(t, Time.iso8601(s))
567
+ assert_equal(s, t.iso8601(2))
568
+
569
+ t = Time.utc(1996, 12, 20, 0, 39, 57)
570
+ s = "1996-12-19T16:39:57-08:00"
571
+ assert_equal(t, Time.iso8601(s))
572
+ # There is no way to generate time string with arbitrary timezone.
573
+ s = "1996-12-20T00:39:57Z"
574
+ assert_equal(t, Time.iso8601(s))
575
+ assert_equal(s, t.iso8601)
576
+
577
+ t = Time.utc(1990, 12, 31, 23, 59, 60)
578
+ s = "1990-12-31T23:59:60Z"
579
+ assert_equal(t, Time.iso8601(s))
580
+ # leap second is representable only if timezone file has it.
581
+ s = "1990-12-31T15:59:60-08:00"
582
+ assert_equal(t, Time.iso8601(s))
583
+
584
+ begin
585
+ Time.at(-1)
586
+ rescue ArgumentError
587
+ # ignore
588
+ else
589
+ t = Time.utc(1937, 1, 1, 11, 40, 27, 870000)
590
+ s = "1937-01-01T12:00:27.87+00:20"
591
+ assert_equal(t, Time.iso8601(s))
592
+ end
593
+ end
594
+
595
+ # http://www.w3.org/TR/xmlschema-2/
596
+ def test_xmlschema
597
+ assert_equal(Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600,
598
+ Time.xmlschema("1999-05-31T13:20:00-05:00"))
599
+ assert_equal(Time.local(2000, 1, 20, 12, 0, 0),
600
+ Time.xmlschema("2000-01-20T12:00:00"))
601
+ assert_equal(Time.utc(2000, 1, 20, 12, 0, 0),
602
+ Time.xmlschema("2000-01-20T12:00:00Z"))
603
+ assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600,
604
+ Time.xmlschema("2000-01-20T12:00:00+12:00"))
605
+ assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600,
606
+ Time.xmlschema("2000-01-20T12:00:00-13:00"))
607
+ assert_equal(Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600,
608
+ Time.xmlschema("2000-03-04T23:00:00+03:00"))
609
+ assert_equal(Time.utc(2000, 3, 4, 20, 0, 0),
610
+ Time.xmlschema("2000-03-04T20:00:00Z"))
611
+ assert_equal(Time.local(2000, 1, 15, 0, 0, 0),
612
+ Time.xmlschema("2000-01-15T00:00:00"))
613
+ assert_equal(Time.local(2000, 2, 15, 0, 0, 0),
614
+ Time.xmlschema("2000-02-15T00:00:00"))
615
+ assert_equal(Time.local(2000, 1, 15, 12, 0, 0),
616
+ Time.xmlschema("2000-01-15T12:00:00"))
617
+ assert_equal(Time.utc(2000, 1, 16, 12, 0, 0),
618
+ Time.xmlschema("2000-01-16T12:00:00Z"))
619
+ assert_equal(Time.local(2000, 1, 1, 12, 0, 0),
620
+ Time.xmlschema("2000-01-01T12:00:00"))
621
+ assert_equal(Time.utc(1999, 12, 31, 23, 0, 0),
622
+ Time.xmlschema("1999-12-31T23:00:00Z"))
623
+ assert_equal(Time.local(2000, 1, 16, 12, 0, 0),
624
+ Time.xmlschema("2000-01-16T12:00:00"))
625
+ assert_equal(Time.local(2000, 1, 16, 0, 0, 0),
626
+ Time.xmlschema("2000-01-16T00:00:00"))
627
+ assert_equal(Time.utc(2000, 1, 12, 12, 13, 14),
628
+ Time.xmlschema("2000-01-12T12:13:14Z"))
629
+ assert_equal(Time.utc(2001, 4, 17, 19, 23, 17, 300000),
630
+ Time.xmlschema("2001-04-17T19:23:17.3Z"))
631
+ end
632
+
633
+ def test_encode_xmlschema
634
+ t = Time.utc(2001, 4, 17, 19, 23, 17, 300000)
635
+ assert_equal("2001-04-17T19:23:17Z", t.xmlschema)
636
+ assert_equal("2001-04-17T19:23:17.3Z", t.xmlschema(1))
637
+ assert_equal("2001-04-17T19:23:17.300000Z", t.xmlschema(6))
638
+ assert_equal("2001-04-17T19:23:17.3000000Z", t.xmlschema(7))
639
+
640
+ t = Time.utc(2001, 4, 17, 19, 23, 17, 123456)
641
+ assert_equal("2001-04-17T19:23:17.1234560Z", t.xmlschema(7))
642
+ assert_equal("2001-04-17T19:23:17.123456Z", t.xmlschema(6))
643
+ assert_equal("2001-04-17T19:23:17.12345Z", t.xmlschema(5))
644
+ assert_equal("2001-04-17T19:23:17.1Z", t.xmlschema(1))
645
+
646
+ begin
647
+ Time.at(-1)
648
+ rescue ArgumentError
649
+ # ignore
650
+ else
651
+ t = Time.utc(1960, 12, 31, 23, 0, 0, 123456)
652
+ assert_equal("1960-12-31T23:00:00.123456Z", t.xmlschema(6))
653
+ end
654
+
655
+ assert_equal(249, Time.xmlschema("2008-06-05T23:49:23.000249+09:00").usec)
656
+ end
657
+
658
+ def test_completion
659
+ now = Time.local(2001,11,29,21,26,35)
660
+ assert_equal(Time.local( 2001,11,29,21,12),
661
+ Time.parse("2001/11/29 21:12", now))
662
+ assert_equal(Time.local( 2001,11,29),
663
+ Time.parse("2001/11/29", now))
664
+ assert_equal(Time.local( 2001,11,29),
665
+ Time.parse( "11/29", now))
666
+ #assert_equal(Time.local(2001,11,1), Time.parse("Nov", now))
667
+ assert_equal(Time.local( 2001,11,29,10,22),
668
+ Time.parse( "10:22", now))
669
+ end
670
+
671
+ def test_invalid
672
+ # They were actually used in some web sites.
673
+ assert_raise(ArgumentError) { Time.httpdate("1 Dec 2001 10:23:57 GMT") }
674
+ assert_raise(ArgumentError) { Time.httpdate("Sat, 1 Dec 2001 10:25:42 GMT") }
675
+ assert_raise(ArgumentError) { Time.httpdate("Sat, 1-Dec-2001 10:53:55 GMT") }
676
+ assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-2001 10:15:34 GMT") }
677
+ assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-101 11:10:07 GMT") }
678
+ assert_raise(ArgumentError) { Time.httpdate("Fri, 30 Nov 2001 21:30:00 JST") }
679
+
680
+ # They were actually used in some mails.
681
+ assert_raise(ArgumentError) { Time.rfc2822("01-5-20") }
682
+ assert_raise(ArgumentError) { Time.rfc2822("7/21/00") }
683
+ assert_raise(ArgumentError) { Time.rfc2822("2001-8-28") }
684
+ assert_raise(ArgumentError) { Time.rfc2822("00-5-6 1:13:06") }
685
+ assert_raise(ArgumentError) { Time.rfc2822("2001-9-27 9:36:49") }
686
+ assert_raise(ArgumentError) { Time.rfc2822("2000-12-13 11:01:11") }
687
+ assert_raise(ArgumentError) { Time.rfc2822("2001/10/17 04:29:55") }
688
+ assert_raise(ArgumentError) { Time.rfc2822("9/4/2001 9:23:19 PM") }
689
+ assert_raise(ArgumentError) { Time.rfc2822("01 Nov 2001 09:04:31") }
690
+ assert_raise(ArgumentError) { Time.rfc2822("13 Feb 2001 16:4 GMT") }
691
+ assert_raise(ArgumentError) { Time.rfc2822("01 Oct 00 5:41:19 PM") }
692
+ assert_raise(ArgumentError) { Time.rfc2822("2 Jul 00 00:51:37 JST") }
693
+ assert_raise(ArgumentError) { Time.rfc2822("01 11 2001 06:55:57 -0500") }
694
+ assert_raise(ArgumentError) { Time.rfc2822("18 \343\366\356\341\370 2000") }
695
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, Oct 2001 18:53:32") }
696
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 2 Nov 2001 03:47:54") }
697
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 27 Jul 2001 11.14.14 +0200") }
698
+ assert_raise(ArgumentError) { Time.rfc2822("Thu, 2 Nov 2000 04:13:53 -600") }
699
+ assert_raise(ArgumentError) { Time.rfc2822("Wed, 5 Apr 2000 22:57:09 JST") }
700
+ assert_raise(ArgumentError) { Time.rfc2822("Mon, 11 Sep 2000 19:47:33 00000") }
701
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 28 Apr 2000 20:40:47 +-900") }
702
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 19 Jan 2001 8:15:36 AM -0500") }
703
+ assert_raise(ArgumentError) { Time.rfc2822("Thursday, Sep 27 2001 7:42:35 AM EST") }
704
+ assert_raise(ArgumentError) { Time.rfc2822("3/11/2001 1:31:57 PM Pacific Daylight Time") }
705
+ assert_raise(ArgumentError) { Time.rfc2822("Mi, 28 Mrz 2001 11:51:36") }
706
+ assert_raise(ArgumentError) { Time.rfc2822("P, 30 sept 2001 23:03:14") }
707
+ assert_raise(ArgumentError) { Time.rfc2822("fr, 11 aug 2000 18:39:22") }
708
+ assert_raise(ArgumentError) { Time.rfc2822("Fr, 21 Sep 2001 17:44:03 -1000") }
709
+ assert_raise(ArgumentError) { Time.rfc2822("Mo, 18 Jun 2001 19:21:40 -1000") }
710
+ assert_raise(ArgumentError) { Time.rfc2822("l\366, 12 aug 2000 18:53:20") }
711
+ assert_raise(ArgumentError) { Time.rfc2822("l\366, 26 maj 2001 00:15:58") }
712
+ assert_raise(ArgumentError) { Time.rfc2822("Dom, 30 Sep 2001 17:36:30") }
713
+ assert_raise(ArgumentError) { Time.rfc2822("%&, 31 %2/ 2000 15:44:47 -0500") }
714
+ assert_raise(ArgumentError) { Time.rfc2822("dom, 26 ago 2001 03:57:07 -0300") }
715
+ assert_raise(ArgumentError) { Time.rfc2822("ter, 04 set 2001 16:27:58 -0300") }
716
+ assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
717
+ assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
718
+ assert_raise(ArgumentError) { Time.rfc2822("ele, 11 h: 2000 12:42:15 -0500") }
719
+ assert_raise(ArgumentError) { Time.rfc2822("Tue, 14 Aug 2001 3:55:3 +0200") }
720
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 25 Aug 2000 9:3:48 +0800") }
721
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 1 Dec 2000 0:57:50 EST") }
722
+ assert_raise(ArgumentError) { Time.rfc2822("Mon, 7 May 2001 9:39:51 +0200") }
723
+ assert_raise(ArgumentError) { Time.rfc2822("Wed, 1 Aug 2001 16:9:15 +0200") }
724
+ assert_raise(ArgumentError) { Time.rfc2822("Wed, 23 Aug 2000 9:17:36 +0800") }
725
+ assert_raise(ArgumentError) { Time.rfc2822("Fri, 11 Aug 2000 10:4:42 +0800") }
726
+ assert_raise(ArgumentError) { Time.rfc2822("Sat, 15 Sep 2001 13:22:2 +0300") }
727
+ assert_raise(ArgumentError) { Time.rfc2822("Wed,16 \276\305\324\302 2001 20:06:25 +0800") }
728
+ assert_raise(ArgumentError) { Time.rfc2822("Wed,7 \312\256\322\273\324\302 2001 23:47:22 +0800") }
729
+ assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=C5=DA),?= 10 2 2001 23:32:26 +0900 (JST)") }
730
+ assert_raise(ArgumentError) { Time.rfc2822("\307\341\314\343\332\311, 30 \344\346\335\343\310\321 2001 10:01:06") }
731
+ assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=BF=E5),?= 12 =?iso-8859-1?Q?9=B7=EE?= 2001 14:52:41\n+0900 (JST)") }
732
+ end
733
+
734
+ def test_zone_0000
735
+ assert_equal(true, Time.parse("2000-01-01T00:00:00Z").utc?)
736
+ assert_equal(true, Time.parse("2000-01-01T00:00:00-00:00").utc?)
737
+ assert_equal(false, Time.parse("2000-01-01T00:00:00+00:00").utc?)
738
+ assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
739
+ assert_equal(true, Time.parse("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
740
+ assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
741
+ assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
742
+ assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
743
+ assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
744
+ assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 UTC").utc?)
745
+ end
746
+
747
+ def test_parse_leap_second
748
+ t = Time.utc(1998,12,31,23,59,59)
749
+ assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))
750
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:59 -0000 1998"));t.localtime
751
+ assert_equal(t, Time.parse("Fri Jan 1 08:59:59 +0900 1999"))
752
+ assert_equal(t, Time.parse("Fri Jan 1 00:59:59 +0100 1999"))
753
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:59 +0000 1998"))
754
+ assert_equal(t, Time.parse("Fri Dec 31 22:59:59 -0100 1998"));t.utc
755
+ t += 1
756
+ assert_equal(t, Time.parse("Thu Dec 31 23:59:60 UTC 1998"))
757
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:60 -0000 1998"));t.localtime
758
+ assert_equal(t, Time.parse("Fri Jan 1 08:59:60 +0900 1999"))
759
+ assert_equal(t, Time.parse("Fri Jan 1 00:59:60 +0100 1999"))
760
+ assert_equal(t, Time.parse("Fri Dec 31 23:59:60 +0000 1998"))
761
+ assert_equal(t, Time.parse("Fri Dec 31 22:59:60 -0100 1998"));t.utc
762
+ t += 1 if t.sec == 60
763
+ assert_equal(t, Time.parse("Thu Jan 1 00:00:00 UTC 1999"))
764
+ assert_equal(t, Time.parse("Fri Jan 1 00:00:00 -0000 1999"));t.localtime
765
+ assert_equal(t, Time.parse("Fri Jan 1 09:00:00 +0900 1999"))
766
+ assert_equal(t, Time.parse("Fri Jan 1 01:00:00 +0100 1999"))
767
+ assert_equal(t, Time.parse("Fri Jan 1 00:00:00 +0000 1999"))
768
+ assert_equal(t, Time.parse("Fri Dec 31 23:00:00 -0100 1998"))
769
+ end
770
+
771
+ def test_rfc2822_leap_second
772
+ t = Time.utc(1998,12,31,23,59,59)
773
+ assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:59 UTC"))
774
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 -0000"));t.localtime
775
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 08:59:59 +0900"))
776
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:59:59 +0100"))
777
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 +0000"))
778
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:59 -0100"));t.utc
779
+ t += 1
780
+ assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:60 UTC"))
781
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 -0000"));t.localtime
782
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 08:59:60 +0900"))
783
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:59:60 +0100"))
784
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 +0000"))
785
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:60 -0100"));t.utc
786
+ t += 1 if t.sec == 60
787
+ assert_equal(t, Time.rfc2822("Thu, 1 Jan 1999 00:00:00 UTC"))
788
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:00:00 -0000"));t.localtime
789
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 09:00:00 +0900"))
790
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 01:00:00 +0100"))
791
+ assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:00:00 +0000"))
792
+ assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:00:00 -0100"))
793
+ end
794
+
795
+ def test_xmlschema_leap_second
796
+ t = Time.utc(1998,12,31,23,59,59)
797
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:59Z"))
798
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:59-00:00"));t.localtime
799
+ assert_equal(t, Time.xmlschema("1999-01-01T08:59:59+09:00"))
800
+ assert_equal(t, Time.xmlschema("1999-01-01T00:59:59+01:00"))
801
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:59+00:00"))
802
+ assert_equal(t, Time.xmlschema("1998-12-31T22:59:59-01:00"));t.utc
803
+ t += 1
804
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:60Z"))
805
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:60-00:00"));t.localtime
806
+ assert_equal(t, Time.xmlschema("1999-01-01T08:59:60+09:00"))
807
+ assert_equal(t, Time.xmlschema("1999-01-01T00:59:60+01:00"))
808
+ assert_equal(t, Time.xmlschema("1998-12-31T23:59:60+00:00"))
809
+ assert_equal(t, Time.xmlschema("1998-12-31T22:59:60-01:00"));t.utc
810
+ t += 1 if t.sec == 60
811
+ assert_equal(t, Time.xmlschema("1999-01-01T00:00:00Z"))
812
+ assert_equal(t, Time.xmlschema("1999-01-01T00:00:00-00:00"));t.localtime
813
+ assert_equal(t, Time.xmlschema("1999-01-01T09:00:00+09:00"))
814
+ assert_equal(t, Time.xmlschema("1999-01-01T01:00:00+01:00"))
815
+ assert_equal(t, Time.xmlschema("1999-01-01T00:00:00+00:00"))
816
+ assert_equal(t, Time.xmlschema("1998-12-31T23:00:00-01:00"))
817
+ end
818
+
819
+ def test_ruby_talk_152866
820
+ t = Time::xmlschema('2005-08-30T22:48:00-07:00')
821
+ assert_equal(31, t.day)
822
+ assert_equal(8, t.mon)
823
+ end
824
+
825
+ def test_parse_fraction
826
+ assert_equal(500000, Time.parse("2000-01-01T00:00:00.5+00:00").tv_usec)
827
+ end
828
+ end
829
+ end
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module Time
3
+ VERSION = "1.0.2"
4
+ end
5
+ end
data/lib/time.rb ADDED
@@ -0,0 +1 @@
1
+ require "rubysl/time"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/time/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-time"
6
+ spec.version = RubySL::Time::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library time.}
10
+ spec.summary = %q{Ruby standard library time.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-time"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_runtime_dependency "rubysl-parsedate", "~> 1.0"
20
+ spec.add_runtime_dependency "rubysl-rational", "~> 1.0"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "mspec", "~> 1.5"
25
+ end
@@ -0,0 +1,20 @@
1
+ require 'time'
2
+
3
+ describe "Time.httpdate" do
4
+ it "parses RFC-2616 strings" do
5
+ t = Time.utc(1994, 11, 6, 8, 49, 37)
6
+ t.should == Time.httpdate("Sun, 06 Nov 1994 08:49:37 GMT")
7
+
8
+ # relies on Time.parse (not yet implemented)
9
+ # t.should == Time.httpdate("Sunday, 06-Nov-94 08:49:37 GMT")
10
+
11
+ t.should == Time.httpdate("Sun Nov 6 08:49:37 1994")
12
+ Time.utc(1995, 11, 15, 6, 25, 24).should == Time.httpdate("Wed, 15 Nov 1995 06:25:24 GMT")
13
+ Time.utc(1995, 11, 15, 4, 58, 8).should == Time.httpdate("Wed, 15 Nov 1995 04:58:08 GMT")
14
+ Time.utc(1994, 11, 15, 8, 12, 31).should == Time.httpdate("Tue, 15 Nov 1994 08:12:31 GMT")
15
+ Time.utc(1994, 12, 1, 16, 0, 0).should == Time.httpdate("Thu, 01 Dec 1994 16:00:00 GMT")
16
+ Time.utc(1994, 10, 29, 19, 43, 31).should == Time.httpdate("Sat, 29 Oct 1994 19:43:31 GMT")
17
+ Time.utc(1994, 11, 15, 12, 45, 26).should == Time.httpdate("Tue, 15 Nov 1994 12:45:26 GMT")
18
+ Time.utc(1999, 12, 31, 23, 59, 59).should == Time.httpdate("Fri, 31 Dec 1999 23:59:59 GMT")
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/xmlschema', __FILE__)
2
+ require 'time'
3
+
4
+ describe "Time.xmlschema" do
5
+ it_behaves_like :time_xmlschema, :iso8601
6
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/rfc2822', __FILE__)
2
+ require 'time'
3
+
4
+ describe "Time.rfc2822" do
5
+ it_behaves_like :time_rfc2822, :rfc2822
6
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/rfc2822', __FILE__)
2
+ require 'time'
3
+
4
+ describe "Time.rfc822" do
5
+ it_behaves_like :time_rfc2822, :rfc822
6
+ end
@@ -0,0 +1,65 @@
1
+ describe :time_rfc2822, :shared => true do
2
+ it "parses RFC-822 strings" do
3
+ t1 = (Time.utc(1976, 8, 26, 14, 30) + 4 * 3600)
4
+ t2 = Time.rfc2822("26 Aug 76 14:30 EDT")
5
+ t1.should == t2
6
+
7
+ t3 = Time.utc(1976, 8, 27, 9, 32) + 7 * 3600
8
+ t4 = Time.rfc2822("27 Aug 76 09:32 PDT")
9
+ t3.should == t4
10
+ end
11
+
12
+ it "parses RFC-2822 strings" do
13
+ t1 = Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600
14
+ t2 = Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600")
15
+ t1.should == t2
16
+
17
+ t3 = Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600
18
+ t4 = Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
19
+ t3.should == t4
20
+
21
+ t5 = Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600
22
+ t6 = Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600")
23
+ t5.should == t6
24
+
25
+ t7 = Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600
26
+ t8 = Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600")
27
+ t7.should == t8
28
+
29
+ t9 = Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600
30
+ t10 = Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800")
31
+ t9.should == t10
32
+
33
+ begin
34
+ Time.at(-1)
35
+ rescue ArgumentError
36
+ # ignore
37
+ else
38
+ t11 = Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60
39
+ t12 = Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330")
40
+ t11.should == t12
41
+
42
+ t13 = Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60
43
+ t14 = Time.rfc2822(" Thu,
44
+ 13
45
+ Feb
46
+ 1969
47
+ 23:32
48
+ -0330 (Newfoundland Time)")
49
+ t13.should == t14
50
+ end
51
+
52
+ t15 = Time.utc(1997, 11, 21, 9, 55, 6)
53
+ t16 = Time.rfc2822("21 Nov 97 09:55:06 GMT")
54
+ t15.should == t16
55
+
56
+ t17 = Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600
57
+ t18 = Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600")
58
+ t17.should == t18
59
+
60
+ lambda {
61
+ # inner comment is not supported.
62
+ Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600")
63
+ }.should raise_error(ArgumentError)
64
+ end
65
+ end
@@ -0,0 +1,53 @@
1
+ describe :time_xmlschema, :shared => true do
2
+ it "parses ISO-8601 strings" do
3
+ t = Time.utc(1985, 4, 12, 23, 20, 50, 520000)
4
+ s = "1985-04-12T23:20:50.52Z"
5
+ t.should == Time.xmlschema(s)
6
+ #s.should == t.xmlschema(2)
7
+
8
+ t = Time.utc(1996, 12, 20, 0, 39, 57)
9
+ s = "1996-12-19T16:39:57-08:00"
10
+ t.should == Time.xmlschema(s)
11
+ # There is no way to generate time string with arbitrary timezone.
12
+ s = "1996-12-20T00:39:57Z"
13
+ t.should == Time.xmlschema(s)
14
+ #assert_equal(s, t.xmlschema)
15
+
16
+ t = Time.utc(1990, 12, 31, 23, 59, 60)
17
+ s = "1990-12-31T23:59:60Z"
18
+ t.should == Time.xmlschema(s)
19
+ # leap second is representable only if timezone file has it.
20
+ s = "1990-12-31T15:59:60-08:00"
21
+ t.should == Time.xmlschema(s)
22
+
23
+ begin
24
+ Time.at(-1)
25
+ rescue ArgumentError
26
+ # ignore
27
+ else
28
+ t = Time.utc(1937, 1, 1, 11, 40, 27, 870000)
29
+ s = "1937-01-01T12:00:27.87+00:20"
30
+ t.should == Time.xmlschema(s)
31
+ end
32
+
33
+ # more
34
+
35
+ # (Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600).should == Time.xmlschema("1999-05-31T13:20:00-05:00")
36
+ # (Time.local(2000, 1, 20, 12, 0, 0)).should == Time.xmlschema("2000-01-20T12:00:00")
37
+ # (Time.utc(2000, 1, 20, 12, 0, 0)).should == Time.xmlschema("2000-01-20T12:00:00Z")
38
+ # (Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600).should == Time.xmlschema("2000-01-20T12:00:00+12:00")
39
+ # (Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600).should == Time.xmlschema("2000-01-20T12:00:00-13:00")
40
+ # (Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600).should == Time.xmlschema("2000-03-04T23:00:00+03:00")
41
+ # (Time.utc(2000, 3, 4, 20, 0, 0)).should == Time.xmlschema("2000-03-04T20:00:00Z")
42
+ # (Time.local(2000, 1, 15, 0, 0, 0)).should == Time.xmlschema("2000-01-15T00:00:00")
43
+ # (Time.local(2000, 2, 15, 0, 0, 0)).should == Time.xmlschema("2000-02-15T00:00:00")
44
+ # (Time.local(2000, 1, 15, 12, 0, 0)).should == Time.xmlschema("2000-01-15T12:00:00")
45
+ # (Time.utc(2000, 1, 16, 12, 0, 0)).should == Time.xmlschema("2000-01-16T12:00:00Z")
46
+ # (Time.local(2000, 1, 1, 12, 0, 0)).should == Time.xmlschema("2000-01-01T12:00:00")
47
+ # (Time.utc(1999, 12, 31, 23, 0, 0)).should == Time.xmlschema("1999-12-31T23:00:00Z")
48
+ # (Time.local(2000, 1, 16, 12, 0, 0)).should == Time.xmlschema("2000-01-16T12:00:00")
49
+ # (Time.local(2000, 1, 16, 0, 0, 0)).should == Time.xmlschema("2000-01-16T00:00:00")
50
+ # (Time.utc(2000, 1, 12, 12, 13, 14)).should == Time.xmlschema("2000-01-12T12:13:14Z")
51
+ # (Time.utc(2001, 4, 17, 19, 23, 17, 300000)).should == Time.xmlschema("2001-04-17T19:23:17.3Z")
52
+ end
53
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/xmlschema', __FILE__)
2
+ require 'time'
3
+
4
+ describe "Time.xmlschema" do
5
+ it_behaves_like :time_xmlschema, :xmlschema
6
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-time
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubysl-parsedate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubysl-rational
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ description: Ruby standard library time.
84
+ email:
85
+ - brixen@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - lib/rubysl/time.rb
97
+ - lib/rubysl/time/time.rb
98
+ - lib/rubysl/time/version.rb
99
+ - lib/time.rb
100
+ - rubysl-time.gemspec
101
+ - spec/httpdate_spec.rb
102
+ - spec/iso8601_spec.rb
103
+ - spec/rfc2822_spec.rb
104
+ - spec/rfc822_spec.rb
105
+ - spec/shared/rfc2822.rb
106
+ - spec/shared/xmlschema.rb
107
+ - spec/xmlschema_spec.rb
108
+ homepage: https://github.com/rubysl/rubysl-time
109
+ licenses:
110
+ - BSD
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.0.7
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Ruby standard library time.
132
+ test_files:
133
+ - spec/httpdate_spec.rb
134
+ - spec/iso8601_spec.rb
135
+ - spec/rfc2822_spec.rb
136
+ - spec/rfc822_spec.rb
137
+ - spec/shared/rfc2822.rb
138
+ - spec/shared/xmlschema.rb
139
+ - spec/xmlschema_spec.rb