W3cubDocs

/Crystal 0.31

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(10, 10, 10)          # => 10:10:10
Time::Span.new(10, 10, 10, 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(20, 10, 10)
span.hours   # => 20
span.minutes # => 10
span.seconds # => 10

Calculation:

a = Time::Span.new(20, 10, 10)
b = Time::Span.new(10, 10, 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

Constructor Detail

def self.new(days : Int, hours : Int, minutes : Int, seconds : Int, nanoseconds : Int = 0)Source

def self.new(hours : Int, minutes : Int, seconds : Int)Source

def self.new(*, seconds : Int, nanoseconds : Int)Source

def self.new(*, nanoseconds : Int)Source

Class Method Detail

def self.zero : Time::SpanSource

Instance Method Detail

def *(number : Float) : Time::SpanSource

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

def *(number : Int) : Time::SpanSource

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

def + : selfSource

def +(other : self) : Time::SpanSource

def - : Time::SpanSource

def -(other : self) : Time::SpanSource

def /(other : self) : Float64Source

def /(number : Float) : Time::SpanSource

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

def /(number : Int) : Time::SpanSource

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

def <=>(other : self)Source

def abs : Time::SpanSource

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

def ago : TimeSource

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

def days : Int64Source

Returns the number of full days in this time span.

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

def duration : Time::SpanSource

Alias of #abs.

def from_now : TimeSource

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

def hours : Int32Source

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

def inspect(io : IO) : NilSource

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)"

def microseconds : Int32Source

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

def milliseconds : Int32Source

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

def minutes : Int32Source

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

def nanoseconds : Int32Source

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

def seconds : Int32Source

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

def to_f : Float64Source

Alias of #total_seconds.

def to_i : Int64Source

Returns the number of full seconds.

def total_days : Float64Source

Converts to a (possibly fractional) number of days.

(36.hours).total_days # => 1.5

def total_hours : Float64Source

Converts to a (possibly fractional) number of hours.

def total_milliseconds : Float64Source

Converts to a number of milliseconds.

def total_minutes : Float64Source

Converts to a (possibly fractional) number of minutes.

def total_nanoseconds : Float64Source

Converts to a number of nanoseconds.

def total_seconds : Float64Source

Converts to a (possibly fractional) number of seconds.

def total_weeks : Float64Source

Converts to a (possibly fractional) number of weeks.

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

def zero? : BoolSource

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/Time/Span.html