From d0398ec55b019eab1f5b028b5cdae9f062e9e8d1 Mon Sep 17 00:00:00 2001 From: jingyuexing <19589872+jingyuexing@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:40:56 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20`RawTime`=20and=20`Co?= =?UTF-8?q?untDays`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datetime.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/datetime.go b/datetime.go index 1caa8ad..a3016b2 100644 --- a/datetime.go +++ b/datetime.go @@ -217,6 +217,10 @@ func (dt DateTime) SetTime(sec int64, ns int64) DateTime { return d } +func (dt DateTime) RawTime() *time.Time { + return &dt.time +} + func (dt DateTime) LocaleCallBack(t string, call func(t string) string) string { return dt.Format(call(t)) } @@ -260,6 +264,16 @@ func (d DateTime) WeekDay() int { return int(d.time.Weekday()) } +func (d DateTime) CountDays() int { + start := time.Date(d.Year, time.January, 1, 0, 0, 0, 0, time.UTC) + end := time.Date(d.Year+1, time.January, 1, 0, 0, 0, 0, time.UTC) + return int(end.Sub(start).Hours() / 24) +} + +func (d DateTime) Progress() float32 { + return float32(d.time.YearDay()) / float32(d.CountDays()) +} + func (d DateTime) WeekOfYear() int { _, week := d.time.ISOWeek() return week @@ -414,6 +428,9 @@ func extractDateParts(date string, formatParts []string) map[string]string { func (dt DateTime) Parse(date string, formatTemplate string) *DateTime { datetime := NewDateTime() + if formatTemplate == "" { + formatTemplate = dt.DateFormat + "T" + dt.TimeFormat + "Z" + } formatParts := parseFormatTemplate(formatTemplate) dateParts := extractDateParts(date, formatParts)