A fast ISO8601 date parser for Go

A fast ISO8601 date parser for Go

go.dev reference GoDoc Build Status Go Report Card

go get github.com/relvacode/iso8601

The built-in RFC3333 time layout in Go is too restrictive to support any ISO8601 date-time.

This library parses any ISO8601 date into a native Go time object without regular expressions.

Usage

import "github.com/relvacode/iso8601"

// iso8601.Time can be used as a drop-in replacement for time.Time with JSON responses
type ExternalAPIResponse struct {
	Timestamp *iso8601.Time
}


func main() {
	// iso8601.ParseString can also be called directly
	t, err := iso8601.ParseString("2020-01-02T16:20:00")
}

Benchmark

BenchmarkParse-16        	13364954	        77.7 ns/op	       0 B/op	       0 allocs/op
Owner
Jason Kingsbury
Small solutions for big systems
Jason Kingsbury
Comments
  • Various v2 additions

    Various v2 additions

    1. Fixes error in go.mod - this is necessary for anyone to use v2.x
    2. Adds Of function for convenient (i.e less syntax) conversion from time.Time to iso8601.Time.
    3. Adds Equal and String methods to override the base time.Time behaviour (which is inappropriate)
    4. Adds more test cases
    5. Adds some comments to explain some reasoning

    This is a bit of a smorgasbord with 5 minor things in one PR. I hope you think it's a worthwhile step forward though. We can discuss merging the different parts separately if you feel it's necessary.

  • ParseString does not fail on invalid input string

    ParseString does not fail on invalid input string

    As per the documentation, ParseString parses an ISO8601 compliant date-time string into a time.Time object

    However, if you try to parse an invalid string, e.g. 2020-02-30T10:24:54, ParseString does not raise any error whereas the date is not valid.

    You can test here: https://play.golang.org/p/4fqmGeTsrjF

  • Leading plus character is considered an error

    Leading plus character is considered an error

    By the ISO 8601, years after and including 0000 may be prefixed with the plus sign +. E.g., +2007-00-00T00:00:00Z is a valid date.

    Trying to parse the date using this library results in a UnexpectedCharactError.

    This problem occurs with v1.1.0. I wasn't able to test the v2.0.0 (due to known problem with go.mod).

  • FIX issue with the time set to zero

    FIX issue with the time set to zero

    After paring a date with this value "0000-00-00" internally the date is looking like -0001-12-31 00:00:00 +0000 UTC so for the round trip json Unmarshal/Marshal you can get this current error : json: error calling MarshalJSON for type iso8601.Time: Time.MarshalJSON: year outside of range [0,9999]

    To keep is safe I use the default zeroed time of Go witch is 0001-01-01 00:00:00 +0000 UTC

  • Don't use v2

    Don't use v2

    On reflection, this module shouldn't be using v2 and should be using v1 minor versions instead.

    Fix the release history to point back to equivalent v1 versions that were originally v2.

  • Timezone of

    Timezone of "-00" and "-00:00" should be invalid per ISO8601 3.4.2

    Wikipedia's breakdown of ISO8601 Section 3.4.2:

    An offset of zero, in addition to having the special representation "Z", can also be stated numerically as "+00:00", "+0000", or "+00". However, it is not permitted to state it numerically with a negative sign, as "−00:00", "−0000", or "−00". The section dictating sign usage (section 3.4.2 in the 2004 edition of the standard) states that a plus sign must be used for a positive or zero value, and a minus sign for a negative value. Contrary to this rule, RFC 3339, which is otherwise a profile of ISO 8601, permits the use of "-00", with the same denotation as "+00" but a differing connotation.

    package main
    
    import (
    	"github.com/relvacode/iso8601"
    	"github.com/davecgh/go-spew/spew"
    )
    
    func main() {
    	t, err := iso8601.ParseString("2020-01-02T16:20:00.999-00")
    	spew.Dump(t, err)
    }
    

    Outputs

    (time.Time) 2020-01-02 16:20:00.999 +0000 +0000
    (interface {}) <nil>
    

    when it should be outputting an error.

GoLang Parse many date strings without knowing format in advance.

Go Date Parser Parse many date strings without knowing format in advance. Uses a scanner to read bytes and use a state machine to find format. Much fa

Dec 31, 2022
:clock1: Date and Time - Golang Formatting Library
:clock1: Date and Time - Golang Formatting Library

