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