[go: up one dir, main page]

chrono

package module
v0.0.0-...-0321719 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: MIT Imports: 7 Imported by: 2

README

Go Reference Maintenance GoReportCard example

chrono - supplementary time and date module

chrono provides additional functionality and improved ergonomics to complement the Go standard library's time package. It is not a replacement for, nor an extension of, the time package, but for certain use cases for which it was not explicitly designed to support, chrono can help to simplify and clarify.

chrono is also designed to look and feel like Go. Many of the ideas and much of the API is inspired by time, and should therefore feel familiar. That said, capable time and date libraries exist for most mainstream languages, and chrono has taken inspiration from several besides Go's time package, including Rust, Java and Python.


Not all features are complete yet. See the roadmap for the current state. If in doubt, create an issue to ask a question or open a feature request.


Use cases

Local (or "civil") dates and times

Often it's necessary to represent civil time, or dates and times without a time zone or time offset component. Usually, this is achieved using the standard library's time.Date function and setting the time values to 0. Alternatively, some people use Google's civil package.

chrono provides 3 types for dealing with these use cases: LocalDate (a date without a time), LocalTime (a time without a date), and LocalDateTime (a combination of LocalDate and LocalTime).

A LocalDate and a LocalTime are initialized with numeric values. A LocalDateTime can either be initialized with numeric values, or by combining a LocalDate and LocalTime (as below):

date := chrono.LocalDateOf(2007, chrono.May, 20)
time := chrono.LocalTimeOf(12, 30, 15, 0)
fmt.Println(chrono.OfLocalDateAndTime(date, time))

See more LocalDate examples.
See more LocalTime examples.
See more LocalDateTime examples.

Parse and format dates and times

chrono differs from the time package because it uses format codes instead of a mnemonic device. The format codes are borrowed from strftime/strptime, and therefore maybe familiar from other languages. The full list is documented here, but here's a simple example of formatting a time:

time := chrono.LocalTimeOf(12, 30, 15, 0)
fmt.Println(time.Format("%H:%M:%S"))

And parsing a time:

var time chrono.LocalTime
fmt.Println(time.Parse("%H:%M:%S", "12:30:15"))

There are also predefined layouts, similar to the time package, but with the addition of layouts compatible with ISO 8601.

Experimental: Parsing without a layout

The example above assumes that you know how a date time string is formatted, but that's not always the case. For these situations, ParseToLayout accepts just a string and attempts to parse it, also returning the layout string.

var c chrono.OffsetDateTime
fmt.Println(chrono.ParseToLayout(
    "2006-04-09",
    chrono.ParseConfig{},
    &c,
)) // %Y-%m-%d

To access this function you need to build with -tag parse.

This API is incomplete and subject to change until a stable release is reached.

Parse and format ISO 8601 durations

When interfacing with systems where the time package's duration formatting is not understood, ISO 8601 is a commonly-adopted standard.

time doesn't support ISO 8601 durations notation. A simple one-liner that uses only the seconds component is possible, but this is neither readable nor solves the problem of parsing such strings:

var d time.Duration
fmt.Printf("PT%dS", int(d.Seconds()))

chrono supports both parsing and formatting of ISO 8601 strings:

period, duration, _ := chrono.ParseDuration("P3Y6M4DT1M5S")
fmt.Println(chrono.FormatDuration(period, duration))

Alternatively, a Period and Duration can be initialized with numeric values:

period := chrono.Period{Years: 3, Months: 6, Days: 4}
duration := chrono.DurationOf(1*chrono.Hour + 30*chrono.Minute + 5*chrono.Second)
fmt.Println(chrono.FormatDuration(period, duration))

See more examples.

Intervals

Intervals as a concept are absent from the time package. chrono introduces the Interval type, which can be used to represent the intervening time between two time points. This can be by reference to a pair of times (start and end), a start time and a duration, a duration and an end time, or just a duration.

Parsing and formatting of intervals using the ISO 8601 notation is supported as follows, including the use of repeating intervals:

interval, _ := chrono.ParseInterval("R5/2007-03-01T13:00:00Z/P1Y2M10DT2H30M")
fmt.Println(interval.String())

See more examples.

Documentation

Overview

Package chrono is a supplementary time and date package.

Index

Examples

Constants

View Source
const (
	Nanosecond  Extent = 1
	Microsecond        = 1000 * Nanosecond
	Millisecond        = 1000 * Microsecond
	Second             = 1000 * Millisecond
	Minute             = 60 * Second
	Hour               = 60 * Minute
)

Common time-based durations relative to 1 nanosecond.

View Source
const (
	// ISO 8601.
	ISO8601                          = ISO8601DateTimeExtended
	ISO8601DateSimple                = "%Y%m%d"                                  // 20060102
	ISO8601DateExtended              = "%Y-%m-%d"                                // 2006-01-02
	ISO8601DateTruncated             = "%Y-%m"                                   // 2006-01
	ISO8601TimeSimple                = "T%H%M%S%z"                               // T030405-0700
	ISO8601TimeExtended              = "T%H:%M:%S%Ez"                            // T03:04:05-07:00
	ISO8601TimeMillisSimple          = "T%H%M%S.%3f%z"                           // T030405.000-0700
	ISO8601TimeMillisExtended        = "T%H:%M:%S.%3f%Ez"                        // T03:04:05.000-07:00
	ISO8601TimeTruncatedMinsSimple   = "T%H%M"                                   // T0304
	ISO8601TimeTruncatedMinsExtended = "T%H:%M"                                  // T03:04
	ISO8601TimeTruncatedHours        = "T%H"                                     // T03
	ISO8601DateTimeSimple            = ISO8601DateSimple + ISO8601TimeSimple     // 20060102T030405-0700
	ISO8601DateTimeExtended          = ISO8601DateExtended + ISO8601TimeExtended // 2006-01-02T03:04:05-07:00
	ISO8601WeekSimple                = "%GW%V"                                   // 2006W01
	ISO8601WeekExtended              = "%G-W%V"                                  // 2006-W01
	ISO8601WeekDaySimple             = "%GW%V%u"                                 // 2006W011
	ISO8601WeekDayExtended           = "%G-W%V-%u"                               // 2006-W01-1
	ISO8601OrdinalDateSimple         = "%Y%j"                                    // 2006002
	ISO8601OrdinalDateExtended       = "%Y-%j"                                   // 2006-002
	// Layouts defined by the time package.
	ANSIC   = "%a %b %d %H:%M:%S %Y" // Mon Jan 02 15:04:05 2006
	Kitchen = "%I:%M%p"              // 3:04PM
)

