1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Update Time documentation

This commit is contained in:
Burdette Lamar 2021-04-25 15:51:31 -05:00 committed by Jeremy Evans
parent 5a451c4b1f
commit 6ee55455a8
10 changed files with 232 additions and 141 deletions

7
doc/time/in.rdoc Normal file
View file

@ -0,0 +1,7 @@
- <tt>in: zone</tt>: a timezone _zone_, which may be:
- A string offset from UTC.
- A single letter offset from UTC, in the range <tt>'A'..'Z'</tt>,
<tt>'J'</tt> (the so-called military timezone) excluded.
- An integer number of seconds.
- A timezone object;
see {Timezone Argument}[#class-Time-label-Timezone+Argument] for details.

8
doc/time/mon-min.rdoc Normal file
View file

@ -0,0 +1,8 @@
- +month+: a month value, which may be:
- An integer month in the range <tt>1..12</tt>.
- A 3-character string that matches regular expression
<tt>/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/i</tt>.
- +day+: an integer day in the range <tt>1..31</tt>
(less than 31 for some months).
- +hour+: an integer hour in the range <tt>0..23</tt>.
- +min+: an integer minute in the range <tt>0..59</tt>.

2
doc/time/msec.rdoc Normal file
View file

@ -0,0 +1,2 @@
- +msec+ is the number of milliseconds (Integer, Float, or Rational)
in the range <tt>0..1000</tt>.

2
doc/time/nsec.rdoc Normal file
View file

@ -0,0 +1,2 @@
- +nsec+ is the number of nanoseconds (Integer, Float, or Rational)
in the range <tt>0..1000000000</tt>.

2
doc/time/sec.rdoc Normal file
View file

@ -0,0 +1,2 @@
- +sec+ is the number of seconds (Integer, Float, or Rational)
in the range <tt>0..60</tt>.

1
doc/time/sec_i.rdoc Normal file
View file

@ -0,0 +1 @@
- +isec_i+ is the integer number of seconds in the range <tt>0..60</tt>.

2
doc/time/usec.rdoc Normal file
View file

@ -0,0 +1,2 @@
- +usec+ is the number of microseconds (Integer, Float, or Rational)
in the range <tt>0..1000000</tt>.

1
doc/time/year.rdoc Normal file
View file

@ -0,0 +1 @@
- +year+: an integer year.

206
time.c
View file

@ -3332,35 +3332,37 @@ tmcmp(struct tm *a, struct tm *b)
/*
* call-seq:
* Time.utc(year) -> time
* Time.utc(year, month) -> time
* Time.utc(year, month, day) -> time
* Time.utc(year, month, day, hour) -> time
* Time.utc(year, month, day, hour, min) -> time
* Time.utc(year, month, day, hour, min, sec_with_frac) -> time
* Time.utc(year, month, day, hour, min, sec, usec_with_frac) -> time
* Time.utc(sec, min, hour, day, month, year, dummy, dummy, dummy, dummy) -> time
* Time.gm(year) -> time
* Time.gm(year, month) -> time
* Time.gm(year, month, day) -> time
* Time.gm(year, month, day, hour) -> time
* Time.gm(year, month, day, hour, min) -> time
* Time.gm(year, month, day, hour, min, sec_with_frac) -> time
* Time.gm(year, month, day, hour, min, sec, usec_with_frac) -> time
* Time.gm(sec, min, hour, day, month, year, dummy, dummy, dummy, dummy) -> time
* Time.utc(year, month=1, day=1, hour=0, min=0, sec_i=0, usec=0) -> new_time
* Time.utc(sec_i, min, hour, day, month, year, dummy, dummy, dummy, dummy) -> new_time
*
* Creates a Time object 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.
* Returns a new \Time object based the on given arguments;
* its timezone is UTC.
*
* +sec_with_frac+ and +usec_with_frac+ can have a fractional part.
* In the first form (up to seven arguments), argument +year+ is required.
*
* Time.utc(2000) # => 2000-01-01 00:00:00 UTC
* Time.utc(0, 1, 2, 3, 4, 5, 6.5) # => 0000-01-02 03:04:05.0000065 UTC
*
* In the second form, all ten arguments are required,
* though the last four are ignored.
* This form is useful for creating a time from a 10-element array
* such as is returned by #to_a.
*
* array = Time.now.to_a
* p array # => [57, 26, 13, 24, 4, 2021, 6, 114, true, "Central Daylight Time"]
* array[5] = 2000
* Time.utc(*array) # => 2000-04-24 13:26:57 UTC
*
* Parameters:
* :include: doc/time/year.rdoc
* :include: doc/time/mon-min.rdoc
* :include: doc/time/sec_i.rdoc
* :include: doc/time/usec.rdoc
*
* Alias: Time.gm.
* Related: Time.local.
*
* 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
*/
static VALUE
time_s_mkutc(int argc, VALUE *argv, VALUE klass)
@ -3373,27 +3375,36 @@ time_s_mkutc(int argc, VALUE *argv, VALUE klass)
/*
* call-seq:
* Time.local(year) -> time
* Time.local(year, month) -> time
* Time.local(year, month, day) -> time
* Time.local(year, month, day, hour) -> time
* Time.local(year, month, day, hour, min) -> time
* Time.local(year, month, day, hour, min, sec_with_frac) -> time
* Time.local(year, month, day, hour, min, sec, usec_with_frac) -> time
* Time.local(sec, min, hour, day, month, year, dummy, dummy, isdst, dummy) -> time
* Time.mktime(year) -> time
* Time.mktime(year, month) -> time
* Time.mktime(year, month, day) -> time
* Time.mktime(year, month, day, hour) -> time
* Time.mktime(year, month, day, hour, min) -> time
* Time.mktime(year, month, day, hour, min, sec_with_frac) -> time
* Time.mktime(year, month, day, hour, min, sec, usec_with_frac) -> time
* Time.mktime(sec, min, hour, day, month, year, dummy, dummy, isdst, dummy) -> time
* Time.local(year, month=1, day=1, hour=0, min=0, sec_i=0, usec=0) -> new_time
* Time.local(sec, min, hour, day, month, year, dummy, dummy, dummy, dummy) -> new_time
*
* Same as Time.utc, but interprets the values in the
* local time zone.
* Returns a new \Time object based the on given arguments;
* its timezone is the local timezone.
*
* Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
* In the first form (up to seven arguments), argument +year+ is required.
*
* Time.local(2000) # => 2000-01-01 00:00:00 -0600
* Time.local(0, 1, 2, 3, 4, 5, 6.5) # => 0000-01-02 03:04:05.0000065 -0600
*
* In the second form, all ten arguments are required,
* though the last four are ignored.
* This form is useful for creating a time from a 10-element array
* such as those returned by #to_a.
*
* array = Time.now.to_a
* p array # => [57, 26, 13, 24, 4, 2021, 6, 114, true, "Central Daylight Time"]
* array[5] = 2000
* Time.local(*array) # => 2000-04-24 13:26:57 -0500
*
* Parameters:
* :include: doc/time/year.rdoc
* :include: doc/time/mon-min.rdoc
* :include: doc/time/sec_i.rdoc
* :include: doc/time/usec.rdoc
*
* Alias: Time.mktime.
*
* Related: Time.utc.
*/
static VALUE
@ -5378,7 +5389,7 @@ tm_from_time(VALUE klass, VALUE time)
/*
* call-seq:
*
* Time::tm.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, tz=nil) -> tm
* Time::tm.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, zone=nil) -> tm
*
* Creates new Time::tm object.
*/
@ -5590,17 +5601,19 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
* GMT is the older way of referring to these baseline times but persists in
* the names of calls on POSIX systems.
*
* Note: A \Time object uses the resolution available on your system clock.
*
* All times may have subsecond. Be aware of this fact when comparing times
* with each other -- times that are apparently equal when displayed may be
* different when compared.
* (Since Ruby 2.7.0, Time#inspect shows subsecond but
* Time#to_s still doesn't show subsecond.)
*
* = Examples
* == Examples
*
* All of these examples were done using the EST timezone which is GMT-5.
*
* == Creating a new Time instance
* === Creating a New \Time Instance
*
* You can create a new instance of Time with Time.new. This will use the
* current system time. Time.now is an alias for this. You can also
@ -5619,8 +5632,8 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
*
* Or a timezone object:
*
* tz = timezone("Europe/Athens") # Eastern European Time, UTC+2
* Time.new(2002, 10, 31, 2, 2, 2, tz) #=> 2002-10-31 02:02:02 +0200
* zone = timezone("Europe/Athens") # Eastern European Time, UTC+2
* Time.new(2002, 10, 31, 2, 2, 2, zone) #=> 2002-10-31 02:02:02 +0200
*
* You can also use Time.local and Time.utc to infer
* local and UTC timezones instead of using the current system
@ -5632,7 +5645,7 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
*
* Time.at(628232400) #=> 1989-11-28 00:00:00 -0500
*
* == Working with an instance of Time
* === Working with an Instance of \Time
*
* Once you have an instance of Time there is a multitude of things you can
* do with it. Below are some examples. For all of the following examples, we
@ -5672,7 +5685,90 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
*
* Time.new(2010,10,31).between?(t1, t2) #=> true
*
* == Timezone argument
* == What's Here
*
* \Class \Time provides methods that are useful for:
*
* - {Creating \Time objects}[#class-Time-label-Methods+for+Creating].
* - {Fetching \Time values}[#class-Time-label-Methods+for+Fetching].
* - {Querying a \Time object}[#class-Time-label-Methods+for+Querying].
* - {Comparing \Time objects}[#class-Time-label-Methods+for+Comparing].
* - {Converting a \Time object}[#class-Time-label-Methods+for+Converting].
* - {Rounding a \Time}[#class-Time-label-Methods+for+Rounding].
*
* === Methods for Creating
*
* - ::new: Returns a new time from specified arguments (year, month, etc.),
* including an optional timezone value.
* - ::local (aliased as ::mktime): Same as ::new, except the
* timezone is the local timezone.
* - ::utc (aliased as ::gm): Same as ::new, except the timezone is UTC.
* - ::at: Returns a new time based on seconds since epoch.
* - ::now: Returns a new time based on the current system time.
* - #+ (plus): Returns a new time increased by the given number of seconds.
* - {-}[#method-i-2D] (minus): Returns a new time
* decreased by the given number of seconds.
*
* === Methods for Fetching
*
* - #year: Returns the year of the time.
* - #month (aliased as #mon): Returns the month of the time.
* - #mday (aliased as #day): Returns the day of the month.
* - #hour: Returns the hours value for the time.
* - #min: Returns the minutes value for the time.
* - #sec: Returns the seconds value for the time.
* - #usec (aliased as #tv_usec): Returns the number of microseconds
* in the subseconds value of the time.
* - #nsec (aliased as #tv_nsec: Returns the number of nanoseconds
* in the subsecond part of the time.
* - #subsec: Returns the subseconds value for the time.
* - #wday: Returns the integer weekday value of the time (0 == Sunday).
* - #yday: Returns the integer yearday value of the time (1 == January 1).
* - #hash: Returns the integer hash value for the time.
* - #utc_offset (aliased as #gmt_offset and #gmtoff): Returns the offset
* in seconds between time and UTC.
* - #to_f: Returns the float number of seconds since epoch for the time.
* - #to_i (aliased as #tv_sec): Returns the integer number of seconds since epoch
* for the time.
* - #to_r: Returns the Rational number of seconds since epoch for the time.
* - #zone: Returns a string representation of the timezone of the time.
*
* === Methods for Querying
*
* - #utc? (aliased as #gmt?): Returns whether the time is UTC.
* - #dst? (aliased as #isdst): Returns whether the time is DST (daylight saving time).
* - #sunday?: Returns whether the time is a Sunday.
* - #monday?: Returns whether the time is a Monday.
* - #tuesday?: Returns whether the time is a Tuesday.
* - #wednesday?: Returns whether the time is a Wednesday.
* - #thursday?: Returns whether the time is a Thursday.
* - #friday?: Returns whether time is a Friday.
* - #saturday?: Returns whether the time is a Saturday.
*
* === Methods for Comparing
*
* - {#<=>}[#method-i-3C-3D-3E]: Compares +self+ to another time.
* - #eql?: Returns whether the time is equal to another time.
*
* === Methods for Converting
*
* - #asctime (aliased as #ctime): Returns the time as a string.
* - #inspect: Returns the time in detail as a string.
* - #strftime: Returns the time as a string, according to a given format.
* - #to_a: Returns a 10-element array of values from the time.
* - #to_s: Returns a string representation of the time.
* - #getutc (aliased as #getgm): Returns a new time converted to UTC.
* - #getlocal: Returns a new time converted to local time.
* - #utc (aliased as #gmtime): Converts time to UTC in place.
* - #localtime: Converts time to local time in place.
*
* === Methods for Rounding
*
* - #round:Returns a new time with subseconds rounded.
* - #ceil: Returns a new time with subseconds raised to a ceiling.
* - #floor: Returns a new time with subseconds lowered to a floor.
*
* == Timezone Argument
*
* A timezone argument must have +local_to_utc+ and +utc_to_local+
* methods, and may have +name+, +abbr+, and +dst?+ methods.
@ -5697,7 +5793,7 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
* The +dst?+ method is called with a +Time+ value and should return whether
* the +Time+ value is in daylight savings time in the zone.
*
* === Auto conversion to Timezone
* === Auto Conversion to Timezone
*
* At loading marshaled data, a timezone name will be converted to a timezone
* object by +find_timezone+ class method, if the method is defined.
@ -5740,9 +5836,9 @@ Init_Time(void)
rb_define_alloc_func(rb_cTime, time_s_alloc);
rb_define_singleton_method(rb_cTime, "utc", time_s_mkutc, -1);
rb_define_singleton_method(rb_cTime, "gm", time_s_mkutc, -1);
rb_define_singleton_method(rb_cTime, "local", time_s_mktime, -1);
rb_define_singleton_method(rb_cTime, "mktime", time_s_mktime, -1);
rb_define_alias(rb_singleton_class(rb_cTime), "gm", "utc");
rb_define_alias(rb_singleton_class(rb_cTime), "mktime", "local");
rb_define_method(rb_cTime, "to_i", time_to_i, 0);
rb_define_method(rb_cTime, "to_f", time_to_f, 0);

138
timev.rb
View file

@ -1,112 +1,82 @@
#
# call-seq:
# Time.now -> time
#
# Creates a new Time object for the current time.
# Creates a new \Time object from the current system time.
# This is the same as Time.new without arguments.
#
# Time.now #=> 2009-06-24 12:39:54 +0900
# Time.now # => 2009-06-24 12:39:54 +0900
# Time.now(in: '+04:00') # => 2021-04-30 01:56:44 +0400
#
# Parameter:
# :include: doc/time/in.rdoc
def Time.now(in: nil)
new(in: __builtin.arg!(:in))
end
# _Time_
#
# call-seq:
# Time.at(time) -> time
# Time.at(seconds_with_frac) -> time
# Time.at(seconds, microseconds_with_frac) -> time
# Time.at(seconds, milliseconds, :millisecond) -> time
# Time.at(seconds, microseconds, :usec) -> time
# Time.at(seconds, microseconds, :microsecond) -> time
# Time.at(seconds, nanoseconds, :nsec) -> time
# Time.at(seconds, nanoseconds, :nanosecond) -> time
# Time.at(time, in: tz) -> time
# Time.at(seconds_with_frac, in: tz) -> time
# Time.at(seconds, microseconds_with_frac, in: tz) -> time
# Time.at(seconds, milliseconds, :millisecond, in: tz) -> time
# Time.at(seconds, microseconds, :usec, in: tz) -> time
# Time.at(seconds, microseconds, :microsecond, in: tz) -> time
# Time.at(seconds, nanoseconds, :nsec, in: tz) -> time
# Time.at(seconds, nanoseconds, :nanosecond, in: tz) -> time
# This form accepts a \Time object +time+
# and optional keyword argument +in+:
#
# Creates a new Time object with the value given by +time+,
# the given number of +seconds_with_frac+, or
# +seconds+ and +microseconds_with_frac+ since the Epoch.
# +seconds_with_frac+ and +microseconds_with_frac+
# can be an Integer, Float, Rational, or other Numeric.
# Time.at(Time.new) # => 2021-04-26 08:52:31.6023486 -0500
# Time.at(Time.new, in: '+09:00') # => 2021-04-26 22:52:32.1480341 +0900
#
# If +in+ argument is given, the result is in that timezone or UTC offset, or
# if a numeric argument is given, the result is in local time.
# The +in+ argument accepts the same types of arguments as +tz+ argument of
# Time.new: string, number of seconds, or a timezone object.
# _Seconds_
#
# This form accepts a numeric number of seconds +sec+
# and optional keyword argument +in+:
#
# 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
# Time.at(946684800, 123456789, :nsec).nsec #=> 123456789
# Time.at(946702800) # => 1999-12-31 23:00:00 -0600
# Time.at(946702800, in: '+09:00') # => 2000-01-01 14:00:00 +0900
#
# Time.at(1582721899, in: "+09:00") #=> 2020-02-26 21:58:19 +0900
# Time.at(1582721899, in: "UTC") #=> 2020-02-26 12:58:19 UTC
# Time.at(1582721899, in: "C") #=> 2020-02-26 13:58:19 +0300
# Time.at(1582721899, in: 32400) #=> 2020-02-26 21:58:19 +0900
# <em>Seconds with Subseconds and Units</em>
#
# This form accepts an integer number of seconds +sec_i+,
# a numeric number of milliseconds +msec+,
# a symbol argument for the subsecond unit type (defaulting to :usec),
# and an optional keyword argument +in+:
#
# Time.at(946702800, 500, :millisecond) # => 1999-12-31 23:00:00.5 -0600
# Time.at(946702800, 500, :millisecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
# Time.at(946702800, 500000) # => 1999-12-31 23:00:00.5 -0600
# Time.at(946702800, 500000, :usec) # => 1999-12-31 23:00:00.5 -0600
# Time.at(946702800, 500000, :microsecond) # => 1999-12-31 23:00:00.5 -0600
# Time.at(946702800, 500000, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
# Time.at(946702800, 500000, :usec, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
# Time.at(946702800, 500000, :microsecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
# Time.at(946702800, 500000000, :nsec) # => 1999-12-31 23:00:00.5 -0600
# Time.at(946702800, 500000000, :nanosecond) # => 1999-12-31 23:00:00.5 -0600
# Time.at(946702800, 500000000, :nsec, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
# Time.at(946702800, 500000000, :nanosecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
#
# Parameters:
# :include: doc/time/sec_i.rdoc
# :include: doc/time/msec.rdoc
# :include: doc/time/usec.rdoc
# :include: doc/time/nsec.rdoc
# :include: doc/time/in.rdoc
#
# require 'tzinfo'
# Time.at(1582721899, in: TZInfo::Timezone.get('Europe/Kiev'))
# #=> 2020-02-26 14:58:19 +0200
def Time.at(time, subsec = (nosubsec = true), unit = (nounit = true), in: nil)
__builtin.time_s_at(time, subsec, unit, __builtin.arg!(:in), nosubsec, nounit)
end
class Time
# call-seq:
# Time.new -> time
# Time.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, tz=nil) -> time
# Time.new(year, month=nil, day=nil, hour=nil, min=nil, sec=nil, in: tz) -> time
# Returns a new \Time object based the on given arguments.
#
# Returns a Time object.
# With no positional arguments, returns the value of Time.now:
#
# It is initialized to the current system time if no argument is given.
# Time.new # => 2021-04-24 17:27:46.0512465 -0500
#
# *Note:* The new object will use the resolution available on your
# system clock, and may include subsecond.
# Otherwise, returns a new \Time object based on the given parameters:
#
# If one or more arguments are specified, the time is initialized to the
# specified time.
# Time.new(2000) # => 2000-01-01 00:00:00 -0600
# Time.new(2000, 12, 31, 23, 59, 59.5) # => 2000-12-31 23:59:59.5 -0600
# Time.new(2000, 12, 31, 23, 59, 59.5, '+09:00') # => 2000-12-31 23:59:59.5 +0900
#
# +sec+ may have subsecond if it is a rational.
# Parameters:
#
# +tz+ specifies the timezone.
# It can be an offset from UTC, given either as a string such as "+09:00"
# or a single letter "A".."Z" excluding "J" (so-called military time zone),
# or as a number of seconds such as 32400.
# Or it can be a timezone object,
# see {Timezone argument}[#class-Time-label-Timezone+argument] for details.
# :include: doc/time/year.rdoc
# :include: doc/time/mon-min.rdoc
# :include: doc/time/sec.rdoc
# :include: doc/time/in.rdoc
#
# a = Time.new #=> 2020-07-21 01:27:44.917547285 +0900
# b = Time.new #=> 2020-07-21 01:27:44.917617713 +0900
# a == b #=> false
# "%.6f" % a.to_f #=> "1595262464.917547"
# "%.6f" % b.to_f #=> "1595262464.917618"
#
# Time.new(2008,6,21, 13,30,0, "+09:00") #=> 2008-06-21 13:30:00 +0900
#
# # A trip for RubyConf 2007
# t1 = Time.new(2007,11,1,15,25,0, "+09:00") # JST (Narita)
# t2 = Time.new(2007,11,1,12, 5,0, "-05:00") # CDT (Minneapolis)
# t3 = Time.new(2007,11,1,13,25,0, "-05:00") # CDT (Minneapolis)
# t4 = Time.new(2007,11,1,16,53,0, "-04:00") # EDT (Charlotte)
# t5 = Time.new(2007,11,5, 9,24,0, "-05:00") # EST (Charlotte)
# t6 = Time.new(2007,11,5,11,21,0, "-05:00") # EST (Detroit)
# t7 = Time.new(2007,11,5,13,45,0, "-05:00") # EST (Detroit)
# t8 = Time.new(2007,11,6,17,10,0, "+09:00") # JST (Narita)
# (t2-t1)/3600.0 #=> 10.666666666666666
# (t4-t3)/3600.0 #=> 2.466666666666667
# (t6-t5)/3600.0 #=> 1.95
# (t8-t7)/3600.0 #=> 13.416666666666666
def initialize(year = (now = true), mon = nil, mday = nil, hour = nil, min = nil, sec = nil, zone = nil, in: nil)
if zone
if __builtin.arg!(:in)