struct Time::Span

Overview

Time::Span represents one period of time.

A Time::Span initializes with the specified period. Different numbers of arguments generate a Time::Span in different length. Check all #new methods for details.

Time::Span.new(nanoseconds: 10_000)                           # => 00:00:00.000010000
Time::Span.new(hours: 10, minutes: 10, seconds: 10)           # => 10:10:10
Time::Span.new(days: 10, hours: 10, minutes: 10, seconds: 10) # => 10.10:10:10

Calculation between Time also returns a Time::Span.

span = Time.utc(2015, 10, 10) - Time.utc(2015, 9, 10)
span       # => 30.00:00:00
span.class # => Time::Span

Inspection:

span = Time::Span.new(hours: 20, minutes: 10, seconds: 10)
span.hours   # => 20
span.minutes # => 10
span.seconds # => 10

Calculation:

a = Time::Span.new(hours: 20, minutes: 10, seconds: 10)
b = Time::Span.new(hours: 10, minutes: 10, seconds: 10)
c = a - b # => 10:00:00
c.hours   # => 10

Included Modules

Defined in:

time/span.cr

Constant Summary

MAX = new(seconds: Int64::MAX, nanoseconds: 999999999)
MIN = new(seconds: Int64::MIN, nanoseconds: -999999999)
ZERO = new(nanoseconds: 0)

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Steppable

step(*, to limit = nil, by step, exclusive : Bool = false, &) : Nil
step(*, to limit = nil, by step, exclusive : Bool = false)
step

Instance methods inherited from module Comparable(Time::Span)

<(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) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io : IO) : Nil to_s

Instance methods inherited from struct 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, root : String)
from_json(string_or_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

def self.additive_identity : self #

Returns the additive identity of this type.

This is .zero.


[View source]
def self.new(*, seconds : Int, nanoseconds : Int) #

Creates a new Time::Span from seconds and nanoseconds.

Nanoseconds get normalized in the range of 0...1_000_000_000, the nanosecond overflow gets added as seconds.

Time::Span.new(seconds: 30)                 # => 00:00:30
Time::Span.new(seconds: 5, nanoseconds: 12) # => 00:00:05.000000012

[View source]
def self.new(*, nanoseconds : Int) #

Creates a new Time::Span from the nanoseconds given

Nanoseconds get normalized in the range of 0...1_000_000_000, the nanosecond overflow gets added as seconds.

Time::Span.new(nanoseconds: 500_000_000)   # => 00:00:00.500000000
Time::Span.new(nanoseconds: 5_500_000_000) # => 00:00:05.500000000

[View source]
def self.new(*, days : Int = 0, hours : Int = 0, minutes : Int = 0, seconds : Int = 0, nanoseconds : Int = 0) #

Creates a new Time::Span from the days, hours, minutes, seconds and nanoseconds given

Any time unit can be omitted.

Time::Span.new(days: 1)                                                   # => 1.00:00:00
Time::Span.new(days: 1, hours: 2, minutes: 3)                             # => 1.02:03:00
Time::Span.new(days: 1, hours: 2, minutes: 3, seconds: 4, nanoseconds: 5) # => 1.02:03:04.000000005

[View source]

Class Method Detail

def self.zero : Time::Span #

Creates a new Time::Span representing a span of zero time.


[View source]

Instance Method Detail

def *(number : Int) : Time::Span #

Returns a Time::Span that is number times longer.


[View source]
def *(number : Float) : Time::Span #

Returns a Time::Span that is number times longer.


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

[View source]
def + : self #

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

[View source]
def - : Time::Span #

[View source]
def /(number : Int) : Time::Span #

Returns a Time::Span that is divided by number.


[View source]
def /(number : Float) : Time::Span #

Returns a Time::Span that is divided by number.


[View source]
def /(other : self) : Float64 #

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

[View source]
def abs : Time::Span #

Returns the absolute (non-negative) amount of time this Time::Span represents by removing the sign.


[View source]
def ago : Time #

Returns a Time that happens earlier by self than the current time.


[View source]
def days : Int64 #

Returns the number of full days in this time span.

(5.days + 25.hours).days # => 6_i64

[View source]
def from_now : Time #

Returns a Time that happens later by self than the current time.


[View source]
def hours : Int32 #

Returns the number of full hours of the day (0..23) in this time span.


[View source]
def inspect(io : IO) : Nil #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

[View source]
def microseconds : Int32 #

Returns the number of microseconds of the second (0..999999) in this time span.


[View source]
def milliseconds : Int32 #

Returns the number of milliseconds of the second (0..999) in this time span.


[View source]
def minutes : Int32 #

Returns the number of full minutes of the hour (0..59) in this time span.


[View source]
def nanoseconds : Int32 #

Returns the number of nanoseconds of the second (0..999_999_999) in this time span.


[View source]
def negative? : Bool #

Returns true if self represents a negative time span.

2.hours.negative? # => false
0.days.negative?  # => false
-3.days.negative? # => true

[View source]
def positive? : Bool #

Returns true if self represents a positive time span.

2.hours.positive? # => true
0.days.positive?  # => false
-3.days.positive? # => false

[View source]
def seconds : Int32 #

Returns the number of full seconds of the minute (0..59) in this time span.


[View source]
def sign : Int32 #

Returns the sign of this time span.

Values are -1, 0, 1 if self is smaller, equal, bigger compared to ZERO.


[View source]
def to_f : Float64 #

Alias of #total_seconds.


[View source]
def to_i : Int64 #

Returns the number of full seconds.


[View source]
def total_days : Float64 #

Converts to a (possibly fractional) number of days.

(36.hours).total_days # => 1.5

[View source]
def total_hours : Float64 #

Converts to a (possibly fractional) number of hours.


[View source]
def total_microseconds : Float64 #

Converts to a number of microseconds.


[View source]
def total_milliseconds : Float64 #

Converts to a number of milliseconds.


[View source]
def total_minutes : Float64 #

Converts to a (possibly fractional) number of minutes.


[View source]
def total_nanoseconds : Float64 #

Converts to a number of nanoseconds.


[View source]
def total_seconds : Float64 #

Converts to a (possibly fractional) number of seconds.


[View source]
def total_weeks : Float64 #

Converts to a (possibly fractional) number of weeks.

(4.weeks + 5.days + 6.hours).total_weeks # => 4.75

[View source]
def zero? : Bool #

Returns true if self represents a span of zero time.

2.hours.zero?  # => false
0.days.zero?   # => true
1.second.zero? # => false

[View source]