These are predefined layouts used for the parsing and formatting of dates, times and date-times. Additional layouts can be composed using the specifiers detailed below:

  • %Y: The ISO 8601 year as a decimal number, padded to 4 digits with leading 0s.
  • %EY: The year in the era as a decimal number, padded to 4 digits with leading 0s.
  • %y: The ISO 8601 year without a century as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 99. See note (1).
  • %Ey: The year in the era without a century as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 99. See notes (1) and (9).
  • %C: The century as a decimal number, padded to 2 digits with a leading 0, e.g. 19 for 1980. See note (9).
  • %EC: The name of the era, either "CE" (for Common Era) "BCE" (for Before the Common Era).
  • %j: The day of the year as a decimal number, padded to 3 digits with leading 0s, in the range 001 to 366. See note (2).
  • %m: The month as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 12.
  • %B: The full month name, e.g. January, February, etc.
  • %b: The abbreviated month name, e.g. Jan, Feb, etc.
  • %d: The day of the month as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 31.

Days of week:

  • %u: The day of the week as a decimal number, e.g. 1 for Monday, 2 for Tuesday, etc. See note (3).
  • %A: The full name of the day of the week, e.g. Monday, Tuesday, etc. See note (3).
  • %a: The abbreviated name of the day of the week, e.g. Mon, Tue, etc. See note (3).

Week numbers:

  • %G: The ISO 8601 week-based year, padded to 4 digits with leading 0s. This may differ by ±1 to the actual calendar year. See note (2).
  • %V: The ISO week number, padded to 2 digits with a leading 0, in the range 01 to 53. See note (2).

Times of day:

  • %P: Either "am" or "pm", where noon is "pm" and midnight is "am".
  • %p: Either "AM" or "PM", where noon is "PM" and midnight is "AM".
  • %I: The hour of the day using the 12-hour clock as a decimal number, padded to 2 digits with a leading 0, in the range 01 to 12. See note (4).

Time components:

  • %H: The hour of the day using the 24-hour clock as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 23. See note (5).
  • %M: The minute as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 59.
  • %S: The second as a decimal number, padded to 2 digits with a leading 0, in the range 00 to 59.

Millisecond precisions:

  • %f: Equivalent to %6f.
  • %3f: The millisecond offset within the represented second, rounded either up or down and padded to 3 digits with leading 0s.
  • %6f: The microsecond offset within the represented second, rounded either up or down and padded to 6 digits with leading 0s.
  • %9f: The nanosecond offset within the represented second, padded to 9 digits with leading 0s.

Time offsets:

  • %z: The UTC offset in the format ±HHMM, preceded always by the sign ('+' or '-'), and padded to 4 digits with leading zeros. See notes (6), (7), and (8).
  • %Ez: Equivalent to %z, except that an offset of +0000 is formatted at 'Z', and other offsets as ±HH:MM. See notes (6) and (7).

When formatting using specifiers that represent padded decimals, leading 0s can be omitted using the '-' character after the '%'. For example, '%m' may produce the string '04' (for March), but '%-m' produces '4'. However, when parsing using these specifiers, it is not required that the input string contains any leading zeros.

When parsing using specifiers that represent textual values (e.g. month names, etc.), the input text is treated case insensitively.

Depending on the context in which the layout is used, only a subset of specifiers may be supported by a particular function. For example, %H is not supported when parsing or formatting a date.

When parsing, if multiple instances of the same specifier, or multiple instances of a specifier that represent the same value, are encountered, only the last instance will be considered. See note (2).

If a specifier is encountered which is not recognized (defined in the list above), or not supported by a particular function, the function will panic with a message that includes the unrecognized sequence.

Any other text is enchoed verbatim when formatting, and is expected to appear verbatim in the parsed text. In order to print the '%' character verbatim (which normally signifies a specifier), the sequence '%%' can be used.

For familiarity, the examples below use the time package's reference time of "2nd Jan 2006 15:04:05 -0700" (Unix time 1136239445). But note that this reference format is not relevant at all to the functioning of this package.

Notes:

  1. When 2-digit years are parsed (%y or %Ey), they are converted according to the POSIX and ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68 are mapped to 2000–2068.
  2. When a date is parsed in combination with a day of year (%j), and/or an ISO week-based date (%G and/or %V), an error will be returned if the represented dates to not match.
  3. When a date is parsed in combination with a day of the week (%a, %A and/or %u), an error will be returned if it does not match the day represented by the parsed date. The day of the week is otherwise ignored - it does not have any effect on the result.
  4. When a time represented in the 12-hour clock format (%I) is parsed, and no time of day (%P or %p) is present, the time of day is assumed to be before noon, i.e. am or AM.
  5. When a time is parsed that contains the time of day (%P or %p), any hour (%H) that is present must be valid on the 12-hour clock.
  6. When UTC offsets are parsed (%z or %Ez) into a type which do not include a time offset element, the offset present in the string is ignored. When UTC offsets are formatted from a type which does not include a time offset element, the offset will not be present in the returned string.
  7. When UTC offsets are parsed (%z or %Ez), the shorted form of ±HH is accepted. However, when formatted, only the full forms are returned (either ±HHMM or ±HH:MM).
  8. When %z is used for parsing a UTC offset, 'Z' can be used to represent an offset of +0000.
  9. When parsing partial years (%Ey and %C) in combination with a full year (%Y or %EY), an error will be returned if the represented years to not match.
  10. When parsing era names (%EC), 'AD' and 'BC' are accepted in place of 'CE' and 'BCE', although only the latter are used to format.
View Source
const UTC = Offset(0)

UTC represents Universal Coordinated Time (UTC).

Variables

View Source
var ErrUnsupportedRepresentation = errors.ErrUnsupported

ErrUnsupportedRepresentation indicates that the requested value cannot be represented, or that the requested value is not present.

Functions

func FormatDuration

func FormatDuration(p Period, d Duration, exclusive ...Designator) string

FormatDuration formats a combined period and duration to a complete ISO 8601 duration.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	p := chrono.Period{Years: 3, Months: 6, Days: 4}
	d := chrono.DurationOf(1*chrono.Hour + 30*chrono.Minute + 5*chrono.Second)

	fmt.Println(chrono.FormatDuration(p, d))
}
Output:

