| Class | Time |
| In: |
time.c
lib/date.rb lib/rss/rss.rb lib/time.rb lib/yaml/rubytypes.rb |
| Parent: | Object |
Implements the extensions to the Time class that are described in the documentation for the time.rb library.
| ZoneOffset | = | { 'UTC' => 0, # ISO 8601 'Z' => 0, # RFC 822 'UT' => 0, 'GMT' => 0, 'EST' => -5, 'EDT' => -4, 'CST' => -6, 'CDT' => -5, 'MST' => -7, 'MDT' => -6, 'PST' => -8, 'PDT' => -7, # Following definition of military zones is original one. # See RFC 1123 and RFC 2822 for the error in RFC 822. 'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6, 'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12, 'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6, 'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12, } |
| LeapYearMonthDays | = | [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| CommonYearMonthDays | = | [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| MonthValue | = | { 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12 |
| RFC2822_DAY_NAME | = | [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' |
| RFC2822_MONTH_NAME | = | [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' |
| rfc2822 | -> | rfc822 |
| xmlschema | -> | iso8601 |
Creates a new time object with the value given by time, the given number of seconds_with_frac, or seconds and microseconds_with_frac from the Epoch. seconds_with_frac and microseconds_with_frac can be Integer, Float, Rational, or other Numeric. non-portable feature allows the offset to be negative on some systems.
Time.at(0) #=> 1969-12-31 18:00:00 -0600 Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600 Time.at(946702800) #=> 1999-12-31 23:00:00 -0600 Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600 Time.at(946684800.2).usec #=> 200000 Time.at(946684800, 123456.789).nsec #=> 123456789
Creates a time based on given values, interpreted as UTC (GMT). The year must be specified. Other values default to the minimum value for that field (and may be nil or omitted). Months may be specified by numbers from 1 to 12, or by the three-letter English month names. Hours are specified on a 24-hour clock (0..23). Raises an ArgumentError if any values are out of range. Will also accept ten arguments in the order output by Time#to_a. sec_with_frac and usec_with_frac can have a fractional part.
Time.utc(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
Parses date as HTTP-date defined by RFC 2616 and converts it to a Time object.
ArgumentError is raised if date is not compliant with RFC 2616 or Time class cannot represent specified date.
See httpdate for more information on this format.
Returns a Time object initialized to the current system time. Note: The object created will be created using the resolution available on your system clock, and so may include fractional seconds.
a = Time.new #=> 2007-11-19 07:50:02 -0600 b = Time.new #=> 2007-11-19 07:50:02 -0600 a == b #=> false "%.6f" % a.to_f #=> "1195480202.282373" "%.6f" % b.to_f #=> "1195480202.283415"
Parses date using Date._parse and converts it to a Time object.
If a block is given, the year described in date is converted by the block. For example:
Time.parse(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
If the upper components of the given time are broken or missing, they are supplied with those of now. For the lower components, the minimum values (1 or 0) are assumed if broken or missing. For example:
# Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
# your timezone is GMT:
Time.parse("16:30") #=> Thu Nov 29 16:30:00 GMT 2001
Time.parse("7/23") #=> Mon Jul 23 00:00:00 GMT 2001
Time.parse("Aug 31") #=> Fri Aug 31 00:00:00 GMT 2001
Since there are numerous conflicts among locally defined timezone abbreviations all over the world, this method is not made to understand all of them. For example, the abbreviation "CST" is used variously as:
-06:00 in America/Chicago,
-05:00 in America/Havana,
+08:00 in Asia/Harbin,
+09:30 in Australia/Darwin,
+10:30 in Australia/Adelaide,
etc.
Based on the fact, this method only understands the timezone abbreviations described in RFC 822 and the system timezone, in the order named. (i.e. a definition in RFC 822 overrides the system timezone definition.) The system timezone is taken from Time.local(year, 1, 1).zone and Time.local(year, 7, 1).zone. If the extracted timezone abbreviation does not match any of them, it is ignored and the given time is regarded as a local time.
ArgumentError is raised if Date._parse cannot extract information from date or Time class cannot represent specified date.
This method can be used as fail-safe for other parsing methods as:
Time.rfc2822(date) rescue Time.parse(date) Time.httpdate(date) rescue Time.parse(date) Time.xmlschema(date) rescue Time.parse(date)
A failure for Time.parse should be checked, though.
Parses date as date-time defined by RFC 2822 and converts it to a Time object. The format is identical to the date format defined by RFC 822 and updated by RFC 1123.
ArgumentError is raised if date is not compliant with RFC 2822 or Time class cannot represent specified date.
See rfc2822 for more information on this format.
Parses date using Date._strptime and converts it to a Time object.
If a block is given, the year described in date is converted by the block. For example:
Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
Creates a time based on given values, interpreted as UTC (GMT). The year must be specified. Other values default to the minimum value for that field (and may be nil or omitted). Months may be specified by numbers from 1 to 12, or by the three-letter English month names. Hours are specified on a 24-hour clock (0..23). Raises an ArgumentError if any values are out of range. Will also accept ten arguments in the order output by Time#to_a. sec_with_frac and usec_with_frac can have a fractional part.
Time.utc(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC
Parses date as dateTime defined by XML Schema and converts it to a Time object. The format is restricted version of the format defined by ISO 8601.
ArgumentError is raised if date is not compliant with the format or Time class cannot represent specified date.
See xmlschema for more information on this format.
Addition—Adds some number of seconds (possibly fractional) to time and returns that value as a new time.
t = Time.now #=> 2007-11-19 08:22:21 -0600 t + (60 * 60 * 24) #=> 2007-11-20 08:22:21 -0600
Difference—Returns a new time that represents the difference between two times, or subtracts the given number of seconds in numeric from time.
t = Time.now #=> 2007-11-19 08:23:10 -0600 t2 = t + 2592000 #=> 2007-12-19 08:23:10 -0600 t2 - t #=> 2592000.0 t2 - 2592000 #=> 2007-11-19 08:23:10 -0600
Comparison—Compares time with other_time.
t = Time.now #=> 2007-11-19 08:12:12 -0600 t2 = t + 2592000 #=> 2007-12-19 08:12:12 -0600 t <=> t2 #=> -1 t2 <=> t #=> 1 t = Time.now #=> 2007-11-19 08:13:38 -0600 t2 = t + 0.1 #=> 2007-11-19 08:13:38 -0600 t.nsec #=> 98222999 t2.nsec #=> 198222999 t <=> t2 #=> -1 t2 <=> t #=> 1 t <=> t #=> 0
Returns true if time occurs during Daylight Saving Time in its time zone.
# CST6CDT: Time.local(2000, 1, 1).zone #=> "CST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "CDT" Time.local(2000, 7, 1).isdst #=> true Time.local(2000, 7, 1).dst? #=> true # Asia/Tokyo: Time.local(2000, 1, 1).zone #=> "JST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "JST" Time.local(2000, 7, 1).isdst #=> false Time.local(2000, 7, 1).dst? #=> false
Return true if time and other_time are both Time objects with the same seconds and fractional seconds.
Returns true if time represents Friday.
t = Time.local(1987, 12, 18) #=> 1987-12-18 00:00:00 -0600 t.friday? #=> true
Returns a new new_time object representing time in UTC.
t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600 t.gmt? #=> false y = t.getgm #=> 2000-01-02 02:15:01 UTC y.gmt? #=> true t == y #=> true
Returns a new new_time object representing time in UTC.
t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600 t.gmt? #=> false y = t.getgm #=> 2000-01-02 02:15:01 UTC y.gmt? #=> true t == y #=> true
Returns true if time represents a time in UTC (GMT).
t = Time.now #=> 2007-11-19 08:15:23 -0600 t.utc? #=> false t = Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.utc? #=> true t = Time.now #=> 2007-11-19 08:16:03 -0600 t.gmt? #=> false t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.gmt? #=> true
Returns the offset in seconds between the timezone of time and UTC.
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.gmt_offset #=> 0 l = t.getlocal #=> 2000-01-01 14:15:01 -0600 l.gmt_offset #=> -21600
Converts time to UTC (GMT), modifying the receiver.
t = Time.now #=> 2007-11-19 08:18:31 -0600 t.gmt? #=> false t.gmtime #=> 2007-11-19 14:18:31 UTC t.gmt? #=> true t = Time.now #=> 2007-11-19 08:18:51 -0600 t.utc? #=> false t.utc #=> 2007-11-19 14:18:51 UTC t.utc? #=> true
Returns the offset in seconds between the timezone of time and UTC.
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.gmt_offset #=> 0 l = t.getlocal #=> 2000-01-01 14:15:01 -0600 l.gmt_offset #=> -21600
Returns a string which represents the time as rfc1123-date of HTTP-date defined by RFC 2616:
day-of-week, DD month-name CCYY hh:mm:ss GMT
Note that the result is always UTC (GMT).
Returns a string representing time. Equivalent to calling Time#strftime with a format string of ``%Y-%m-%d %H:%M:%S %z’’ for a local time and ``%Y-%m-%d %H:%M:%S UTC’’ for a UTC time.
Time.now.to_s #=> "2007-10-05 16:09:51 +0900" Time.now.utc.to_s #=> "2007-10-05 07:09:51 UTC"
Returns true if time occurs during Daylight Saving Time in its time zone.
# CST6CDT: Time.local(2000, 1, 1).zone #=> "CST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "CDT" Time.local(2000, 7, 1).isdst #=> true Time.local(2000, 7, 1).dst? #=> true # Asia/Tokyo: Time.local(2000, 1, 1).zone #=> "JST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "JST" Time.local(2000, 7, 1).isdst #=> false Time.local(2000, 7, 1).dst? #=> false
Returns the minute of the hour (0..59) for time.
t = Time.now #=> 2007-11-19 08:25:51 -0600 t.min #=> 25
Returns true if time represents Monday.
t = Time.local(2003, 8, 4) #=> 2003-08-04 00:00:00 -0500 p t.monday? #=> true
Returns just the number of nanoseconds for time.
t = Time.now #=> 2007-11-17 15:18:03 +0900 "%10.9f" % t.to_f #=> "1195280283.536151409" t.nsec #=> 536151406
The lowest digit of to_f and nsec is different because IEEE 754 double is not accurate enough to represent nanoseconds from the Epoch. The accurate value is returned by nsec.
Returns true if time represents Saturday.
t = Time.local(2006, 6, 10) #=> 2006-06-10 00:00:00 -0500 t.saturday? #=> true
Returns the second of the minute (0..60)[Yes, seconds really can range from zero to 60. This allows the system to inject leap seconds every now and then to correct for the fact that years are not really a convenient number of hours long.] for time.
t = Time.now #=> 2007-11-19 08:25:02 -0600 t.sec #=> 2
Formats time according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.
Format meaning:
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
t = Time.now #=> 2007-11-19 08:37:48 -0600
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007"
t.strftime("at %I:%M%p") #=> "at 08:37AM"
Return a new time object, one second later than time.
t = Time.now #=> 2007-11-19 08:23:57 -0600
t.succ #=> 2007-11-19 08:23:58 -0600
Returns true if time represents Sunday.
t = Time.local(1990, 4, 1) #=> 1990-04-01 00:00:00 -0600 t.sunday? #=> true
Returns true if time represents Thursday.
t = Time.local(1995, 12, 21) #=> 1995-12-21 00:00:00 -0600 p t.thursday? #=> true
Returns a ten-element array of values for time: {[ sec, min, hour, day, month, year, wday, yday, isdst, zone ]}. See the individual methods for an explanation of the valid ranges of each value. The ten elements can be passed directly to Time::utc or Time::local to create a new Time.
t = Time.now #=> 2007-11-19 08:36:01 -0600 now = t.to_a #=> [1, 36, 8, 19, 11, 2007, 1, 323, false, "CST"]
Returns the value of time as a floating point number of seconds since the Epoch.
t = Time.now "%10.5f" % t.to_f #=> "1049896564.13654" t.to_i #=> 1049896564
Note that IEEE 754 double is not accurate enough to represent nanoseconds from the Epoch.
Returns the value of time as an integer number of seconds since the Epoch.
t = Time.now "%10.5f" % t.to_f #=> "1049896564.17839" t.to_i #=> 1049896564
Returns a string representing time. Equivalent to calling Time#strftime with a format string of ``%Y-%m-%d %H:%M:%S %z’’ for a local time and ``%Y-%m-%d %H:%M:%S UTC’’ for a UTC time.
Time.now.to_s #=> "2007-10-05 16:09:51 +0900" Time.now.utc.to_s #=> "2007-10-05 07:09:51 UTC"
Returns true if time represents Tuesday.
t = Time.local(1991, 2, 19) #=> 1991-02-19 00:00:00 -0600 p t.tuesday? #=> true
Returns just the number of nanoseconds for time.
t = Time.now #=> 2007-11-17 15:18:03 +0900 "%10.9f" % t.to_f #=> "1195280283.536151409" t.nsec #=> 536151406
The lowest digit of to_f and nsec is different because IEEE 754 double is not accurate enough to represent nanoseconds from the Epoch. The accurate value is returned by nsec.
Returns the value of time as an integer number of seconds since the Epoch.
t = Time.now "%10.5f" % t.to_f #=> "1049896564.17839" t.to_i #=> 1049896564
Returns just the number of microseconds for time.
t = Time.now #=> 2007-11-19 08:03:26 -0600 "%10.6f" % t.to_f #=> "1195481006.775195" t.usec #=> 775195
Returns just the number of microseconds for time.
t = Time.now #=> 2007-11-19 08:03:26 -0600 "%10.6f" % t.to_f #=> "1195481006.775195" t.usec #=> 775195
Converts time to UTC (GMT), modifying the receiver.
t = Time.now #=> 2007-11-19 08:18:31 -0600 t.gmt? #=> false t.gmtime #=> 2007-11-19 14:18:31 UTC t.gmt? #=> true t = Time.now #=> 2007-11-19 08:18:51 -0600 t.utc? #=> false t.utc #=> 2007-11-19 14:18:51 UTC t.utc? #=> true
Returns true if time represents a time in UTC (GMT).
t = Time.now #=> 2007-11-19 08:15:23 -0600 t.utc? #=> false t = Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.utc? #=> true t = Time.now #=> 2007-11-19 08:16:03 -0600 t.gmt? #=> false t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.gmt? #=> true
Returns the offset in seconds between the timezone of time and UTC.
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.gmt_offset #=> 0 l = t.getlocal #=> 2000-01-01 14:15:01 -0600 l.gmt_offset #=> -21600
Returns an integer representing the day of the week, 0..6, with Sunday == 0.
t = Time.now #=> 2007-11-20 02:35:35 -0600 t.wday #=> 2 t.sunday? #=> false t.monday? #=> false t.tuesday? #=> true t.wednesday? #=> false t.thursday? #=> false t.friday? #=> false t.saturday? #=> false
Returns true if time represents Wednesday.
t = Time.local(1993, 2, 24) #=> 1993-02-24 00:00:00 -0600 p t.wednesday? #=> true
Returns a string which represents the time as 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_seconds specifies a number of digits of fractional seconds. Its default value is 0.
Returns the year for time (including the century).
t = Time.now #=> 2007-11-19 08:27:51 -0600 t.year #=> 2007
Returns the name of the time zone used for time. As of Ruby 1.8, returns ``UTC’’ rather than ``GMT’’ for UTC times.
t = Time.gm(2000, "jan", 1, 20, 15, 1) t.zone #=> "UTC" t = Time.local(2000, "jan", 1, 20, 15, 1) t.zone #=> "CST"
ruby-doc.org is a service of James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, AZ.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.