Implements the extensions to the Time class that are described in the documentation for the time.rb library.

Methods

httpdate   httpdate   iso8601   parse   rfc2822   rfc2822   rfc822   to_yaml   w3cdtf   yaml_new   zone_offset  

Constants

ZoneOffset = { 'UTC' => 0, # ISO 8601 'Z' => 0, # RFC 822 'UT' => 0, 'GMT' => 0, 'EST' => -5, 'EDT' => -4, 'CST' => -6, 'CDT' => -5, 'MST' => -7, 'MDT' => -6, 'PST' => -8, 'PDT' => -7, # Following definition of military zones is original one. # See RFC 1123 and RFC 2822 for the error in RFC 822. 'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6, 'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12, 'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6, 'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12, }
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
MonthValue = { 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
RFC2822_DAY_NAME = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
RFC2822_MONTH_NAME = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'

External Aliases

iso8601 -> w3cdtf
rfc2822 -> rfc822
-> iso8601

Public Class methods

Parses date as HTTP-date defined by RFC 2616 and converts it to a Time object.

ArgumentError is raised if date is not compliant with RFC 2616 or Time class cannot represent specified date.

See httpdate for more information on this format.

[Source]

     # File time.rb, line 310
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:         Time.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:         Time.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:         Time.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

Parses date using Date._parse and converts it to a Time object.

If a block is given, the year described in date is converted by the block. For example:

    Time.parse(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}

If the upper components of the given time are broken or missing, they are supplied with those of now. For the lower components, the minimum values (1 or 0) are assumed if broken or missing. For example:

    # Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
    # your timezone is GMT:
    Time.parse("16:30")     #=> Thu Nov 29 16:30:00 GMT 2001
    Time.parse("7/23")      #=> Mon Jul 23 00:00:00 GMT 2001
    Time.parse("Aug 31")    #=> Fri Aug 31 00:00:00 GMT 2001

Since there are numerous conflicts among locally defined timezone abbreviations all over the world, this method is not made to understand all of them. For example, the abbreviation "CST" is used variously as:

    -06:00 in America/Chicago,
    -05:00 in America/Havana,
    +08:00 in Asia/Harbin,
    +09:30 in Australia/Darwin,
    +10:30 in Australia/Adelaide,
    etc.

Based on the fact, this method only understands the timezone abbreviations described in RFC 822 and the system timezone, in the order named. (i.e. a definition in RFC 822 overrides the system timezone definition.) The system timezone is taken from Time.local(year, 1, 1).zone and Time.local(year, 7, 1).zone. If the extracted timezone abbreviation does not match any of them, it is ignored and the given time is regarded as a local time.

ArgumentError is raised if Date._parse cannot extract information from date or Time class cannot represent specified date.

This method can be used as fail-safe for other parsing methods as:

  Time.rfc2822(date) rescue Time.parse(date)
  Time.httpdate(date) rescue Time.parse(date)

A failure for Time.parse should be checked, though.

[Source]

     # File time.rb, line 239
239:     def parse(date, now=Time.now)
240:       d = Date._parse(date, false)
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

Parses date as date-time defined by RFC 2822 and converts it to a Time object. The format is identical to the date format defined by RFC 822 and updated by RFC 1123.

ArgumentError is raised if date is not compliant with RFC 2822 or Time class cannot represent specified date.

See rfc2822 for more information on this format.

[Source]

     # File time.rb, line 261
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 = Time.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

[Source]

    # File rss/rss.rb, line 6
 6:       def w3cdtf(date)
 7:         if /\A\s*
 8:             (-?\d+)-(\d\d)-(\d\d)
 9:             (?:T
10:             (\d\d):(\d\d)(?::(\d\d))?
11:             (\.\d+)?
12:             (Z|[+-]\d\d:\d\d)?)?
13:             \s*\z/ix =~ date and (($5 and $8) or (!$5 and !$8))
14:           datetime = [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i] 
15:           datetime << $7.to_f * 1000000 if $7
16:           if $8
17:             Time.utc(*datetime) - zone_offset($8)
18:           else
19:             Time.local(*datetime)
20:           end
21:         else
22:           raise ArgumentError.new("invalid date: #{date.inspect}")
23:         end
24:       end

Parses date as dateTime defined by XML Schema and converts it to a Time object. The format is restricted version of the format defined by ISO 8601.

ArgumentError is raised if date is not compliant with the format or Time class cannot represent specified date.

format.

[Source]

     # File time.rb, line 351
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.to_f * 1000000 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:           Time.utc(year, mon, day, hour, min, sec, usec)
372:         else
373:           Time.local(year, mon, day, hour, min, sec, usec)
374:         end
375:       else
376:         raise ArgumentError.new("invalid date: #{date.inspect}")
377:       end
378:     end

[Source]

     # File yaml/rubytypes.rb, line 302
302:     def Time.yaml_new( klass, tag, val )
303:         if Hash === val
304:             t = val.delete( 'at' )
305:             val.each { |k,v| t.instance_variable_set( k, v ) }
306:             t
307:         else
308:             raise YAML::TypeError, "Invalid Time: " + val.inspect
309:         end
310:     end

[Source]

    # File time.rb, line 69
69:     def zone_offset(zone, year=Time.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 = Time.local(year, 1, 1)).zone.upcase == zone rescue false)
79:         off = t.utc_offset
80:       elsif ((t = Time.local(year, 7, 1)).zone.upcase == zone rescue false)
81:         off = t.utc_offset
82:       end
83:       off
84:     end

Public Instance methods

Returns a string which represents the time as rfc1123-date of HTTP-date defined by RFC 2616:

  day-of-week, DD month-name CCYY hh:mm:ss GMT

Note that the result is always UTC (GMT).

[Source]

     # File time.rb, line 422
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:   end
iso8601(fraction_digits=0)

Returns a string which represents the time as date-time defined by RFC 2822:

  day-of-week, DD month-name CCYY hh:mm:ss zone

where zone is [+-]hhmm.

If self is a UTC time, -0000 is used as zone.

[Source]

     # File time.rb, line 391
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
rfc822()

Alias for rfc2822

[Source]

     # File yaml/rubytypes.rb, line 311
311:         def to_yaml( opts = {} )
312:                 YAML::quick_emit( object_id, opts ) do |out|
313:             tz = "Z"
314:             # from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
315:             unless self.utc?
316:                 utc_same_instant = self.dup.utc
317:                 utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
318:                 difference_to_utc = utc_same_writing - utc_same_instant
319:                 if (difference_to_utc < 0) 
320:                     difference_sign = '-'
321:                     absolute_difference = -difference_to_utc
322:                 else
323:                     difference_sign = '+'
324:                     absolute_difference = difference_to_utc
325:                 end
326:                 difference_minutes = (absolute_difference/60).round
327:                 tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
328:             end
329:             standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
330:             standard += ".%06d" % [usec] if usec.nonzero?
331:             standard += " %s" % [tz]
332:             if to_yaml_properties.empty?
333:                 out.scalar( taguri, standard, :plain )
334:             else
335:                 out.map( taguri, to_yaml_style ) do |map|
336:                     map.add( 'at', standard )
337:                     to_yaml_properties.each do |m|
338:                         map.add( m, instance_variable_get( m ) )
339:                     end
340:                 end
341:             end
342:         end
343:         end

Returns a string which represents the time as dateTime defined by XML Schema:

  CCYY-MM-DDThh:mm:ssTZD
  CCYY-MM-DDThh:mm:ss.sssTZD

where TZD is Z or [+-]hh:mm.

If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.

fractional_seconds specifies a number of digits of fractional seconds. Its default value is 0.

[Source]

     # File time.rb, line 444
445:     sprintf('%d-%02d-%02dT%02d:%02d:%02d',
446:       year, mon, day, hour, min, sec) +
447:     if fraction_digits == 0
448:       ''
449:     elsif fraction_digits <= 6
450:       '.' + sprintf('%06d', usec)[0, fraction_digits]
451:     else
452:       '.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
453:     end +
454:     if utc?
455:       'Z'
456:     else
457:       off = utc_offset
458:       sign = off < 0 ? '-' : '+'
459:       sprintf('%s%02d:%02d', sign, *(off.abs / 60).divmod(60))
460:     end
461:   end

Search

Google

Ruby API Docs

Links