Documentation
¶
Overview ¶
Package chrono is a supplementary time and date package.
Index ¶
- Constants
- Variables
- func FormatDuration(p Period, d Duration, exclusive ...Designator) string
- func ParseDuration(s string) (Period, Duration, error)
- type Designator
- type Duration
- func (d Duration) Add(d2 Duration) Duration
- func (d Duration) CanAdd(d2 Duration) bool
- func (d Duration) Compare(d2 Duration) int
- func (d Duration) Format(exclusive ...Designator) string
- func (d Duration) Hours() float64
- func (d Duration) Microseconds() float64
- func (d Duration) Milliseconds() float64
- func (d Duration) Minutes() float64
- func (d Duration) Nanoseconds() float64
- func (d *Duration) Parse(s string) error
- func (d Duration) Seconds() float64
- func (d Duration) String() string
- func (d Duration) Units() (hours, mins, secs, nsec int)
- type Extent
- func (e Extent) Format(exclusive ...Designator) string
- func (e Extent) Hours() float64
- func (e Extent) Microseconds() float64
- func (e Extent) Milliseconds() float64
- func (e Extent) Minutes() float64
- func (e Extent) Nanoseconds() int64
- func (e *Extent) Parse(s string) error
- func (e Extent) Seconds() float64
- func (e Extent) String() string
- func (e Extent) Truncate(m Extent) Extent
- func (e Extent) Units() (hours, mins, secs, nsec int)
- type Instant
- type Interval
- func IntervalOfDurationEnd(period Period, duration Duration, end OffsetDateTime, repetitions int) Interval
- func IntervalOfStartDuration(start OffsetDateTime, period Period, duration Duration, repetitions int) Interval
- func IntervalOfStartEnd(start, end OffsetDateTime, repetitions int) Interval
- func ParseInterval(s string) (Interval, error)
- type LocalDate
- func (d LocalDate) AddDate(years, months, days int) LocalDate
- func (d LocalDate) CanAddDate(years, months, days int) bool
- func (d LocalDate) Date() (year int, month Month, day int)
- func (d LocalDate) Format(layout string) string
- func (d LocalDate) ISOWeek() (isoYear, isoWeek int)
- func (d LocalDate) IsLeapYear() bool
- func (d *LocalDate) Parse(layout, value string) error
- func (d LocalDate) String() string
- func (d LocalDate) Weekday() Weekday
- func (d LocalDate) YearDay() int
- type LocalDateTime
- func LocalDateTimeOf(year int, month Month, day, hour, min, sec, nsec int) LocalDateTime
- func MaxLocalDateTime() LocalDateTime
- func MinLocalDateTime() LocalDateTime
- func OfLocalDateTime(date LocalDate, time LocalTime) LocalDateTime
- func Unix(secs, nsecs int64) LocalDateTime
- func UnixMicro(usecs int64) LocalDateTime
- func UnixMilli(msecs int64) LocalDateTime
- func (d LocalDateTime) Add(v Duration) LocalDateTime
- func (d LocalDateTime) AddDate(years, months, days int) LocalDateTime
- func (d LocalDateTime) CanAdd(v Duration) bool
- func (d LocalDateTime) CanAddDate(years, months, days int) bool
- func (d LocalDateTime) Compare(d2 LocalDateTime) int
- func (d LocalDateTime) Format(layout string) string
- func (d LocalDateTime) In(offset Offset) OffsetDateTime
- func (d *LocalDateTime) Parse(layout, value string) error
- func (d LocalDateTime) Split() (LocalDate, LocalTime)
- func (d LocalDateTime) String() string
- func (d LocalDateTime) Sub(u LocalDateTime) Duration
- func (d LocalDateTime) UTC() OffsetDateTime
- func (d LocalDateTime) Unix() int64
- func (d LocalDateTime) UnixMicro() int64
- func (d LocalDateTime) UnixMilli() int64
- func (d LocalDateTime) UnixNano() int64
- type LocalTime
- func (t LocalTime) Add(v Extent) LocalTime
- func (t LocalTime) BusinessHour() int
- func (t LocalTime) CanAdd(v Extent) bool
- func (t LocalTime) Clock() (hour, min, sec int)
- func (t LocalTime) Compare(t2 LocalTime) int
- func (t LocalTime) Format(layout string) string
- func (t LocalTime) In(offset Offset) OffsetTime
- func (t LocalTime) Nanosecond() int
- func (t *LocalTime) Parse(layout, value string) error
- func (t LocalTime) String() string
- func (t LocalTime) Sub(u LocalTime) Extent
- func (t LocalTime) UTC() OffsetTime
- type Month
- type Offset
- type OffsetDateTime
- func (d OffsetDateTime) Add(v Duration) OffsetDateTime
- func (d OffsetDateTime) AddDate(years, months, days int) OffsetDateTime
- func (d OffsetDateTime) CanAdd(v Duration) bool
- func (d OffsetDateTime) CanAddDate(years, months, days int) bool
- func (d OffsetDateTime) Compare(d2 OffsetDateTime) int
- func (d OffsetDateTime) Format(layout string) string
- func (d OffsetDateTime) In(offset Offset) OffsetDateTime
- func (d OffsetDateTime) Local() LocalDateTime
- func (d OffsetDateTime) Offset() Offset
- func (d *OffsetDateTime) Parse(layout, value string) error
- func (d OffsetDateTime) Split() (LocalDate, OffsetTime)
- func (d OffsetDateTime) String() string
- func (d OffsetDateTime) Sub(u OffsetDateTime) Duration
- func (d OffsetDateTime) UTC() OffsetDateTime
- type OffsetTime
- func (t OffsetTime) Add(v Extent) OffsetTime
- func (t OffsetTime) BusinessHour() int
- func (t OffsetTime) CanAdd(v Extent) bool
- func (t OffsetTime) Clock() (hour, min, sec int)
- func (t OffsetTime) Compare(t2 OffsetTime) int
- func (t OffsetTime) Format(layout string) string
- func (t OffsetTime) In(offset Offset) OffsetTime
- func (t OffsetTime) Local() LocalTime
- func (t OffsetTime) Nanosecond() int
- func (t OffsetTime) Offset() Offset
- func (t *OffsetTime) Parse(layout, value string) error
- func (t OffsetTime) String() string
- func (t OffsetTime) Sub(u OffsetTime) Extent
- func (t OffsetTime) UTC() OffsetTime
- type Period
- type Weekday
Examples ¶
- Duration.Format
- Duration.Parse
- FormatDuration
- LocalDate (Add_subtract)
- LocalDate (Compare)
- LocalDate (Difference)
- LocalDate.AddDate
- LocalDate.Format
- LocalDate.Parse
- LocalDate.Weekday
- LocalDateOf
- LocalDateTime.Add
- LocalDateTime.AddDate
- LocalDateTime.Compare
- LocalDateTime.Format
- LocalDateTime.Parse
- LocalDateTime.Split
- LocalDateTimeOf
- LocalTime.Add
- LocalTime.BusinessHour
- LocalTime.Compare
- LocalTime.Format
- LocalTime.Parse
- LocalTime.Sub
- LocalTimeOf
- OfDayOfYear
- OfFirstWeekday
- OfLocalDateTime
- OfLocalDateTimeOffset
- OffsetDateTime.Add
- OffsetDateTime.AddDate
- OffsetDateTime.Compare
- OffsetDateTime.Format
- OffsetDateTime.Parse
- OffsetDateTime.Split
- OffsetDateTimeOf
- OffsetTime.Add
- OffsetTime.BusinessHour
- OffsetTime.Compare
- OffsetTime.Format
- OffsetTime.Parse
- OffsetTime.Sub
- OffsetTimeOf
- ParseDuration
- ParseInterval
- Period.Parse
- Period.String
Constants ¶
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.
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:
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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).
- When %z is used for parsing a UTC offset, 'Z' can be used to represent an offset of +0000.
- 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.
- When parsing era names (%EC), 'AD' and 'BC' are accepted in place of 'CE' and 'BCE', although only the latter are used to format.
const UTC = Offset(0)
UTC represents Universal Coordinated Time (UTC).
Variables ¶
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 ¶
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 ¶
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 (Duration) Add ¶
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) Compare ¶
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) Microseconds ¶
Microseconds returns the duration as a floating point number of microseconds.
func (Duration) Milliseconds ¶
Milliseconds returns the duration as a floating point number of milliseconds.
func (Duration) Nanoseconds ¶
Nanoseconds returns the duration as a floating point number of nanoseconds.
func (*Duration) Parse ¶
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
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) Microseconds ¶
Microseconds returns the duration as a floating point number of microseconds.
func (Extent) Milliseconds ¶
Milliseconds returns the duration as a floating point number of milliseconds.
func (Extent) Nanoseconds ¶
Nanoseconds returns the extent as an integer nanosecond count.
func (*Extent) Parse ¶
Parse the time portion of an ISO 8601 duration. Behaves the same as Duration.Parse.
func (Extent) String ¶
String returns a string formatted according to ISO 8601. It is equivalent to calling Format with no arguments.
type Instant ¶
type Instant struct {
// contains filtered or unexported fields
}
Instant represents an instantaneous point in time with nanosecond resolution.
func (Instant) Compare ¶
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.
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 ¶
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 ¶
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 ¶
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.
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 ¶
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 OfDayOfYear ¶
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 ¶
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 ¶
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 ¶
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 ¶
CanAddDate returns false if AddDate would panic if passed the same arguments.
func (LocalDate) Format ¶
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 ¶
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 ¶
IsLeapYear reports whether d is a leap year (contains 29th February, and thus 266 days instead of 265).
func (*LocalDate) Parse ¶
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
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 ¶
func (d LocalDateTime) Add(v Duration) LocalDateTime
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 ¶
func (d LocalDateTime) Sub(u LocalDateTime) Duration
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 ¶
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 ¶
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 ¶
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) Clock ¶
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 ¶
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 ¶
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 ¶
Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
func (*LocalTime) Parse ¶
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) Sub ¶
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, ...).
type Offset ¶
type Offset Extent
Offset represents a time zone offset from UTC with precision to the minute.
func OffsetOf ¶
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 ¶
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 ¶
func (d OffsetDateTime) Add(v Duration) OffsetDateTime
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 ¶
func (d OffsetDateTime) Sub(u OffsetDateTime) Duration
Sub returns the duration d-u.
func (OffsetDateTime) UTC ¶
func (d OffsetDateTime) UTC() OffsetDateTime
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) 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
type Period ¶
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) Parse ¶
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 ¶
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