P3Y6M4DT1H30M5S

func ParseDuration

func ParseDuration(s string) (Period, Duration, error)

ParseDuration parses a complete ISO 8601 duration.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	p, d, _ := chrono.ParseDuration("P3Y6M4DT1M5S")

	fmt.Println(p.Years, "years;", p.Months, "months;", p.Weeks, "weeks;", p.Days, "days;", d.Seconds(), "seconds")
}
Output:

3 years; 6 months; 0 weeks; 4 days; 65 seconds

Types

type Designator

type Designator rune

Designator of date and time elements present in ISO 8601.

const (
	Hours   Designator = 'H'
	Minutes Designator = 'M'
	Seconds Designator = 'S'
)

Designators.

type Duration

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

Duration represents a period of time with nanosecond precision, with a range of approximately ±292,300,000,000 years.

func DurationOf

func DurationOf(v Extent) Duration

DurationOf creates a new duration from the supplied extent. Durations and extents are semantically equivalent, except that durations exceed, and can therefore not be converted to, Go's basic types. Extents are represented as a single integer.

func MaxDuration

func MaxDuration() Duration

MaxDuration returns the maximum supported duration.

func MinDuration

func MinDuration() Duration

MinDuration returns the minimum supported duration.

func (Duration) Add

func (d Duration) Add(d2 Duration) Duration

Add returns the duration d+d2. If the operation would overflow the maximum duration, or underflow the minimum duration, it panics. Use CanAdd to test whether aa panic would occur.

func (Duration) CanAdd

func (d Duration) CanAdd(d2 Duration) bool

CanAdd returns false if Add would panic if passed the same argument.

func (Duration) Compare

func (d Duration) Compare(d2 Duration) int

Compare compares d with d2. If d is less than d2, it returns -1; if d is greater than d2, it returns 1; if they're equal, it returns 0.

func (Duration) Format

func (d Duration) Format(exclusive ...Designator) string

Format the duration according to ISO 8601. The output consists of only the time component - the period component is never included. Thus the output always consists of "PT" followed by at least one unit of the time component (hours, minutes, seconds).

The default format, obtained by calling the function with no arguments, consists of the most significant non-zero units, presented non-breaking but trimmed. 5 minutes is formatted as PT5M, trimming 0-value hours and seconds. 1 hour and 5 seconds is formatted at PT1H0M5S, ensuring the sequence of units is not broken.

A list of designators can be optionally passed to the function in order to control which units are included. When passed, only those specified units are included in the formatted string, and are present regardless of whether their value is 0.

Fractional values are automatically applied to the least significant unit, if applicable. In order to format only integers, the round functions should be used before calling this function.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.DurationOf(1*chrono.Hour + 30*chrono.Minute + 5*chrono.Second)

	fmt.Println(d.Format())
}
Output:

PT1H30M5S

func (Duration) Hours

func (d Duration) Hours() float64

Hours returns the duration as a floating point number of hours.

func (Duration) Microseconds

func (d Duration) Microseconds() float64

Microseconds returns the duration as a floating point number of microseconds.

func (Duration) Milliseconds

func (d Duration) Milliseconds() float64

Milliseconds returns the duration as a floating point number of milliseconds.

func (Duration) Minutes

func (d Duration) Minutes() float64

Minutes returns the duration as a floating point number of minutes.

func (Duration) Nanoseconds

func (d Duration) Nanoseconds() float64

Nanoseconds returns the duration as a floating point number of nanoseconds.

func (*Duration) Parse

func (d *Duration) Parse(s string) error

Parse the time portion of an ISO 8601 duration.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var d chrono.Duration
	_ = d.Parse("PT1M5S")

	fmt.Println(d.Seconds())
}
Output:

65

func (Duration) Seconds

func (d Duration) Seconds() float64

Seconds returns the duration as a floating point number of seconds.

func (Duration) String

func (d Duration) String() string

String returns a string formatted according to ISO 8601. It is equivalent to calling [Format] with no arguments.

func (Duration) Units

func (d Duration) Units() (hours, mins, secs, nsec int)

Units returns the whole numbers of hours, minutes, seconds, and nanosecond offset represented by d.

type Extent

type Extent int64

Extent represents a period of time measured in nanoseconds. The represented value is exactly equivalent to the standard library's time.Duration.

func (Extent) Format

func (e Extent) Format(exclusive ...Designator) string

Format the extent according to ISO 8601. Behaves the same as Duration.Format.

func (Extent) Hours

func (e Extent) Hours() float64

Hours returns the duration as a floating point number of hours.

func (Extent) Microseconds

func (e Extent) Microseconds() float64

Microseconds returns the duration as a floating point number of microseconds.

func (Extent) Milliseconds

func (e Extent) Milliseconds() float64

Milliseconds returns the duration as a floating point number of milliseconds.

func (Extent) Minutes

func (e Extent) Minutes() float64

Minutes returns the duration as a floating point number of minutes.

func (Extent) Nanoseconds

func (e Extent) Nanoseconds() int64

Nanoseconds returns the extent as an integer nanosecond count.

func (*Extent) Parse

func (e *Extent) Parse(s string) error

Parse the time portion of an ISO 8601 duration. Behaves the same as Duration.Parse.

func (Extent) Seconds

func (e Extent) Seconds() float64

Seconds returns the duration as a floating point number of seconds.

func (Extent) String

func (e Extent) String() string

String returns a string formatted according to ISO 8601. It is equivalent to calling Format with no arguments.

func (Extent) Truncate

func (e Extent) Truncate(m Extent) Extent

Truncate returns the result of rounding e toward zero to a multiple of m.

func (Extent) Units

func (e Extent) Units() (hours, mins, secs, nsec int)

Units returns the whole numbers of hours, minutes, seconds, and nanosecond offset represented by e.

type Instant

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

Instant represents an instantaneous point in time with nanosecond resolution.

func Now

func Now() Instant

Now returns the Instant that represents the current point in time.

func (Instant) Compare

func (i Instant) Compare(i2 Instant) int

Compare compares i with i2. If i is before i2, it returns -1; if i is after i2, it returns 1; if they're the same, it returns 0.

func (Instant) Elapsed

func (i Instant) Elapsed() Duration

Elapsed is shorthand for i.Until(chrono.Now()).

func (Instant) String

