struct Time
Overview
Time represents a date-time instant in
incremental time
observed in a specific time zone.
The calendaric calculations are based on the rules of the proleptic Gregorian calendar as specified inISO 8601. Leap seconds are ignored.
Internally, the time is stored as anInt64 representing seconds from epoch
(0001-01-01 00:00:00Z) and anInt32 representing
nanosecond-of-second with value range0..999_999_999.
The supported date range is0001-01-01 00:00:00.0 to
9999-12-31 23:59:59.999_999_999 in any local time zone.
Telling the Time
There are several methods to retrieve aTime instance representing the
current time:
Time.utc # returns the current time in UTC
Time.local Time::Location.load("Europe/Berlin") # returns the current time in time zone Europe/Berlin
Time.local # returns the current time in current time zone
It is generally recommended to keep instances in UTC and only apply a local time zone when formatting for user display, unless the domain logic requires having a specific time zone (for example for calendaric operations).
Creating a Specific Instant
Time instances representing a specific instant can be created by
.utc or.new with the date-time specified as individual arguments:
time = Time.utc(2016, 2, 15, 10, 20, 30)
time.to_s # => "2016-02-15 10:20:30 UTC"
time = Time.local(2016, 2, 15, 10, 20, 30, location: Time::Location.load("Europe/Berlin"))
time.to_s # => "2016-02-15 10:20:30 +01:00"
# The time-of-day can be omitted and defaults to midnight (start of day):
time = Time.utc(2016, 2, 15)
time.to_s # => "2016-02-15 00:00:00 UTC"
Retrieving Time Information
EachTime instance allows querying calendar data:
time = Time.utc(2016, 2, 15, 10, 20, 30)
time.year # => 2016
time.month # => 2
time.day # => 15
time.hour # => 10
time.minute # => 20
time.second # => 30
time.millisecond # => 0
time.nanosecond # => 0
time.day_of_week # => Time::DayOfWeek::Monday
time.day_of_year # => 46
time.monday? # => true
time.time_of_day # => 10:20:30
For querying if a time is at a specific day of week,Time offers named
predicate methods, delegating to#day_of_week:
time.monday? # => true
# ...
time.sunday? # => false
Time Zones
Each time is attached to a specific time zone, represented by aLocation
(see#location).
#zone returns the time zone observed in this location at the current time
(i.e. the instant represented by thisTime).
#offset returns the offset of the current zone in seconds.
time = Time.local(2018, 3, 8, 22, 5, 13, location: Time::Location.load("Europe/Berlin"))
time # => 2018-03-08 22:05:13+01:00[Europe/Berlin]
time.location # => #<Time::Location Europe/Berlin>
time.zone # => #<Time::Location::Zone CET +01:00 (3600s) STD>
time.offset # => 3600
Using.utc, the location isTime::Location::UTC:
time = Time.utc(2018, 3, 8, 22, 5, 13)
time # => 2018-03-08 22:05:13Z
time.location # => #<Time::Location UTC>
time.zone # => #<Time::Location::Zone UTC +00:00 (0s) STD>
time.offset # => 0
ATime instance can be transformed to a different time zone while retaining
the same instant using#in:
time_de = Time.local(2018, 3, 8, 22, 5, 13, location: Time::Location.load("Europe/Berlin"))
time_ar = time_de.in Time::Location.load("America/Argentina/Buenos_Aires")
time_de # => 2018-03-08 22:05:13+01:00[Europe/Berlin]
time_ar # => 2018-03-08 18:05:13-03:00[America/Argentina/Buenos_Aires]
BothTime instances show a different local date-time, but they represent
the same date-time in the instant time-line, therefore they are considered
equal:
time_de.to_utc # => 2018-03-08 21:05:13Z
time_ar.to_utc # => 2018-03-08 21:05:13Z
time_de == time_ar # => true
There are also two special methods for converting to UTC and local time zone:
time.to_utc # equals time.in(Location::UTC)
time.to_local # equals time.in(Location.local)
#to_local_in allows changing the time zone while keeping
the same local date-time (wall clock) which results in a different instant
on the time line.
Formatting and Parsing Time
To make date-time instances exchangeable between different computer systems or readable to humans, they are usually converted to and from a string representation.
The method#to_s formats the date-time according to a specified pattern.
time = Time.utc(2015, 10, 12, 10, 30, 0)
time.to_s("%Y-%m-%d %H:%M:%S %:z") # => "2015-10-12 10:30:00 +00:00"
Similarly,Time.parse andTime.parse! are used to construct aTime instance from date-time
information in a string, according to a specified pattern:
Time.parse("2015-10-12 10:30:00 +00:00", "%Y-%m-%d %H:%M:%S %z", Time::Location::UTC)
Time.parse!("2015-10-12 10:30:00 +00:00", "%Y-%m-%d %H:%M:%S %z")
SeeTime::Format for all directives.
Calculations
Time.utc(2015, 10, 10) - 5.days # => 2015-10-05 00:00:00 +00:00
span = Time.utc(2015, 10, 10) - Time.utc(2015, 9, 10)
span.days # => 30
span.total_hours # => 720
span.total_minutes # => 43200
Measuring Time
The typical time representation provided by the operating system is based on a"wall clock" which is subject to changes for clock synchronization. This can result in discontinuous jumps in the time-line making it not suitable for accurately measuring elapsed time.
Instances ofTime are focused on telling time – using a"wall clock".
WhenTime.local is called multiple times, the difference between the
returned instances is not guaranteed to equal to the time elapsed between
making the calls; even the order of the returnedTime instances might
not reflect invocation order.
t1 = Time.utc
# operation that takes 1 minute
t2 = Time.utc
t2 - t1 # => ?
The resultingTime::Span could be anything, even negative, if the
computer's wall clock has changed between both calls.
As an alternative, the operating system also provides a monotonic clock. Its time-line has no specified starting point but is strictly linearly increasing.
This monotonic clock should always be used for measuring elapsed time.
A reading from this clock can be taken using.instant:
t1 = Time.instant
# operation that takes 1 minute
t2 = Time.instant
t2.duration_since(t1) # => 1.minute (approximately)
The execution time of a block can be measured using.measure:
elapsed_time = Time.measure do
# operation that takes 20 milliseconds
end
elapsed_time # => 20.milliseconds (approximately)
Included Modules
Defined in:
json/to_json.crtime.cr
uri/params/to_www_form.cr
yaml/to_yaml.cr
Constant Summary
-
UNIX_EPOCH =
new(unsafe_utc_seconds: 62135596800_i64) -
This constant is defined as
1970-01-01 00:00:00Z". Can be used to create aTime::Spanthat represents an Unix Epoch time duration.Time.utc - Time::UNIX_EPOCH
Constructors
-
.local(year : Int32, month : Int32, day : Int32, hour : Int32 = 0, minute : Int32 = 0, second : Int32 = 0, *, nanosecond : Int32 = 0, location : Location = Location.local) : Time
Creates a new
Timeinstance representing the given local date-time inlocation (defaults to local time zone). -
.local(location : Location = Location.local) : Time
Creates a new
Timeinstance representing the current time from the system clock observed inlocation (defaults to local time zone). -
.month_week_date(year : Int32, month : Int32, week : Int32, day_of_week : Int32 | DayOfWeek, hour : Int32 = 0, minute : Int32 = 0, second : Int32 = 0, *, nanosecond : Int32 = 0, location : Location = Location.local) : self
Creates an instance representing theweek-thday_of_week in the givenyear andmonth.
- .new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
- .new(pull : JSON::PullParser) : self
-
.new(*, seconds : Int64, nanoseconds : Int32, location : Location)
Creates a new
Timeinstance that corresponds to the number ofseconds andnanoseconds elapsed from epoch (0001-01-01 00:00:00.0) observed inlocation. -
.parse(time : String, pattern : String, location : Location) : Time
Parses a
Timefromtime string using the givenpattern. -
.parse!(time : String, pattern : String) : Time
Parses a
Timefromtime string using the givenpattern. -
.parse_local(time : String, pattern : String) : Time
Parses a
Timefromtime string using the givenpattern andTime::Location.localas default location -
.parse_rfc2822(time : String) : self
Parse time format specified byRFC 2822.
- .parse_rfc3339(time : String) : self
-
.parse_utc(time : String, pattern : String) : Time
Parses a
Timefromtime string using the givenpattern andTime::Location::UTCas default location. -
.unix(seconds : Int) : Time
Creates a new
Timeinstance that corresponds to the number ofseconds elapsed since the Unix epoch (1970-01-01 00:00:00Z). -
.unix_ms(milliseconds : Int) : Time
Creates a new
Timeinstance that corresponds to the number ofmilliseconds elapsed since the Unix epoch (1970-01-01 00:00:00Z). -
.unix_ns(nanoseconds : Int) : Time
Creates a new
Timeinstance that corresponds to the number ofnanoseconds elapsed since the Unix epoch (1970-01-01 00:00:00Z). -
.utc(year : Int32, month : Int32, day : Int32, hour : Int32 = 0, minute : Int32 = 0, second : Int32 = 0, *, nanosecond : Int32 = 0) : Time
Creates a new
Timeinstance representing the given date-time in UTC. -
.utc : Time
Creates a new
Timeinstance representing the current time from the system clock in UTC. -
.utc(*, seconds : Int64, nanoseconds : Int32) : Time
Creates a new
Timeinstance that corresponds to the number ofseconds andnanoseconds elapsed from epoch (0001-01-01 00:00:00Z) in UTC. -
.week_date(year : Int32, week : Int32, day_of_week : Int32 | DayOfWeek, hour : Int32 = 0, minute : Int32 = 0, second : Int32 = 0, *, nanosecond : Int32 = 0, location : Location = Location.local) : self
Creates an instance specified by a commercial week date consisting of ISO calendaryear,week and aday_of_week.
Class Method Summary
-
.days_in_month(year : Int, month : Int) : Int32
Returns the number of days inmonth (value range:
1..12) taking account of theyear. -
.days_in_year(year : Int) : Int32
Returns the number of days inyear.
-
.instant : Time::Instant
Returns the current reading of the monotonic clock.
-
.leap_year?(year : Int) : Bool
Returns
trueifyear is a leap year in the proleptic Gregorian calendar. -
.measure(&) : Time::Span
Measures the execution time ofblock.
-
.monotonic : Time::Span
Returns a reading from the monotonic clock to measure elapsed time.
DEPRECATED Use
Time.instantinstead. -
.parse_iso8601(time : String)
Parse datetime format specified byISO 8601.
Instance Method Summary
-
#+(span : Time::Span) : Time
Returns a copy of this
Timewithspan added. -
#+(span : Time::MonthSpan) : Time
Returns a copy of this
Timewithspan added. -
#-(other : Time) : Time::Span
Returns a
Time::Spanamounting to the duration betweenother andself. -
#-(span : Time::Span) : Time
Returns a copy of this
Timewithspan subtracted. -
#-(span : Time::MonthSpan) : Time
Returns a copy of this
Timewithspan subtracted. -
#<=>(other : Time) : Int32
Compares this
Timewithother. -
#==(other : Time) : Bool
Compares this
Timewithother for equality. -
#at_beginning_of_day : Time
Returns a copy of this
Timerepresenting the beginning of the day. -
#at_beginning_of_hour : Time
Returns a copy of this
Timerepresenting the beginning of the hour. -
#at_beginning_of_minute : Time
Returns a copy of this
Timerepresenting the beginning of the minute. -
#at_beginning_of_month : Time
Returns a copy of this
Timerepresenting the beginning of the month. -
#at_beginning_of_quarter : Time
Returns a copy of this
Timerepresenting the beginning of the quarter. -
#at_beginning_of_second : Time
Returns a copy of this
Timerepresenting the beginning of the seconds. -
#at_beginning_of_semester : Time
Returns a copy of this
Timerepresenting the beginning of the semester. -
#at_beginning_of_week(start_day : Time::DayOfWeek = :monday) : Time
Returns a copy of this
Timerepresenting the beginning of the week. -
#at_beginning_of_year : Time
Returns a copy of this
Timerepresenting the beginning of the year. -
#at_end_of_day : Time
Returns a copy of this
Timerepresenting the end of the day. -
#at_end_of_hour : Time
Returns a copy of this
Timerepresenting the end of the hour. -
#at_end_of_minute : Time
Returns a copy of this
Timerepresenting the end of the minute. -
#at_end_of_month : Time
Returns a copy of this
Timerepresenting the end of the month. -
#at_end_of_quarter : Time
Returns a copy of this
Timerepresenting the end of the quarter. -
#at_end_of_second : Time
Returns a copy of this
Timerepresenting the end of the second. -
#at_end_of_semester : Time
Returns a copy of this
Timerepresenting the end of the semester. -
#at_end_of_week : Time
Returns a copy of this
Timerepresenting the end of the week. -
#at_end_of_year : Time
Returns a copy of this
Timerepresenting the end of the year. -
#at_midday : Time
Returns a copy of this
Timerepresenting midday (12:00) of the same day. -
#calendar_week : Tuple(Int32, Int32)
Returns the ISO calendar year and week in which this instance occurs.
- #clone : Time
- #date : Tuple(Int32, Int32, Int32)
-
#day : Int32
Returns the day of the month (
1..31). -
#day_of_week : Time::DayOfWeek
Returns the day of the week (
Monday..Sunday). -
#day_of_year : Int32
Returns the day of the year.
-
#friday? : Bool
Returns
trueif the day of week is Friday. - #hash(hasher)
-
#hour : Int32
Returns the hour of the day (
0..23). -
#in(location : Location) : Time
Returns a copy of this
Timerepresenting the same instant observed inlocation. -
#inspect(io : IO, with_nanoseconds) : Nil
Prints this
Timetoio.DEPRECATED
with_nanosecondsis deprecated, please use a custom format if necessary -
#inspect(io : IO) : Nil
Prints this
Timetoio. -
#local? : Bool
Returns
trueif#locationequals to the local time zone (Time::Location.local). - #location : Location
-
#millisecond : Int32
Returns the millisecond of the second (
0..999). -
#minute : Int32
Returns the minute of the hour (
0..59). -
#monday? : Bool
Returns
trueif the day of week is Monday. -
#month : Int32
Returns the month of the year (
1..12). -
#nanosecond : Int32
Returns the nanosecond of the second (
0..999_999_999). -
#offset : Int32
Returns the offset from UTC (in seconds) in effect in
#locationat this instant. -
#saturday? : Bool
Returns
trueif the day of week is Saturday. -
#second : Int32
Returns the second of the minute (
0..59). -
#shift(seconds : Int, nanoseconds : Int) : Time
Returns a copy of this
Timeshifted by the number ofseconds andnanoseconds. -
#shift(*, years : Int = 0, months : Int = 0, weeks : Int = 0, days : Int = 0, hours : Int = 0, minutes : Int = 0, seconds : Int = 0, nanoseconds : Int = 0)
Returns a copy of this
Timeshifted by the amount of calendaric units provided as arguments. -
#sunday? : Bool
Returns
trueif the day of week is Sunday. -
#thursday? : Bool
Returns
trueif the day of week is Thursday. -
#time_of_day : Time::Span
Returns the duration between this
Timeand midnight of the same day. - #to_json(json : JSON::Builder) : Nil
-
#to_local : Time
Returns a copy of this
Timerepresenting the same instant in the local time zone (Time::Location.local). -
#to_local_in(location : Location) : Time
Creates a new
Timeinstance with the same local date-time representation (wall clock) in a differentlocation. -
#to_rfc2822(io : IO)
Format this time using the format specified byRFC 2822 into the givenio.
-
#to_rfc2822 : String
Format this time using the format specified byRFC 2822.
- #to_rfc3339(io : IO, *, fraction_digits : Int = 0) : Nil
- #to_rfc3339(*, fraction_digits : Int = 0)
-
#to_s(io : IO, format : String) : Nil
Formats this
Timeaccording to the pattern informat to the givenio. -
#to_s(io : IO) : Nil
Prints this
Timetoio. -
#to_s(format : String) : String
Formats this
Timeaccording to the pattern informat. -
#to_unix : Int64
Returns the number of seconds since the Unix epoch (
1970-01-01 00:00:00Z). -
#to_unix_f : Float64
Returns the number of seconds since the Unix epoch (
1970-01-01 00:00:00Z) asFloat64with nanosecond precision. -
#to_unix_ms : Int64
Returns the number of milliseconds since the Unix epoch (
1970-01-01 00:00:00Z). -
#to_unix_ns : Int128
Returns the number of nanoseconds since the Unix epoch (
1970-01-01 00:00:00Z). -
#to_utc : Time
Returns a copy of this
Timerepresenting the same instant in UTC (Time::Location::UTC). - #to_yaml(yaml : YAML::Nodes::Builder) : Nil
-
#tuesday? : Bool
Returns
trueif the day of week is Tuesday. -
#utc? : Bool
Returns
trueif#locationequals toLocation::UTC. -
#wednesday? : Bool
Returns
trueif the day of week is Wednesday. -
#year : Int32
Returns the year of the proleptic Georgian Calendar (
0..9999). -
#zone : Time::Location::Zone
Returns the time zone in effect in
#locationat this instant.
Instance methods inherited from module Steppable
step(*, to limit = nil, by step, exclusive : Bool = false, &) : Nilstep(*, to limit = nil, by step, exclusive : Bool = false) step
Instance methods inherited from module Comparable(Time)
<(other : T) : Bool
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T) : Bool
>,
>=(other : T)
>=,
clamp(min, max)clamp(range : Range) clamp
Instance methods inherited from struct Struct
==(other : YAML::Any)==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io : IO) : Nil to_s
Class methods inherited from struct Struct
pre_initialize(address : Pointer) : Nil
pre_initialize
Instance methods inherited from struct Value
==(other : Log::Metadata::Value)==(other : JSON::Any)
==(other : YAML::Any)
==(other) ==, dup dup
Instance methods inherited from class Object
! : Bool
!,
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(collection : Object) : Bool
in?(*values : Object) : Bool in?, inspect(io : IO) : Nil
inspect : String inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String to_json, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil to_pretty_json, to_s(io : IO) : Nil
to_s : String to_s, to_yaml(io : IO) : Nil
to_yaml : String to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as
Class methods inherited from class Object
from_json(string_or_io : String | IO, root : String)from_json(string_or_io : String | IO) from_json, from_yaml(string_or_io : String | IO) from_yaml
Macros inherited from class Object
class_getter(*names, &block)
class_getter,
class_getter!(*names)
class_getter!,
class_getter?(*names, &block)
class_getter?,
class_property(*names, &block)
class_property,
class_property!(*names)
class_property!,
class_property?(*names, &block)
class_property?,
class_setter(*names)
class_setter,
def_clone
def_clone,
def_equals(*fields)
def_equals,
def_equals_and_hash(*fields)
def_equals_and_hash,
def_hash(*fields)
def_hash,
delegate(*methods, to object)
delegate,
forward_missing_to(delegate)
forward_missing_to,
getter(*names, &block)
getter,
getter!(*names)
getter!,
getter?(*names, &block)
getter?,
property(*names, &block)
property,
property!(*names)
property!,
property?(*names, &block)
property?,
setter(*names)
setter
Constructor Detail
Creates a newTime instance representing the given local date-time in
location (defaults to local time zone).
time = Time.local(2016, 2, 15, 10, 20, 30, location: Time::Location.load("Europe/Berlin"))
time.inspect # => "2016-02-15 10:20:30+01:00[Europe/Berlin]"
Valid value ranges for the individual fields:
#year:1..9999#month:1..12#day:1-28/29/30/31(depending on month and year)#hour:0..23#minute:0..59#second:0..59#nanosecond:0..999_999_999
The time-of-day can be omitted and defaults to midnight (start of day):
time = Time.utc(2016, 2, 15)
time.to_s # => "2016-02-15 00:00:00 UTC"
The local date-time representation is resolved to a single instant based on the offset observed in thelocation at this time.
This process can sometimes be ambiguous, mostly due skipping or repeating
times at time zone transitions. For example, inAmerica/New_York the
date-time2011-03-13 02:15:00 never occurred, there is a gap between time
zones. In return,2011-11-06 01:15:00 occurred twice because of overlapping
time zones.
In such cases, the choice of time zone, and therefore the time, is not well-defined. This method returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.
Creates a newTime instance representing the current time from the
system clock observed inlocation (defaults to local time zone).
Creates an instance representing theweek-thday_of_week in the given year andmonth.
Ifweek is5 and there are not enough weeks in the given month (e.g.
February in a non-leap year), the last week of the month is used instead.
Valid value ranges for the individual fields:
#year:1..9999#month:1..12week:1..5#day_of_week:0..7
Reads a string from JSON parser as a time formatted according toRFC 3339 or other variations ofISO 8601.
The JSON format itself does not specify a time data type, this method just assumes that a string holding a ISO 8601 time format can be interpreted as a time value.
See#to_json for reference.
Creates a newTime instance that corresponds to the number ofseconds
andnanoseconds elapsed from epoch (0001-01-01 00:00:00.0)
observed inlocation.
Valid range forseconds is0..315_537_897_599.
Fornanoseconds it is0..999_999_999.
Parses aTime fromtime string using the givenpattern.
SeeTime::Format for details.
Time.parse("2016-04-05", "%F", Time::Location.load("Europe/Berlin")) # => 2016-04-05 00:00:00+02:00[Europe/Berlin]
If there is no time zone information in the formatted time,location will
be assumed. Whenlocation isnil, in such a case the parser will raise
Time::Format::Error.
Parses aTime fromtime string using the givenpattern.
SeeTime::Format for details.
Time.parse!("2016-04-05 +00:00", "%F %:z") # => 2016-04-05 00:00:00.0 +00:00
Time.parse!("2016-04-05", "%F") # raises Time::Format::Error
If there is no time zone information in the formatted time, the parser will raise
Time::Format::Error.
Parses aTime fromtime string using the givenpattern and
Time::Location.local as default location
SeeTime::Format for details.
Time.parse_utc("2016-04-05", "%F") # => 2016-04-05 00:00:00.0 +00:00
Time::Location.local will only be used as#location if the formatted time
does not contain any time zone information. The return value can't be
assumed to be a UTC time (this can be achieved by calling#to_local).
Parse time format specified byRFC 2822.
This is also compatible toRFC 882 andRFC 1123.
Time.parse_rfc2822("Mon, 15 Feb 2016 04:35:50 UTC") # => 2016-02-15 04:35:50Z
Parse time format specified byRFC 3339 (ISO 8601 profile).
Time.parse_rfc3339("2016-02-15T04:35:50Z") # => 2016-02-15 04:35:50Z
Parses aTime fromtime string using the givenpattern and
Time::Location::UTC as default location.
SeeTime::Format for details.
Time.parse_utc("2016-04-05", "%F") # => 2016-04-05 00:00:00.0 +00:00
Time::Location::UTC will only be used as#location if the formatted time
does not contain any time zone information. The return value can't be
assumed to be a UTC time (this can be achieved by calling#to_utc).
Creates a newTime instance that corresponds to the number of
seconds elapsed since the Unix epoch (1970-01-01 00:00:00Z).
The time zone is always UTC.
Time.unix(981173106) # => 2001-02-03 04:05:06Z
Creates a newTime instance that corresponds to the number of
milliseconds elapsed since the Unix epoch (1970-01-01 00:00:00Z).
The time zone is always UTC.
time = Time.unix_ms(981173106789) # => 2001-02-03 04:05:06.789Z
time.millisecond # => 789
Creates a newTime instance that corresponds to the number of
nanoseconds elapsed since the Unix epoch (1970-01-01 00:00:00Z).
The time zone is always UTC.
time = Time.unix_ns(981173106789479273) # => 2001-02-03 04:05:06.789479273Z
time.nanosecond # => 789479273
Creates a newTime instance representing the given date-time in UTC.
time = Time.utc(2016, 2, 15, 10, 20, 30)
time.to_s # => "2016-02-15 10:20:30 UTC"
Valid value ranges for the individual fields:
#year:1..9999#month:1..12#day:1-28/29/30/31(depending on month and year)#hour:0..23#minute:0..59#second:0..59#nanosecond:0..999_999_999
The time-of-day can be omitted and defaults to midnight (start of day):
time = Time.utc(2016, 2, 15)
time.to_s # => "2016-02-15 00:00:00 UTC"
Since UTC does not have any time zone transitions, each date-time is unambiguously resolved.
Creates a newTime instance that corresponds to the number ofseconds
andnanoseconds elapsed from epoch (0001-01-01 00:00:00Z)
in UTC.
Valid range forseconds is0..315_537_897_599.
Fornanoseconds it is0..999_999_999.
Creates an instance specified by a commercial week date consisting of ISO calendaryear,week and aday_of_week.
This equates to the results from#calendar_week and#day_of_week.
Valid value ranges for the individual fields:
#year:1..9999week:1..53#day_of_week:1..7
Class Method Detail
Returns the number of days inmonth (value range:1..12) taking account
of theyear.
The returned value is either28,29,30 or31 depending on the
month and whetheryear is leap.
Time.days_in_month(2016, 2) # => 29
Time.days_in_month(1990, 4) # => 30
Returns the number of days inyear.
A normal year has365 days, a leap year366 days.
Time.days_in_year(1990) # => 365
Time.days_in_year(2004) # => 366
Returns the current reading of the monotonic clock.
The execution time of a block can be measured using.measure.
Returnstrue ifyear is a leap year in the proleptic Gregorian
calendar.
Measures the execution time ofblock.
The measurement relies on the monotonic clock and is not
affected by fluctuations of the system clock (see#monotonic).
elapsed_time = Time.measure do
# operation that takes 20 milliseconds
end
elapsed_time # => 20.milliseconds (approximately)
Returns a reading from the monotonic clock to measure elapsed time.
Values from the monotonic clock and wall clock are not comparable.
This method does not return aTime instance but aTime::Span amounting
to the number of nanoseconds elapsed since the unspecified starting point
of the monotonic clock.
The returned values are strictly linearly increasing.
This clock should be independent from discontinuous jumps in the system time, such as leap seconds, time zone adjustments or manual changes to the computer's clock.
Subtracting two results from this method equals to the time elapsed between both readings:
start = Time.monotonic
# operation that takes 20 milliseconds
elapsed = Time.monotonic - start # => 20.milliseconds (approximately)
# operation that takes 50 milliseconds
elapsed_total = Time.monotonic - start # => 70.milliseconds (approximately)
The execution time of a block can be measured using.measure.
DEPRECATED UseTime.instant instead.
Parse datetime format specified byISO 8601.
This is similar to.parse_rfc3339 but RFC 3339 defines a more strict format.
In ISO 8601 for examples, field delimiters (#-,:) are optional.
Use#to_rfc3339 to format aTime according to .
Time.parse_iso8601("2016-02-15T04:35:50Z") # => 2016-02-15 04:35:50Z
Instance Method Detail
Returns a copy of thisTime withspan added.
It adds the number of months with overflow increasing the year. If the resulting day-of-month would be invalid, it is adjusted to the last valid day of the month.
For example, adding1.month to2007-03-31 would result in the invalid
date2007-04-31 which will be adjusted to2007-04-30.
This operates on the local time-line, such that the local date-time representations of month and year are increased by the specified amount.
If the resulting date-time is ambiguous due to time zone transitions, a correct time will be returned, but it does not guarantee which.
Returns aTime::Span amounting to the duration betweenother andself.
The time span is negative ifself is beforeother.
The duration amounts to the actual time elapsed between both instances, on the instant time-line. The difference between local date-time representations may equal to a different duration, depending on time zone transitions.
Returns a copy of thisTime withspan subtracted.
It adds the number of months with overflow decreasing the year. If the resulting day-of-month would be invalid, it is adjusted to the last valid day of the month.
For example, subtracting1.month from2007-05-31 would result in the invalid
date2007-04-31 which will be adjusted to2007-04-30.
This operates on the local time-line, such that the local date-time representations of month and year are decreased by the specified amount.
If the resulting date-time is ambiguous due to time zone transitions, a correct time will be returned, but it does not guarantee which.
Compares thisTime withother.
The comparison is based on the instant time-line, even if the local date-time representation (wall clock) would compare differently.
To ensure the comparison is also true for local wall clock, both date-times need to be transformed to the same time zone.
Compares thisTime withother for equality.
Two instances are considered equal if they represent the same date-time in the instant time-line, even if they show a different local date-time.
time_de = Time.local(2018, 3, 8, 22, 5, 13, location: Time::Location.load("Europe/Berlin"))
time_ar = Time.local(2018, 3, 8, 18, 5, 13, location: Time::Location.load("America/Argentina/Buenos_Aires"))
time_de == time_ar # => true
# both times represent the same instant:
time_de.to_utc # => 2018-03-08 21:05:13Z
time_ar.to_utc # => 2018-03-08 21:05:13Z
Returns a copy of thisTime representing the beginning of the seconds.
This essentially resetsnanoseconds to 0.
Returns a copy of thisTime representing the beginning of the week.
The week starts on Monday by default, but can be configured by passing a differentstart_day as aTime::DayOfWeek.
now = Time.utc(2023, 5, 16, 17, 53, 22)
now.at_beginning_of_week # => 2023-05-15 00:00:00Z
now.at_beginning_of_week(:sunday) # => 2023-05-14 00:00:00Z
now.at_beginning_of_week(:wednesday) # => 2023-05-10 00:00:00Z
Returns the ISO calendar year and week in which this instance occurs.
The ISO calendar year to which the week belongs is not always in the same as the year of the regular calendar date. The first three days of January sometimes belong to week 52 (or 53) of the previous year; equally the last three days of December sometimes are already in week 1 of the following year.
For that reason, this method returns a tupleyear, week consisting of the
calendar year to which the calendar week belongs and the ordinal number of
the week within that year.
Together with#day_of_week this represents a specific day as commercial or
week date formatyear, week, day_of_week in the same way as the typical
formatyear, month, day.
.week_date creates aTime instance from a week date.
Returns the day of the year.
The value range is1..365 in normal years and1..366 in leap years.
Returns a copy of thisTime representing the same instant observed in
location.
This method changes the time zone and retains the instant, which will usually result in a different representation of local date-time (unless both locations have the same offset).
Ambiguous time zone transitions such as gaps and overlaps have no effect on the result because it retains the same instant.
time_de = Time.local(2018, 3, 8, 22, 5, 13, location: Time::Location.load("Europe/Berlin"))
time_ar = time_de.in Time::Location.load("America/Argentina/Buenos_Aires")
time_de # => 2018-03-08 22:05:13+01:00[Europe/Berlin]
time_ar # => 2018-03-08 18:05:13-03:00[America/Argentina/Buenos_Aires]
In contrast,#to_local_in changes to a different location while
preserving the same wall time, which results in different instants on the
time line.
Prints thisTime toio.
It's formatted according to the Internet Extended Date/Time Format (IXDTF) as specified inRFC 9557: AnRFC 3339 formatted local date-time string with nanosecond precision followed by a time zone name suffix.
It is similar to the format%FT%T.%N%::z[%Z]. Some parts may be omitted or
shortened.
Nanoseconds are omitted if#nanoseconds is zero. Zero offset seconds are
omitted. The name of the location is omitted for fixed zone offset.
Time.utc(2014, 1, 2, 3, 4, 5) # => 2014-01-02 03:04:05Z
Time.utc(2014, 1, 2, 3, 4, 5, nanosecond: 123_456) # => 2014-01-02 03:04:05.123456000Z
Time.local(2014, 1, 2, 3, 4, 5, location: Time::Location.load("Europe/Berlin")) # => 2014-01-02 03:04:05+01:00[Europe/Berlin]
Time.local(2014, 1, 2, 3, 4, 5, location: Time::Location.fixed(3600)) # => 2014-01-02 03:04:05+01:00
Time.local(2014, 1, 2, 3, 4, 5, location: Time::Location.fixed(3601)) # => 2014-01-02 03:04:05+01:00:01
Nanoseconds are omitted ifwith_nanoseconds isfalse ornanoseconds
are zero.
DEPRECATED with_nanoseconds is deprecated, please use a custom format if necessary
Prints thisTime toio.
It's formatted according to the Internet Extended Date/Time Format (IXDTF) as specified inRFC 9557: AnRFC 3339 formatted local date-time string with nanosecond precision followed by a time zone name suffix.
It is similar to the format%FT%T.%N%::z[%Z]. Some parts may be omitted or
shortened.
Nanoseconds are omitted if#nanoseconds is zero. Zero offset seconds are
omitted. The name of the location is omitted for fixed zone offset.
Time.utc(2014, 1, 2, 3, 4, 5) # => 2014-01-02 03:04:05Z
Time.utc(2014, 1, 2, 3, 4, 5, nanosecond: 123_456) # => 2014-01-02 03:04:05.123456000Z
Time.local(2014, 1, 2, 3, 4, 5, location: Time::Location.load("Europe/Berlin")) # => 2014-01-02 03:04:05+01:00[Europe/Berlin]
Time.local(2014, 1, 2, 3, 4, 5, location: Time::Location.fixed(3600)) # => 2014-01-02 03:04:05+01:00
Time.local(2014, 1, 2, 3, 4, 5, location: Time::Location.fixed(3601)) # => 2014-01-02 03:04:05+01:00:01
Returnstrue if#location equals to the local time zone
(Time::Location.local).
Since the system's settings may change during a program's runtime, the result may not be identical between different invocations.
Returns a copy of thisTime shifted by the number ofseconds and
nanoseconds.
Positive values result in a later time, negative values in an earlier time.
This operates on the instant time-line, such that adding the equivalent of one hour will always be a duration of one hour later. The local date-time representation may change by a different amount, depending on time zone transitions.
Overflow innanoseconds will be transferred toseconds.
There is no explicit limit on the input values but the addition must result
in a valid time between0001-01-01 00:00:00.0 and
9999-12-31 23:59:59.999_999_999. OtherwiseArgumentError is raised.
Returns a copy of thisTime shifted by the amount of calendaric units
provided as arguments.
Positive values result in a later time, negative values in an earlier time.
This operates on the local time-line, such that the local date-time representation of the result will be apart by the specified amounts, but the elapsed time between both instances might not equal to the combined default duration. This is the case for example when adding a day over a daylight-savings time change:
start = Time.local(2017, 10, 28, 13, 37, location: Time::Location.load("Europe/Berlin"))
one_day_later = start.shift days: 1
one_day_later - start # => 25.hours
years is equivalent to12 months andweeks is equivalent to7 days.
If the day-of-month resulting from shifting byyears andmonths would be
invalid, the date is adjusted to the last valid day of the month.
For example, adding one month to2018-08-31 would result in the invalid
date2018-09-31 which will be adjusted to2018-09-30:
Time.utc(2018, 7, 31).shift(months: 1) # => Time.utc(2018, 8, 31)
Time.utc(2018, 8, 31).shift(months: 1) # => Time.utc(2018, 9, 30)
Overflow in smaller units is transferred to the next larger unit.
Changes are applied in the same order as the arguments, sorted by increasing granularity. This is relevant because the order of operations can change the result:
Time.utc(2018, 8, 31).shift(months: 1, days: -1) # => Time.utc(2018, 9, 29)
Time.utc(2018, 8, 31).shift(months: 1).shift(days: -1) # => Time.utc(2018, 9, 29)
Time.utc(2018, 8, 31).shift(days: -1).shift(months: 1) # => Time.utc(2018, 9, 30)
There is no explicit limit on the input values but the shift must result
in a valid time between0001-01-01 00:00:00.0 and
9999-12-31 23:59:59.999_999_999. OtherwiseArgumentError is raised.
If the resulting date-time is ambiguous due to time zone transitions, a correct time will be returned, but it does not guarantee which.
Returns the duration between thisTime and midnight of the same day.
This is equivalent to creating aTime::Span from the time-of-day fields:
time.time_of_day == Time::Span.new(hours: time.hour, minutes: time.minute, seconds: time.second, nanoseconds: time.nanosecond)
Emits a string formatted according toRFC 3339 (ISO 8601 profile).
The JSON format itself does not specify a time data type, this method just assumes that a string holding a RFC 3339 time format will be interpreted as a time value.
See#from_json for reference.
Returns a copy of thisTime representing the same instant in the local
time zone (Time::Location.local).
See#in for details.
Creates a newTime instance with the same local date-time representation
(wall clock) in a differentlocation.
Unlike#in, which always preserves the same instant in time,#to_local_in
adjusts the instant such that it results in the same local date-time
representation. Both instants are apart from each other by the difference in
zone offsets.
new_year = Time.utc(2019, 1, 1, 0, 0, 0)
tokyo = new_year.to_local_in(Time::Location.load("Asia/Tokyo"))
new_york = new_year.to_local_in(Time::Location.load("America/New_York"))
tokyo.inspect # => "2019-01-01 00:00:00+09:00[Asia/Tokyo]"
new_york.inspect # => "2019-01-01 00:00:00-05:00[America/New_York]"
If the given wall clock is a gap or fold in the target location, no unambiguous equivalent representation exists.
- In case of a gap, the clock jumps to the smaller offset and shows a time
before the gap. The wall clock is different from
self. - In case of a fold, the clock chooses the bigger offset and shows a time
after the fold. The wall clock is identical to
self.
nyc = Time::Location.load("America/New_York")
# gap on 2025-03-09 local time: 02:00:00 STD -> 03:00:00 DST
Time.utc(2025, 3, 9, 2, 12, 34).to_local_in(nyc) # => 2025-03-09 01:12:34.0-05:00[America/New_York]
# overlap on 2025-11-02 local time: 02:00:00 DST -> 01:00:00 STD
Time.utc(2025, 11, 2, 1, 12, 34).to_local_in(nyc) # => 2025-11-02 01:12:34.0-04:00[America/New_York]
Format this time using the format specified byRFC 2822.
Time.utc(2016, 2, 15).to_rfc2822 # => "Mon, 15 Feb 2016 00:00:00 +0000"
Format this time using the format specified byRFC 3339 (ISO 8601 profile). into the givenio.
Number of seconds decimals can be selected withfraction_digits. Values accepted are 0 (the default, no decimals), 3 (milliseconds), 6 (microseconds) or 9 (nanoseconds).
Format this time using the format specified byRFC 3339 (ISO 8601 profile).
Time.utc(2016, 2, 15).to_rfc3339 # => "2016-02-15 00:00:00Z"
ISO 8601 allows some freedom over the syntax and RFC 3339 exercises that freedom to rigidly define a fixed format intended for use in internet protocols and standards.
Number of seconds decimals can be selected withfraction_digits. Values accepted are 0 (the default, no decimals), 3 (milliseconds), 6 (microseconds) or 9 (nanoseconds).
Prints thisTime toio.
The local date-time is formatted as date stringYYYY-MM-DD HH:mm:ss +ZZ:ZZ:ZZ.
Nanoseconds are always omitted.
When the location isUTC, the offset is replaced with the stringUTC.
Offset seconds are omitted if0.
Formats thisTime according to the pattern informat.
SeeTime::Format for details.
time = Time.local(2016, 4, 5)
time.to_s("%F") # => "2016-04-05"
Returns the number of seconds since the Unix epoch
(1970-01-01 00:00:00Z).
time = Time.utc(2016, 1, 12, 3, 4, 5)
time.to_unix # => 1452567845
Returns the number of seconds since the Unix epoch
(1970-01-01 00:00:00Z) asFloat64 with nanosecond precision.
time = Time.utc(2016, 1, 12, 3, 4, 5, nanosecond: 678_000_000)
time.to_unix_f # => 1452567845.678
Returns the number of milliseconds since the Unix epoch
(1970-01-01 00:00:00Z).
time = Time.utc(2016, 1, 12, 3, 4, 5, nanosecond: 678_000_000)
time.to_unix_ms # => 1452567845678
Returns the number of nanoseconds since the Unix epoch
(1970-01-01 00:00:00Z).
time = Time.utc(2016, 1, 12, 3, 4, 5, nanosecond: 678_910_123)
time.to_unix_ns # => 1452567845678910123
Returns a copy of thisTime representing the same instant in UTC
(Time::Location::UTC).
See#in for details.