date

package module
v0.0.0-...-8bf9529 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2018 License: MIT Imports: 3 Imported by: 1

README

date

JSON date formats for Go.

Marshal and unmarshal date times in yyyy-MM-dd and yyyy-MM-ddThh:mm:ss formats.

{
  "ymd":"2008-12-25",
  "ymdhms":"1742-12-25T13:32:20"
}

Example usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/a-h/date"
)

type example struct {
	YMD    date.YYYYMMDD       `json:"ymd"`
	YMDHMS date.YYYYMMDDHHMMSS `json:"ymdhms"`
}

func main() {
	j := `{"ymd":"2008-12-25","ymdhms":"1742-12-25T13:32:20"}`
	var e example
	err := json.Unmarshal([]byte(j), &e)
	if err != nil {
		fmt.Println("Error unmarshalling data: ", err)
		return
	}
	fmt.Println(e.YMD)
	fmt.Println(e.YMDHMS)
	output, err := json.Marshal(e)
	if err != nil {
		fmt.Println("Error marshalling data: ", err)
		return
	}
	fmt.Println(string(output)) // {"ymd":"2008-12-25","ymdhms":"1742-12-25T13:32:20"}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrInvalidDateFormatYYYYMMDD

type ErrInvalidDateFormatYYYYMMDD error

ErrInvalidDateFormatYYYYMMDD is returned when the field cannot be parsed.

type ErrInvalidDateFormatYYYYMMDDHHMMSS

type ErrInvalidDateFormatYYYYMMDDHHMMSS error

ErrInvalidDateFormatYYYYMMDDHHMMSS is returned when the field cannot be parsed.

type YYYYMMDD

type YYYYMMDD time.Time

YYYYMMDD provides a year, month and day type.

func (YYYYMMDD) MarshalJSON

func (d YYYYMMDD) MarshalJSON() ([]byte, error)

MarshalJSON outputs JSON.

func (YYYYMMDD) String

func (d YYYYMMDD) String() string

String // String returns the time formatted using the format string

"2006-01-02 15:04:05.999999999 -0700 MST"

If the time has a monotonic clock reading, the returned string includes a final field "m=±<value>", where value is the monotonic clock reading formatted as a decimal number of seconds.

The returned string is meant for debugging; for a stable serialized representation, use t.MarshalText, t.MarshalBinary, or t.Format with an explicit format string.

func (*YYYYMMDD) UnmarshalJSON

func (d *YYYYMMDD) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON handles incoming JSON.

type YYYYMMDDHHMMSS

type YYYYMMDDHHMMSS time.Time

YYYYMMDDHHMMSS provides a yyyy-MM-ddThh:mm:ss date.

func (YYYYMMDDHHMMSS) MarshalJSON

func (d YYYYMMDDHHMMSS) MarshalJSON() ([]byte, error)

MarshalJSON outputs JSON.

func (YYYYMMDDHHMMSS) String

func (d YYYYMMDDHHMMSS) String() string

String // String returns the time formatted using the format string

"2006-01-02 15:04:05.999999999 -0700 MST"

If the time has a monotonic clock reading, the returned string includes a final field "m=±<value>", where value is the monotonic clock reading formatted as a decimal number of seconds.

The returned string is meant for debugging; for a stable serialized representation, use t.MarshalText, t.MarshalBinary, or t.Format with an explicit format string.

func (*YYYYMMDDHHMMSS) UnmarshalJSON

func (d *YYYYMMDDHHMMSS) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON handles incoming JSON.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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