package grmi

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

type Time time.Time

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

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

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

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

func (self *Time) String() string {
	if self == nil {
		return ""
	}
	date := self.Restore()
	return date.Format(DateTimeOutFormat)
}