func (i Instant) String() string

func (Instant) Until

func (i Instant) Until(i2 Instant) Duration

Until returns the Duration that represents the elapsed time from i to v.

type Interval

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

Interval represents the intervening time between two time points.

func IntervalOfDurationEnd

func IntervalOfDurationEnd(period Period, duration Duration, end OffsetDateTime, repetitions int) Interval

IntervalOfDurationEnd creates an Interval from the provided duration and end time point.

func IntervalOfStartDuration

func IntervalOfStartDuration(start OffsetDateTime, period Period, duration Duration, repetitions int) Interval

IntervalOfStartDuration creates an Interval from the provided start time point and duration.

func IntervalOfStartEnd

func IntervalOfStartEnd(start, end OffsetDateTime, repetitions int) Interval

IntervalOfStartEnd creates an Interval from the provided start and end time points.

func ParseInterval

func ParseInterval(s string) (Interval, error)

ParseInterval parses an ISO 8601 time interval, or a repeating time interval. Time intervals can be expressed in the following forms:

  • <start>/<end>
  • <start>/<duration>
  • <duration>/<end>
  • <duration>

where <start> and <end> is any string that can be parsed by [Parse], and <duration> is any string that can be parsed by ParseDuration.

Repeating time intervals are expressed as such:

  • Rn/<interval>
  • R/<interval>

where <interval> is one the forms in the first list, R is the character itself, and n is a signed integer value of the number of repetitions. Additionally, the following values have special meanings:

  • 0 is no repetitions, equivalent to not including the repeat expression at all (i.e. using the forms in the first list as-is);
  • <=-1 is unbounded number of repetitions, equivalent to not specifying a value at all (i.e. 'R-1' is the same as 'R').

Additionally, '--' can be used as the separator, instead of the default '/' character.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	i, _ := chrono.ParseInterval("R5/2007-03-01T13:00:00Z/P1Y2M10DT2H30M")

	s, _ := i.Start()
	p, d, _ := i.Duration()
	e, _ := i.End()

	fmt.Printf("start: %v; duration: %v; end: %v; repetitions: %v", s, chrono.FormatDuration(p, d), e, i.Repetitions())
}
Output:

start: 2007-03-01 13:00:00Z; duration: P1Y2M10DT2H30M; end: 2008-05-11 15:30:00Z; repetitions: 5

func (Interval) Duration

func (i Interval) Duration() (Period, Duration, error)

Duration returns the Period and Duration if present, or a calculated Duration if possible by substracting i.Start() from i.End(). Note that the latter case, the Period returned will always be the zero value.

func (Interval) End

func (i Interval) End() (OffsetDateTime, error)

End returns the end time point if present, or a calculated time point if possible by adding i.Duration() to i.Start(). If neither are possible, (i.e. only a duration is present), then ErrUnsupportedRepresentation is returned instead.

func (Interval) Repetitions

func (i Interval) Repetitions() int

Repetitions returns the number of repetitions of a repeating interval. Any negative number, meaning an unbounded number of repitions, is normalized to -1.

func (Interval) Start

func (i Interval) Start() (OffsetDateTime, error)

Start returns the start time point if present, or a calculated time point if possible by subtracting i.Duration() from i.End(). If neither are possible (i.e. only a duration is present), ErrUnsupportedRepresentation is returned instead.

func (Interval) String

func (i Interval) String() string

String returns the formatted Interval that can be parsed by i.Parse().

type LocalDate

type LocalDate int32

LocalDate is a date without a time zone or time component, according to ISO 8601. It represents a year-month-day in the proleptic Gregorian calendar, but cannot represent an instant on a timeline without additional time offset information.

The date is encoded as a Julian Day Number (JDN). Since LocalDate is stored as an integer, any two LocalDates can be compared to each other to determine their relationship, and the difference can be calculated to determine the number of days between them. Additionally, standard addition and subtraction operators can be used to shift a LocalDate by a number of days.

To make usage of LocalDate easier, the default value, 0, represents the date of the Unix epoch, 1st January 1970. This differs from the Richards interpretation of JDNs, where 0 represents the date 24th November 4714 BCE (24/11/-4713).

According to ISO 8601, 0000 is a valid year, whereas in the Gregorian calendar, year 0 does not exist. The user must be aware of this difference when interfacing between LocalDate and the BCE/CE notation. Thus, when using LocalDate, year 0 is intepreted to mean 1 BCE, and year -1 is 2 BCE, and so on. In order to format a string according to the Gregorian calendar, use Format("%EY %EC").

Example (Add_subtract)
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)

	d += 8
	d -= 3

	fmt.Println(d)
}
Output:

2007-05-25
Example (Compare)
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d1 := chrono.LocalDateOf(2007, chrono.May, 20)
	d2 := chrono.LocalDateOf(2009, chrono.June, 5)

	if d2 > d1 {
		fmt.Println(d2, "is after", d1)
	}
}
Output:

2009-06-05 is after 2007-05-20
Example (Difference)
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d1 := chrono.LocalDateOf(2007, chrono.May, 20)
	d2 := chrono.LocalDateOf(2007, chrono.May, 25)

	fmt.Printf("There are %d days from %s to %s\n", d2-d1, d1, d2)
}
Output:

There are 5 days from 2007-05-20 to 2007-05-25

func LocalDateOf

func LocalDateOf(year int, month Month, day int) LocalDate

LocalDateOf returns the LocalDate that represents the specified year, month and day. This function panics if the provided date would overflow the internal type, or if it is earlier than the first date that can be represented by this type - 24th November -4713 (4714 BCE).

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)

	fmt.Println(d)
}
Output:

2007-05-20

func MaxLocalDate

func MaxLocalDate() LocalDate

MaxLocalDate returns the latest supported date.

func MinLocalDate

func MinLocalDate() LocalDate

MinLocalDate returns the earliest supported date.

func OfDayOfYear

func OfDayOfYear(year, day int) LocalDate

OfDayOfYear returns the LocalDate that represents the specified day of the year. This function panics if the provided date would overflow the internal type, or if it is earlier than the first date that can be represented by this type - 24th November -4713 (4714 BCE).

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.OfDayOfYear(2020, 80)

	fmt.Println("The 80th day of 2020 is", d)
}
Output:

The 80th day of 2020 is 2020-03-20

func OfFirstWeekday

func OfFirstWeekday(year int, month Month, weekday Weekday) LocalDate

