Repository: matrixorigin/matrixone
Pull request #4585 (2022-08-19 03:46:13)

Fix wrong timezone testcases
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [x] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #

## What this PR does / why we need it:


Patch:
diff --git a/pkg/container/types/datetime.go b/pkg/container/types/datetime.go
index eb8eaa330..f138aa0d6 100644
--- a/pkg/container/types/datetime.go
+++ b/pkg/container/types/datetime.go
@@ -203,10 +203,6 @@ func FromUnix(loc *time.Location, ts int64) Datetime {
 	return Datetime((ts+int64(offset))*microSecsPerSec + unixEpoch)
 }
 
-func UnixToTimestamp(ts int64) Timestamp {
-	return Timestamp(ts*microSecsPerSec + unixEpoch)
-}
-
 func Now(loc *time.Location) Datetime {
 	now := time.Now().In(loc)
 	_, offset := now.Zone()
diff --git a/pkg/container/types/timestamp.go b/pkg/container/types/timestamp.go
index 942edee9f..1c0afb185 100644
--- a/pkg/container/types/timestamp.go
+++ b/pkg/container/types/timestamp.go
@@ -184,12 +184,18 @@ func TimestampToDatetime(loc *time.Location, xs []Timestamp, rs []Datetime) ([]D
 }
 
 // FromClockUTC gets the utc time value in Timestamp
-func FromClockUTC(year int32, month, day, hour, min, sec uint8, msec uint32) Timestamp {
+func FromClockUTC(year int32, month, day, hour, minute, sec uint8, msec uint32) Timestamp {
 	days := FromCalendar(year, month, day)
-	secs := int64(days)*secsPerDay + int64(hour)*secsPerHour + int64(min)*secsPerMinute + int64(sec)
+	secs := int64(days)*secsPerDay + int64(hour)*secsPerHour + int64(minute)*secsPerMinute + int64(sec)
 	return Timestamp(secs*microSecsPerSec + int64(msec))
 }
 
+// FromClockZone gets the local time value in Timestamp
+func FromClockZone(loc *time.Location, year int32, month, day, hour, minute, sec uint8, msec uint32) Timestamp {
+	t := time.Date(int(year), time.Month(month), int(day), int(hour), int(minute), int(sec), int(msec*1000), loc)
+	return Timestamp(t.UnixMicro() + unixEpoch)
+}
+
 func CurrentTimestamp() Timestamp {
 	return Timestamp(time.Now().UnixMicro() + unixEpoch)
 }
@@ -197,3 +203,11 @@ func CurrentTimestamp() Timestamp {
 func ValidTimestamp(timestamp Timestamp) bool {
 	return timestamp > TimestampMinValue
 }
+
+func UnixToTimestamp(ts int64) Timestamp {
+	return Timestamp(ts*microSecsPerSec + unixEpoch)
+}
+
+func UnixMicroToTimestamp(ts int64) Timestamp {
+	return Timestamp(ts + unixEpoch)
+}


Test patch:
diff --git a/pkg/sql/plan/function/builtin/unary/timestamp_test.go b/pkg/sql/plan/function/builtin/unary/timestamp_test.go
index d2b58d507..80be649c5 100644
--- a/pkg/sql/plan/function/builtin/unary/timestamp_test.go
+++ b/pkg/sql/plan/function/builtin/unary/timestamp_test.go
@@ -16,6 +16,7 @@ package unary
 
 import (
 	"testing"
+	"time"
 
 	"github.com/matrixorigin/matrixone/pkg/container/nulls"
 	"github.com/matrixorigin/matrixone/pkg/container/types"
@@ -36,7 +37,7 @@ func TestDateToTimestamp(t *testing.T) {
 			name: "TEST01",
 			vecs: makeDateToTimestampVectors("2022-01-01", true),
 			proc: testutil.NewProc(),
-			want: []types.Timestamp{types.FromClockUTC(2022, 1, 1, 0, 0, 0, 0)},
+			want: []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)},
 		},
 	}
 
@@ -63,7 +64,7 @@ func TestDatetimeToTimestamp(t *testing.T) {
 			name: "TEST01",
 			vecs: makeDatetimeToTimestampVectors("2022-01-01 00:00:00", true),
 			proc: testutil.NewProc(),
-			want: []types.Timestamp{types.FromClockUTC(2022, 1, 1, 0, 0, 0, 0)},
+			want: []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)},
 		},
 	}
 
@@ -91,14 +92,14 @@ func TestDateStringAdd(t *testing.T) {
 			name:    "TEST01",
 			vecs:    makeDateStringToTimestampVectors("2022-01-01", true),
 			proc:    testutil.NewProc(),
-			want:    []types.Timestamp{types.FromClockUTC(2022, 1, 1, 0, 0, 0, 0)},
+			want:    []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)},
 			contain: false,
 		},
 		{
 			name:    "TEST02",
 			vecs:    makeDateStringToTimestampVectors("2022-01-01 00:00:00", true),
 			proc:    testutil.NewProc(),
-			want:    []types.Timestamp{types.FromClockUTC(2022, 1, 1, 0, 0, 0, 0)},
+			want:    []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)},
 			contain: false,
 		},
 		{