* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / timev.rb
blob892740c5a2b62f9449def4caabb00afebd3e607d
1 # Time is an abstraction of dates and times. Time is stored internally as
2 # the number of seconds with subsecond since the _Epoch_,
3 # 1970-01-01 00:00:00 UTC.
5 # The Time class treats GMT
6 # (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
7 # GMT is the older way of referring to these baseline times but persists in
8 # the names of calls on POSIX systems.
10 # Note: A \Time object uses the resolution available on your system clock.
12 # All times may have subsecond. Be aware of this fact when comparing times
13 # with each other -- times that are apparently equal when displayed may be
14 # different when compared.
15 # (Since Ruby 2.7.0, Time#inspect shows subsecond but
16 # Time#to_s still doesn't show subsecond.)
18 # == Examples
20 # All of these examples were done using the EST timezone which is GMT-5.
22 # === Creating a New \Time Instance
24 # You can create a new instance of Time with Time.new. This will use the
25 # current system time. Time.now is an alias for this. You can also
26 # pass parts of the time to Time.new such as year, month, minute, etc. When
27 # you want to construct a time this way you must pass at least a year. If you
28 # pass the year with nothing else time will default to January 1 of that year
29 # at 00:00:00 with the current system timezone. Here are some examples:
31 #   Time.new(2002)         #=> 2002-01-01 00:00:00 -0500
32 #   Time.new(2002, 10)     #=> 2002-10-01 00:00:00 -0500
33 #   Time.new(2002, 10, 31) #=> 2002-10-31 00:00:00 -0500
35 # You can pass a UTC offset:
37 #   Time.new(2002, 10, 31, 2, 2, 2, "+02:00") #=> 2002-10-31 02:02:02 +0200
39 # Or a timezone object:
41 #   zone = timezone("Europe/Athens")      # Eastern European Time, UTC+2
42 #   Time.new(2002, 10, 31, 2, 2, 2, zone) #=> 2002-10-31 02:02:02 +0200
44 # You can also use Time.local and Time.utc to infer
45 # local and UTC timezones instead of using the current system
46 # setting.
48 # You can also create a new time using Time.at which takes the number of
49 # seconds (with subsecond) since the {Unix
50 # Epoch}[https://en.wikipedia.org/wiki/Unix_time].
52 #   Time.at(628232400) #=> 1989-11-28 00:00:00 -0500
54 # === Working with an Instance of \Time
56 # Once you have an instance of Time there is a multitude of things you can
57 # do with it. Below are some examples. For all of the following examples, we
58 # will work on the assumption that you have done the following:
60 #   t = Time.new(1993, 02, 24, 12, 0, 0, "+09:00")
62 # Was that a monday?
64 #   t.monday? #=> false
66 # What year was that again?
68 #   t.year #=> 1993
70 # Was it daylight savings at the time?
72 #   t.dst? #=> false
74 # What's the day a year later?
76 #   t + (60*60*24*365) #=> 1994-02-24 12:00:00 +0900
78 # How many seconds was that since the Unix Epoch?
80 #   t.to_i #=> 730522800
82 # You can also do standard functions like compare two times.
84 #   t1 = Time.new(2010)
85 #   t2 = Time.new(2011)
87 #   t1 == t2 #=> false
88 #   t1 == t1 #=> true
89 #   t1 <  t2 #=> true
90 #   t1 >  t2 #=> false
92 #   Time.new(2010,10,31).between?(t1, t2) #=> true
94 # == What's Here
96 # First, what's elsewhere. \Class \Time:
98 # - Inherits from {class Object}[Object.html#class-Object-label-What-27s+Here].
99 # - Includes {module Comparable}[Comparable.html#module-Comparable-label-What-27s+Here].
101 # Here, class \Time provides methods that are useful for:
103 # - {Creating \Time objects}[#class-Time-label-Methods+for+Creating].
104 # - {Fetching \Time values}[#class-Time-label-Methods+for+Fetching].
105 # - {Querying a \Time object}[#class-Time-label-Methods+for+Querying].
106 # - {Comparing \Time objects}[#class-Time-label-Methods+for+Comparing].
107 # - {Converting a \Time object}[#class-Time-label-Methods+for+Converting].
108 # - {Rounding a \Time}[#class-Time-label-Methods+for+Rounding].
110 # === Methods for Creating
112 # - ::new: Returns a new time from specified arguments (year, month, etc.),
113 #   including an optional timezone value.
114 # - ::local (aliased as ::mktime): Same as ::new, except the
115 #   timezone is the local timezone.
116 # - ::utc (aliased as ::gm): Same as ::new, except the timezone is UTC.
117 # - ::at: Returns a new time based on seconds since epoch.
118 # - ::now: Returns a new time based on the current system time.
119 # - #+ (plus): Returns a new time increased by the given number of seconds.
120 # - {-}[#method-i-2D] (minus): Returns a new time
121 #                              decreased by the given number of seconds.
123 # === Methods for Fetching
125 # - #year: Returns the year of the time.
126 # - #month (aliased as #mon): Returns the month of the time.
127 # - #mday (aliased as #day): Returns the day of the month.
128 # - #hour: Returns the hours value for the time.
129 # - #min: Returns the minutes value for the time.
130 # - #sec: Returns the seconds value for the time.
131 # - #usec (aliased as #tv_usec): Returns the number of microseconds
132 #   in the subseconds value of the time.
133 # - #nsec (aliased as #tv_nsec: Returns the number of nanoseconds
134 #   in the subsecond part of the time.
135 # - #subsec: Returns the subseconds value for the time.
136 # - #wday: Returns the integer weekday value of the time (0 == Sunday).
137 # - #yday: Returns the integer yearday value of the time (1 == January 1).
138 # - #hash: Returns the integer hash value for the time.
139 # - #utc_offset (aliased as #gmt_offset and #gmtoff): Returns the offset
140 #   in seconds between time and UTC.
141 # - #to_f: Returns the float number of seconds since epoch for the time.
142 # - #to_i (aliased as #tv_sec): Returns the integer number of seconds since epoch
143 #   for the time.
144 # - #to_r: Returns the Rational number of seconds since epoch for the time.
145 # - #zone: Returns a string representation of the timezone of the time.
147 # === Methods for Querying
149 # - #utc? (aliased as #gmt?): Returns whether the time is UTC.
150 # - #dst? (aliased as #isdst): Returns whether the time is DST (daylight saving time).
151 # - #sunday?: Returns whether the time is a Sunday.
152 # - #monday?: Returns whether the time is a Monday.
153 # - #tuesday?: Returns whether the time is a Tuesday.
154 # - #wednesday?: Returns whether the time is a Wednesday.
155 # - #thursday?: Returns whether the time is a Thursday.
156 # - #friday?: Returns whether time is a Friday.
157 # - #saturday?: Returns whether the time is a Saturday.
159 # === Methods for Comparing
161 # - {#<=>}[#method-i-3C-3D-3E]: Compares +self+ to another time.
162 # - #eql?: Returns whether the time is equal to another time.
164 # === Methods for Converting
166 # - #asctime (aliased as #ctime): Returns the time as a string.
167 # - #inspect: Returns the time in detail as a string.
168 # - #strftime: Returns the time as a string, according to a given format.
169 # - #to_a: Returns a 10-element array of values from the time.
170 # - #to_s: Returns a string representation of the time.
171 # - #getutc (aliased as #getgm): Returns a new time converted to UTC.
172 # - #getlocal: Returns a new time converted to local time.
173 # - #utc (aliased as #gmtime): Converts time to UTC in place.
174 # - #localtime: Converts time to local time in place.
176 # === Methods for Rounding
178 # - #round:Returns a new time with subseconds rounded.
179 # - #ceil: Returns a new time with subseconds raised to a ceiling.
180 # - #floor: Returns a new time with subseconds lowered to a floor.
182 # == Timezone Argument
184 # A timezone argument must have +local_to_utc+ and +utc_to_local+
185 # methods, and may have +name+, +abbr+, and +dst?+ methods.
187 # The +local_to_utc+ method should convert a Time-like object from
188 # the timezone to UTC, and +utc_to_local+ is the opposite.  The
189 # result also should be a Time or Time-like object (not necessary to
190 # be the same class).  The #zone of the result is just ignored.
191 # Time-like argument to these methods is similar to a Time object in
192 # UTC without subsecond; it has attribute readers for the parts,
193 # e.g. #year, #month, and so on, and epoch time readers, #to_i.  The
194 # subsecond attributes are fixed as 0, and #utc_offset, #zone,
195 # #isdst, and their aliases are same as a Time object in UTC.
196 # Also #to_time, #+, and #- methods are defined.
198 # The +name+ method is used for marshaling. If this method is not
199 # defined on a timezone object, Time objects using that timezone
200 # object can not be dumped by Marshal.
202 # The +abbr+ method is used by '%Z' in #strftime.
204 # The +dst?+ method is called with a +Time+ value and should return whether
205 # the +Time+ value is in daylight savings time in the zone.
207 # === Auto Conversion to Timezone
209 # At loading marshaled data, a timezone name will be converted to a timezone
210 # object by +find_timezone+ class method, if the method is defined.
212 # Similarly, that class method will be called when a timezone argument does
213 # not have the necessary methods mentioned above.
214 class Time
215   # Creates a new \Time object from the current system time.
216   # This is the same as Time.new without arguments.
217   #
218   #    Time.now               # => 2009-06-24 12:39:54 +0900
219   #    Time.now(in: '+04:00') # => 2009-06-24 07:39:54 +0400
220   #
221   # Parameter:
222   # :include: doc/time/in.rdoc
223   def self.now(in: nil)
224     Primitive.time_s_now(Primitive.arg!(:in))
225   end
227   # _Time_
228   #
229   # This form accepts a \Time object +time+
230   # and optional keyword argument +in+:
231   #
232   #   Time.at(Time.new)               # => 2021-04-26 08:52:31.6023486 -0500
233   #   Time.at(Time.new, in: '+09:00') # => 2021-04-26 22:52:31.6023486 +0900
234   #
235   # _Seconds_
236   #
237   # This form accepts a numeric number of seconds +sec+
238   # and optional keyword argument +in+:
239   #
240   #   Time.at(946702800)               # => 1999-12-31 23:00:00 -0600
241   #   Time.at(946702800, in: '+09:00') # => 2000-01-01 14:00:00 +0900
242   #
243   # <em>Seconds with Subseconds and Units</em>
244   #
245   # This form accepts an integer number of seconds +sec_i+,
246   # a numeric number of milliseconds +msec+,
247   # a symbol argument for the subsecond unit type (defaulting to :usec),
248   # and an optional keyword argument +in+:
249   #
250   #   Time.at(946702800, 500, :millisecond)               # => 1999-12-31 23:00:00.5 -0600
251   #   Time.at(946702800, 500, :millisecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
252   #   Time.at(946702800, 500000)                             # => 1999-12-31 23:00:00.5 -0600
253   #   Time.at(946702800, 500000, :usec)                      # => 1999-12-31 23:00:00.5 -0600
254   #   Time.at(946702800, 500000, :microsecond)               # => 1999-12-31 23:00:00.5 -0600
255   #   Time.at(946702800, 500000, in: '+09:00')               # => 2000-01-01 14:00:00.5 +0900
256   #   Time.at(946702800, 500000, :usec, in: '+09:00')        # => 2000-01-01 14:00:00.5 +0900
257   #   Time.at(946702800, 500000, :microsecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
258   #   Time.at(946702800, 500000000, :nsec)                     # => 1999-12-31 23:00:00.5 -0600
259   #   Time.at(946702800, 500000000, :nanosecond)               # => 1999-12-31 23:00:00.5 -0600
260   #   Time.at(946702800, 500000000, :nsec, in: '+09:00')       # => 2000-01-01 14:00:00.5 +0900
261   #   Time.at(946702800, 500000000, :nanosecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
262   #
263   # Parameters:
264   # :include: doc/time/sec_i.rdoc
265   # :include: doc/time/msec.rdoc
266   # :include: doc/time/usec.rdoc
267   # :include: doc/time/nsec.rdoc
268   # :include: doc/time/in.rdoc
269   #
270   def self.at(time, subsec = false, unit = :microsecond, in: nil)
271     if Primitive.mandatory_only?
272       Primitive.time_s_at1(time)
273     else
274       Primitive.time_s_at(time, subsec, unit, Primitive.arg!(:in))
275     end
276   end
278   # Returns a new \Time object based on the given arguments.
279   #
280   # With no positional arguments, returns the value of Time.now:
281   #
282   #   Time.new                                       # => 2021-04-24 17:27:46.0512465 -0500
283   #
284   # Otherwise, returns a new \Time object based on the given parameters:
285   #
286   #   Time.new(2000)                                 # => 2000-01-01 00:00:00 -0600
287   #   Time.new(2000, 12, 31, 23, 59, 59.5)           # => 2000-12-31 23:59:59.5 -0600
288   #   Time.new(2000, 12, 31, 23, 59, 59.5, '+09:00') # => 2000-12-31 23:59:59.5 +0900
289   #
290   # Parameters:
291   #
292   # :include: doc/time/year.rdoc
293   # :include: doc/time/mon-min.rdoc
294   # :include: doc/time/sec.rdoc
295   # :include: doc/time/zone_and_in.rdoc
296   #
297   def initialize(year = (now = true), mon = nil, mday = nil, hour = nil, min = nil, sec = nil, zone = nil, in: nil)
298     if zone
299       if Primitive.arg!(:in)
300         raise ArgumentError, "timezone argument given as positional and keyword arguments"
301       end
302     else
303       zone = Primitive.arg!(:in)
304     end
306     if now
307       return Primitive.time_init_now(zone)
308     end
310     Primitive.time_init_args(year, mon, mday, hour, min, sec, zone)
311   end