package utils

import (
	"time"
)

type JavaDate struct {
	Day time.Weekday `json:"day"` // day of the week

	Hours          int   `json:"hours"`
	Minutes        int   `json:"minutes"`
	Seconds        int   `json:"seconds"`
	Time           int64 `json:"time"`
	TimezoneOffset int   `json:"timezoneOffset"`

	Date  int        `json:"date"`
	Month time.Month `json:"month"`
	Year  int        `json:"year"`
}

func NewJavaDate() *JavaDate {
	return NewJavaDateFromTime(time.Now())
}

func NewJavaDateFromTime(tm time.Time) (jd *JavaDate) {
	jd = &JavaDate{
		Time:    tm.UnixNano() / 1000000,
		Day:     tm.Weekday(),
		Hours:   tm.Hour(),
		Minutes: tm.Minute(),
		Seconds: tm.Second(),
	}
	jd.Year, jd.Month, jd.Date = tm.Date()
	jd.Year -= 1900
	jd.Month--
	_, jd.TimezoneOffset = tm.Zone()
	jd.TimezoneOffset /= 60
	jd.TimezoneOffset = -jd.TimezoneOffset
	return jd
}

func (j *JavaDate) GoTime() time.Time {
	if j == nil {
		return DefaultTimeValue
	}
	return Timestamp2Time(j.Time / 1000)
}

func (j *JavaDate) String() string {
	return Timestamp2Str(j.Time)
}
