iso8601

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidDurationFormat          = errors.New("iso8601: invalid duration format")
	ErrInvalidIntervalFormat          = errors.New("iso8601: invalid interval format")
	ErrInvalidRecurringIntervalFormat = errors.New("iso8601: invalid recurring interval format")
)

Functions

This section is empty.

Types

type Duration

type Duration struct {
	// contains filtered or unexported fields
}

Duration is the type of an ISO 8601 duration. A duration is defined as a non-negative quantity attributed to a time interval, the value of which is equal to the difference between the time points of the final instant and the initial instant of the time interval, when the time points are quantitative marks.

"P" is used as a duration indicator. An ISO 8601 representation of a duration always starts with a "P" character designator. It is followed by a string in the form "<n>W" or "<n>Y<n>M<n>DT<n>H<n>M<n>S".

When a component designator is zero, it may be omitted, except that at least one component must be present. The smallest component provided may be a fractional span represented as a decimal (for example, PT1.5M).

A third form, P<date>T<time>, is defined, but is not supported by this implementation.

For more information, see ISO 8601:2004(E) § 2.1.6, 2.1.7, 3.4, and 4.4.3.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/puppetlabs/leg/timeutil/pkg/iso8601"
)

func main() {
	rel1, _ := time.Parse(time.RFC3339, "2018-02-01T00:00:00Z")
	rel2, _ := time.Parse(time.RFC3339, "2018-03-01T00:00:00Z")

	d, err := iso8601.ParseDuration("P1MT6H1.5M")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(d.After(rel1))
	fmt.Println(d.After(rel2))

}
Output:

678h1m30s
750h1m30s

func ParseDuration

func ParseDuration(text string) (d Duration, err error)

ParseDuration parses the given string according to the ISO 8601 duration representation.

func (Duration) After

func (d Duration) After(from time.Time) time.Duration

After returns an absolute duration of time in Go's native duration format (nanoseconds) as if the duration occurred immediately after the given time.

func (Duration) Before

func (d Duration) Before(from time.Time) time.Duration

Before returns an absolute duration of time in Go's native duration format (nanoseconds) as if the duration occurred immediately before the given time.

func (Duration) IsZero

func (d Duration) IsZero() bool

IsZero returns true if this duration has not been set. It does not return zero if an explicit duration of no time has been provided.

func (Duration) Mul

func (d Duration) Mul(n int) (dn Duration)

Mul returns a new duration given by the product of the current duration and the given operand. It is precise to the definition of the current duration; for example, multiplying a span of 1 year 6 months by 2 will return 3 years exactly.

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

Interval is the type of an ISO 8601 time interval. ISO 8601 defines a time interval as part of the time axis limited by two instants.

Intervals are represented in one of three ways: by a start and end time, by a start time and a duration, or by a duration and an end time. A fourth representation specified in ISO 8601, given only by a duration and extended context information, is not supported by this implementation.

The two components of an interval are separated by a designator character sequence, normally "/". In rare cases, such as when an interval is used in a file name, the sequence "--" is used.

For more information, see ISO 8601:2004(E) § 2.1.3, 3.4, and 4.4.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/puppetlabs/leg/timeutil/pkg/iso8601"
)

func main() {
	e, err := iso8601.ParseInterval("2018-04-01T00:00:00Z/P1M10D")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(e.Start().Format(time.RFC3339), e.End().Format(time.RFC3339), e.Duration())

	// Move backward by 2 interval durations.
	e = e.Offset(-2)

	fmt.Println(e.Start().Format(time.RFC3339), e.End().Format(time.RFC3339), e.Duration())

}
Output:

2018-04-01T00:00:00Z 2018-05-11T00:00:00Z 960h0m0s
2018-01-12T00:00:00Z 2018-02-22T00:00:00Z 984h0m0s

func ParseInterval

func ParseInterval(text string) (i Interval, err error)

ParseInterval parses the given string according to the ISO 8601 time interval representation.

func (Interval) Designator

func (i Interval) Designator() string

Designator returns the designator character sequence used to separate the components of this interval.

func (Interval) Duration

func (i Interval) Duration() time.Duration

Duration returns the absolute duration of this interval regardless of how the interval was constructed.

func (Interval) End

func (i Interval) End() time.Time

End returns the absolute end time of this interval regardless of how the interval was constructed.

func (Interval) MarshalText

func (i Interval) MarshalText() ([]byte, error)

func (Interval) Offset

func (i Interval) Offset(n int) (in Interval)

Offset returns a new interval offset a given number of times from the current interval. It correctly accounts for duration precision. For example, offsetting an interval specified as "2018-10-01/P1M" by 2 will return the interval specified by "2018-12-01/P1M".

func (Interval) Start

func (i Interval) Start() time.Time

Start returns the absolute start time of this interval regardless of how the interval was constructed.

func (Interval) String

func (i Interval) String() string

func (*Interval) UnmarshalText

func (i *Interval) UnmarshalText(text []byte) error

type RecurringInterval

type RecurringInterval struct {
	// contains filtered or unexported fields
}

RecurringInterval is the type of an ISO 8601 recurring time interval. ISO 8601 defines a recurring time interval as a series of consecutive time intervals of the same duration or nominal duration.

Recurring intervals are represented either by "R<n>/<interval>", where <n> indicates the number of repetitions, or by "R/<interval>" to indicate unbounded repetition. The first repetition of the recurring interval occurs at the start time of the given interval, and it repeats immediately after the duration of the interval has elapsed for the number of given repetitions.

The two components of a recurring interval are separated by a designator character sequence, normally "/". In rare cases, such as when a recurring interval is used in a file name, the sequence "--" is used.

For more information, see ISO 8601:2004(E) § 2.1.17, 3.4, and 4.5.

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/puppetlabs/leg/timeutil/pkg/iso8601"
)

func main() {
	r, err := iso8601.ParseRecurringInterval("R10/2018-10-01T00:00:00Z/P1Y")
	if err != nil {
		log.Fatal(err)
	}

	repetitions, _ := r.Repetitions()
	fmt.Println(repetitions)

	rel1, _ := time.Parse(time.RFC3339, "2019-12-01T00:00:00Z")
	rel2, _ := time.Parse(time.RFC3339, "2029-01-01T00:00:00Z")

	next, _ := r.Next(rel1)
	fmt.Println(next)

	_, ok := r.Next(rel2)
	fmt.Println(ok)

}
Output:

10
2020-10-01T00:00:00Z/P1Y
false

func ParseRecurringInterval

func ParseRecurringInterval(text string) (ri RecurringInterval, err error)

ParseRecurringInterval parses the given string according to the ISO 8601 recurring time interval representation.

func (RecurringInterval) Designator

func (ri RecurringInterval) Designator() string

Designator returns the designator character sequence used to separate the components of this recurring interval.

func (RecurringInterval) Interval

func (ri RecurringInterval) Interval() Interval

Interval returns the initial interval of this recurring interval.

func (RecurringInterval) MarshalText

func (ri RecurringInterval) MarshalText() ([]byte, error)

func (RecurringInterval) Next

func (ri RecurringInterval) Next(from time.Time) (Interval, bool)

Next returns the interval starting at or after the given time. If there are no intervals after the given time, it sets its second return value to false.

func (RecurringInterval) Repetitions

func (ri RecurringInterval) Repetitions() (n int, ok bool)

Repetitions returns the number of repetitions specified by this recurring interval. If this recurring interval is unbounded, it sets its second return value to false.

func (RecurringInterval) String

func (ri RecurringInterval) String() string

func (*RecurringInterval) UnmarshalText

func (ri *RecurringInterval) UnmarshalText(text []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL