package grmi

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"fmt"
	json "github.com/json-iterator/go"
	"time"
)

type DateTime time.Time

func (self DateTime) MarshalJSON() ([]byte, error) {
	return []byte(fmt.Sprintf("\"%s\"", time.Time(self).Format(URLDateTimeFormat))), nil
}

func (self *DateTime) UnmarshalJSON(data []byte) error {
	var jsonString string
	err := json.Unmarshal(data, &jsonString)
	if err != nil {
		return err
	}
	result, err := time.ParseInLocation(URLDateTimeFormat, jsonString, utils.TimezoneLocation)
	if err != nil {
		return err
	}
	*self = DateTime(result)
	return nil
}

func (self *DateTime) Restore() time.Time {
	return time.Time(*self)
}

func (self *DateTime) ToString() string {
	date := self.Restore()
	return date.Format(DatetimeOutFormat)
}
