From 0a87e3a21d613940696ee35592116c69caf82b7d Mon Sep 17 00:00:00 2001 From: John Wang Date: Sat, 9 Dec 2023 10:14:00 -0800 Subject: [PATCH] feat: `time/timeutil`: add `TimeMore{}` funcs: `.HalfYear()`, `.YearHalf()`, `.YearQuarter()`, `.YearMonth()` --- time/timeutil/timemore.go | 29 +++++++++++++++++++++++++---- time/timeutil/timemore_test.go | 34 ++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/time/timeutil/timemore.go b/time/timeutil/timemore.go index 69638e13..689a191d 100644 --- a/time/timeutil/timemore.go +++ b/time/timeutil/timemore.go @@ -1,6 +1,7 @@ package timeutil import ( + "fmt" "time" ) @@ -37,10 +38,17 @@ func NewTimeMoreQuarterEndString(yyyyq string, d time.Weekday) (TimeMore, error) func (tm TimeMore) Time() time.Time { return tm.time } func (tm TimeMore) WeekStartDay() time.Weekday { return tm.weekStartDay } -func (tm TimeMore) DayStart() time.Time { return dayStart(tm.time) } -func (tm TimeMore) DayEnd() time.Time { return dayEnd(tm.time) } -func (tm TimeMore) WeekStart() time.Time { return weekStart(tm.time, tm.weekStartDay) } -func (tm TimeMore) WeekEnd() time.Time { return weekEnd(tm.time, tm.weekStartDay) } +func (tm TimeMore) DayStart() time.Time { return dayStart(tm.time) } +func (tm TimeMore) DayEnd() time.Time { return dayEnd(tm.time) } +func (tm TimeMore) WeekStart() time.Time { return weekStart(tm.time, tm.weekStartDay) } +func (tm TimeMore) WeekEnd() time.Time { return weekEnd(tm.time, tm.weekStartDay) } +func (tm TimeMore) HalfYear() uint8 { + if tm.time.Month() < time.July { + return 1 + } else { + return 2 + } +} func (tm TimeMore) MonthStart() time.Time { return monthStart(tm.time) } func (tm TimeMore) MonthEnd() time.Time { return monthEnd(tm.time) } func (tm TimeMore) IsMonthStart() bool { return isMonthStart(tm.time) } @@ -56,6 +64,19 @@ func (tm TimeMore) IntervalStart(interval Interval) (time.Time, error) { return intervalStart(tm.time, interval, tm.weekStartDay) } +// YearHalf returns a string in the format of "2006H1" +func (tm TimeMore) YearHalf() string { return fmt.Sprintf("%dH%d", tm.time.Year(), int(tm.HalfYear())) } + +// YearQuarter returns a string in the format of "2006Q1" +func (tm TimeMore) YearQuarter() string { + return fmt.Sprintf("%dQ%d", tm.time.Year(), int(tm.QuarterCalendar())) +} + +// YearMonth returns a string in the format of "2006M1" +func (tm TimeMore) YearMonth() string { + return fmt.Sprintf("%dM%d", tm.time.Year(), int(tm.time.Month())) +} + // QuarterCalendar returns the quarter of the year specified by tm.Time. func (tm TimeMore) QuarterCalendar() Yearquarter { m := tm.time.Month() diff --git a/time/timeutil/timemore_test.go b/time/timeutil/timemore_test.go index 73aa787f..ffe6e607 100644 --- a/time/timeutil/timemore_test.go +++ b/time/timeutil/timemore_test.go @@ -13,13 +13,20 @@ var timemoreTests = []struct { monthStart string weekStart string dayStart string + yearHalf string + yearQuarter string + yearMonth string }{ - {"2023-05-15T12:30:30Z", time.Monday, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-15T00:00:00Z", "2023-05-15T00:00:00Z"}, - {"2023-05-15T12:30:30Z", 7001, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-15T00:00:00Z", "2023-05-15T00:00:00Z"}, - {"2023-05-15T12:30:30Z", time.Sunday, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-14T00:00:00Z", "2023-05-15T00:00:00Z"}, - {"2023-05-15T12:30:30Z", 14000, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-14T00:00:00Z", "2023-05-15T00:00:00Z"}, - {"2023-05-15T12:30:30-08:00", time.Monday, "2023-01-01T00:00:00-08:00", "2023-04-01T00:00:00-08:00", "2023-05-01T00:00:00-08:00", "2023-05-15T00:00:00-08:00", "2023-05-15T00:00:00-08:00"}, - {"2023-05-15T12:30:30-08:00", time.Tuesday, "2023-01-01T00:00:00-08:00", "2023-04-01T00:00:00-08:00", "2023-05-01T00:00:00-08:00", "2023-05-09T00:00:00-08:00", "2023-05-15T00:00:00-08:00"}, + {"2023-05-15T12:30:30Z", time.Monday, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-15T00:00:00Z", "2023-05-15T00:00:00Z", + "2023H1", "2023Q2", "2023M5"}, + {"2023-05-15T12:30:30Z", 7001, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-15T00:00:00Z", "2023-05-15T00:00:00Z", + "2023H1", "2023Q2", "2023M5"}, + {"2023-05-15T12:30:30Z", time.Sunday, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-14T00:00:00Z", "2023-05-15T00:00:00Z", + "2023H1", "2023Q2", "2023M5"}, + {"2023-05-15T12:30:30Z", 14000, "2023-01-01T00:00:00Z", "2023-04-01T00:00:00Z", "2023-05-01T00:00:00Z", "2023-05-14T00:00:00Z", "2023-05-15T00:00:00Z", + "2023H1", "2023Q2", "2023M5"}, + {"2023-05-15T12:30:30-08:00", time.Monday, "2023-01-01T00:00:00-08:00", "2023-04-01T00:00:00-08:00", "2023-05-01T00:00:00-08:00", "2023-05-15T00:00:00-08:00", "2023-05-15T00:00:00-08:00", "2023H1", "2023Q2", "2023M5"}, + {"2023-05-15T12:30:30-08:00", time.Tuesday, "2023-01-01T00:00:00-08:00", "2023-04-01T00:00:00-08:00", "2023-05-01T00:00:00-08:00", "2023-05-09T00:00:00-08:00", "2023-05-15T00:00:00-08:00", "2023H1", "2023Q2", "2023M5"}, } func TestTimeMore(t *testing.T) { @@ -57,5 +64,20 @@ func TestTimeMore(t *testing.T) { if ds.Format(time.RFC3339) != tt.dayStart { t.Errorf("mismatch TimeMore.DayStart() TimeMore(\"%s\", %v): want [%v], got [%v]", tt.t, tt.dow.String(), tt.dayStart, ds.Format(time.RFC3339)) } + + yh := tm.YearHalf() + if yh != tt.yearHalf { + t.Errorf("mismatch TimeMore.YearHalf() TimeMore(\"%s\", %v): want [%v], got [%v]", tt.t, tt.dow.String(), tt.yearHalf, yh) + } + + yq := tm.YearQuarter() + if yq != tt.yearQuarter { + t.Errorf("mismatch TimeMore.YearQuarrter() TimeMore(\"%s\", %v): want [%v], got [%v]", tt.t, tt.dow.String(), tt.yearQuarter, yq) + } + + ym := tm.YearMonth() + if ym != tt.yearMonth { + t.Errorf("mismatch TimeMore.YearMonth() TimeMore(\"%s\", %v): want [%v], got [%v]", tt.t, tt.dow.String(), tt.yearMonth, ym) + } } }