summaryrefslogtreecommitdiff
path: root/lib/time.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/time.rb')
-rw-r--r--lib/time.rb92
1 files changed, 51 insertions, 41 deletions
diff --git a/lib/time.rb b/lib/time.rb
index 43c4d802e5..e6aab3fa5d 100644
--- a/lib/time.rb
+++ b/lib/time.rb
@@ -24,7 +24,11 @@ require 'date'
# :startdoc:
+# #
class Time
+
+ VERSION = "0.4.2" # :nodoc:
+
class << Time
#
@@ -76,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
@@ -85,10 +89,13 @@ 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
@@ -277,7 +284,7 @@ class Time
#
# 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
+ # cryptic result. Should consider to use Time.strptime instead
# of this method as possible.
#
# require 'time'
@@ -388,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:
#
@@ -402,7 +411,7 @@ 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
@@ -439,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)
#
@@ -452,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
@@ -509,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.
@@ -690,35 +699,36 @@ class Time
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.
- #
- # +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")
+ 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
-