Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: time.rb
[0] Fix | Delete
#
[1] Fix | Delete
# == Introduction
[2] Fix | Delete
#
[3] Fix | Delete
# This library extends the Time class:
[4] Fix | Delete
# * conversion between date string and time object.
[5] Fix | Delete
# * date-time defined by RFC 2822
[6] Fix | Delete
# * HTTP-date defined by RFC 2616
[7] Fix | Delete
# * dateTime defined by XML Schema Part 2: Datatypes (ISO 8601)
[8] Fix | Delete
# * various formats handled by Date._parse (string to time only)
[9] Fix | Delete
#
[10] Fix | Delete
# == Design Issues
[11] Fix | Delete
#
[12] Fix | Delete
# === Specialized interface
[13] Fix | Delete
#
[14] Fix | Delete
# This library provides methods dedicated to special purposes:
[15] Fix | Delete
# * RFC 2822, RFC 2616 and XML Schema.
[16] Fix | Delete
# * They makes usual life easier.
[17] Fix | Delete
#
[18] Fix | Delete
# === Doesn't depend on strftime
[19] Fix | Delete
#
[20] Fix | Delete
# This library doesn't use +strftime+. Especially #rfc2822 doesn't depend
[21] Fix | Delete
# on +strftime+ because:
[22] Fix | Delete
#
[23] Fix | Delete
# * %a and %b are locale sensitive
[24] Fix | Delete
#
[25] Fix | Delete
# Since they are locale sensitive, they may be replaced to
[26] Fix | Delete
# invalid weekday/month name in some locales.
[27] Fix | Delete
# Since ruby-1.6 doesn't invoke setlocale by default,
[28] Fix | Delete
# the problem doesn't arise until some external library invokes setlocale.
[29] Fix | Delete
# Ruby/GTK is the example of such library.
[30] Fix | Delete
#
[31] Fix | Delete
# * %z is not portable
[32] Fix | Delete
#
[33] Fix | Delete
# %z is required to generate zone in date-time of RFC 2822
[34] Fix | Delete
# but it is not portable.
[35] Fix | Delete
#
[36] Fix | Delete
# == Revision Information
[37] Fix | Delete
#
[38] Fix | Delete
# $Id$
[39] Fix | Delete
#
[40] Fix | Delete
[41] Fix | Delete
require 'parsedate'
[42] Fix | Delete
[43] Fix | Delete
#
[44] Fix | Delete
# Implements the extensions to the Time class that are described in the
[45] Fix | Delete
# documentation for the time.rb library.
[46] Fix | Delete
#
[47] Fix | Delete
class Time
[48] Fix | Delete
class << Time
[49] Fix | Delete
[50] Fix | Delete
ZoneOffset = {
[51] Fix | Delete
'UTC' => 0,
[52] Fix | Delete
# ISO 8601
[53] Fix | Delete
'Z' => 0,
[54] Fix | Delete
# RFC 822
[55] Fix | Delete
'UT' => 0, 'GMT' => 0,
[56] Fix | Delete
'EST' => -5, 'EDT' => -4,
[57] Fix | Delete
'CST' => -6, 'CDT' => -5,
[58] Fix | Delete
'MST' => -7, 'MDT' => -6,
[59] Fix | Delete
'PST' => -8, 'PDT' => -7,
[60] Fix | Delete
# Following definition of military zones is original one.
[61] Fix | Delete
# See RFC 1123 and RFC 2822 for the error in RFC 822.
[62] Fix | Delete
'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
[63] Fix | Delete
'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12,
[64] Fix | Delete
'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
[65] Fix | Delete
'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12,
[66] Fix | Delete
}
[67] Fix | Delete
def zone_offset(zone, year=self.now.year)
[68] Fix | Delete
off = nil
[69] Fix | Delete
zone = zone.upcase
[70] Fix | Delete
if /\A([+-])(\d\d):?(\d\d)\z/ =~ zone
[71] Fix | Delete
off = ($1 == '-' ? -1 : 1) * ($2.to_i * 60 + $3.to_i) * 60
[72] Fix | Delete
elsif /\A[+-]\d\d\z/ =~ zone
[73] Fix | Delete
off = zone.to_i * 3600
[74] Fix | Delete
elsif ZoneOffset.include?(zone)
[75] Fix | Delete
off = ZoneOffset[zone] * 3600
[76] Fix | Delete
elsif ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false)
[77] Fix | Delete
off = t.utc_offset
[78] Fix | Delete
elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false)
[79] Fix | Delete
off = t.utc_offset
[80] Fix | Delete
end
[81] Fix | Delete
off
[82] Fix | Delete
end
[83] Fix | Delete
[84] Fix | Delete
def zone_utc?(zone)
[85] Fix | Delete
# * +0000 means localtime. [RFC 2822]
[86] Fix | Delete
# * GMT is a localtime abbreviation in Europe/London, etc.
[87] Fix | Delete
if /\A(?:-00:00|-0000|-00|UTC|Z|UT)\z/i =~ zone
[88] Fix | Delete
true
[89] Fix | Delete
else
[90] Fix | Delete
false
[91] Fix | Delete
end
[92] Fix | Delete
end
[93] Fix | Delete
private :zone_utc?
[94] Fix | Delete
[95] Fix | Delete
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
[96] Fix | Delete
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
[97] Fix | Delete
def month_days(y, m)
[98] Fix | Delete
if ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)
[99] Fix | Delete
LeapYearMonthDays[m-1]
[100] Fix | Delete
else
[101] Fix | Delete
CommonYearMonthDays[m-1]
[102] Fix | Delete
end
[103] Fix | Delete
end
[104] Fix | Delete
private :month_days
[105] Fix | Delete
[106] Fix | Delete
def apply_offset(year, mon, day, hour, min, sec, off)
[107] Fix | Delete
if off < 0
[108] Fix | Delete
off = -off
[109] Fix | Delete
off, o = off.divmod(60)
[110] Fix | Delete
if o != 0 then sec += o; o, sec = sec.divmod(60); off += o end
[111] Fix | Delete
off, o = off.divmod(60)
[112] Fix | Delete
if o != 0 then min += o; o, min = min.divmod(60); off += o end
[113] Fix | Delete
off, o = off.divmod(24)
[114] Fix | Delete
if o != 0 then hour += o; o, hour = hour.divmod(24); off += o end
[115] Fix | Delete
if off != 0
[116] Fix | Delete
day += off
[117] Fix | Delete
if month_days(year, mon) < day
[118] Fix | Delete
mon += 1
[119] Fix | Delete
if 12 < mon
[120] Fix | Delete
mon = 1
[121] Fix | Delete
year += 1
[122] Fix | Delete
end
[123] Fix | Delete
day = 1
[124] Fix | Delete
end
[125] Fix | Delete
end
[126] Fix | Delete
elsif 0 < off
[127] Fix | Delete
off, o = off.divmod(60)
[128] Fix | Delete
if o != 0 then sec -= o; o, sec = sec.divmod(60); off -= o end
[129] Fix | Delete
off, o = off.divmod(60)
[130] Fix | Delete
if o != 0 then min -= o; o, min = min.divmod(60); off -= o end
[131] Fix | Delete
off, o = off.divmod(24)
[132] Fix | Delete
if o != 0 then hour -= o; o, hour = hour.divmod(24); off -= o end
[133] Fix | Delete
if off != 0 then
[134] Fix | Delete
day -= off
[135] Fix | Delete
if day < 1
[136] Fix | Delete
mon -= 1
[137] Fix | Delete
if mon < 1
[138] Fix | Delete
year -= 1
[139] Fix | Delete
mon = 12
[140] Fix | Delete
end
[141] Fix | Delete
day = month_days(year, mon)
[142] Fix | Delete
end
[143] Fix | Delete
end
[144] Fix | Delete
end
[145] Fix | Delete
return year, mon, day, hour, min, sec
[146] Fix | Delete
end
[147] Fix | Delete
private :apply_offset
[148] Fix | Delete
[149] Fix | Delete
def make_time(year, mon, day, hour, min, sec, sec_fraction, zone, now)
[150] Fix | Delete
usec = nil
[151] Fix | Delete
usec = (sec_fraction * 1000000).to_i if sec_fraction
[152] Fix | Delete
if now
[153] Fix | Delete
begin
[154] Fix | Delete
break if year; year = now.year
[155] Fix | Delete
break if mon; mon = now.mon
[156] Fix | Delete
break if day; day = now.day
[157] Fix | Delete
break if hour; hour = now.hour
[158] Fix | Delete
break if min; min = now.min
[159] Fix | Delete
break if sec; sec = now.sec
[160] Fix | Delete
break if sec_fraction; usec = now.tv_usec
[161] Fix | Delete
end until true
[162] Fix | Delete
end
[163] Fix | Delete
[164] Fix | Delete
year ||= 1970
[165] Fix | Delete
mon ||= 1
[166] Fix | Delete
day ||= 1
[167] Fix | Delete
hour ||= 0
[168] Fix | Delete
min ||= 0
[169] Fix | Delete
sec ||= 0
[170] Fix | Delete
usec ||= 0
[171] Fix | Delete
[172] Fix | Delete
off = nil
[173] Fix | Delete
off = zone_offset(zone, year) if zone
[174] Fix | Delete
[175] Fix | Delete
if off
[176] Fix | Delete
year, mon, day, hour, min, sec =
[177] Fix | Delete
apply_offset(year, mon, day, hour, min, sec, off)
[178] Fix | Delete
t = self.utc(year, mon, day, hour, min, sec, usec)
[179] Fix | Delete
t.localtime if !zone_utc?(zone)
[180] Fix | Delete
t
[181] Fix | Delete
else
[182] Fix | Delete
self.local(year, mon, day, hour, min, sec, usec)
[183] Fix | Delete
end
[184] Fix | Delete
end
[185] Fix | Delete
private :make_time
[186] Fix | Delete
[187] Fix | Delete
#
[188] Fix | Delete
# Parses +date+ using Date._parse and converts it to a Time object.
[189] Fix | Delete
#
[190] Fix | Delete
# If a block is given, the year described in +date+ is converted by the
[191] Fix | Delete
# block. For example:
[192] Fix | Delete
#
[193] Fix | Delete
# Time.parse(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
[194] Fix | Delete
#
[195] Fix | Delete
# If the upper components of the given time are broken or missing, they are
[196] Fix | Delete
# supplied with those of +now+. For the lower components, the minimum
[197] Fix | Delete
# values (1 or 0) are assumed if broken or missing. For example:
[198] Fix | Delete
#
[199] Fix | Delete
# # Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
[200] Fix | Delete
# # your timezone is GMT:
[201] Fix | Delete
# Time.parse("16:30") #=> Thu Nov 29 16:30:00 GMT 2001
[202] Fix | Delete
# Time.parse("7/23") #=> Mon Jul 23 00:00:00 GMT 2001
[203] Fix | Delete
# Time.parse("Aug 31") #=> Fri Aug 31 00:00:00 GMT 2001
[204] Fix | Delete
#
[205] Fix | Delete
# Since there are numerous conflicts among locally defined timezone
[206] Fix | Delete
# abbreviations all over the world, this method is not made to
[207] Fix | Delete
# understand all of them. For example, the abbreviation "CST" is
[208] Fix | Delete
# used variously as:
[209] Fix | Delete
#
[210] Fix | Delete
# -06:00 in America/Chicago,
[211] Fix | Delete
# -05:00 in America/Havana,
[212] Fix | Delete
# +08:00 in Asia/Harbin,
[213] Fix | Delete
# +09:30 in Australia/Darwin,
[214] Fix | Delete
# +10:30 in Australia/Adelaide,
[215] Fix | Delete
# etc.
[216] Fix | Delete
#
[217] Fix | Delete
# Based on the fact, this method only understands the timezone
[218] Fix | Delete
# abbreviations described in RFC 822 and the system timezone, in the
[219] Fix | Delete
# order named. (i.e. a definition in RFC 822 overrides the system
[220] Fix | Delete
# timezone definition.) The system timezone is taken from
[221] Fix | Delete
# <tt>Time.local(year, 1, 1).zone</tt> and
[222] Fix | Delete
# <tt>Time.local(year, 7, 1).zone</tt>.
[223] Fix | Delete
# If the extracted timezone abbreviation does not match any of them,
[224] Fix | Delete
# it is ignored and the given time is regarded as a local time.
[225] Fix | Delete
#
[226] Fix | Delete
# ArgumentError is raised if Date._parse cannot extract information from
[227] Fix | Delete
# +date+ or Time class cannot represent specified date.
[228] Fix | Delete
#
[229] Fix | Delete
# This method can be used as fail-safe for other parsing methods as:
[230] Fix | Delete
#
[231] Fix | Delete
# Time.rfc2822(date) rescue Time.parse(date)
[232] Fix | Delete
# Time.httpdate(date) rescue Time.parse(date)
[233] Fix | Delete
# Time.xmlschema(date) rescue Time.parse(date)
[234] Fix | Delete
#
[235] Fix | Delete
# A failure for Time.parse should be checked, though.
[236] Fix | Delete
#
[237] Fix | Delete
def parse(date, now=self.now)
[238] Fix | Delete
d = Date._parse(date, false)
[239] Fix | Delete
year = d[:year]
[240] Fix | Delete
year = yield(year) if year && block_given?
[241] Fix | Delete
make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
[242] Fix | Delete
end
[243] Fix | Delete
[244] Fix | Delete
MonthValue = {
[245] Fix | Delete
'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
[246] Fix | Delete
'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
#
[250] Fix | Delete
# Parses +date+ as date-time defined by RFC 2822 and converts it to a Time
[251] Fix | Delete
# object. The format is identical to the date format defined by RFC 822 and
[252] Fix | Delete
# updated by RFC 1123.
[253] Fix | Delete
#
[254] Fix | Delete
# ArgumentError is raised if +date+ is not compliant with RFC 2822
[255] Fix | Delete
# or Time class cannot represent specified date.
[256] Fix | Delete
#
[257] Fix | Delete
# See #rfc2822 for more information on this format.
[258] Fix | Delete
#
[259] Fix | Delete
def rfc2822(date)
[260] Fix | Delete
if /\A\s*
[261] Fix | Delete
(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?
[262] Fix | Delete
(\d{1,2})\s+
[263] Fix | Delete
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+
[264] Fix | Delete
(\d{2,})\s+
[265] Fix | Delete
(\d{2})\s*
[266] Fix | Delete
:\s*(\d{2})\s*
[267] Fix | Delete
(?::\s*(\d{2}))?\s+
[268] Fix | Delete
([+-]\d{4}|
[269] Fix | Delete
UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date
[270] Fix | Delete
# Since RFC 2822 permit comments, the regexp has no right anchor.
[271] Fix | Delete
day = $1.to_i
[272] Fix | Delete
mon = MonthValue[$2.upcase]
[273] Fix | Delete
year = $3.to_i
[274] Fix | Delete
hour = $4.to_i
[275] Fix | Delete
min = $5.to_i
[276] Fix | Delete
sec = $6 ? $6.to_i : 0
[277] Fix | Delete
zone = $7
[278] Fix | Delete
[279] Fix | Delete
# following year completion is compliant with RFC 2822.
[280] Fix | Delete
year = if year < 50
[281] Fix | Delete
2000 + year
[282] Fix | Delete
elsif year < 1000
[283] Fix | Delete
1900 + year
[284] Fix | Delete
else
[285] Fix | Delete
year
[286] Fix | Delete
end
[287] Fix | Delete
[288] Fix | Delete
year, mon, day, hour, min, sec =
[289] Fix | Delete
apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
[290] Fix | Delete
t = self.utc(year, mon, day, hour, min, sec)
[291] Fix | Delete
t.localtime if !zone_utc?(zone)
[292] Fix | Delete
t
[293] Fix | Delete
else
[294] Fix | Delete
raise ArgumentError.new("not RFC 2822 compliant date: #{date.inspect}")
[295] Fix | Delete
end
[296] Fix | Delete
end
[297] Fix | Delete
alias rfc822 rfc2822
[298] Fix | Delete
[299] Fix | Delete
#
[300] Fix | Delete
# Parses +date+ as HTTP-date defined by RFC 2616 and converts it to a Time
[301] Fix | Delete
# object.
[302] Fix | Delete
#
[303] Fix | Delete
# ArgumentError is raised if +date+ is not compliant with RFC 2616 or Time
[304] Fix | Delete
# class cannot represent specified date.
[305] Fix | Delete
#
[306] Fix | Delete
# See #httpdate for more information on this format.
[307] Fix | Delete
#
[308] Fix | Delete
def httpdate(date)
[309] Fix | Delete
if /\A\s*
[310] Fix | Delete
(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\x20
[311] Fix | Delete
(\d{2})\x20
[312] Fix | Delete
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
[313] Fix | Delete
(\d{4})\x20
[314] Fix | Delete
(\d{2}):(\d{2}):(\d{2})\x20
[315] Fix | Delete
GMT
[316] Fix | Delete
\s*\z/ix =~ date
[317] Fix | Delete
self.rfc2822(date)
[318] Fix | Delete
elsif /\A\s*
[319] Fix | Delete
(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday),\x20
[320] Fix | Delete
(\d\d)-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d\d)\x20
[321] Fix | Delete
(\d\d):(\d\d):(\d\d)\x20
[322] Fix | Delete
GMT
[323] Fix | Delete
\s*\z/ix =~ date
[324] Fix | Delete
self.parse(date)
[325] Fix | Delete
elsif /\A\s*
[326] Fix | Delete
(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\x20
[327] Fix | Delete
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
[328] Fix | Delete
(\d\d|\x20\d)\x20
[329] Fix | Delete
(\d\d):(\d\d):(\d\d)\x20
[330] Fix | Delete
(\d{4})
[331] Fix | Delete
\s*\z/ix =~ date
[332] Fix | Delete
self.utc($6.to_i, MonthValue[$1.upcase], $2.to_i,
[333] Fix | Delete
$3.to_i, $4.to_i, $5.to_i)
[334] Fix | Delete
else
[335] Fix | Delete
raise ArgumentError.new("not RFC 2616 compliant date: #{date.inspect}")
[336] Fix | Delete
end
[337] Fix | Delete
end
[338] Fix | Delete
[339] Fix | Delete
#
[340] Fix | Delete
# Parses +date+ as dateTime defined by XML Schema and converts it to a Time
[341] Fix | Delete
# object. The format is restricted version of the format defined by ISO
[342] Fix | Delete
# 8601.
[343] Fix | Delete
#
[344] Fix | Delete
# ArgumentError is raised if +date+ is not compliant with the format or Time
[345] Fix | Delete
# class cannot represent specified date.
[346] Fix | Delete
#
[347] Fix | Delete
# See #xmlschema for more information on this format.
[348] Fix | Delete
#
[349] Fix | Delete
def xmlschema(date)
[350] Fix | Delete
if /\A\s*
[351] Fix | Delete
(-?\d+)-(\d\d)-(\d\d)
[352] Fix | Delete
T
[353] Fix | Delete
(\d\d):(\d\d):(\d\d)
[354] Fix | Delete
(\.\d*)?
[355] Fix | Delete
(Z|[+-]\d\d:\d\d)?
[356] Fix | Delete
\s*\z/ix =~ date
[357] Fix | Delete
year = $1.to_i
[358] Fix | Delete
mon = $2.to_i
[359] Fix | Delete
day = $3.to_i
[360] Fix | Delete
hour = $4.to_i
[361] Fix | Delete
min = $5.to_i
[362] Fix | Delete
sec = $6.to_i
[363] Fix | Delete
usec = 0
[364] Fix | Delete
usec = ($7[1..-1] + '000000')[0,6].to_i if $7
[365] Fix | Delete
if $8
[366] Fix | Delete
zone = $8
[367] Fix | Delete
year, mon, day, hour, min, sec =
[368] Fix | Delete
apply_offset(year, mon, day, hour, min, sec, zone_offset(zone))
[369] Fix | Delete
self.utc(year, mon, day, hour, min, sec, usec)
[370] Fix | Delete
else
[371] Fix | Delete
self.local(year, mon, day, hour, min, sec, usec)
[372] Fix | Delete
end
[373] Fix | Delete
else
[374] Fix | Delete
raise ArgumentError.new("invalid date: #{date.inspect}")
[375] Fix | Delete
end
[376] Fix | Delete
end
[377] Fix | Delete
alias iso8601 xmlschema
[378] Fix | Delete
end # class << self
[379] Fix | Delete
[380] Fix | Delete
#
[381] Fix | Delete
# Returns a string which represents the time as date-time defined by RFC 2822:
[382] Fix | Delete
#
[383] Fix | Delete
# day-of-week, DD month-name CCYY hh:mm:ss zone
[384] Fix | Delete
#
[385] Fix | Delete
# where zone is [+-]hhmm.
[386] Fix | Delete
#
[387] Fix | Delete
# If +self+ is a UTC time, -0000 is used as zone.
[388] Fix | Delete
#
[389] Fix | Delete
def rfc2822
[390] Fix | Delete
sprintf('%s, %02d %s %d %02d:%02d:%02d ',
[391] Fix | Delete
RFC2822_DAY_NAME[wday],
[392] Fix | Delete
day, RFC2822_MONTH_NAME[mon-1], year,
[393] Fix | Delete
hour, min, sec) +
[394] Fix | Delete
if utc?
[395] Fix | Delete
'-0000'
[396] Fix | Delete
else
[397] Fix | Delete
off = utc_offset
[398] Fix | Delete
sign = off < 0 ? '-' : '+'
[399] Fix | Delete
sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))
[400] Fix | Delete
end
[401] Fix | Delete
end
[402] Fix | Delete
alias rfc822 rfc2822
[403] Fix | Delete
[404] Fix | Delete
RFC2822_DAY_NAME = [
[405] Fix | Delete
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
[406] Fix | Delete
]
[407] Fix | Delete
RFC2822_MONTH_NAME = [
[408] Fix | Delete
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
[409] Fix | Delete
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
[410] Fix | Delete
]
[411] Fix | Delete
[412] Fix | Delete
#
[413] Fix | Delete
# Returns a string which represents the time as rfc1123-date of HTTP-date
[414] Fix | Delete
# defined by RFC 2616:
[415] Fix | Delete
#
[416] Fix | Delete
# day-of-week, DD month-name CCYY hh:mm:ss GMT
[417] Fix | Delete
#
[418] Fix | Delete
# Note that the result is always UTC (GMT).
[419] Fix | Delete
#
[420] Fix | Delete
def httpdate
[421] Fix | Delete
t = dup.utc
[422] Fix | Delete
sprintf('%s, %02d %s %d %02d:%02d:%02d GMT',
[423] Fix | Delete
RFC2822_DAY_NAME[t.wday],
[424] Fix | Delete
t.day, RFC2822_MONTH_NAME[t.mon-1], t.year,
[425] Fix | Delete
t.hour, t.min, t.sec)
[426] Fix | Delete
end
[427] Fix | Delete
[428] Fix | Delete
#
[429] Fix | Delete
# Returns a string which represents the time as dateTime defined by XML
[430] Fix | Delete
# Schema:
[431] Fix | Delete
#
[432] Fix | Delete
# CCYY-MM-DDThh:mm:ssTZD
[433] Fix | Delete
# CCYY-MM-DDThh:mm:ss.sssTZD
[434] Fix | Delete
#
[435] Fix | Delete
# where TZD is Z or [+-]hh:mm.
[436] Fix | Delete
#
[437] Fix | Delete
# If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
[438] Fix | Delete
#
[439] Fix | Delete
# +fractional_seconds+ specifies a number of digits of fractional seconds.
[440] Fix | Delete
# Its default value is 0.
[441] Fix | Delete
#
[442] Fix | Delete
def xmlschema(fraction_digits=0)
[443] Fix | Delete
sprintf('%d-%02d-%02dT%02d:%02d:%02d',
[444] Fix | Delete
year, mon, day, hour, min, sec) +
[445] Fix | Delete
if fraction_digits == 0
[446] Fix | Delete
''
[447] Fix | Delete
elsif fraction_digits <= 6
[448] Fix | Delete
'.' + sprintf('%06d', usec)[0, fraction_digits]
[449] Fix | Delete
else
[450] Fix | Delete
'.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
[451] Fix | Delete
end +
[452] Fix | Delete
if utc?
[453] Fix | Delete
'Z'
[454] Fix | Delete
else
[455] Fix | Delete
off = utc_offset
[456] Fix | Delete
sign = off < 0 ? '-' : '+'
[457] Fix | Delete
sprintf('%s%02d:%02d', sign, *(off.abs / 60).divmod(60))
[458] Fix | Delete
end
[459] Fix | Delete
end
[460] Fix | Delete
alias iso8601 xmlschema
[461] Fix | Delete
end
[462] Fix | Delete
[463] Fix | Delete
if __FILE__ == $0
[464] Fix | Delete
require 'test/unit'
[465] Fix | Delete
[466] Fix | Delete
class TimeExtentionTest < Test::Unit::TestCase # :nodoc:
[467] Fix | Delete
def test_rfc822
[468] Fix | Delete
assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600,
[469] Fix | Delete
Time.rfc2822("26 Aug 76 14:30 EDT"))
[470] Fix | Delete
assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600,
[471] Fix | Delete
Time.rfc2822("27 Aug 76 09:32 PDT"))
[472] Fix | Delete
end
[473] Fix | Delete
[474] Fix | Delete
def test_rfc2822
[475] Fix | Delete
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
[476] Fix | Delete
Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600"))
[477] Fix | Delete
assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600,
[478] Fix | Delete
Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200"))
[479] Fix | Delete
assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600,
[480] Fix | Delete
Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600"))
[481] Fix | Delete
assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600,
[482] Fix | Delete
Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600"))
[483] Fix | Delete
assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600,
[484] Fix | Delete
Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800"))
[485] Fix | Delete
begin
[486] Fix | Delete
Time.at(-1)
[487] Fix | Delete
rescue ArgumentError
[488] Fix | Delete
# ignore
[489] Fix | Delete
else
[490] Fix | Delete
assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60,
[491] Fix | Delete
Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330"))
[492] Fix | Delete
assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60,
[493] Fix | Delete
Time.rfc2822(" Thu,
[494] Fix | Delete
13
[495] Fix | Delete
Feb
[496] Fix | Delete
1969
[497] Fix | Delete
23:32
[498] Fix | Delete
-0330 (Newfoundland Time)"))
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function