diff options
Diffstat (limited to 'lib/time.rb')
| -rw-r--r-- | lib/time.rb | 152 |
1 files changed, 78 insertions, 74 deletions
diff --git a/lib/time.rb b/lib/time.rb index 81c0ebd7ba..e6aab3fa5d 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# shareable_constant_value: literal require 'date' @@ -23,7 +24,11 @@ require 'date' # :startdoc: +# # class Time + + VERSION = "0.4.2" # :nodoc: + class << Time # @@ -75,7 +80,7 @@ class Time # # You must require 'time' to use this method. # - def zone_offset(zone, year=self.now.year) + def zone_offset(zone, year=nil) off = nil zone = zone.upcase if /\A([+-])(\d\d)(:?)(\d\d)(?:\3(\d\d))?\z/ =~ zone @@ -84,14 +89,18 @@ class Time off = zone.to_i * 3600 elsif ZoneOffset.include?(zone) off = ZoneOffset[zone] * 3600 - elsif ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false) - off = t.utc_offset - elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false) - off = t.utc_offset + else + year ||= self.now.year + if ((t = self.local(year, 1, 1)).zone.upcase == zone rescue false) + off = t.utc_offset + elsif ((t = self.local(year, 7, 1)).zone.upcase == zone rescue false) + off = t.utc_offset + end end off end + # :stopdoc: def zone_utc?(zone) # * +0000 # In RFC 2822, +0000 indicate a time zone at Universal Time. @@ -267,11 +276,17 @@ class Time end end private :make_time + # :startdoc: # # Takes a string representation of a Time and attempts to parse it # using a heuristic. # + # This method **does not** function as a validator. If the input + # string does not match valid formats strictly, you may get a + # cryptic result. Should consider to use Time.strptime instead + # of this method as possible. + # # require 'time' # # Time.parse("2010-10-31") #=> 2010-10-31 00:00:00 -0500 @@ -380,6 +395,8 @@ class Time # heuristic to detect the format of the input string, you provide # a second argument that describes the format of the string. # + # Raises ArgumentError if the date or format is invalid. + # # If a block is given, the year described in +date+ is converted by the # block. For example: # @@ -394,9 +411,12 @@ class Time # %c :: The preferred local date and time representation # %C :: Century (20 in 2009) # %d :: Day of the month (01..31) - # %D :: Date (%m/%d/%y) + # %D :: \Date (%m/%d/%y) # %e :: Day of the month, blank-padded ( 1..31) # %F :: Equivalent to %Y-%m-%d (the ISO 8601 date format) + # %g :: The last two digits of the commercial year + # %G :: The week-based year according to ISO-8601 (week 1 starts on Monday + # and includes January 4) # %h :: Equivalent to %b # %H :: Hour of the day, 24-hour clock (00..23) # %I :: Hour of the day, 12-hour clock (01..12) @@ -410,7 +430,6 @@ class Time # %N :: Fractional seconds digits # %p :: Meridian indicator ("AM" or "PM") # %P :: Meridian indicator ("am" or "pm") - # %Q :: Number of milliseconds since 1970-01-01 00:00:00 UTC. # %r :: time, 12-hour (same as %I:%M:%S %p) # %R :: time, 24-hour (%H:%M) # %s :: Number of seconds since 1970-01-01 00:00:00 UTC. @@ -429,8 +448,8 @@ class Time # %X :: Preferred representation for the time alone, no date # %y :: Year without a century (00..99) # %Y :: Year which may include century, if provided - # %z :: Time zone as hour offset from UTC (e.g. +0900) - # %Z :: Time zone name + # %z :: \Time zone as hour offset from UTC (e.g. +0900) + # %Z :: \Time zone name # %% :: Literal "%" character # %+ :: date(1) (%a %b %e %H:%M:%S %Z %Y) # @@ -442,7 +461,7 @@ class Time # def strptime(date, format, now=self.now) d = Date._strptime(date, format) - raise ArgumentError, "invalid date or strptime format - `#{date}' `#{format}'" unless d + raise ArgumentError, "invalid date or strptime format - '#{date}' '#{format}'" unless d if seconds = d[:seconds] if sec_fraction = d[:sec_fraction] usec = sec_fraction * 1000000 @@ -457,7 +476,15 @@ class Time else year = d[:year] year = yield(year) if year && block_given? - t = make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) + yday = d[:yday] + if (d[:cwyear] && !year) || ((d[:cwday] || d[:cweek]) && !(d[:mon] && d[:mday])) + # make_time doesn't deal with cwyear/cwday/cweek + return Date.strptime(date, format).to_time + end + if (d[:wnum0] || d[:wnum1]) && !yday && !(d[:mon] && d[:mday]) + yday = Date.strptime(date, format).yday + end + t = make_time(date, year, yday, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) end t end @@ -491,8 +518,8 @@ class Time (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+ (\d{2,})\s+ (\d{2})\s* - :\s*(\d{2})\s* - (?::\s*(\d{2}))?\s+ + :\s*(\d{2}) + (?:\s*:\s*(\d\d))?\s+ ([+-]\d{4}| UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date # Since RFC 2822 permit comments, the regexp has no right anchor. @@ -580,12 +607,12 @@ class Time end # - # Parses +date+ as a dateTime defined by the XML Schema and converts it to + # Parses +time+ as a dateTime defined by the XML Schema and converts it to # a Time object. The format is a restricted version of the format defined # by ISO 8601. # - # ArgumentError is raised if +date+ is not compliant with the format or if - # the Time class cannot represent specified date. + # ArgumentError is raised if +time+ is not compliant with the format or if + # the Time class cannot represent the specified time. # # See #xmlschema for more information on this format. # @@ -596,14 +623,14 @@ class Time # # You must require 'time' to use this method. # - def xmlschema(date) + def xmlschema(time) if /\A\s* (-?\d+)-(\d\d)-(\d\d) T (\d\d):(\d\d):(\d\d) (\.\d+)? (Z|[+-]\d\d(?::?\d\d)?)? - \s*\z/ix =~ date + \s*\z/ix =~ time year = $1.to_i mon = $2.to_i day = $3.to_i @@ -626,7 +653,7 @@ class Time self.local(year, mon, day, hour, min, sec, usec) end else - raise ArgumentError.new("invalid date: #{date.inspect}") + raise ArgumentError.new("invalid xmlschema format: #{time.inspect}") end end alias iso8601 xmlschema @@ -649,30 +676,10 @@ class Time # You must require 'time' to use this method. # def rfc2822 - sprintf('%s, %02d %s %0*d %02d:%02d:%02d ', - RFC2822_DAY_NAME[wday], - day, RFC2822_MONTH_NAME[mon-1], year < 0 ? 5 : 4, year, - hour, min, sec) << - if utc? - '-0000' - else - off = utc_offset - sign = off < 0 ? '-' : '+' - sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60)) - end + strftime('%a, %d %b %Y %T ') << (utc? ? '-0000' : strftime('%z')) end alias rfc822 rfc2822 - - RFC2822_DAY_NAME = [ # :nodoc: - 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' - ] - - RFC2822_MONTH_NAME = [ # :nodoc: - 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', - 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' - ] - # # Returns a string which represents the time as RFC 1123 date of HTTP-date # defined by RFC 2616: @@ -689,42 +696,39 @@ class Time # You must require 'time' to use this method. # def httpdate - t = dup.utc - sprintf('%s, %02d %s %0*d %02d:%02d:%02d GMT', - RFC2822_DAY_NAME[t.wday], - t.day, RFC2822_MONTH_NAME[t.mon-1], t.year < 0 ? 5 : 4, t.year, - t.hour, t.min, t.sec) + getutc.strftime('%a, %d %b %Y %T GMT') end - # - # Returns a string which represents the time as a dateTime defined by XML - # Schema: - # - # CCYY-MM-DDThh:mm:ssTZD - # CCYY-MM-DDThh:mm:ss.sssTZD - # - # where TZD is Z or [+-]hh:mm. - # - # If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise. - # - # +fractional_digits+ specifies a number of digits to use for fractional - # seconds. Its default value is 0. - # - # require 'time' - # - # t = Time.now - # t.iso8601 # => "2011-10-05T22:26:12-04:00" - # - # You must require 'time' to use this method. - # - def xmlschema(fraction_digits=0) - fraction_digits = fraction_digits.to_i - s = strftime("%FT%T") - if fraction_digits > 0 - s << strftime(".%#{fraction_digits}N") + unless method_defined?(:xmlschema) + # + # Returns a string which represents the time as a dateTime defined by XML + # Schema: + # + # CCYY-MM-DDThh:mm:ssTZD + # CCYY-MM-DDThh:mm:ss.sssTZD + # + # where TZD is Z or [+-]hh:mm. + # + # If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise. + # + # +fraction_digits+ specifies a number of digits to use for fractional + # seconds. Its default value is 0. + # + # require 'time' + # + # t = Time.now + # t.iso8601 # => "2011-10-05T22:26:12-04:00" + # + # You must require 'time' to use this method. + # + def xmlschema(fraction_digits=0) + fraction_digits = fraction_digits.to_i + s = strftime("%FT%T") + if fraction_digits > 0 + s << strftime(".%#{fraction_digits}N") + end + s << (utc? ? 'Z' : strftime("%:z")) end - s << (utc? ? 'Z' : strftime("%:z")) end - alias iso8601 xmlschema + alias iso8601 xmlschema unless method_defined?(:iso8601) end - |
