Skip to content

An extra time package for unmarshalling and marshalling json and xml.

License

Notifications You must be signed in to change notification settings

johejo/extratime

Repository files navigation

extratime

An extra package for unmarshalling and marshalling time format to json and xml.

ci codecov go.dev reference Go Report Card

Supported Formats

  • ANSIC
  • UnixDate
  • RFC1123
  • RFC1123Z
  • RFC822
  • RFC822Z
  • RFC850
  • Kitchen
  • Stamp
  • StampMilli
  • StampMicro
  • StampNano
  • RubyDate
  • UnixTimeStamp

Install

go get github.com/johejo/extratime

Example

package extratime_test

import (
	"encoding/json"
	"encoding/xml"
	"fmt"

	"github.com/johejo/extratime"
)

type A struct {
	XMLName xml.Name          `xml:"a"`
	Text    string            `xml:",chardata"`
	B       extratime.RFC1123 `xml:"b"`
}

func Example() {
	// xml
	const x = "<a><b>Mon, 02 Jan 2006 15:04:05 MST</b></a>"
	var a A
	if err := xml.Unmarshal([]byte(x), &a); err != nil {
		panic(err)
	}
	xb, err := xml.Marshal(a)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(xb))

	// json
	const j = `{"t": "Mon, 02 Jan 2006 15:04:05 MST"}`
	var m map[string]extratime.RFC1123
	if err := json.Unmarshal([]byte(j), &m); err != nil {
		panic(err)
	}
	jb, err := json.Marshal(m)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(jb))

	// Output:
	// <a><b>Mon, 02 Jan 2006 15:04:05 MST</b></a>
	// {"t":"Mon, 02 Jan 2006 15:04:05 MST"}
}

License

MIT

Author

Mitsuo Heijo (@johejo)

About

An extra time package for unmarshalling and marshalling json and xml.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages