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