Using Duration

The complete source of builtInTypes_duration.ek9, compiled by the EK9 compiler. Shown on builtInTypes.html.

#!ek9
defines module site.builtintypes.duration

  defines function

    durationExamples()
      stdout <- Stdout()

      //Some examples of using Duration
      d1 <- PT1H2M3S //results in 1 hour, 2 minutes and 3 seconds
      d2 <- P3DT2H59M18S //results in 3 days, 2 hours, 59 minutes and 18 seconds
      d3 <- P2Y6W3DT8H5M8S //results in 2 years, 45 days, 8 hours, 5 minutes and 8 seconds
      //Note that 6 weeks was converted to 42 days and then the additional 3 days are added to that
      d4 <- P2Y2M1W3DT8H5M8S //results in 2 years, 2 months, 10 days, 8 hours, 5 minutes and 8 seconds
      d5 <- P2W //results in 14 days

      //Mathematics with durations
      d6 <- PT1H2M1S
      twice <- d6 * 2 //results in PT2H4M2S i.e. twice the duration in d6
      scaled <- d6 * 8.25 //results in PT8H31M38S
      divided <- scaled / 3 //results in PT2H50M32S
      extended <- d6 + PT20M //results in PT1H22M1S
      reduced <- d6 - PT2M6S //results in PT59M55S
      reduced += PT2H5S //results in PT3H

      stdout.println(`${d1} ${d2} ${d3} ${d4} ${d5}`)
      stdout.println(`${twice} ${scaled} ${divided} ${extended} ${reduced}`)

    durationsOnDates()
      stdout <- Stdout()

      durations as List of Duration := [P1D, P1M]

      //Adding Durations to a Base Date
      date <- 2019-06-01
      //Definition of durations omitted
      cat durations >> date
      //The Easiest way to move a date on is by a number of durations

      stdout.println(date)

  defines program

    DurationUse()
      durationExamples()
      durationsOnDates()

Download builtInTypes_duration.ek9

Output

PT1H2M3S P3DT2H59M18S P2Y45DT8H5M8S P2Y2M10DT8H5M8S P14D
PT2H4M2S PT8H31M38S PT2H50M32S PT1H22M1S PT3H
2019-07-02