Skip to content

datetime: fix interval arithmetic with timezones #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Decimal package use a test function GetNumberLength instead of a
package-level function getNumberLength (#219)
- Datetime location after encode + decode is unequal (#217)
- Wrong interval arithmetic with timezones (#221)

## [1.8.0] - 2022-08-17

Expand Down
3 changes: 3 additions & 0 deletions datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func (dtime *Datetime) Sub(ival Interval) (*Datetime, error) {
func (dtime *Datetime) Interval(next *Datetime) Interval {
curIval := intervalFromDatetime(dtime)
nextIval := intervalFromDatetime(next)
_, curOffset := dtime.time.Zone()
_, nextOffset := next.time.Zone()
curIval.Min -= int64(curOffset-nextOffset) / 60
return nextIval.Sub(curIval)
}

Expand Down
35 changes: 26 additions & 9 deletions datetime/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ func TestDatetimeAddOutOfRange(t *testing.T) {
}

func TestDatetimeInterval(t *testing.T) {
var first = "2015-03-20T17:50:56.000000009Z"
var second = "2013-01-31T17:51:56.000000009Z"
var first = "2015-03-20T17:50:56.000000009+02:00"
var second = "2013-01-31T11:51:58.00000009+01:00"

tmFirst, err := time.Parse(time.RFC3339, first)
if err != nil {
Expand All @@ -345,14 +345,23 @@ func TestDatetimeInterval(t *testing.T) {
ivalFirst := dtFirst.Interval(dtSecond)
ivalSecond := dtSecond.Interval(dtFirst)

expectedFirst := Interval{-2, -2, 0, 11, 0, 1, 0, 0, NoneAdjust}
expectedSecond := Interval{2, 2, 0, -11, 0, -1, 0, 0, NoneAdjust}
expectedFirst := Interval{-2, -2, 0, 11, -6, 61, 2, 81, NoneAdjust}
expectedSecond := Interval{2, 2, 0, -11, 6, -61, -2, -81, NoneAdjust}

if !reflect.DeepEqual(ivalFirst, expectedFirst) {
t.Errorf("Unexpected interval %v, expected %v", ivalFirst, expectedFirst)
}
if !reflect.DeepEqual(ivalSecond, expectedSecond) {
t.Errorf("Unexpected interval %v, expected %v", ivalFirst, expectedSecond)
t.Errorf("Unexpected interval %v, expected %v", ivalSecond, expectedSecond)
}

dtFirst, err = dtFirst.Add(ivalFirst)
if err != nil {
t.Fatalf("Unable to add an interval: %s", err)
}
if !dtFirst.ToTime().Equal(dtSecond.ToTime()) {
t.Errorf("Incorrect add an interval result: %s, expected %s",
dtFirst.ToTime(), dtSecond.ToTime())
}
}

Expand All @@ -363,12 +372,20 @@ func TestDatetimeTarantoolInterval(t *testing.T) {
defer conn.Close()

dates := []string{
"2015-03-20T17:50:56.000000009+01:00",
// We could return tests with timezones after a release with a fix of
// the bug:
// https://github.com/tarantool/tarantool/issues/7698
//
// "2010-02-24T23:03:56.0000013-04:00",
// "2015-03-20T17:50:56.000000009+01:00",
// "2020-01-01T01:01:01+11:30",
// "2025-08-01T00:00:00.000000003+11:00",
"2010-02-24T23:03:56.0000013Z",
"2015-03-20T17:50:56.000000009Z",
"2020-01-01T01:01:01Z",
"2025-08-01T00:00:00.000000003Z",
"2015-12-21T17:50:53Z",
"2010-02-24T23:03:56.0000013-04:00",
"1980-03-28T13:18:39.000099Z",
"2025-08-01T00:00:00.000000003+11:00",
"2020-01-01T01:01:01+11:30",
}
datetimes := []*Datetime{}
for _, date := range dates {
Expand Down