Durations - craterdog/go-component-framework GitHub Wiki

Overview

A duration is a primitive element that represents a duration of time consistent with the ISO 8601 standard. Each duration has the following string format:

$duration: "~P" (WEEKS | (YEARS? MONTHS? DAYS? ('T' HOURS? MINUTES? SECONDS?)?))

The source of this format is Bali Document Notation™ (Bali) defined here.

Each duration consists of one or more of the following parts:

  • years - the number of years the duration spans
  • months - the number of months the duration spans
  • weeks - the number of weeks the duration spans (only if an exact amount)
  • days - the number of days the duration spans
  • hours - the number of hours the duration spans
  • minutes - the number of minutes the duration spans
  • seconds - the number of seconds the duration spans

UML Diagram

A Quick Example

To whet your appetite, here is some short example code that demonstrates the use of durations.

package main

import (
	fmt "fmt"
	fra "github.com/craterdog/go-component-framework/v7"
)

func main() {
	// Retrieve the class reference.
	var class = fra.DurationClass()

	// Retrieve a class constant.
	var millisecondsPerWeek = class.MillisecondsPerWeek()

	// Create some new durations.
	var duration = class.DurationFromString("~P1Y2M3DT4H5M6.789S")
	fmt.Println(duration)
	var weeks = class.Duration(3 * millisecondsPerWeek)
	fmt.Println(weeks)

	// Retrieve the parts of the first duration.
	fmt.Println(duration.GetYears(), "year")
	fmt.Println(duration.GetMonths(), "months")
	fmt.Println(duration.GetDays(), "days")
	fmt.Println(duration.GetHours(), "hours")
	fmt.Println(duration.GetMinutes(), "minutes")
	fmt.Println(duration.GetSeconds(), "seconds")
	fmt.Println(duration.GetMilliseconds(), "milliseconds")
}

Try it out in the Go Playground...