Skip to content

Commit

Permalink
Merge pull request #30 from hsfzxjy/quarter-semaster
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex authored Dec 16, 2023
2 parents fbe8d85 + c8bfcee commit d13ca99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
17 changes: 13 additions & 4 deletions quarter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import "time"
type Quarter[T TimeZone] struct {
year int
number int
t Time[T]
}

// Quarter gets current quarter.
func (t Time[T]) Quarter() Quarter[T] {
return Quarter[T]{
year: t.Year(),
number: numberOfQuarter(t.Month()),
t: t,
}
}

Expand All @@ -24,10 +22,21 @@ func (q Quarter[T]) Year() int { return q.year }
func (q Quarter[T]) Number() int { return q.number }

// Start returns start time in the quarter.
func (q Quarter[T]) Start() Time[T] { return q.t.StartOfQuarter() }
func (q Quarter[T]) Start() Time[T] {
year, quarter, day := q.year, q.number, 1
return New[T](year, time.Month(3*quarter-2), day, 0, 0, 0, 0)
}

// End returns end time in the quarter.
func (q Quarter[T]) End() Time[T] { return q.t.EndOfQuarter() }
func (q Quarter[T]) End() Time[T] {
year, quarter := q.year, q.number
day := 31
switch quarter {
case 2, 3:
day = 30
}
return New[T](year, time.Month(3*quarter), day, 23, 59, 59, 999999999)
}

// After reports whether the Quarter instant q is after u.
func (q Quarter[T]) After(u Quarter[T]) bool {
Expand Down
20 changes: 16 additions & 4 deletions semester.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import "time"
type Semester[T TimeZone] struct {
year int
number int
t Time[T]
}

// Semester gets current semester.
func (t Time[T]) Semester() Semester[T] {
return Semester[T]{
year: t.Year(),
number: numberOfSemester(t.Month()),
t: t,
}
}

Expand All @@ -24,10 +22,24 @@ func (s Semester[T]) Year() int { return s.year }
func (s Semester[T]) Number() int { return s.number }

// Start returns start time in the semester.
func (s Semester[T]) Start() Time[T] { return s.t.StartOfSemester() }
func (s Semester[T]) Start() Time[T] {
year, semester, day := s.year, s.number, 1
month := time.January
if semester == 2 {
month = time.July
}
return New[T](year, month, day, 0, 0, 0, 0)
}

// End returns end time in the semester.
func (s Semester[T]) End() Time[T] { return s.t.EndOfSemester() }
func (s Semester[T]) End() Time[T] {
year, semester := s.year, s.number
month, day := time.June, 30
if semester == 2 {
month, day = time.December, 31
}
return New[T](year, month, day, 23, 59, 59, 999999999)
}

// After reports whether the Semester instant s is after u.
func (s Semester[T]) After(u Semester[T]) bool {
Expand Down
25 changes: 4 additions & 21 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,22 @@ func (t Time[T]) EndOfWeek() Time[T] {

// StartOfQuarter returns a Time for start of the quarter.
func (t Time[T]) StartOfQuarter() Time[T] {
year, quarter, day := t.Year(), numberOfQuarter(t.Month()), 1
return New[T](year, time.Month(3*quarter-2), day, 0, 0, 0, 0)
return t.Quarter().Start()
}

// EndOfQuarter returns a Time for end of the quarter.
func (t Time[T]) EndOfQuarter() Time[T] {
year, quarter := t.Year(), numberOfQuarter(t.Month())
day := 31
switch quarter {
case 2, 3:
day = 30
}
return New[T](year, time.Month(3*quarter), day, 23, 59, 59, 999999999)
return t.Quarter().End()
}

// StartOfSemester returns a Time for start of the semester.
func (t Time[T]) StartOfSemester() Time[T] {
year, semester, day := t.Year(), numberOfSemester(t.Month()), 1
month := time.January
if semester == 2 {
month = time.July
}
return New[T](year, month, day, 0, 0, 0, 0)
return t.Semester().Start()
}

// EndOfSemester returns a Time for end of the semester.
func (t Time[T]) EndOfSemester() Time[T] {
year, semester := t.Year(), numberOfSemester(t.Month())
month, day := time.June, 30
if semester == 2 {
month, day = time.December, 31
}
return New[T](year, month, day, 23, 59, 59, 999999999)
return t.Semester().End()
}

// IsLeapYear returns true if t is leap year.
Expand Down

0 comments on commit d13ca99

Please sign in to comment.