struct Time

Overview

The Time library allows you to inspect, analyze, calculate, and format 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"

# Format specifiers include but are not limited to:
%Y  => year
%m  => month
%d  => day
%H  => hour
%M  => minute
%S  => second
%D  => date
%u  => weekday

Calculation

Time.new(2015,10,10) - 5.day
#=> 2015-10-05 00:00:00

# Time calculation returns a Time::Span instance,
# which can be analyzed with its methods.
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       #=> 02:00:00
span.class #=> Time::Span
span.hours #=> 2

Included Modules

Defined in:

time/time.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

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module Comparable(T)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, between?(min, max) between?

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) ==, nil? nil?

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

def self.days_in_month(year, month) #

[View source]
def self.epoch(seconds : Int) #

[View source]
def self.epoch_ms(milliseconds : Int) #

[View source]
def self.leap_year?(year) #

[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.new(time : LibC::TimeSpec, kind = Kind::Unspecified) #

[View source]
def self.now #

[View source]
def self.parse(time, pattern, kind = Time::Kind::Unspecified) #

[View source]
def self.utc_now #

[View source]
def self.utc_ticks #

[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 #

[View source]

Instance Method Detail

def +(other : MonthSpan) #

[View source]
def +(other : Span) #

[View source]
def -(other : Span) #

[View source]
def -(other : MonthSpan) #

[View source]
def -(other : Int) #

[View source]
def -(other : Time) #

[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 date #

[View source]
def day #

[View source]
def day_of_week #

[View source]
def day_of_year #

[View source]
def epoch #

Returns the number of seconds since the Epoch


[View source]
def epoch_f #

[View source]
def epoch_ms #

[View source]
def friday? #

[View source]
def hash #

[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_local #

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

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

[View source]
def to_utc #

[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]

Macro Detail

macro def_at(name) #

[View source]