Time
Duration
Imported from the upstream Vibescript examples at time/duration.vibe and runnable in the browser today.
Source
time/duration.vibe
# vibe: 0.2
def after_time(base)
5.minutes.after(base).to_s
end
def ago_time(base)
2.hours.ago(base).to_s
end
def duration_parts
d = Duration.parse("P1DT1H1M1S")
{
iso: d.iso8601,
parts: d.parts
}
end
def duration_parse_build
Duration.build(90).iso8601
end
def duration_to_i
Duration.parse("P1DT30M").to_i
end
def duration_until(base)
90.minutes.until(base).to_s
end
def duration_math
d1 = 4.seconds + 2.hours
d2 = 2.hours - 4.seconds
d3 = 10.seconds * 3
d4 = 3 * 10.seconds
d5 = 10.seconds / 2
ratio = 10.seconds / 4.seconds
mod = 10.seconds % 4.seconds
compare = {
lt: 2.seconds < 3.seconds,
eq: 5.seconds == 5.seconds,
gt: 10.seconds > 3.seconds
}
{
add: d1.iso8601,
subtract: d2.iso8601,
multiply: d3.iso8601,
multiply_left: d4.iso8601,
divide: d5.iso8601,
divide_duration: ratio,
modulo: mod.iso8601,
compare: compare
}
end
def duration_to_i_math
(3.seconds + 5.seconds).to_i
end
def run
{
parts: duration_parts,
parse_build: duration_parse_build,
to_i: duration_to_i,
math: duration_math,
to_i_math: duration_to_i_math
}
end
Output
Press run to execute run from this example.