struct Duration
- Duration
- Struct
- Value
- Object
Overview
The Duration type represents calendar months, calendar days, and monotonic
time spans, allowing for more precise temporal math.
Defined in:
duration.crparser/iso8601.cr
pg.cr
Constant Summary
-
VERSION =
"0.1.0"
Constructors
-
.between(earlier : Time, later : Time) : self
Get the
Durationbetweenearlierandlater. -
.new(month_span : Time::MonthSpan, span : Time::Span)
Instantiate a
Durationfrom both aTime::MonthSpanand aTime::Span. -
.new(span : Time::Span)
Instantiate a
Durationfrom aTime::Span. -
.new(month_span : Time::MonthSpan)
Instantiate a
Durationfrom aTime::MonthSpan. - .new(*, years : Int32 = 0, months : Int32 = 0, weeks : Int64 = 0, days : Int32 = 0, hours : Int64 = 0_i64, minutes : Int64 = 0_i64, seconds : Int64 = 0_i64, milliseconds : Int64 = 0_i64, microseconds : Int64 = 0_i64, nanoseconds : Int64 = 0_i64)
Class Method Summary
-
.parse_iso8601(string : String)
Parse ISO8601 duration strings like
"P3Y6M4DT12H30M5S"intoDurationinstances.
Instance Method Summary
-
#*(factor : Int) : self
Multiplies this
Durationby the given factor. -
#+(other : Time::Span | Time::MonthSpan) : self
Add a
Time::SpanaTime::MonthSpanfrom the crystal standard library to thisDuration. -
#+(other : self) : self
Returns the sum of two
Durationinstances. -
#-(other : Time::Span | Time::MonthSpan) : self
Subtract a
Time::SpanorTime::MonthSpan(from the crystal standard library) to thisDuration. -
#-(other : self) : self
Returns the difference between two
Durationinstances. - #- : self
-
#//(factor : Int) : self
Divides this
Durtationby the given scalar. -
#ago(location = Time::Location.local)
Returns the time that this
Durationrepresents before the current local time. -
#before(time : Time)
Returns the time that this
Durationrepresents before the given time. - #days : Int32
-
#from(time : Time)
Returns the time that this
Durationrepresents from the given time. -
#from_now(location = Time::Location.local)
Returns the time that this
Durationrepresents from the current local time. -
#hours : Float64
Returns monotonic hours, including the fractional part.
-
#microseconds : Float64
Returns monotonic microseconds, including the fractional part.
-
#milliseconds : Float64
Returns monotonic milliseconds, including the fractional part.
-
#minutes : Float64
Returns monotonic minutes, including the fractional part.
- #months : Int32
- #nanoseconds : Int64
-
#seconds : Float64
Returns monotonic seconds, including the fractional part.
- #sign
- #to_iso8601(io : IO) : Nil
-
#to_iso8601 : String
Output an ISO8601 representation of this
Durationto the givenIO -
#to_month_span
Return the month portion of this
Durationas a Crystal stdlibTime::MonthSpaninstance. - #to_postgres(io) : Nil
- #to_postgres
-
#to_span(include_days = false)
Return the monotonic portion of this
Durationas a Crystal stdlibTime::Spaninstance. -
#weeks : Float64
Returns the number of calendar weeks represented by this
Duration, including the fractional part -
#years : Float64
Returns the number of calendar years represented by this
Duration, including the fractional part -
#zero?
Returns
trueif thisDurationdoes not measure any time at all,falseotherwise.
Constructor Detail
Get the Duration between earlier and later. This doesn't just return
the nanoseconds-based time between the two timestamps the way that
latest - earliest does. Instead, it calculates how many months and days
are also in between, then takes the nanoseconds.
now = Time.utc
before = now - 1.month
duration = Duration.between(before, now)
# => Duration(@months=1, @days=0, @nanoseconds=0)
Instantiate a Duration from both a Time::MonthSpan and a Time::Span.
Duration.new(5.seconds)
# => Duration(@months=0, @days=0, @nanoseconds=5000000000)
NOTE While you can get months and monotonic time, there is no way to get calendar days from this constructor.
Instantiate a Duration from a Time::Span.
Duration.new(5.seconds)
# => Duration(@months=0, @days=0, @nanoseconds=5000000000)
Instantiate a Duration from a Time::MonthSpan.
Duration.new(6.months)
# => Duration(@months=6, @days=0, @nanoseconds=0)
Class Method Detail
Parse ISO8601 duration strings
like "P3Y6M4DT12H30M5S" into Duration instances.
# 3 years, 6 months, 4 days, 12 hours, 30 minutes, 5.5 seconds
Duration.parse_iso8601("P3Y6M4DT12H30M5.5S")
# => Duration(@months=42, @days=4, @nanoseconds=45005500000000)
The parser is incredibly efficient and performs no heap allocations.
Instance Method Detail
Add a Time::Span a Time::MonthSpan from the crystal standard library to this Duration. The Time::Span will be added to the monotonic portion of this Duration and the Time::MonthSpan will be added to the #months portion.
Duration.new(years: 1) + 1.month + 1.hour
# => Duration(@months=13, @days=0, @nanoseconds=3600000000000)
Subtract a Time::Span or Time::MonthSpan (from the crystal standard
library) to this Duration. The Time::Span will be subtracted from the
monotonic portion of this Duration and the Time::MonthSpan will be
subtracted from the #months portion.
Divides this Durtation by the given scalar. Note that only integer division is supported.
Returns the time that this Duration represents before the given time.
previous_run = 1.calendar_day.before(next_scheduled_run)
Returns the time that this Duration represents from the given time.
next_bill_at = 1.calendar_month.from(subscription.last_billed_at)
Returns monotonic hours, including the fractional part.
Duration.new(hours: 3, minutes: 30).hours # => 3.5
Returns monotonic microseconds, including the fractional part.
Duration.new(nanoseconds: 15_500).microseconds # => 15.5
Returns monotonic milliseconds, including the fractional part.
Duration.new(microseconds: 15_500).milliseconds # => 15.5
Returns monotonic minutes, including the fractional part.
Duration.new(minutes: 3, seconds: 30).minutes # => 3.5
Returns monotonic seconds, including the fractional part.
Duration.new(milliseconds: 15_500).seconds # => 15.5
Return the month portion of this Duration as a Crystal stdlib
Time::MonthSpan instance.
Return the monotonic portion of this Duration as a Crystal stdlib
Time::Span instance.
NOTE Since the Crystal stdlib has no representation of calendar days, it is
not currently possible to incorporate the concept of calendar days. If you
are comfortable with approximating the number of days as 24 monotonic hours
you can pass include_days: true, however keep in mind that this may return
an incorrect value when performing arithmetic on a Duration and a Time
crosses a daylight savings boundary.
Returns the number of calendar weeks represented by this Duration, including the fractional part
Duration.new(days: 45).weeks # => 6.428571428571429