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.cr
json/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

Class Method Summary

Instance Method Summary

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) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io) : self from_yaml

Constructor Detail

def self.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).

Time.epoch(981173106) # => 2001-02-03 04:05:06 UTC

View source
def self.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).

time = Time.epoch_ms(981173106789) # => 2001-02-03 04:05:06.789 UTC
time.millisecond                   # => 789

View source
def self.new(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0, kind = Kind::Unspecified) #

View source
def self.new(ticks : Int, kind = Kind::Unspecified) #

View source
def self.new(time : LibC::Timespec, kind = Kind::Unspecified) #

View source
def self.new(pull : JSON::PullParser) #

View source
def self.new(pull : YAML::PullParser) #

View source
def self.new #

View source
def self.now : self #

View source
def 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).

Time.parse("2016-04-05", "%F") # => 2016-04-05 00:00:00

View source
def self.utc_now : self #

View source

Class Method Detail

def self.days_in_month(year, month) : Int32 #

View source
def self.leap_year?(year) : Bool #

View source
def self.local_offset_in_minutes #

Returns the local time offset in minutes relative to GMT.

# Assume in Argentina, where it's GMT-3
Time.local_offset_in_minutes # => -180

View source
def self.local_ticks #

View source
def self.utc_ticks #

View source

Instance Method Detail

def +(other : Span) #

View source
def +(other : MonthSpan) #

View source
def -(other : Int) #

View source
def -(other : Time) #

View source
def -(other : Span) #

View source
def -(other : MonthSpan) #

View source
def <=>(other : self) #

View source
def add_ticks(value) #

View source
def at_beginning_of_day #

View source
def at_beginning_of_hour #

View source
def at_beginning_of_minute #

View source
def at_beginning_of_month #

View source
def at_beginning_of_quarter #

View source
def at_beginning_of_semester #

View source
def at_beginning_of_week #

View source
def at_beginning_of_year #

View source
def at_end_of_day #

View source
def at_end_of_hour #

View source
def at_end_of_minute #

View source
def at_end_of_month #

View source
def at_end_of_quarter #

View source
def at_end_of_semester #

View source
def at_end_of_week #

View source
def at_end_of_year #

View source
def at_midday #

View source
def clone #

View source
def date #

View source
def day #

View source
def day_of_week #

View source
def day_of_year #

View source
def epoch : Int64 #

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

View source
def epoch_f : Float64 #

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

View source
def epoch_ms : Int64 #

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

View source
def friday? #

View source
def hash : Int64 #

View source
def hour #

View source
def inspect(io : IO) #

View source
def kind #

Returns Kind of the instance.


View source
def local? #

Returns true if Kind is set to Local.


View source
def millisecond #

View source
def minute #

View source
def monday? #

View source
def month #

View source
def saturday? #

View source
def second #

View source
def sunday? #

View source
def thursday? #

View source
def ticks #

View source
def time_of_day #

View source
def to_json(json : JSON::Builder) #

View source
def to_local #

View source
def to_s(format : String, io : IO) #

Formats this time using the given format (see Time::Format) into the given io.


View source
def to_s(format : String) : String #

Formats this time using the given format (see Time::Format).

time = Time.new(2016, 4, 5)
time.to_s("%F") # => "2016-04-05"

View source
def to_utc #

View source
def to_yaml(yaml : YAML::Builder) #

View source
def tuesday? #

View source
def utc? #

Returns true if Kind is set to Utc.


View source
def wednesday? #

View source
def year #

View source