struct Time
Overview
Time
represents an instance in time. Here are some examples:
Basic Usage
time = Time.new(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.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 = Time.new(2015, 10, 12)
time.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.crjson/to_json.cr
yaml/to_yaml.cr
Constant 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
Constructors
-
.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). - .new(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0, kind = Kind::Unspecified)
- .new(ticks : Int, kind = Kind::Unspecified)
- .new(time : LibC::Timespec, kind = Kind::Unspecified)
- .new(pull : JSON::PullParser)
- .new(pull : YAML::PullParser)
- .new
- .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
Class Method Summary
- .days_in_month(year, month) : Int32
- .leap_year?(year) : Bool
-
.local_offset_in_minutes
Returns the local time offset in minutes relative to GMT.
- .local_ticks
- .utc_ticks
Instance Method Summary
- #+(other : Span)
- #+(other : MonthSpan)
- #-(other : Int)
- #-(other : Time)
- #-(other : Span)
- #-(other : MonthSpan)
- #<=>(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
- #clone
- #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
ifKind
is set to Local. - #millisecond
- #minute
- #monday?
- #month
- #saturday?
- #second
- #sunday?
- #thursday?
- #ticks
- #time_of_day
- #to_json(json : JSON::Builder)
- #to_local
-
#to_s(format : String, io : IO)
Formats this time using the given format (see
Time::Format
) into the given io. -
#to_s(format : String) : String
Formats this time using the given format (see
Time::Format
). - #to_utc
- #to_yaml(yaml : YAML::Builder)
- #tuesday?
-
#utc?
Returns
true
ifKind
is set to Utc. - #wednesday?
- #year
Instance methods inherited from module Comparable(self)
<(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,
pretty_print(pp) : Nil
pretty_print,
to_s(io)
to_s
Instance methods inherited from struct Value
==(other)
==,
dup
dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
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(io : IO)
to_yaml to_yaml, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as
Constructor methods inherited from class Object
from_json(string_or_io, root : String) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io) : self from_yaml
Constructor 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.789 UTC
time.millisecond # => 789
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
Class Method Detail
Returns the local time offset in minutes relative to GMT.
# Assume in Argentina, where it's GMT-3
Time.local_offset_in_minutes # => -180
Instance Method Detail
Returns the number of seconds since the Epoch for this time.
time = Time.parse("2016-01-12 03:04:05 UTC", "%F %T %z")
time.epoch # => 1452567845
Returns the number of seconds since the Epoch for this time,
as a Float64
.
time = Time.parse("2016-01-12 03:04:05.678 UTC", "%F %T.%L %z")
time.epoch_f # => 1452567845.678
Returns the number of milliseconds since the Epoch for this time.
time = Time.parse("2016-01-12 03:04:05.678 UTC", "%F %T.%L %z")
time.epoch_ms # => 1452567845678
Formats this time using the given format (see Time::Format
)
into the given io.
Formats this time using the given format (see Time::Format
).
time = Time.new(2016, 4, 5)
time.to_s("%F") # => "2016-04-05"