Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/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
# This method **does not** function as a validator. If the input
[274] Fix | Delete
# string does not match valid formats strictly, you may get a
[275] Fix | Delete
# cryptic result. Should consider to use `Time.strptime` instead
[276] Fix | Delete
# of this method as possible.
[277] Fix | Delete
#
[278] Fix | Delete
# require 'time'
[279] Fix | Delete
#
[280] Fix | Delete
# Time.parse("2010-10-31") #=> 2010-10-31 00:00:00 -0500
[281] Fix | Delete
#
[282] Fix | Delete
# Any missing pieces of the date are inferred based on the current date.
[283] Fix | Delete
#
[284] Fix | Delete
# require 'time'
[285] Fix | Delete
#
[286] Fix | Delete
# # assuming the current date is "2011-10-31"
[287] Fix | Delete
# Time.parse("12:00") #=> 2011-10-31 12:00:00 -0500
[288] Fix | Delete
#
[289] Fix | Delete
# We can change the date used to infer our missing elements by passing a second
[290] Fix | Delete
# object that responds to #mon, #day and #year, such as Date, Time or DateTime.
[291] Fix | Delete
# We can also use our own object.
[292] Fix | Delete
#
[293] Fix | Delete
# require 'time'
[294] Fix | Delete
#
[295] Fix | Delete
# class MyDate
[296] Fix | Delete
# attr_reader :mon, :day, :year
[297] Fix | Delete
#
[298] Fix | Delete
# def initialize(mon, day, year)
[299] Fix | Delete
# @mon, @day, @year = mon, day, year
[300] Fix | Delete
# end
[301] Fix | Delete
# end
[302] Fix | Delete
#
[303] Fix | Delete
# d = Date.parse("2010-10-28")
[304] Fix | Delete
# t = Time.parse("2010-10-29")
[305] Fix | Delete
# dt = DateTime.parse("2010-10-30")
[306] Fix | Delete
# md = MyDate.new(10,31,2010)
[307] Fix | Delete
#
[308] Fix | Delete
# Time.parse("12:00", d) #=> 2010-10-28 12:00:00 -0500
[309] Fix | Delete
# Time.parse("12:00", t) #=> 2010-10-29 12:00:00 -0500
[310] Fix | Delete
# Time.parse("12:00", dt) #=> 2010-10-30 12:00:00 -0500
[311] Fix | Delete
# Time.parse("12:00", md) #=> 2010-10-31 12:00:00 -0500
[312] Fix | Delete
#
[313] Fix | Delete
# If a block is given, the year described in +date+ is converted
[314] Fix | Delete
# by the block. This is specifically designed for handling two
[315] Fix | Delete
# digit years. For example, if you wanted to treat all two digit
[316] Fix | Delete
# years prior to 70 as the year 2000+ you could write this:
[317] Fix | Delete
#
[318] Fix | Delete
# require 'time'
[319] Fix | Delete
#
[320] Fix | Delete
# Time.parse("01-10-31") {|year| year + (year < 70 ? 2000 : 1900)}
[321] Fix | Delete
# #=> 2001-10-31 00:00:00 -0500
[322] Fix | Delete
# Time.parse("70-10-31") {|year| year + (year < 70 ? 2000 : 1900)}
[323] Fix | Delete
# #=> 1970-10-31 00:00:00 -0500
[324] Fix | Delete
#
[325] Fix | Delete
# If the upper components of the given time are broken or missing, they are
[326] Fix | Delete
# supplied with those of +now+. For the lower components, the minimum
[327] Fix | Delete
# values (1 or 0) are assumed if broken or missing. For example:
[328] Fix | Delete
#
[329] Fix | Delete
# require 'time'
[330] Fix | Delete
#
[331] Fix | Delete
# # Suppose it is "Thu Nov 29 14:33:20 2001" now and
[332] Fix | Delete
# # your time zone is EST which is GMT-5.
[333] Fix | Delete
# now = Time.parse("Thu Nov 29 14:33:20 2001")
[334] Fix | Delete
# Time.parse("16:30", now) #=> 2001-11-29 16:30:00 -0500
[335] Fix | Delete
# Time.parse("7/23", now) #=> 2001-07-23 00:00:00 -0500
[336] Fix | Delete
# Time.parse("Aug 31", now) #=> 2001-08-31 00:00:00 -0500
[337] Fix | Delete
# Time.parse("Aug 2000", now) #=> 2000-08-01 00:00:00 -0500
[338] Fix | Delete
#
[339] Fix | Delete
# Since there are numerous conflicts among locally defined time zone
[340] Fix | Delete
# abbreviations all over the world, this method is not intended to
[341] Fix | Delete
# understand all of them. For example, the abbreviation "CST" is
[342] Fix | Delete
# used variously as:
[343] Fix | Delete
#
[344] Fix | Delete
# -06:00 in America/Chicago,
[345] Fix | Delete
# -05:00 in America/Havana,
[346] Fix | Delete
# +08:00 in Asia/Harbin,
[347] Fix | Delete
# +09:30 in Australia/Darwin,
[348] Fix | Delete
# +10:30 in Australia/Adelaide,
[349] Fix | Delete
# etc.
[350] Fix | Delete
#
[351] Fix | Delete
# Based on this fact, this method only understands the time zone
[352] Fix | Delete
# abbreviations described in RFC 822 and the system time zone, in the
[353] Fix | Delete
# order named. (i.e. a definition in RFC 822 overrides the system
[354] Fix | Delete
# time zone definition.) The system time zone is taken from
[355] Fix | Delete
# <tt>Time.local(year, 1, 1).zone</tt> and
[356] Fix | Delete
# <tt>Time.local(year, 7, 1).zone</tt>.
[357] Fix | Delete
# If the extracted time zone abbreviation does not match any of them,
[358] Fix | Delete
# it is ignored and the given time is regarded as a local time.
[359] Fix | Delete
#
[360] Fix | Delete
# ArgumentError is raised if Date._parse cannot extract information from
[361] Fix | Delete
# +date+ or if the Time class cannot represent specified date.
[362] Fix | Delete
#
[363] Fix | Delete
# This method can be used as a fail-safe for other parsing methods as:
[364] Fix | Delete
#
[365] Fix | Delete
# Time.rfc2822(date) rescue Time.parse(date)
[366] Fix | Delete
# Time.httpdate(date) rescue Time.parse(date)
[367] Fix | Delete
# Time.xmlschema(date) rescue Time.parse(date)
[368] Fix | Delete
#
[369] Fix | Delete
# A failure of Time.parse should be checked, though.
[370] Fix | Delete
#
[371] Fix | Delete
# You must require 'time' to use this method.
[372] Fix | Delete
#
[373] Fix | Delete
def parse(date, now=self.now)
[374] Fix | Delete
comp = !block_given?
[375] Fix | Delete
d = Date._parse(date, comp)
[376] Fix | Delete
year = d[:year]
[377] Fix | Delete
year = yield(year) if year && !comp
[378] Fix | Delete
make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
[379] Fix | Delete
end
[380] Fix | Delete
[381] Fix | Delete
#
[382] Fix | Delete
# Works similar to +parse+ except that instead of using a
[383] Fix | Delete
# heuristic to detect the format of the input string, you provide
[384] Fix | Delete
# a second argument that describes the format of the string.
[385] Fix | Delete
#
[386] Fix | Delete
# If a block is given, the year described in +date+ is converted by the
[387] Fix | Delete
# block. For example:
[388] Fix | Delete
#
[389] Fix | Delete
# Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
[390] Fix | Delete
#
[391] Fix | Delete
# Below is a list of the formatting options:
[392] Fix | Delete
#
[393] Fix | Delete
# %a :: The abbreviated weekday name ("Sun")
[394] Fix | Delete
# %A :: The full weekday name ("Sunday")
[395] Fix | Delete
# %b :: The abbreviated month name ("Jan")
[396] Fix | Delete
# %B :: The full month name ("January")
[397] Fix | Delete
# %c :: The preferred local date and time representation
[398] Fix | Delete
# %C :: Century (20 in 2009)
[399] Fix | Delete
# %d :: Day of the month (01..31)
[400] Fix | Delete
# %D :: Date (%m/%d/%y)
[401] Fix | Delete
# %e :: Day of the month, blank-padded ( 1..31)
[402] Fix | Delete
# %F :: Equivalent to %Y-%m-%d (the ISO 8601 date format)
[403] Fix | Delete
# %g :: The last two digits of the commercial year
[404] Fix | Delete
# %G :: The week-based year according to ISO-8601 (week 1 starts on Monday
[405] Fix | Delete
# and includes January 4)
[406] Fix | Delete
# %h :: Equivalent to %b
[407] Fix | Delete
# %H :: Hour of the day, 24-hour clock (00..23)
[408] Fix | Delete
# %I :: Hour of the day, 12-hour clock (01..12)
[409] Fix | Delete
# %j :: Day of the year (001..366)
[410] Fix | Delete
# %k :: hour, 24-hour clock, blank-padded ( 0..23)
[411] Fix | Delete
# %l :: hour, 12-hour clock, blank-padded ( 0..12)
[412] Fix | Delete
# %L :: Millisecond of the second (000..999)
[413] Fix | Delete
# %m :: Month of the year (01..12)
[414] Fix | Delete
# %M :: Minute of the hour (00..59)
[415] Fix | Delete
# %n :: Newline (\n)
[416] Fix | Delete
# %N :: Fractional seconds digits
[417] Fix | Delete
# %p :: Meridian indicator ("AM" or "PM")
[418] Fix | Delete
# %P :: Meridian indicator ("am" or "pm")
[419] Fix | Delete
# %r :: time, 12-hour (same as %I:%M:%S %p)
[420] Fix | Delete
# %R :: time, 24-hour (%H:%M)
[421] Fix | Delete
# %s :: Number of seconds since 1970-01-01 00:00:00 UTC.
[422] Fix | Delete
# %S :: Second of the minute (00..60)
[423] Fix | Delete
# %t :: Tab character (\t)
[424] Fix | Delete
# %T :: time, 24-hour (%H:%M:%S)
[425] Fix | Delete
# %u :: Day of the week as a decimal, Monday being 1. (1..7)
[426] Fix | Delete
# %U :: Week number of the current year, starting with the first Sunday as
[427] Fix | Delete
# the first day of the first week (00..53)
[428] Fix | Delete
# %v :: VMS date (%e-%b-%Y)
[429] Fix | Delete
# %V :: Week number of year according to ISO 8601 (01..53)
[430] Fix | Delete
# %W :: Week number of the current year, starting with the first Monday
[431] Fix | Delete
# as the first day of the first week (00..53)
[432] Fix | Delete
# %w :: Day of the week (Sunday is 0, 0..6)
[433] Fix | Delete
# %x :: Preferred representation for the date alone, no time
[434] Fix | Delete
# %X :: Preferred representation for the time alone, no date
[435] Fix | Delete
# %y :: Year without a century (00..99)
[436] Fix | Delete
# %Y :: Year which may include century, if provided
[437] Fix | Delete
# %z :: Time zone as hour offset from UTC (e.g. +0900)
[438] Fix | Delete
# %Z :: Time zone name
[439] Fix | Delete
# %% :: Literal "%" character
[440] Fix | Delete
# %+ :: date(1) (%a %b %e %H:%M:%S %Z %Y)
[441] Fix | Delete
#
[442] Fix | Delete
# require 'time'
[443] Fix | Delete
#
[444] Fix | Delete
# Time.strptime("2000-10-31", "%Y-%m-%d") #=> 2000-10-31 00:00:00 -0500
[445] Fix | Delete
#
[446] Fix | Delete
# You must require 'time' to use this method.
[447] Fix | Delete
#
[448] Fix | Delete
def strptime(date, format, now=self.now)
[449] Fix | Delete
d = Date._strptime(date, format)
[450] Fix | Delete
raise ArgumentError, "invalid date or strptime format - `#{date}' `#{format}'" unless d
[451] Fix | Delete
if seconds = d[:seconds]
[452] Fix | Delete
if sec_fraction = d[:sec_fraction]
[453] Fix | Delete
usec = sec_fraction * 1000000
[454] Fix | Delete
usec *= -1 if seconds < 0
[455] Fix | Delete
else
[456] Fix | Delete
usec = 0
[457] Fix | Delete
end
[458] Fix | Delete
t = Time.at(seconds, usec)
[459] Fix | Delete
if zone = d[:zone]
[460] Fix | Delete
force_zone!(t, zone)
[461] Fix | Delete
end
[462] Fix | Delete
else
[463] Fix | Delete
year = d[:year]
[464] Fix | Delete
year = yield(year) if year && block_given?
[465] Fix | Delete
yday = d[:yday]
[466] Fix | Delete
if (d[:cwyear] && !year) || ((d[:cwday] || d[:cweek]) && !(d[:mon] && d[:mday]))
[467] Fix | Delete
# make_time doesn't deal with cwyear/cwday/cweek
[468] Fix | Delete
return Date.strptime(date, format).to_time
[469] Fix | Delete
end
[470] Fix | Delete
if (d[:wnum0] || d[:wnum1]) && !yday && !(d[:mon] && d[:mday])
[471] Fix | Delete
yday = Date.strptime(date, format).yday
[472] Fix | Delete
end
[473] Fix | Delete
t = make_time(date, year, yday, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
[474] Fix | Delete
end
[475] Fix | Delete
t
[476] Fix | Delete
end
[477] Fix | Delete
[478] Fix | Delete
MonthValue = { # :nodoc:
[479] Fix | Delete
'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
[480] Fix | Delete
'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
#
[484] Fix | Delete
# Parses +date+ as date-time defined by RFC 2822 and converts it to a Time
[485] Fix | Delete
# object. The format is identical to the date format defined by RFC 822 and
[486] Fix | Delete
# updated by RFC 1123.
[487] Fix | Delete
#
[488] Fix | Delete
# ArgumentError is raised if +date+ is not compliant with RFC 2822
[489] Fix | Delete
# or if the Time class cannot represent specified date.
[490] Fix | Delete
#
[491] Fix | Delete
# See #rfc2822 for more information on this format.
[492] Fix | Delete
#
[493] Fix | Delete
# require 'time'
[494] Fix | Delete
#
[495] Fix | Delete
# Time.rfc2822("Wed, 05 Oct 2011 22:26:12 -0400")
[496] Fix | Delete
# #=> 2010-10-05 22:26:12 -0400
[497] Fix | Delete
#
[498] Fix | Delete
# You must require 'time' to use this method.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function