OfFirstWeekday returns the LocalDate that represents the first of the specified weekday of the supplied month and year. This function panics if the provided date would overflow the internal type, or if it earlier than the first date that can be represented by this type - 24th November -4713 (4714 BCE).

By providing January as the month, the result is therefore also the first specified weekday of the year. And by adding increments of 7 to the result, it is therefore possible to find the nth instance of a particular weekday.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.OfFirstWeekday(2020, chrono.July, chrono.Friday)

	fmt.Println("The first Friday of July 2020 is", d)
}
Output:

The first Friday of July 2020 is 2020-07-03

func OfISOWeek

func OfISOWeek(year, week int, day Weekday) (LocalDate, error)

OfISOWeek returns the LocalDate that represents the supplied ISO 8601 year, week number, and weekday. See LocalDate.ISOWeek for further explanation of ISO week numbers.

func (LocalDate) AddDate

func (d LocalDate) AddDate(years, months, days int) LocalDate

AddDate returns the date corresponding to adding the given number of years, months, and days to d.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)
	d = d.AddDate(0, 1, 1)

	fmt.Println(d)
}
Output:

2007-06-21

func (LocalDate) CanAddDate

func (d LocalDate) CanAddDate(years, months, days int) bool

CanAddDate returns false if AddDate would panic if passed the same arguments.

func (LocalDate) Date

func (d LocalDate) Date() (year int, month Month, day int)

Date returns the ISO 8601 year, month and day represented by d.

func (LocalDate) Format

func (d LocalDate) Format(layout string) string

Format returns a textual representation of the date value formatted according to the layout defined by the argument. See the constants section of the documentation to see how to represent the layout format. Time format specifiers encountered in the layout results in a panic.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)

	fmt.Println(d.Format(chrono.ISO8601DateExtended))
}
Output:

2007-05-20

func (LocalDate) ISOWeek

func (d LocalDate) ISOWeek() (isoYear, isoWeek int)

ISOWeek returns the ISO 8601 year and week number in which d occurs. Week ranges from 1 to 53 (even for years that are not themselves leap years). Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1 of year n+1.

func (LocalDate) IsLeapYear

func (d LocalDate) IsLeapYear() bool

IsLeapYear reports whether d is a leap year (contains 29th February, and thus 266 days instead of 265).

func (*LocalDate) Parse

func (d *LocalDate) Parse(layout, value string) error

Parse a formatted string and store the value it represents in d. See the constants section of the documentation to see how to represent the layout format. Time format specifiers encountered in the layout results in a panic.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var d chrono.LocalDate
	_ = d.Parse(chrono.ISO8601DateExtended, "2007-05-20")

	fmt.Println(d)
}
Output:

2007-05-20

func (LocalDate) String

func (d LocalDate) String() string

func (LocalDate) Weekday

func (d LocalDate) Weekday() Weekday

Weekday returns the day of the week specified by d.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)

	fmt.Println(d.Weekday())
}
Output:

Sunday

func (LocalDate) YearDay

func (d LocalDate) YearDay() int

YearDay returns the day of the year specified by d, in the range [1,365] for non-leap years, and [1,366] in leap years.

type LocalDateTime

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

LocalDateTime is a date and time without a time zone or time component. This is a combination of a LocalDate and LocalTime.

func LocalDateTimeOf

func LocalDateTimeOf(year int, month Month, day, hour, min, sec, nsec int) LocalDateTime

LocalDateTimeOf returns the LocalDateTime that stores the specified year, month, day, hour, minute, second, and nanosecond offset within the specified second. The same range of values as supported by OfLocalDate and OfLocalTime are allowed here.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0)

	fmt.Println(dt)
}
Output:

2007-05-20 12:30:15

func MaxLocalDateTime

func MaxLocalDateTime() LocalDateTime

MaxLocalDateTime returns the latest supported datetime.

func MinLocalDateTime

func MinLocalDateTime() LocalDateTime

MinLocalDateTime returns the earliest supported datetime.

func OfLocalDateTime

func OfLocalDateTime(date LocalDate, time LocalTime) LocalDateTime

OfLocalDateTime combines the supplied LocalDate and LocalTime into a single LocalDateTime.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)
	t := chrono.LocalTimeOf(12, 30, 15, 0)

	dt := chrono.OfLocalDateTime(d, t)

	fmt.Println(dt)
}
Output:

2007-05-20 12:30:15

func Unix

func Unix(secs, nsecs int64) LocalDateTime

Unix returns the LocalDateTime that is represented by the supplied Unix time (seconds and/or nanoseconds elapsed since 1st January 1970). nsecs may be outside of the range [0, 999999999].

func UnixMicro

func UnixMicro(usecs int64) LocalDateTime

UnixMicro returns the LocalDateTime represented by the supplied Unix time (microseconds elapsed since 1st January 1970).

func UnixMilli

func UnixMilli(msecs int64) LocalDateTime

UnixMilli returns the LocalDateTime represented by the supplied Unix time (milliseconds elapsed since 1st January 1970).

func (LocalDateTime) Add

Add returns the datetime d+v. This function panics if the resulting datetime would fall outside of the allowed range.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0)

	fmt.Println(dt.Add(chrono.DurationOf(26 * chrono.Hour)))
}
Output:

2007-05-21 14:30:15

func (LocalDateTime) AddDate

func (d LocalDateTime) AddDate(years, months, days int) LocalDateTime

AddDate returns the datetime corresponding to adding the given number of years, months, and days to d. This function panic if the resulting datetime would fall outside of the allowed date range.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0)

	fmt.Println(dt.AddDate(2, 6, 8))
}
Output:

2009-11-28 12:30:15

func (LocalDateTime) CanAdd

func (d LocalDateTime) CanAdd(v Duration) bool

CanAdd returns false if Add would panic if passed the same arguments.

func (LocalDateTime) CanAddDate

func (d LocalDateTime) CanAddDate(years, months, days int) bool

CanAddDate returns false if AddDate would panic if passed the same arguments.

func (LocalDateTime) Compare

func (d LocalDateTime) Compare(d2 LocalDateTime) int

Compare compares d with d2. If d is before d2, it returns -1; if d is after d2, it returns 1; if they're the same, it returns 0.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt1 := chrono.LocalDateTimeOf(2007, chrono.May, 26, 12, 30, 15, 0)
	dt2 := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0)

	if dt2.Compare(dt1) == -1 {
		fmt.Println(dt2, "is before", dt1)
	}
}
Output:

