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
new(seconds: Int64::MAX, nanoseconds: 999999999) new(seconds: Int64::MIN, nanoseconds: -999999999) new(nanoseconds: 0) Returns the additive identity of this type.
Creates a new Time::Span from seconds and nanoseconds.
Creates a new Time::Span from the nanoseconds given
Creates a new Time::Span from the *weeks, days, hours, minutes, seconds and nanoseconds given
Creates a new Time::Span representing a span of zero time.
Returns a Time::Span that is number times longer.
Returns a Time::Span that is number times longer.
Returns a Time::Span that is divided by number.
Returns a Time::Span that is divided by number.
Returns the absolute (non-negative) amount of time this Time::Span represents by removing the sign.
Returns a Time that happens earlier by self than the current time.
Returns the number of full days in this time span.
Returns a Time that happens later by self than the current time.
Returns the number of full hours of the day (0..23) in this time span.
Appends this struct's name and instance variables names and values to the given IO.
Returns the number of microseconds of the second (0..999999) in this time span.
Returns the number of milliseconds of the second (0..999) in this time span.
Returns the number of full minutes of the hour (0..59) in this time span.
Returns the number of nanoseconds of the second (0..999_999_999) in this time span.
Returns true if self represents a negative time span.
Returns true if self represents a positive time span.
Returns the number of full seconds of the minute (0..59) in this time span.
Returns the sign of this time span.
Alias of #total_seconds.
Returns the number of full seconds.
Converts to a (possibly fractional) number of days.
Converts to a (possibly fractional) number of hours.
Converts to a number of microseconds.
Converts to a number of milliseconds.
Converts to a (possibly fractional) number of minutes.
Converts to a number of nanoseconds.
Converts to a (possibly fractional) number of seconds.
Converts to a (possibly fractional) number of weeks.
Returns true if self represents a span of zero time.
Steppable
Comparable(Time::Span)
Struct
Struct
Value
Object
Object
Object
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
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
Creates a new Time::Span from the *weeks, 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
Creates a new Time::Span representing a span of zero time.
Returns a Time::Span that is number times longer.
Returns a Time::Span that is number times longer.
Returns a Time::Span that is divided by number.
Returns a Time::Span that is divided by number.
Returns the absolute (non-negative) amount of time this Time::Span represents by removing the sign.
Returns the number of full days in this time span.
(5.days + 25.hours).days # => 6_i64
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)"
Returns the number of microseconds of the second (0..999999) in this time span.
Returns the number of milliseconds of the second (0..999) in this time span.
Returns the number of nanoseconds of the second (0..999_999_999) in this time span.
Returns true if self represents a negative time span.
2.hours.negative? # => false 0.days.negative? # => false -3.days.negative? # => true
Returns true if self represents a positive time span.
2.hours.positive? # => true 0.days.positive? # => false -3.days.positive? # => false
Returns the number of full seconds of the minute (0..59) in this time span.
Returns the sign of this time span.
Values are -1, 0, 1 if self is smaller, equal, bigger compared to ZERO.
Alias of #total_seconds.
Converts to a (possibly fractional) number of days.
(36.hours).total_days # => 1.5
Converts to a (possibly fractional) number of weeks.
(4.weeks + 5.days + 6.hours).total_weeks # => 4.75
Returns true if self represents a span of zero time.
2.hours.zero? # => false 0.days.zero? # => true 1.second.zero? # => false
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/Time/Span.html