iso8601

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 6 Imported by: 0

README

iso8601

Go Report Card Top Language License

Format and parse ISO 8601 dates, times, datetimes, time zones, weeks, ordinal dates and durations. Use this right out of the box, or as a starting point to build upon for more specialized needs.

Contents

Installation

$ go get github.com/spatialtime/iso8601

Usage

import(
   "fmt"
    "github.com/spatialtime/iso8601"
    "time"
)

func main(){
    // ISO weeks
    formatted := iso8601.FormatWeek(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), false)
    fmt.Println(formatted) // 2020-W53-5
    date, err := iso8601.ParseWeek(formatted)
    if err != nil{
        fmt.Println(err)
        return
    }
    fmt.Println(date) // 2021-01-01 00:00:00 +0000 UTC

    // Ordinal dates 
    formatted = iso8601.FormatOrdinalDate(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
    fmt.Println(formatted) // 2020-001
    date, err = iso8601.ParseOrdinalDate(formatted)
    if err != nil{
        fmt.Println(err)
        return
    }
    fmt.Println(date) // 2020-01-01 00:00:00 +0000 UTC

    // Durations
    t1 := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
    t2 := time.Date(2020, 1, 3, 0, 0, 0, 0, time.UTC)
    formatted = iso8601.FormatDuration(t2.Sub(t1))
    fmt.Println(formatted) // PT24H0M0S
    duration, err := iso8601.ParseDuration(formatted)
    if err != nil{
        fmt.Println(err)
        return
    }
    fmt.Println(duration) // 24h0m0s
}

Notes

I included nine ISO-specific layout strings to expedite parsing and formatting of dates, times and datetimes. Use these in calls to time.Parse() and time.Format().

const (
	ISOYear                 = "2006"
	ISOYearMonth            = "2006-01"
	ISOFullDate             = "2006-01-02"
	ISOHoursMinutes         = "15:04"
	ISOHoursMinutesSeconds  = "15:04:05"
	ISOFullTime             = "15:04:05.000"
	ISOTzZulu               = "Z"
	ISOTzOffsetHours        = "-07"
	ISOTzOffsetHoursMinutes = "-07:00"
)

Example usage:

t := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(t.Format(iso8601.ISOFullDate)) // 2020-01-01

Author

Copyright © 2020 Matt Savage | MIT license

Documentation

Overview

Package iso8601 provides support for parsing and formatting ISO 8601 constructs. Formatting methods accept a time.Time instance and return the appropriate ISO 8601 string. Parsing methods accept an ISO 8601 string and return a time.Time instance.

Index

Constants

View Source
const (
	ISOYear                 = "2006"
	ISOYearMonth            = "2006-01"
	ISOFullDate             = "2006-01-02"
	ISOHoursMinutes         = "15:04"
	ISOHoursMinutesSeconds  = "15:04:05"
	ISOFullTime             = "15:04:05.000"
	ISOTzZulu               = "Z"
	ISOTzOffsetHours        = "-07"
	ISOTzOffsetHoursMinutes = "-07:00"
	MinWeek                 = 1
	MinYear                 = 1
	MaxYear                 = 9999
)

These are layout constants that supplement the layout constants provided by time.Time. Note: golang's time.RFC3339 constant represents a layout string containing ISO Date, full ISO time (minus milliseconds) and time zone.

Variables

View Source
var ErrWeekRange = fmt.Errorf("week is out of range (valid range: %d–number of iso weeks in the given year inclusive)", MinWeek)

ErrWeekRange is returned when a week is not within our permitted range.

View Source
var ErrYearRange = fmt.Errorf("year is out of range (valid range: %d–%d inclusive)", MinYear, MaxYear)

ErrYearRange is returned when a week is not within our permitted range.

Functions

func FormatDateTime

func FormatDateTime(t time.Time, layout string) string

FormatDateTime returns an ISO 8601 date.

func FormatDuration

func FormatDuration(dur time.Duration) string

FormatDuration returns an ISO 8601 duration string.

func FormatOrdinalDate

func FormatOrdinalDate(date time.Time) string

FormatOrdinalDate returns an ISO 8601 ordinal date string. In this context, Ordinal date represents the nth day of the year.

func FormatWeek

func FormatWeek(date time.Time, shortForm bool) string

FormatWeek returns an ISO 8601 week string.

func ISOYearWeeks

func ISOYearWeeks(gregYear int) int

ISOYearWeeks returns the number of ISO weeks contained in a given Gregorian calendar year.

func ParseDateTime

func ParseDateTime(isoTime, layout string) (time.Time, error)

ParseDateTime parses an ISO 8601 string representing a date or time or date+time, and returns the resultant golang time.Time insance.

func ParseDuration

func ParseDuration(isoDuration string) (time.Duration, error)

ParseDuration parses an ISO 8601 string representing a duration, and returns the resultant golang time.Duration instance.

func ParseOrdinalDate

func ParseOrdinalDate(isoOrdinalDate string) (time.Time, error)

ParseOrdinalDate parses an ISO 8601 string representing a ordinal date, and returns the resultant golang time.Time insance.

func ParseWeek

func ParseWeek(isoWeek string) (time.Time, error)

ParseWeek parses an ISO 8601 string representing an ISO week, and returns the resultant golang time.Time instance. Note: if the ISO week is of the short form (doesn't include day of week), this function will return a time.Time instance with day of week of Monday.

func Weekday

func Weekday(year, month, day int) int

Weekday returns day of week with Monday=0...Sunday=6. Utilizes Zeller's Congruence. see: https://en.wikipedia.org/wiki/Zeller%27s_congruence

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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