datetime

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

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

Go to latest
Published: Jan 2, 2023 License: MIT Imports: 3 Imported by: 1

README

w3c-datetime Go Reference

Implements the W3C datetime format for Golang.

package main

import (
	"fmt"

	datetime "github.com/bored-engineer/w3c-datetime"
)

func main() {
	d, err := datetime.Parse("1994-11-05")
	if err != nil {
		panic(err)
	}
	fmt.Println(d.Precision) // YYYY-MM-DD
    fmt.Println(d.Time) // 1994-11-05 00:00:00 +0000 UTC
 
	dt, err := datetime.Parse("1994-11-05T08:15:30Z")
	if err != nil {
		panic(err)
	}
	fmt.Println(dt.Precision) // YYYY-MM-DDThh:mm:ssTZD
    fmt.Println(dt.Time) // 1994-11-05 08:15:30 +0000 UTC
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Precision

type Precision int

Precision is the granularity of the datetime

const (
	PrecisionUnknown Precision = iota
	// Year: YYYY (eg 1997)
	PrecisionYear
	// Year and month: YYYY-MM (eg 1997-07)
	PrecisionMonth
	// Complete date: YYYY-MM-DD (eg 1997-07-16)
	PrecisionDay
	// Complete date plus hours and minutes: YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
	PrecisionHour
	// Complete date plus hours, minutes and seconds: YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
	PrecisionSecond
	// Complete date plus hours, minutes, seconds and a decimal fraction of a second YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
	PrecisionNanosecond
)

func ParsePrecision

func ParsePrecision(datetime string) Precision

ParsePrecision detects the precision level in the provided string

func (Precision) Layout

func (p Precision) Layout() string

Returns the layout expected by time.Parse or "" if unknown

func (Precision) String

func (p Precision) String() string

String implements fmt.Stringer

type Time

type Time struct {
	time.Time
	Precision Precision
}

Time represents a W3C datetime https://www.w3.org/TR/NOTE-datetime

func New

func New(t time.Time) Time

New creates a Time with the highest precision (PrecisionNanosecond)

func NewWithPrecision

func NewWithPrecision(t time.Time, p Precision) Time

New creates a Time with the specified precision

func Parse

func Parse(datetime string) (Time, error)

Parse parses a formatted datetime string and returns the time value/precision it represents.

func (Time) MarshalText

func (t Time) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (Time) String

func (t Time) String() string

String implements fmt.Stringer

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler

Jump to

Keyboard shortcuts

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