iso8601

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

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

Go to latest
Published: May 10, 2020 License: MIT Imports: 6 Imported by: 1

README

iso8601-go

Golang code for formatting and parsing 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.

Installation

$ go get github.com/spatialtime/iso8601-go

Usage

import(
   "fmt"
    "spatialtime/cmd/iso8601"
    "time"
)

func main(){
    formatted := iso8601.FormatWeek(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), false)
    fmt.Println(formatted) // 2020-W53-5
}

Notes

I included 9 ISO-specific layout strings to expedite parsing and formatting of dates, times and date-times. Use these in calls to time.Parse() and time.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

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"
)

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 ErrDayOfYearRange = errors.New("day of year is out of range")
View Source
var ErrMonthRange = errors.New("month is out of range (valid range: 1-12)")

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 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