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