Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: time.rb
# frozen_string_literal: true
[0] Fix | Delete
[1] Fix | Delete
require 'date'
[2] Fix | Delete
[3] Fix | Delete
# = time.rb
[4] Fix | Delete
#
[5] Fix | Delete
# When 'time' is required, Time is extended with additional methods for parsing
[6] Fix | Delete
# and converting Times.
[7] Fix | Delete
#
[8] Fix | Delete
# == Features
[9] Fix | Delete
#
[10] Fix | Delete
# This library extends the Time class with the following conversions between
[11] Fix | Delete
# date strings and Time objects:
[12] Fix | Delete
#
[13] Fix | Delete
# * date-time defined by {RFC 2822}[http://www.ietf.org/rfc/rfc2822.txt]
[14] Fix | Delete
# * HTTP-date defined by {RFC 2616}[http://www.ietf.org/rfc/rfc2616.txt]
[15] Fix | Delete
# * dateTime defined by XML Schema Part 2: Datatypes ({ISO
[16] Fix | Delete
# 8601}[http://www.iso.org/iso/date_and_time_format])
[17] Fix | Delete
# * various formats handled by Date._parse
[18] Fix | Delete
# * custom formats handled by Date._strptime
[19] Fix | Delete
#
[20] Fix | Delete
# == Examples
[21] Fix | Delete
#
[22] Fix | Delete
# All examples assume you have loaded Time with:
[23] Fix | Delete
#
[24] Fix | Delete
# require 'time'
[25] Fix | Delete
#
[26] Fix | Delete
# All of these examples were done using the EST timezone which is GMT-5.
[27] Fix | Delete
#
[28] Fix | Delete
# === Converting to a String
[29] Fix | Delete
#
[30] Fix | Delete
# t = Time.now
[31] Fix | Delete
# t.iso8601 # => "2011-10-05T22:26:12-04:00"
[32] Fix | Delete
# t.rfc2822 # => "Wed, 05 Oct 2011 22:26:12 -0400"
[33] Fix | Delete
# t.httpdate # => "Thu, 06 Oct 2011 02:26:12 GMT"
[34] Fix | Delete
#
[35] Fix | Delete
# === Time.parse
[36] Fix | Delete
#
[37] Fix | Delete
# #parse takes a string representation of a Time and attempts to parse it
[38] Fix | Delete
# using a heuristic.
[39] Fix | Delete
#
[40] Fix | Delete
# Time.parse("2010-10-31") #=> 2010-10-31 00:00:00 -0500
[41] Fix | Delete
#
[42] Fix | Delete
# Any missing pieces of the date are inferred based on the current date.
[43] Fix | Delete
#
[44] Fix | Delete
# # assuming the current date is "2011-10-31"
[45] Fix | Delete
# Time.parse("12:00") #=> 2011-10-31 12:00:00 -0500
[46] Fix | Delete
#
[47] Fix | Delete
# We can change the date used to infer our missing elements by passing a second
[48] Fix | Delete
# object that responds to #mon, #day and #year, such as Date, Time or DateTime.
[49] Fix | Delete
# We can also use our own object.
[50] Fix | Delete
#
[51] Fix | Delete
# class MyDate
[52] Fix | Delete
# attr_reader :mon, :day, :year
[53] Fix | Delete
#
[54] Fix | Delete
# def initialize(mon, day, year)
[55] Fix | Delete
# @mon, @day, @year = mon, day, year
[56] Fix | Delete
# end
[57] Fix | Delete
# end
[58] Fix | Delete
#
[59] Fix | Delete
# d = Date.parse("2010-10-28")
[60] Fix | Delete
# t = Time.parse("2010-10-29")
[61] Fix | Delete
# dt = DateTime.parse("2010-10-30")
[62] Fix | Delete
# md = MyDate.new(10,31,2010)
[63] Fix | Delete
#
[64] Fix | Delete
# Time.parse("12:00", d) #=> 2010-10-28 12:00:00 -0500
[65] Fix | Delete
# Time.parse("12:00", t) #=> 2010-10-29 12:00:00 -0500
[66] Fix | Delete
# Time.parse("12:00", dt) #=> 2010-10-30 12:00:00 -0500
[67] Fix | Delete
# Time.parse("12:00", md) #=> 2010-10-31 12:00:00 -0500
[68] Fix | Delete
#
[69] Fix | Delete
# #parse also accepts an optional block. You can use this block to specify how
[70] Fix | Delete
# to handle the year component of the date. This is specifically designed for
[71] Fix | Delete
# handling two digit years. For example, if you wanted to treat all two digit
[72] Fix | Delete
# years prior to 70 as the year 2000+ you could write this:
[73] Fix | Delete
#
[74] Fix | Delete
# Time.parse("01-10-31") {|year| year + (year < 70 ? 2000 : 1900)}
[75] Fix | Delete
# #=> 2001-10-31 00:00:00 -0500
[76] Fix | Delete
# Time.parse("70-10-31") {|year| year + (year < 70 ? 2000 : 1900)}
[77] Fix | Delete
# #=> 1970-10-31 00:00:00 -0500
[78] Fix | Delete
#
[79] Fix | Delete
# === Time.strptime
[80] Fix | Delete
#
[81] Fix | Delete
# #strptime works similar to +parse+ except that instead of using a heuristic
[82] Fix | Delete
# to detect the format of the input string, you provide a second argument that
[83] Fix | Delete
# describes the format of the string. For example:
[84] Fix | Delete
#
[85] Fix | Delete
# Time.strptime("2000-10-31", "%Y-%m-%d") #=> 2000-10-31 00:00:00 -0500
[86] Fix | Delete
[87] Fix | Delete
class Time
[88] Fix | Delete
class << Time
[89] Fix | Delete
[90] Fix | Delete
#
[91] Fix | Delete
# A hash of timezones mapped to hour differences from UTC. The
[92] Fix | Delete
# set of time zones corresponds to the ones specified by RFC 2822
[93] Fix | Delete
# and ISO 8601.
[94] Fix | Delete
#
[95] Fix | Delete
ZoneOffset = { # :nodoc:
[96] Fix | Delete
'UTC' => 0,
[97] Fix | Delete
# ISO 8601
[98] Fix | Delete
'Z' => 0,
[99] Fix | Delete
# RFC 822
[100] Fix | Delete
'UT' => 0, 'GMT' => 0,
[101] Fix | Delete
'EST' => -5, 'EDT' => -4,
[102] Fix | Delete
'CST' => -6, 'CDT' => -5,
[103] Fix | Delete
'MST' => -7, 'MDT' => -6,
[104] Fix | Delete
'PST' => -8, 'PDT' => -7,
[105] Fix | Delete
# Following definition of military zones is original one.
[106] Fix | Delete
# See RFC 1123 and RFC 2822 for the error in RFC 822.
[107] Fix | Delete
'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
[108] Fix | Delete
'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12,
[109] Fix | Delete
'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
[110] Fix | Delete
'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12,
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
#
[114] Fix | Delete
# Return the number of seconds the specified time zone differs
[115] Fix | Delete
# from UTC.
[116] Fix | Delete
#
[117] Fix | Delete
# Numeric time zones that include minutes, such as
[118] Fix | Delete
# <code>-10:00</code> or <code>+1330</code> will work, as will
[119] Fix | Delete
# simpler hour-only time zones like <code>-10</code> or
[120] Fix | Delete
# <code>+13</code>.
[121] Fix | Delete
#
[122] Fix | Delete
# Textual time zones listed in ZoneOffset are also supported.
[123] Fix | Delete
#
[124] Fix | Delete
# If the time zone does not match any of the above, +zone_offset+
[125] Fix | Delete
# will check if the local time zone (both with and without
[126] Fix | Delete
# potential Daylight Saving \Time changes being in effect) matches
[127] Fix | Delete
# +zone+. Specifying a value for +year+ will change the year used
[128] Fix | Delete
# to find the local time zone.
[129] Fix | Delete
#
[130] Fix | Delete
# If +zone_offset+ is unable to determine the offset, nil will be
[131] Fix | Delete
# returned.
[132] Fix | Delete
def zone_offset(zone, year=self.now.year)
[133] Fix | Delete
off = nil
[134] Fix | Delete
zone = zone.upcase
[135] Fix | Delete
if /\A([+-])(\d\d)(:?)(\d\d)(?:\3(\d\d))?\z/ =~ zone
[136] Fix | Delete
off = ($1 == '-' ? -1 : 1) * (($2.to_i * 60 + $4.to_i) * 60 + $5.to_i)
[137] Fix | Delete
elsif /\A[+-]\d\d\z/ =~ zone
[138] Fix | Delete
off = zone.to_i * 3600
[139] Fix | Delete
elsif ZoneOffset.include?(zone)
[140] Fix | Delete
off = ZoneOffset[zone] * 3600
[141] Fix | Delete
elsif ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false)
[142] Fix | Delete
off = t.utc_offset
[143] Fix | Delete
elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false)
[144] Fix | Delete
off = t.utc_offset
[145] Fix | Delete
end
[146] Fix | Delete
off
[147] Fix | Delete
end
[148] Fix | Delete
[149] Fix | Delete
def zone_utc?(zone)
[150] Fix | Delete
# * +0000
[151] Fix | Delete
# In RFC 2822, +0000 indicate a time zone at Universal Time.
[152] Fix | Delete
# Europe/Lisbon is "a time zone at Universal Time" in Winter.
[153] Fix | Delete
# Atlantic/Reykjavik is "a time zone at Universal Time".
[154] Fix | Delete
# Africa/Dakar is "a time zone at Universal Time".
[155] Fix | Delete
# So +0000 is a local time such as Europe/London, etc.
[156] Fix | Delete
# * GMT
[157] Fix | Delete
# GMT is used as a time zone abbreviation in Europe/London,
[158] Fix | Delete
# Africa/Dakar, etc.
[159] Fix | Delete
# So it is a local time.
[160] Fix | Delete
#
[161] Fix | Delete
# * -0000, -00:00
[162] Fix | Delete
# In RFC 2822, -0000 the date-time contains no information about the
[163] Fix | Delete
# local time zone.
[164] Fix | Delete
# In RFC 3339, -00:00 is used for the time in UTC is known,
[165] Fix | Delete
# but the offset to local time is unknown.
[166] Fix | Delete
# They are not appropriate for specific time zone such as
[167] Fix | Delete
# Europe/London because time zone neutral,
[168] Fix | Delete
# So -00:00 and -0000 are treated as UTC.
[169] Fix | Delete
if /\A(?:-00:00|-0000|-00|UTC|Z|UT)\z/i =~ zone
[170] Fix | Delete
true
[171] Fix | Delete
else
[172] Fix | Delete
false
[173] Fix | Delete
end
[174] Fix | Delete
end
[175] Fix | Delete
private :zone_utc?
[176] Fix | Delete
[177] Fix | Delete
def force_zone!(t, zone, offset=nil)
[178] Fix | Delete
if zone_utc?(zone)
[179] Fix | Delete
t.utc
[180] Fix | Delete
elsif offset ||= zone_offset(zone)
[181] Fix | Delete
# Prefer the local timezone over the fixed offset timezone because
[182] Fix | Delete
# the former is a real timezone and latter is an artificial timezone.
[183] Fix | Delete
t.localtime
[184] Fix | Delete
if t.utc_offset != offset
[185] Fix | Delete
# Use the fixed offset timezone only if the local timezone cannot
[186] Fix | Delete
# represent the given offset.
[187] Fix | Delete
t.localtime(offset)
[188] Fix | Delete
end
[189] Fix | Delete
else
[190] Fix | Delete
t.localtime
[191] Fix | Delete
end
[192] Fix | Delete
end
[193] Fix | Delete
private :force_zone!
[194] Fix | Delete
[195] Fix | Delete
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # :nodoc:
[196] Fix | Delete
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # :nodoc:
[197] Fix | Delete
def month_days(y, m)
[198] Fix | Delete
if ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)
[199] Fix | Delete
LeapYearMonthDays[m-1]
[200] Fix | Delete
else
[201] Fix | Delete
CommonYearMonthDays[m-1]
[202] Fix | Delete
end
[203] Fix | Delete
end
[204] Fix | Delete
private :month_days
[205] Fix | Delete
[206] Fix | Delete
def apply_offset(year, mon, day, hour, min, sec, off)
[207] Fix | Delete
if off < 0
[208] Fix | Delete
off = -off
[209] Fix | Delete
off, o = off.divmod(60)
[210] Fix | Delete
if o != 0 then sec += o; o, sec = sec.divmod(60); off += o end
[211] Fix | Delete
off, o = off.divmod(60)
[212] Fix | Delete
if o != 0 then min += o; o, min = min.divmod(60); off += o end
[213] Fix | Delete
off, o = off.divmod(24)
[214] Fix | Delete
if o != 0 then hour += o; o, hour = hour.divmod(24); off += o end
[215] Fix | Delete
if off != 0
[216] Fix | Delete
day += off
[217] Fix | Delete
days = month_days(year, mon)
[218] Fix | Delete
if days and days < day
[219] Fix | Delete
mon += 1
[220] Fix | Delete
if 12 < mon
[221] Fix | Delete
mon = 1
[222] Fix | Delete
year += 1
[223] Fix | Delete
end
[224] Fix | Delete
day = 1
[225] Fix | Delete
end
[226] Fix | Delete
end
[227] Fix | Delete
elsif 0 < off
[228] Fix | Delete
off, o = off.divmod(60)
[229] Fix | Delete
if o != 0 then sec -= o; o, sec = sec.divmod(60); off -= o end
[230] Fix | Delete
off, o = off.divmod(60)
[231] Fix | Delete
if o != 0 then min -= o; o, min = min.divmod(60); off -= o end
[232] Fix | Delete
off, o = off.divmod(24)
[233] Fix | Delete
if o != 0 then hour -= o; o, hour = hour.divmod(24); off -= o end
[234] Fix | Delete
if off != 0 then
[235] Fix | Delete
day -= off
[236] Fix | Delete
if day < 1
[237] Fix | Delete
mon -= 1
[238] Fix | Delete
if mon < 1
[239] Fix | Delete
year -= 1
[240] Fix | Delete
mon = 12
[241] Fix | Delete
end
[242] Fix | Delete
day = month_days(year, mon)
[243] Fix | Delete
end
[244] Fix | Delete
end
[245] Fix | Delete
end
[246] Fix | Delete
return year, mon, day, hour, min, sec
[247] Fix | Delete
end
[248] Fix | Delete
private :apply_offset
[249] Fix | Delete
[250] Fix | Delete
def make_time(date, year, mon, day, hour, min, sec, sec_fraction, zone, now)
[251] Fix | Delete
if !year && !mon && !day && !hour && !min && !sec && !sec_fraction
[252] Fix | Delete
raise ArgumentError, "no time information in #{date.inspect}"
[253] Fix | Delete
end
[254] Fix | Delete
[255] Fix | Delete
off = nil
[256] Fix | Delete
if year || now
[257] Fix | Delete
off_year = year || now.year
[258] Fix | Delete
off = zone_offset(zone, off_year) if zone
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
if now
[262] Fix | Delete
if off
[263] Fix | Delete
now = now.getlocal(off) if now.utc_offset != off
[264] Fix | Delete
else
[265] Fix | Delete
now = now.getlocal
[266] Fix | Delete
end
[267] Fix | Delete
end
[268] Fix | Delete
[269] Fix | Delete
usec = nil
[270] Fix | Delete
usec = sec_fraction * 1000000 if sec_fraction
[271] Fix | Delete
[272] Fix | Delete
if now
[273] Fix | Delete
begin
[274] Fix | Delete
break if year; year = now.year
[275] Fix | Delete
break if mon; mon = now.mon
[276] Fix | Delete
break if day; day = now.day
[277] Fix | Delete
break if hour; hour = now.hour
[278] Fix | Delete
break if min; min = now.min
[279] Fix | Delete
break if sec; sec = now.sec
[280] Fix | Delete
break if sec_fraction; usec = now.tv_usec
[281] Fix | Delete
end until true
[282] Fix | Delete
end
[283] Fix | Delete
[284] Fix | Delete
year ||= 1970
[285] Fix | Delete
mon ||= 1
[286] Fix | Delete
day ||= 1
[287] Fix | Delete
hour ||= 0
[288] Fix | Delete
min ||= 0
[289] Fix | Delete
sec ||= 0
[290] Fix | Delete
usec ||= 0
[291] Fix | Delete
[292] Fix | Delete
if year != off_year
[293] Fix | Delete
off = nil
[294] Fix | Delete
off = zone_offset(zone, year) if zone
[295] Fix | Delete
end
[296] Fix | Delete
[297] Fix | Delete
if off
[298] Fix | Delete
year, mon, day, hour, min, sec =
[299] Fix | Delete
apply_offset(year, mon, day, hour, min, sec, off)
[300] Fix | Delete
t = self.utc(year, mon, day, hour, min, sec, usec)
[301] Fix | Delete
force_zone!(t, zone, off)
[302] Fix | Delete
t
[303] Fix | Delete
else
[304] Fix | Delete
self.local(year, mon, day, hour, min, sec, usec)
[305] Fix | Delete
end
[306] Fix | Delete
end
[307] Fix | Delete
private :make_time
[308] Fix | Delete
[309] Fix | Delete
#
[310] Fix | Delete
# Parses +date+ using Date._parse and converts it to a Time object.
[311] Fix | Delete
#
[312] Fix | Delete
# If a block is given, the year described in +date+ is converted by the
[313] Fix | Delete
# block. For example:
[314] Fix | Delete
#
[315] Fix | Delete
# Time.parse(...) {|y| 0 <= y && y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
[316] Fix | Delete
#
[317] Fix | Delete
# If the upper components of the given time are broken or missing, they are
[318] Fix | Delete
# supplied with those of +now+. For the lower components, the minimum
[319] Fix | Delete
# values (1 or 0) are assumed if broken or missing. For example:
[320] Fix | Delete
#
[321] Fix | Delete
# # Suppose it is "Thu Nov 29 14:33:20 2001" now and
[322] Fix | Delete
# # your time zone is EST which is GMT-5.
[323] Fix | Delete
# now = Time.parse("Thu Nov 29 14:33:20 2001")
[324] Fix | Delete
# Time.parse("16:30", now) #=> 2001-11-29 16:30:00 -0500
[325] Fix | Delete
# Time.parse("7/23", now) #=> 2001-07-23 00:00:00 -0500
[326] Fix | Delete
# Time.parse("Aug 31", now) #=> 2001-08-31 00:00:00 -0500
[327] Fix | Delete
# Time.parse("Aug 2000", now) #=> 2000-08-01 00:00:00 -0500
[328] Fix | Delete
#
[329] Fix | Delete
# Since there are numerous conflicts among locally defined time zone
[330] Fix | Delete
# abbreviations all over the world, this method is not intended to
[331] Fix | Delete
# understand all of them. For example, the abbreviation "CST" is
[332] Fix | Delete
# used variously as:
[333] Fix | Delete
#
[334] Fix | Delete
# -06:00 in America/Chicago,
[335] Fix | Delete
# -05:00 in America/Havana,
[336] Fix | Delete
# +08:00 in Asia/Harbin,
[337] Fix | Delete
# +09:30 in Australia/Darwin,
[338] Fix | Delete
# +10:30 in Australia/Adelaide,
[339] Fix | Delete
# etc.
[340] Fix | Delete
#
[341] Fix | Delete
# Based on this fact, this method only understands the time zone
[342] Fix | Delete
# abbreviations described in RFC 822 and the system time zone, in the
[343] Fix | Delete
# order named. (i.e. a definition in RFC 822 overrides the system
[344] Fix | Delete
# time zone definition.) The system time zone is taken from
[345] Fix | Delete
# <tt>Time.local(year, 1, 1).zone</tt> and
[346] Fix | Delete
# <tt>Time.local(year, 7, 1).zone</tt>.
[347] Fix | Delete
# If the extracted time zone abbreviation does not match any of them,
[348] Fix | Delete
# it is ignored and the given time is regarded as a local time.
[349] Fix | Delete
#
[350] Fix | Delete
# ArgumentError is raised if Date._parse cannot extract information from
[351] Fix | Delete
# +date+ or if the Time class cannot represent specified date.
[352] Fix | Delete
#
[353] Fix | Delete
# This method can be used as a fail-safe for other parsing methods as:
[354] Fix | Delete
#
[355] Fix | Delete
# Time.rfc2822(date) rescue Time.parse(date)
[356] Fix | Delete
# Time.httpdate(date) rescue Time.parse(date)
[357] Fix | Delete
# Time.xmlschema(date) rescue Time.parse(date)
[358] Fix | Delete
#
[359] Fix | Delete
# A failure of Time.parse should be checked, though.
[360] Fix | Delete
#
[361] Fix | Delete
# You must require 'time' to use this method.
[362] Fix | Delete
#
[363] Fix | Delete
def parse(date, now=self.now)
[364] Fix | Delete
comp = !block_given?
[365] Fix | Delete
d = Date._parse(date, comp)
[366] Fix | Delete
year = d[:year]
[367] Fix | Delete
year = yield(year) if year && !comp
[368] Fix | Delete
make_time(date, year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
[369] Fix | Delete
end
[370] Fix | Delete
[371] Fix | Delete
#
[372] Fix | Delete
# Parses +date+ using Date._strptime and converts it to a Time object.
[373] Fix | Delete
#
[374] Fix | Delete
# If a block is given, the year described in +date+ is converted by the
[375] Fix | Delete
# block. For example:
[376] Fix | Delete
#
[377] Fix | Delete
# Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
[378] Fix | Delete
#
[379] Fix | Delete
# Below is a list of the formatting options:
[380] Fix | Delete
#
[381] Fix | Delete
# %a :: The abbreviated weekday name ("Sun")
[382] Fix | Delete
# %A :: The full weekday name ("Sunday")
[383] Fix | Delete
# %b :: The abbreviated month name ("Jan")
[384] Fix | Delete
# %B :: The full month name ("January")
[385] Fix | Delete
# %c :: The preferred local date and time representation
[386] Fix | Delete
# %C :: Century (20 in 2009)
[387] Fix | Delete
# %d :: Day of the month (01..31)
[388] Fix | Delete
# %D :: Date (%m/%d/%y)
[389] Fix | Delete
# %e :: Day of the month, blank-padded ( 1..31)
[390] Fix | Delete
# %F :: Equivalent to %Y-%m-%d (the ISO 8601 date format)
[391] Fix | Delete
# %h :: Equivalent to %b
[392] Fix | Delete
# %H :: Hour of the day, 24-hour clock (00..23)
[393] Fix | Delete
# %I :: Hour of the day, 12-hour clock (01..12)
[394] Fix | Delete
# %j :: Day of the year (001..366)
[395] Fix | Delete
# %k :: hour, 24-hour clock, blank-padded ( 0..23)
[396] Fix | Delete
# %l :: hour, 12-hour clock, blank-padded ( 0..12)
[397] Fix | Delete
# %L :: Millisecond of the second (000..999)
[398] Fix | Delete
# %m :: Month of the year (01..12)
[399] Fix | Delete
# %M :: Minute of the hour (00..59)
[400] Fix | Delete
# %n :: Newline (\n)
[401] Fix | Delete
# %N :: Fractional seconds digits, default is 9 digits (nanosecond)
[402] Fix | Delete
# %3N :: millisecond (3 digits)
[403] Fix | Delete
# %6N :: microsecond (6 digits)
[404] Fix | Delete
# %9N :: nanosecond (9 digits)
[405] Fix | Delete
# %p :: Meridian indicator ("AM" or "PM")
[406] Fix | Delete
# %P :: Meridian indicator ("am" or "pm")
[407] Fix | Delete
# %r :: time, 12-hour (same as %I:%M:%S %p)
[408] Fix | Delete
# %R :: time, 24-hour (%H:%M)
[409] Fix | Delete
# %s :: Number of seconds since 1970-01-01 00:00:00 UTC.
[410] Fix | Delete
# %S :: Second of the minute (00..60)
[411] Fix | Delete
# %t :: Tab character (\t)
[412] Fix | Delete
# %T :: time, 24-hour (%H:%M:%S)
[413] Fix | Delete
# %u :: Day of the week as a decimal, Monday being 1. (1..7)
[414] Fix | Delete
# %U :: Week number of the current year, starting with the first Sunday as
[415] Fix | Delete
# the first day of the first week (00..53)
[416] Fix | Delete
# %v :: VMS date (%e-%b-%Y)
[417] Fix | Delete
# %V :: Week number of year according to ISO 8601 (01..53)
[418] Fix | Delete
# %W :: Week number of the current year, starting with the first Monday
[419] Fix | Delete
# as the first day of the first week (00..53)
[420] Fix | Delete
# %w :: Day of the week (Sunday is 0, 0..6)
[421] Fix | Delete
# %x :: Preferred representation for the date alone, no time
[422] Fix | Delete
# %X :: Preferred representation for the time alone, no date
[423] Fix | Delete
# %y :: Year without a century (00..99)
[424] Fix | Delete
# %Y :: Year which may include century, if provided
[425] Fix | Delete
# %z :: Time zone as hour offset from UTC (e.g. +0900)
[426] Fix | Delete
# %Z :: Time zone name
[427] Fix | Delete
# %% :: Literal "%" character
[428] Fix | Delete
[429] Fix | Delete
def strptime(date, format, now=self.now)
[430] Fix | Delete
d = Date._strptime(date, format)
[431] Fix | Delete
raise ArgumentError, "invalid strptime format - `#{format}'" unless d
[432] Fix | Delete
if seconds = d[:seconds]
[433] Fix | Delete
if sec_fraction = d[:sec_fraction]
[434] Fix | Delete
usec = sec_fraction * 1000000
[435] Fix | Delete
usec *= -1 if seconds < 0
[436] Fix | Delete
else
[437] Fix | Delete
usec = 0
[438] Fix | Delete
end
[439] Fix | Delete
t = Time.at(seconds, usec)
[440] Fix | Delete
if zone = d[:zone]
[441] Fix | Delete
force_zone!(t, zone)
[442] Fix | Delete
end
[443] Fix | Delete
else
[444] Fix | Delete
year = d[:year]
[445] Fix | Delete
year = yield(year) if year && block_given?
[446] Fix | Delete
t = make_time(date, year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
[447] Fix | Delete
end
[448] Fix | Delete
t
[449] Fix | Delete
end
[450] Fix | Delete
[451] Fix | Delete
MonthValue = { # :nodoc:
[452] Fix | Delete
'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
[453] Fix | Delete
'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
[454] Fix | Delete
}
[455] Fix | Delete
[456] Fix | Delete
#
[457] Fix | Delete
# Parses +date+ as date-time defined by RFC 2822 and converts it to a Time
[458] Fix | Delete
# object. The format is identical to the date format defined by RFC 822 and
[459] Fix | Delete
# updated by RFC 1123.
[460] Fix | Delete
#
[461] Fix | Delete
# ArgumentError is raised if +date+ is not compliant with RFC 2822
[462] Fix | Delete
# or if the Time class cannot represent specified date.
[463] Fix | Delete
#
[464] Fix | Delete
# See #rfc2822 for more information on this format.
[465] Fix | Delete
#
[466] Fix | Delete
# You must require 'time' to use this method.
[467] Fix | Delete
#
[468] Fix | Delete
def rfc2822(date)
[469] Fix | Delete
if /\A\s*
[470] Fix | Delete
(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?
[471] Fix | Delete
(\d{1,2})\s+
[472] Fix | Delete
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+
[473] Fix | Delete
(\d{2,})\s+
[474] Fix | Delete
(\d{2})\s*
[475] Fix | Delete
:\s*(\d{2})
[476] Fix | Delete
(?:\s*:\s*(\d\d))?\s+
[477] Fix | Delete
([+-]\d{4}|
[478] Fix | Delete
UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date
[479] Fix | Delete
# Since RFC 2822 permit comments, the regexp has no right anchor.
[480] Fix | Delete
day = $1.to_i
[481] Fix | Delete
mon = MonthValue[$2.upcase]
[482] Fix | Delete
year = $3.to_i
[483] Fix | Delete
short_year_p = $3.length <= 3
[484] Fix | Delete
hour = $4.to_i
[485] Fix | Delete
min = $5.to_i
[486] Fix | Delete
sec = $6 ? $6.to_i : 0
[487] Fix | Delete
zone = $7
[488] Fix | Delete
[489] Fix | Delete
if short_year_p
[490] Fix | Delete
# following year completion is compliant with RFC 2822.
[491] Fix | Delete
year = if year < 50
[492] Fix | Delete
2000 + year
[493] Fix | Delete
else
[494] Fix | Delete
1900 + year
[495] Fix | Delete
end
[496] Fix | Delete
end
[497] Fix | Delete
[498] Fix | Delete
off = zone_offset(zone)
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function