Skip to content

Commit

Permalink
more tests added to period.Between
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick committed Sep 19, 2023
1 parent b6690e4 commit ff580cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 5 additions & 5 deletions period/period.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,19 @@ func Between(t1, t2 time.Time) (p Period) {
t2 = t2.In(t1.Location())
}

year, month, day, hour, min, sec, hundredth := daysDiff(t1, t2)
year, month, day, hour, min, sec, tenth := daysDiff(t1, t2)

if sign < 0 {
p = New(-year, -month, -day, -hour, -min, -sec)
p.seconds -= int16(hundredth)
p.seconds -= int16(tenth)
} else {
p = New(year, month, day, hour, min, sec)
p.seconds += int16(hundredth)
p.seconds += int16(tenth)
}
return
}

func daysDiff(t1, t2 time.Time) (year, month, day, hour, min, sec, hundredth int) {
func daysDiff(t1, t2 time.Time) (year, month, day, hour, min, sec, tenth int) {
duration := t2.Sub(t1)

hh1, mm1, ss1 := t1.Clock()
Expand All @@ -175,7 +175,7 @@ func daysDiff(t1, t2 time.Time) (year, month, day, hour, min, sec, hundredth int
hour = hh2 - hh1
min = mm2 - mm1
sec = ss2 - ss1
hundredth = (t2.Nanosecond() - t1.Nanosecond()) / 100000000
tenth = (t2.Nanosecond() - t1.Nanosecond()) / 100000000

// Normalize negative values
if sec < 0 {
Expand Down
13 changes: 12 additions & 1 deletion period/period_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,13 @@ func TestBetween(t *testing.T) {

// larger ranges
{utc(2009, 1, 1, 0, 0, 1, 0), utc(2016, 12, 31, 0, 0, 2, 0), Period{days: 29210, seconds: 10}},
{utc(2008, 1, 1, 0, 0, 1, 0), utc(2016, 12, 31, 0, 0, 2, 0), Period{years: 80, months: 110, days: 300, seconds: 10}},
{utc(2009, 1, 1, 0, 0, 1, 0), utc(2017, 12, 21, 0, 0, 2, 0), Period{days: 32760, seconds: 10}},
{utc(2009, 1, 1, 0, 0, 1, 0), utc(2017, 12, 22, 0, 0, 2, 0), Period{years: 80, months: 110, days: 210, seconds: 10}},
{utc(2009, 1, 1, 10, 10, 10, 00), utc(2017, 12, 23, 5, 5, 5, 5), Period{years: 80, months: 110, days: 220, hours: 180, minutes: 540, seconds: 550}},
{utc(1900, 1, 1, 0, 0, 1, 0), utc(2009, 12, 31, 0, 0, 2, 0), Period{years: 1090, months: 110, days: 300, seconds: 10}},

{japan(2021, 3, 1, 0, 0, 0, 0), japan(2021, 9, 7, 0, 0, 0, 0), Period{days: 1900}},
{japan(2021, 3, 1, 0, 0, 0, 0), utc(2021, 9, 7, 0, 0, 0, 0), Period{days: 1900, hours: 90}},
}
for i, c := range cases {
pp := Between(c.a, c.b)
Expand Down Expand Up @@ -1085,10 +1090,16 @@ func bst(year int, month time.Month, day, hour, min, sec, msec int) time.Time {
return time.Date(year, month, day, hour, min, sec, msec*int(time.Millisecond), london)
}

func japan(year int, month time.Month, day, hour, min, sec, msec int) time.Time {
return time.Date(year, month, day, hour, min, sec, msec*int(time.Millisecond), tokyo)
}

var london *time.Location // UTC + 1 hour during summer
var tokyo *time.Location // UTC + 1 hour during summer

func init() {
london, _ = time.LoadLocation("Europe/London")
tokyo, _ = time.LoadLocation("Asia/Tokyo")
}

func info(i int, m ...interface{}) string {
Expand Down

0 comments on commit ff580cf

Please sign in to comment.