Kair Date and Time - Golang Formatting Library Setup To get Kair > Go CLI go get github.com/GuilhermeCaruso/kair > Go DEP dep ensure -add github.com/G

Sep 26, 2022
Go-timeparser - Flexible Time Parser for Golang

go-timeparser Flexible Time Parser for Golang Installation Download timeparser w

Dec 29, 2022
fasttime - fast time formatting for go

fasttime - fast time formatting for go

Dec 13, 2022
Fast strftime for Go

strftime Fast strftime for Go SYNOPSIS f := strftime.New(`.... pattern ...`) if err := f.Format(buf, time.Now()); err != nil { log.Println(err.Err

Nov 28, 2022
A natural language date/time parser with pluggable rules

when when is a natural language date/time parser with pluggable rules and merge strategies Examples tonight at 11:10 pm at Friday afternoon the deadli

Dec 26, 2022
A natural language date/time parser with pluggable rules

when when is a natural language date/time parser with pluggable rules and merge strategies Examples tonight at 11:10 pm at Friday afternoon the deadli

Dec 26, 2022
GoLang Parse many date strings without knowing format in advance.

Go Date Parser Parse many date strings without knowing format in advance. Uses a scanner to read bytes and use a state machine to find format. Much fa

Dec 31, 2022
:clock1: Date and Time - Golang Formatting Library
:clock1: Date and Time - Golang Formatting Library

Kair Date and Time - Golang Formatting Library Setup To get Kair > Go CLI go get github.com/GuilhermeCaruso/kair > Go DEP dep ensure -add github.com/G

Sep 26, 2022
A simple Cron library for go that can execute closures or functions at varying intervals, from once a second to once a year on a specific date and time. Primarily for web applications and long running daemons.

Cron.go This is a simple library to handle scheduled tasks. Tasks can be run in a minimum delay of once a second--for which Cron isn't actually design

Dec 17, 2022
Installs git repos onto your system and keeps them up-to-date

Gitfile Installs git repos onto your system and keeps them up-to-date. It's a lightweight package manager for things that haven't been published to a

Jan 16, 2021
A simple Cron library for go that can execute closures or functions at varying intervals, from once a second to once a year on a specific date and time. Primarily for web applications and long running daemons.

Cron.go This is a simple library to handle scheduled tasks. Tasks can be run in a minimum delay of once a second--for which Cron isn't actually design

Dec 17, 2022
manage your mocks / run mockgen more quickly / mocks up-to-date checking
manage your mocks / run mockgen more quickly / mocks up-to-date checking

gomockhandler If you find any bugs or have feature requests, please feel free to create an issue. gomockhandler is handler of golang/mock, as the name

Dec 30, 2022
A tiny date object in Go. Tinydate uses only 4 bytes of memory

go-tinydate A tiny date object in Go. Tinydate uses 4 bytes of memory vs the 24 bytes of a standard time.Time{} A tinydate only has day precision. It

Mar 17, 2022
Watchtower for Git: automatically keep local Git repositories up to date with their remotes

CrowsNest Watchtower for Git: automatically keep local Git repositories up to date with their remotes. Configuration Flags --run-once or -r: Normally

Oct 30, 2022
Birthdays is a web service that stores the birthday date of users and calculates the remaining days until the user's birthday.
Birthdays is a web service that stores the birthday date of users and calculates the remaining days until the user's birthday.

Birthdays is a web service that stores the birthday date of users and calculates the remaining days until the user's birthday. Features Metrics servic

May 2, 2022
indodate is a plugin for golang programming language for date convertion on indonesian format

indodate is a package for golang programming language for date conversion on indonesian format

Oct 23, 2021
Command-line tool to organize large directories of media files recursively by date, detecting duplicates.

go-media-organizer Command-line tool written in Go to organise all media files in a directory recursively by date, detecting duplicates.

Jan 6, 2022
Advent of Code Input Loader, provide a session cookie and a problem date, returns a string or []byte of the input

Advent of Code Get (aocget) A small lib to download your puzzle input for a given day. Uses your session token to authenticate to obtain your personal

Dec 9, 2021
Installs git repos onto your system and keeps them up-to-date

Gitfile Installs git repos onto your system and keeps them up-to-date. It's a lightweight package manager for things that haven't been published to a

Jan 16, 2021