vickyramachandra / time

Custom time implementation for unmarshal of only date in json to time.Time

Repository from Github https://github.comvickyramachandra/timeRepository from Github https://github.comvickyramachandra/time

time

Custom time implementation for unmarshalling of time format with only date "2006-01-02" in json to time.Time

Deserialization from json to struct (which consists of a time.Time property) fails when the time format in the json isn't RFC3339 ≈ "2006-01-02T15:04:05Z07:00"

Hence use this simple implementation to support time format with only date in your json payload.

package main

import (
	"encoding/json"
	"fmt"

	mytime "github.com/vickyramachandra/time"
)

type MyStruct struct {
	Time *mytime.Ctime `json:"time"`
}

func main() {
	// json payload with date only format
	dateFormat := `{"time":"2018-02-18"}`

	var myStruct = new(MyStruct)
	// this will invoke the unmarshal impl defined in this package
	err := json.Unmarshal([]byte(dateFormat), &myStruct)
	if err != nil {
		fmt.Println(err)
		return
	}
	// prints 2018 February 18
	fmt.Println(myStruct.Time.Date())
}

About

Custom time implementation for unmarshal of only date in json to time.Time

License:Apache License 2.0


Languages

Language:Go 100.0%