2007-05-20 12:30:15 is before 2007-05-26 12:30:15

func (LocalDateTime) Format

func (d LocalDateTime) Format(layout string) string

Format returns a textual representation of the date-time value formatted according to the layout defined by the argument. See the constants section of the documentation to see how to represent the layout format.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0)

	fmt.Println(dt.Format(chrono.ISO8601DateTimeExtended))
}
Output:

2007-05-20T12:30:15

func (LocalDateTime) In

func (d LocalDateTime) In(offset Offset) OffsetDateTime

In returns the OffsetDateTime represeting d with the specified offset.

func (*LocalDateTime) Parse

func (d *LocalDateTime) Parse(layout, value string) error

Parse a formatted string and store the value it represents in d. See the constants section of the documentation to see how to represent the layout format.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var dt chrono.LocalDateTime
	_ = dt.Parse(chrono.ISO8601DateTimeExtended, "2007-05-20T12:30:15")

	fmt.Println(dt)
}
Output:

2007-05-20 12:30:15

func (LocalDateTime) Split

func (d LocalDateTime) Split() (LocalDate, LocalTime)

Split returns separate a LocalDate and LocalTime that together represent d.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.LocalDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0)
	d, t := dt.Split()

	fmt.Printf("date = %s, time = %s", d, t)
}
Output:

date = 2007-05-20, time = 12:30:15

func (LocalDateTime) String

func (d LocalDateTime) String() string

func (LocalDateTime) Sub

Sub returns the duration d-u.

func (LocalDateTime) UTC

func (d LocalDateTime) UTC() OffsetDateTime

UTC returns the OffsetDateTime represeting d at the UTC offset.

func (LocalDateTime) Unix

func (d LocalDateTime) Unix() int64

Unix returns the Unix time (seconds elapsed since 1st January 1970).

func (LocalDateTime) UnixMicro

func (d LocalDateTime) UnixMicro() int64

UnixMicro returns the Unix time (microseconds elapsed since 1st January 1970).

func (LocalDateTime) UnixMilli

func (d LocalDateTime) UnixMilli() int64

UnixMilli returns the Unix time (milliseconds elapsed since 1st January 1970).

func (LocalDateTime) UnixNano

func (d LocalDateTime) UnixNano() int64

UnixNano returns the Unix time (nanoseconds elapsed since 1st January 1970).

type LocalTime

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

LocalTime is a time without a time zone or date component. It represents a time within the 24-hour clock system with nanosecond precision, according to ISO 8601.

Additional flexibility is provided whereby times after 23:59:59.999999999 are also considered valid. This feature supports various usecases where times such as 25:00 (instead of 01:00) represent business hours that extend beyond midnight. LocalTime supports a maximum hour of 99.

func LocalTimeOf

func LocalTimeOf(hour, min, sec, nsec int) LocalTime

LocalTimeOf returns a LocalTime that represents the specified hour, minute, second, and nanosecond offset within the specified second. A valid time is between 00:00:00 and 99:59:59.999999999. If an invalid time is specified, this function panics.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.LocalTimeOf(12, 30, 15, 0)

	fmt.Println(t)
}
Output:

12:30:15

func (LocalTime) Add

func (t LocalTime) Add(v Extent) LocalTime

Add returns the time t+v. If the result exceeds the maximum representable time, this function panics. If the result would be earlier than 00:00:00, the returned time is moved to the previous day, e.g. 01:00:00 - 2 hours = 23:00:00.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.LocalTimeOf(12, 30, 0, 0)

	fmt.Println(t.Add(4 * chrono.Hour))
}
Output:

16:30:00

func (LocalTime) BusinessHour

func (t LocalTime) BusinessHour() int

BusinessHour returns the hour specified by t. If the hour is greater than 23, that hour is returned without normalization.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.LocalTimeOf(24, 15, 0, 0)

	fmt.Println(t.BusinessHour())
}
Output:

24

func (LocalTime) CanAdd

func (t LocalTime) CanAdd(v Extent) bool

CanAdd returns false if Add would panic if passed the same argument.

func (LocalTime) Clock

func (t LocalTime) Clock() (hour, min, sec int)

Clock returns the hour, minute and second represented by t. If hour is greater than 23, the returned value is normalized so as to fit within the 24-hour clock as specified by ISO 8601, e.g. 25 is returned as 01.

func (LocalTime) Compare

func (t LocalTime) Compare(t2 LocalTime) int

Compare compares t with t2. If t is before t2, it returns -1; if t is after t2, it returns 1; if they're the same, it returns 0.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t1 := chrono.LocalTimeOf(12, 30, 0, 0)
	t2 := chrono.LocalTimeOf(12, 15, 0, 0)

	if t2.Compare(t1) == -1 {
		fmt.Println(t2, "is before", t1)
	}
}
Output:

12:15:00 is before 12:30:00

func (LocalTime) Format

func (t LocalTime) Format(layout string) string

Format returns a textual representation of the time value formatted according to the layout defined by the argument. See the constants section of the documentation to see how to represent the layout format. Date format specifiers encountered in the layout results in a panic.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.LocalTimeOf(12, 30, 15, 0)

	fmt.Println(t.Format(chrono.ISO8601TimeExtended))
}
Output:

T12:30:15

func (LocalTime) In

func (t LocalTime) In(offset Offset) OffsetTime

In returns the OffsetTime represeting t with the specified offset.

func (LocalTime) Nanosecond

func (t LocalTime) Nanosecond() int

Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].

func (*LocalTime) Parse

func (t *LocalTime) Parse(layout, value string) error

Parse a formatted string and store the value it represents in t. See the constants section of the documentation to see how to represent the layout format. Date format specifiers encountered in the layout results in a panic.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var t chrono.LocalTime
	t.Parse(chrono.ISO8601TimeExtended, "T12:30:15")

	fmt.Println(t)
}
Output:

12:30:15

func (LocalTime) String

func (t LocalTime) String() string

func (LocalTime) Sub

func (t LocalTime) Sub(u LocalTime) Extent

Sub returns the duration t-u.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t1 := chrono.LocalTimeOf(12, 30, 0, 0)
	t2 := chrono.LocalTimeOf(12, 15, 0, 0)

	fmt.Println(t1.Sub(t2))
}
Output:

PT15M

func (LocalTime) UTC

func (t LocalTime) UTC() OffsetTime

UTC returns the OffsetTime represeting t at the UTC offset.

type Month

type Month int

Month specifies the month of the year (January = 1, ...).

const (
	January Month = iota + 1
	February
	March
	April
	May
	June
	July
	August
	September
	October
	November
	December
)

The months of the year.

func (Month) String

func (m Month) String() string

type Offset

type Offset Extent

Offset represents a time zone offset from UTC with precision to the minute.

func OffsetOf

func OffsetOf(hours, mins int) Offset

OffsetOf returns the Offset represented by a number of hours and minutes. If hours is non-zero, the sign of minutes is ignored, e.g.:

  • OffsetOf(-2, 30) = -02h:30m
  • OffsetOf(2, -30) = 02h:30m
  • OffsetOf(0, 30) = 00h:30m
  • OffsetOf(0, -30) = -00h:30m

func (Offset) String

func (o Offset) String() string

String returns the time zone designator according to ISO 8601, truncating first to the minute. If o == 0, String returns "Z" for the UTC offset. In all other cases, a string in the format of ±hh:mm is returned. Note that the sign and number of minutes is always included, even if 0.

type OffsetDateTime

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

OffsetDateTime has the same semantics as LocalDateTime, but with the addition of a timezone offset.

func OfLocalDateOffsetTime

func OfLocalDateOffsetTime(date LocalDate, time OffsetTime) OffsetDateTime

OfLocalDateOffsetTime combines a LocalDate and OffsetTime into an OffsetDateTime.

func OfLocalDateTimeOffset

func OfLocalDateTimeOffset(date LocalDate, time LocalTime, offset Extent) OffsetDateTime

OfLocalDateTimeOffset combines a LocalDate, LocalTime, and Offset into an OffsetDateTime.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	d := chrono.LocalDateOf(2007, chrono.May, 20)
	t := chrono.LocalTimeOf(12, 30, 15, 0)

	dt := chrono.OfLocalDateTimeOffset(d, t, 2*chrono.Hour+30*chrono.Minute)

	fmt.Println(dt)
}
Output:

2007-05-20 12:30:15+02:30

func OffsetDateTimeOf

func OffsetDateTimeOf(year int, month Month, day, hour, min, sec, nsec, offsetHours, offsetMins int) OffsetDateTime

OffsetDateTimeOf returns an OffsetDateTime that represents the specified year, month, day, hour, minute, second, and nanosecond offset within the specified second. The supplied offset is applied to the returned OffsetDateTime in the same manner as OffsetOf. The same range of values as supported by OfLocalDate and OfLocalTime are allowed here.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30)

	fmt.Println(dt)
}
Output:

2007-05-20 12:30:15+02:30

func (OffsetDateTime) Add

Add returns the datetime d+v. This function panics if the resulting datetime would fall outside of the allowed range.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30)

	fmt.Println(dt.Add(chrono.DurationOf(26 * chrono.Hour)))
}
Output:

2007-05-21 14:30:15+02:30

func (OffsetDateTime) AddDate

func (d OffsetDateTime) AddDate(years, months, days int) OffsetDateTime

AddDate returns the datetime corresponding to adding the given number of years, months, and days to d. This function panic if the resulting datetime would fall outside of the allowed date range.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30)

	fmt.Println(dt.AddDate(2, 6, 8))
}
Output:

2009-11-28 12:30:15+02:30

func (OffsetDateTime) CanAdd

func (d OffsetDateTime) CanAdd(v Duration) bool

CanAdd returns false if Add would panic if passed the same arguments.

func (OffsetDateTime) CanAddDate

func (d OffsetDateTime) CanAddDate(years, months, days int) bool

CanAddDate returns false if AddDate would panic if passed the same arguments.

func (OffsetDateTime) Compare

func (d OffsetDateTime) Compare(d2 OffsetDateTime) int

Compare compares d with d2. If d is before d2, it returns -1; if d is after d2, it returns 1; if they're the same, it returns 0.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt1 := chrono.OffsetDateTimeOf(2007, chrono.May, 26, 12, 30, 15, 0, 2, 30)
	dt2 := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30)

	if dt2.Compare(dt1) == -1 {
		fmt.Println(dt2, "is before", dt1)
	}
}
Output:

2007-05-20 12:30:15+02:30 is before 2007-05-26 12:30:15+02:30

func (OffsetDateTime) Format

func (d OffsetDateTime) Format(layout string) string

Format returns a textual representation of the date-time value formatted according to the layout defined by the argument. See the constants section of the documentation to see how to represent the layout format.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30)

	fmt.Println(dt.Format(chrono.ISO8601DateTimeExtended))
}
Output:

2007-05-20T12:30:15+02:30

func (OffsetDateTime) In

func (d OffsetDateTime) In(offset Offset) OffsetDateTime

In returns a copy of t, adjusted to the supplied offset.

func (OffsetDateTime) Local

func (d OffsetDateTime) Local() LocalDateTime

Local returns the LocalDateTime represented by d.

func (OffsetDateTime) Offset

func (d OffsetDateTime) Offset() Offset

Offset returns the offset of d.

func (*OffsetDateTime) Parse

func (d *OffsetDateTime) Parse(layout, value string) error

Parse a formatted string and store the value it represents in d. See the constants section of the documentation to see how to represent the layout format.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var dt chrono.OffsetDateTime
	_ = dt.Parse(chrono.ISO8601DateTimeExtended, "2007-05-20T12:30:15+02:30")

	fmt.Println(dt)
}
Output:

2007-05-20 12:30:15+02:30

func (OffsetDateTime) Split

func (d OffsetDateTime) Split() (LocalDate, OffsetTime)

Split returns separate a LocalDate and OffsetTime that together represent d.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	dt := chrono.OffsetDateTimeOf(2007, chrono.May, 20, 12, 30, 15, 0, 2, 30)
	d, t := dt.Split()

	fmt.Printf("date = %s, time = %s", d, t)
}
Output:

date = 2007-05-20, time = 12:30:15+02:30

func (OffsetDateTime) String

func (d OffsetDateTime) String() string

func (OffsetDateTime) Sub

Sub returns the duration d-u.

func (OffsetDateTime) UTC

UTC is a shortcut for t.In(UTC).

type OffsetTime

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

