Skip to content

Commit

Permalink
feat: time/timeutil: add TimeMore{} funcs: .HalfYear(), `.YearH…
Browse files Browse the repository at this point in the history
…alf()`, `.YearQuarter()`, `.YearMonth()`
  • Loading branch information
grokify committed Dec 9, 2023
1 parent 1f0911f commit 0a87e3a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
29 changes: 25 additions & 4 deletions time/timeutil/timemore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package timeutil

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -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) }
Expand All @@ -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()
Expand Down
34 changes: 28 additions & 6 deletions time/timeutil/timemore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 0a87e3a

Please sign in to comment.