struct Time
Overview
Time
represents an instance in time. Here are some examples:
Basic Usage
time = Time.now # => 2016-02-15 10:20:30 UTC
time.year # => 2015
time.month # => 2
time.day # => 15
time.hour # => 10
time.minute # => 20
time.second # => 30
time.monday? # => true
# Creating a time instance with a date only
Time.new(2016, 2, 15) # => 2016-02-15 00:00:00
# Specifying a time
Time.new(2016, 2, 15, 10, 20, 30) # => 2016-02-15 10:20:30 UTC
Formatting Time
The #to_s
method returns a String
value in the assigned format.
Time.now.to_s("%Y-%m-%d") # => "2015-10-12"
See Time::Format
for all the directives.
Calculation
Time.new(2015, 10, 10) - 5.days # => 2015-10-05 00:00:00
# Time calculation returns a Time::Span instance
span = Time.new(2015, 10, 10) - Time.new(2015, 9, 10)
span.days # => 30
span.total_hours # => 720
span.total_minutes # => 43200
# Calculation between Time::Span instances
span_a = Time::Span.new(3, 0, 0)
span_b = Time::Span.new(2, 0, 0)
span = span_a - span_b
span # => 01:00:00
span.class # => Time::Span
span.hours # => 1
Included Modules
Defined in:
time.crConstant Summary
-
DAYS_MONTH =
[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
-
DAYS_MONTH_LEAP =
[0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
-
DP100 =
36524
-
DP4 =
1461
-
DP400 =
146097
-
KindMask =
13835058055282163712_u64
-
KindShift =
62_i64
-
MAX_VALUE_TICKS =
3155378975999999999_i64
-
MaxValue =
new(3155378975999999999_i64)
-
MinValue =
new(0)
-
TicksMask =
4611686018427387903_i64
-
UnixEpoch =
621355968000000000_i64
Class Method Summary
- .days_in_month(year, month) : Int32
-
.epoch(seconds : Int) : self
Returns a new
Time
instance that corresponds to the number seconds elapsed since the unix epoch (00:00:00 UTC on 1 January 1970) -
.epoch_ms(milliseconds : Int) : self
Returns a new
Time
instance that corresponds to the number milliseconds elapsed since the unix epoch (00:00:00 UTC on 1 January 1970) - .leap_year?(year) : Bool
-
.local_offset_in_minutes
Returns the local time offset in minutes relative to GMT.
- .local_ticks
- .new(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0, kind = Kind::Unspecified)
- .new
- .new(ticks : Int, kind = Kind::Unspecified)
- .new(time : LibC::Timespec, kind = Kind::Unspecified)
- .now : self
-
.parse(time : String, pattern : String, kind = Time::Kind::Unspecified) : self
Parses a Time in the given time string, using the given pattern (see
Time::Format
). - .utc_now : self
- .utc_ticks
Instance Method Summary
- #+(other : MonthSpan)
- #+(other : Span)
- #-(other : Span)
- #-(other : MonthSpan)
- #-(other : Int)
- #-(other : Time)
- #<=>(other : self)
- #add_ticks(value)
- #at_beginning_of_day
- #at_beginning_of_hour
- #at_beginning_of_minute
- #at_beginning_of_month
- #at_beginning_of_quarter
- #at_beginning_of_semester
- #at_beginning_of_week
- #at_beginning_of_year
- #at_end_of_day
- #at_end_of_hour
- #at_end_of_minute
- #at_end_of_month
- #at_end_of_quarter
- #at_end_of_semester
- #at_end_of_week
- #at_end_of_year
- #at_midday
- #date
- #day
- #day_of_week
- #day_of_year
-
#epoch : Int64
Returns the number of seconds since the Epoch for this time.
-
#epoch_f : Float64
Returns the number of seconds since the Epoch for this time, as a
Float64
. -
#epoch_ms : Int64
Returns the number of milliseconds since the Epoch for this time.
- #friday?
- #hash : Int64
- #hour
- #inspect(io : IO)
-
#kind
Returns
Kind
of the instance. -
#local?
Returns true if
Kind
is set to Local. - #millisecond
- #minute
- #monday?
- #month
- #saturday?
- #second
- #sunday?
- #thursday?
- #ticks
- #time_of_day
- #to_local
-
#to_s(format : String) : String
Formats this time using the given format (see
Time::Format
). -
#to_s(format : String, io : IO)
Formats this time using the given format (see
Time::Format
) into the given io. - #to_utc
- #tuesday?
-
#utc?
Returns true if
Kind
is set to Utc. - #wednesday?
- #year
Macro Summary
Instance methods inherited from module Comparable({"T", T})
<(other : T)
<,
<=(other : T)
<=,
<=>(other : T)
<=>,
==(other : T)
==,
>(other : T)
>,
>=(other : T)
>=
Instance methods inherited from struct Struct
==(other : self) : Bool
==,
hash : Int32
hash,
inspect(io : IO) : Nil
inspect,
to_s(io)
to_s
Instance methods inherited from struct Value
==(other)
==,
dup
dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml
to_yaml(io : IO) to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
clone
clone,
dup
dup,
from_json(string_or_io) : selffrom_json(string_or_io, root : String) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |
Class Method Detail
Returns a new Time
instance that corresponds to the number
seconds elapsed since the unix epoch (00:00:00 UTC on 1 January 1970)
Time.epoch(981173106) # => 2001-02-03 04:05:06 UTC
Returns a new Time
instance that corresponds to the number
milliseconds elapsed since the unix epoch (00:00:00 UTC on 1 January 1970)
time = Time.epoch_ms(981173106789) # => 2001-02-03 04:05:06 UTC
time.millisecond # => 789
Returns the local time offset in minutes relative to GMT.
# Assume in Argentina, where it's GMT-3
Time.local_offset_in_minutes # => -180
Parses a Time in the given time string, using the given pattern (see
Time::Format
).
Time.parse("2016-04-05", "%F") # => 2016-04-05 00:00:00
Instance Method Detail
Returns the number of seconds since the Epoch for this time.
Time.new(2016, 1, 2, 3, 4, 5).epoch # => 1451714645
Returns the number of seconds since the Epoch for this time,
as a Float64
.
Time.new(2016, 1, 2, 3, 4, 5, 678).epoch_f # => 1.45171e+09
Returns the number of milliseconds since the Epoch for this time.
Time.new(2016, 1, 2, 3, 4, 5, 678).epoch_ms # => 1451714645678
Formats this time using the given format (see Time::Format
).
Time.now.to_s("%F") # => "2016-04-05"
Formats this time using the given format (see Time::Format
)
into the given io.