OffsetTime has the same semantics as LocalTime, but with the addition of a timezone offset.

func OfTimeOffset

func OfTimeOffset(time LocalTime, offset Offset) OffsetTime

OfTimeOffset combines a LocalTime and Offset into an OffsetTime.

func OffsetTimeOf

func OffsetTimeOf(hour, min, sec, nsec, offsetHours, offsetMins int) OffsetTime

OffsetTimeOf returns an OffsetTime that represents the specified hour, minute, second, and nanosecond offset within the specified second. The supplied offset is applied to the returned OffsetTime in the same manner as OffsetOf. A valid time is between 00:00:00 and 99:59:59.999999999. If an invalid time is specified, this function panics.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.OffsetTimeOf(12, 30, 15, 0, 2, 0)

	fmt.Println(t)
}
Output:

12:30:15+02:00

func (OffsetTime) Add

func (t OffsetTime) Add(v Extent) OffsetTime

Add returns the time t+v, maintaining the offset of t. If the result exceeds the maximum representable time, this function panics. If the result would be earlier than 00:00:00, the returned time is moved to the previous day, e.g. 01:00:00 - 2 hours = 23:00:00.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.OffsetTimeOf(12, 30, 0, 0, 2, 0)

	fmt.Println(t.Add(4 * chrono.Hour))
}
Output:

16:30:00+02:00

func (OffsetTime) BusinessHour

func (t OffsetTime) BusinessHour() int

BusinessHour returns the hour specified by t. If the hour is greater than 23, that hour is returned without normalization.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.OffsetTimeOf(24, 15, 0, 0, 2, 0)

	fmt.Println(t.BusinessHour())
}
Output:

24

func (OffsetTime) CanAdd

func (t OffsetTime) CanAdd(v Extent) bool

CanAdd returns false if Add would panic if passed the same argument.

func (OffsetTime) Clock

func (t OffsetTime) Clock() (hour, min, sec int)

Clock returns the hour, minute and second represented by t. If hour is greater than 23, the returned value is normalized so as to fit within the 24-hour clock as specified by ISO 8601, e.g. 25 is returned as 01.

func (OffsetTime) Compare

func (t OffsetTime) Compare(t2 OffsetTime) int

Compare compares t with t2. If t is before t2, it returns -1; if t is after t2, it returns 1; if they're the same, it returns 0.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t1 := chrono.OffsetTimeOf(12, 30, 0, 0, 1, 0)
	t2 := chrono.OffsetTimeOf(12, 30, 0, 0, 2, 0)

	if t2.Compare(t1) == -1 {
		fmt.Println(t2, "is before", t1)
	}
}
Output:

12:30:00+02:00 is before 12:30:00+01:00

func (OffsetTime) Format

func (t OffsetTime) Format(layout string) string

Format returns a textual representation of the time value formatted according to the layout defined by the argument. See the constants section of the documentation to see how to represent the layout format. Date format specifiers encountered in the layout results in a panic.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t := chrono.OffsetTimeOf(12, 30, 15, 0, 2, 30)

	fmt.Println(t.Format(chrono.ISO8601TimeExtended))
}
Output:

T12:30:15+02:30

func (OffsetTime) In

func (t OffsetTime) In(offset Offset) OffsetTime

In returns a copy of t, adjusted to the supplied offset.

func (OffsetTime) Local

func (t OffsetTime) Local() LocalTime

Local returns the LocalTime represented by t.

func (OffsetTime) Nanosecond

func (t OffsetTime) Nanosecond() int

Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].

func (OffsetTime) Offset

func (t OffsetTime) Offset() Offset

Offset returns the offset of t.

func (*OffsetTime) Parse

func (t *OffsetTime) Parse(layout, value string) error

Parse a formatted string and store the value it represents in t. See the constants section of the documentation to see how to represent the layout format. Date format specifiers encountered in the layout results in a panic.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var t chrono.OffsetTime
	t.Parse(chrono.ISO8601TimeExtended, "T12:30:15+02:30")

	fmt.Println(t)
}
Output:

12:30:15+02:30

func (OffsetTime) String

func (t OffsetTime) String() string

func (OffsetTime) Sub

func (t OffsetTime) Sub(u OffsetTime) Extent

Sub returns the duration t-u.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	t1 := chrono.OffsetTimeOf(12, 30, 0, 0, 1, 0)
	t2 := chrono.OffsetTimeOf(12, 15, 0, 0, 2, 0)

	fmt.Println(t1.Sub(t2))
}
Output:

PT1H15M

func (OffsetTime) UTC

func (t OffsetTime) UTC() OffsetTime

UTC is a shortcut for t.In(UTC).

type Period

type Period struct {
	Years  float32
	Months float32
	Weeks  float32
	Days   float32
}

Period represents an amount of time in years, months, weeks and days. A period is not a measurable quantity since the lengths of these components is ambiguous.

func (Period) Equal

func (p Period) Equal(p2 Period) bool

Equal reports whether p and p2 represent the same period of time.

func (*Period) Parse

func (p *Period) Parse(s string) error

Parse the period portion of an ISO 8601 duration. This function supports the ISO 8601-2 extension, which allows weeks (W) to appear in combination with years, months, and days, such as P3W1D. Additionally, it allows a sign character to appear at the start of string, such as +P1M, or -P1M.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	var p chrono.Period
	_ = p.Parse("P3Y6M4D")

	fmt.Println(p.Years, "years;", p.Months, "months;", p.Weeks, "weeks;", p.Days, "days")
}
Output:

3 years; 6 months; 0 weeks; 4 days

func (Period) String

func (p Period) String() string

String returns a string formatted according to ISO 8601. The output consists of only the period component - the time component is never included.

Example
package main

import (
	"fmt"

	"github.com/go-chrono/chrono"
)

func main() {
	p := chrono.Period{Years: 3, Months: 6, Days: 4}

	fmt.Println(p.String())
}
Output:

P3Y6M4D

type Weekday

type Weekday int

Weekday specifies the day of the week (Monday = 0, ...). Not compatible standard library's time.Weekday (in which Sunday = 0, ...).

const (
	Monday Weekday = iota + 1
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
	Sunday
)

The days of the week.

func (Weekday) String

func (d Weekday) String() string

Directories

Path Synopsis
Package chronotest provides functionality useful for testing chrono.
Package chronotest provides functionality useful for testing chrono.

Jump to

Keyboard shortcuts

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