Duration
Duration is the representation of a time interval in Juttle, defined as the time span between two Juttle moments; refer to the Time module for more. The Duration module provides functions for working with durations, including conversion from/to seconds and strings.
Duration.as
Return the value of duration as a floating-point number in units of timeunit.
Duration.as(duration, timeunit)
If you were to multiply this number by Duration(1, timeunit), you would get back the original duration.
Example: output elapsed time as seconds
const millenium = Date.new("2001-01-01");
emit -limit 1 | put elapsed_seconds_third_millenium = Duration.as(:now: - millenium, "seconds")
Duration.format
Format the duration as a string.
Duration.format(duration)
Duration.format(duration, format_string)
The optional format_string
is as described in the npm moment-duration-format package. When no format is given, an approximate format will be built based on the magnitude of the duration.
Example
emit -limit 1
| put duration = :2 months and 2 weeks and 2 days and 1 hour and 3 minutes:
| put default=Duration.format(duration) // approximate, depends on duration magnitude
| put asdays=Duration.format(duration, "DD [days]") // rounded to nearest whole day
| put dotnet=Duration.format(duration,"dd.hh:mm:ss") // in .NET format, to nearest second.
| remove time, duration // duration would display precisely, in a nonstandard mixed calendar/time format
| view table
Duration.get
Return the value of a given time unit for a duration (such as, "days") as an integer.
Duration.get(duration, timeunit)
Example: split duration into its time unit components
const millenial = :now: - Date.new("2001-01-01");
emit -limit 1
| put y = Duration.get(millenial, "years"),
M = Duration.get(millenial, "months"),
d = Duration.get(millenial, "days"),
h = Duration.get(millenial, "hours"),
m = Duration.get(millenial, "minutes"),
s = Duration.get(millenial, "seconds")
| put ticker = "Time lived in this millenium: ${y}y ${M}m ${d}d ${h}h:${m}m:${s}s"
| keep ticker
Duration.milliseconds
Return the value of duration as a floating-point number in milliseconds.
Duration.milliseconds(duration)
Note:
Equivalent to Duration.as(duration, "ms")
.
Example: convert duration of Hadron Epoch to milliseconds
emit -limit 1 | put hadron_epoch_ms = Duration.milliseconds(:1s:)
Duration.new
Return a duration.
Duration.new(seconds)
Example: Some ways to represent a minute
emit -limit 1
| put this_minute = Duration.new(60), that_minute = Duration.new("00:01:00")
| put diff = that_minute - this_minute
Duration.seconds
Return the value of duration as a floating-point number in units of seconds.
Duration.seconds(duration)
Note:
Equivalent to Duration.as(duration, "seconds")
.
Example: how many seconds in a year
emit -limit 1
| put normal_year_seconds = Duration.seconds(:365 days:),
leap_year_seconds = Duration.seconds(:366 days:)
Duration.toString()
Convert a value of type Duration to type String.
Duration.toString(duration)
Example: print duration of this day so far
emit -limit 1
| put today_so_far = :now: - Date.startOf(:now:, "day")
| put message = "This day has lasted " + Duration.toString(today_so_far)
| keep message