datetime

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2019 License: MIT Imports: 9 Imported by: 10

README

datetime Build Status Coverage Status

datetime provides a Parse function for turning commonly-used ISO 8601 date/time formats into Golang time.Time variables. datetime.Parse takes two arguments:

  • the string you want to parse
  • the timezone location to be used if there's not one specified inside the string

Unlike Go's built-in RFC-3339 time format, this package automatically supports ISO 8601 date and time stamps with varying levels of granularity. Examples:

package main

import (
	"fmt"
	"time"

	"github.com/btubbs/datetime"
)

func main() {
	// just a year, defaulting to the time.UTC timezone
	fmt.Println(datetime.Parse("2007", time.UTC)) // 2007-01-01 00:00:00 +0000 UTC <nil>

	// a year and a month, this time defaulting to time.Local timezone
	fmt.Println(datetime.Parse("2007-11", time.Local)) // 2007-11-01 00:00:00 -0600 MDT <nil>

	// a full date
	fmt.Println(datetime.Parse("2007-11-22", time.UTC)) // 2007-11-22 00:00:00 +0000 UTC <nil>

	// adding time
	fmt.Println(datetime.Parse("2007-11-22T12:30:22", time.UTC)) // 2007-11-22 12:30:22 -0700 MST <nil>

	// fractions of a second
	fmt.Println(datetime.Parse("2007-11-22T12:30:22.321", time.UTC)) // 2007-11-22 12:30:22.321 -0700 MST <nil>

	// omitting dashes and colons, as ISO 8601 allows
	fmt.Println(datetime.Parse("20071122T123022", time.UTC)) // 2007-11-22 12:30:22 -0700 MST <nil>

	// a timezone offset inside the input will override the default provided to datetime.Parse
	fmt.Println(datetime.Parse("2007-11-22T12:30:22+0800", time.Local)) // 2007-11-22 12:30:22 +0800 +0800 <nil>

	// adding separators to the offset too
	fmt.Println(datetime.Parse("2007-11-22T12:30:22+08:00", time.UTC)) // 2007-11-22 12:30:22 +0800 +08:00 <nil>

	// using a shorthand for UTC
	fmt.Println(datetime.Parse("2007-11-22T12:30:22Z", time.Local)) // 2007-11-22 12:30:22 +0000 UTC <nil>
}

DefaultUTC and DefaultLocal types are also provided. Used as struct fields, their Scan, Value, and UnmarshalJSON methods support easy parsing of ISO 8601 timestamps from external systems.

Documentation

Overview

Package datetime provides a ParseTime function for turning commonly-used ISO 8601 date/time formats into Golang time.Time variables.

Unlike Go's built-in RFC-3339 time format, this package automatically supports ISO 8601 date and time stamps with varying levels of granularity. Examples:

Index

Constants

View Source
const (
	ILLEGAL token = iota
	EOF
	NUMBER
	DASH
	COLON
	DOT
	PLUS
	T
	Z
)

Parsing tokens for internal use, only capitalized for stylistic reasons.

Variables

This section is empty.

Functions

func JSONParse

func JSONParse(data []byte, loc *time.Location) (time.Time, error)

JSONParse will take a JSON bytes value with quotes around it, and parse it into a time.Time.

func Parse

func Parse(s string, defaultLocation *time.Location) (time.Time, error)

Parse takes a string with a ISO 8601 timestamp in it, and a default location to use for timestamps that don't include one, and returns a time.Time.

func ParseLocal

func ParseLocal(s string) (time.Time, error)

ParseLocal takes a string with a ISO 8601 timestamp in it and returns a time.Time. For inputs that do not specify a location, time.Local will be used.

func ParseUTC

func ParseUTC(s string) (time.Time, error)

ParseUTC takes a string with a ISO 8601 timestamp in it and returns a time.Time. For inputs that do not specify a location, time.UTC will be used.

Types

type DefaultLocal

type DefaultLocal time.Time

DefaultLocal is just like a time.Time but serializes as an RFC3339Nano format when stringified or marshaled to JSON. When parsed/unmarshaled, it uses time.Local as the location for timestamps that don't specify one.

func (*DefaultLocal) Scan

func (d *DefaultLocal) Scan(value interface{}) error

Scan implements the sql Scanner interface, allowing datetime.DefaultLocal fields to be read from database columns.

func (DefaultLocal) String

func (d DefaultLocal) String() string

String returns the DefaultLocal's RFC3339Nano representation.

func (*DefaultLocal) UnmarshalJSON

func (d *DefaultLocal) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the JSON Unmarshaler interface, allowing datetime.DefaultLocal struct fields to be read from JSON string fields.

func (DefaultLocal) Value

func (d DefaultLocal) Value() (driver.Value, error)

Value implements the sql Valuer interface, allowing datetime.DefaultLocal fields to be saved to database columns.

type DefaultUTC

type DefaultUTC time.Time

DefaultUTC is just like a time.Time but serializes as an RFC3339Nano format when stringified or marshaled to JSON. When parsed/unmarshaled, it uses time.UTC as the location for timestamps that don't specify one.

func (*DefaultUTC) Scan

func (d *DefaultUTC) Scan(value interface{}) error

Scan implements the sql Scanner interface, allowing datetime.DefaultUTC fields to be read from database columns.

func (DefaultUTC) String

func (d DefaultUTC) String() string

String returns the DefaultUTC's RFC3339Nano representation.

func (*DefaultUTC) UnmarshalJSON

func (d *DefaultUTC) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the JSON Unmarshaler interface, allowing datetime.DefaultUTC struct fields to be read from JSON string fields.

func (DefaultUTC) Value

func (d DefaultUTC) Value() (driver.Value, error)

Value implements the sql Valuer interface, allowing datetime.DefaultUTC fields to be saved to database columns.

Jump to

Keyboard shortcuts

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