aboutsummaryrefslogtreecommitdiffhomepage
path: root/timezone
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-03-01 21:24:58 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-03-01 21:43:04 -0800
commit0c7039de0e22587913e23988a4e2d6c06fd3bb34 (patch)
tree62a9372254418f7f87bd422583bb2fce903df939 /timezone
parentf110384f113002ad022c677ea138a0f77fad4a62 (diff)
Entries date should contains user timezone (API)
Diffstat (limited to 'timezone')
-rw-r--r--timezone/timezone.go47
-rw-r--r--timezone/timezone_test.go75
2 files changed, 122 insertions, 0 deletions
diff --git a/timezone/timezone.go b/timezone/timezone.go
new file mode 100644
index 0000000..96b7fe4
--- /dev/null
+++ b/timezone/timezone.go
@@ -0,0 +1,47 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package timezone
+
+import (
+ "time"
+)
+
+// Convert converts provided date time to actual timezone.
+func Convert(tz string, t time.Time) time.Time {
+ userTimezone := getLocation(tz)
+
+ if t.Location().String() == "" {
+ // In this case, the provided date is already converted to the user timezone by Postgres,
+ // but the timezone information is not set in the time struct.
+ // We cannot use time.In() because the date will be converted a second time.
+ t = time.Date(
+ t.Year(),
+ t.Month(),
+ t.Day(),
+ t.Hour(),
+ t.Minute(),
+ t.Second(),
+ t.Nanosecond(),
+ userTimezone,
+ )
+ } else if t.Location() != userTimezone {
+ t = t.In(userTimezone)
+ }
+
+ return t
+}
+
+// Now returns the current time with the given timezone.
+func Now(tz string) time.Time {
+ return time.Now().In(getLocation(tz))
+}
+
+func getLocation(tz string) *time.Location {
+ loc, err := time.LoadLocation(tz)
+ if err != nil {
+ loc = time.Local
+ }
+ return loc
+}
diff --git a/timezone/timezone_test.go b/timezone/timezone_test.go
new file mode 100644
index 0000000..eb960f2
--- /dev/null
+++ b/timezone/timezone_test.go
@@ -0,0 +1,75 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package timezone
+
+import (
+ "testing"
+ "time"
+)
+
+func TestNow(t *testing.T) {
+ tz := "Europe/Paris"
+ now := Now(tz)
+
+ if now.Location().String() != tz {
+ t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), tz)
+ }
+}
+
+func TestNowWithInvalidTimezone(t *testing.T) {
+ tz := "Invalid Timezone"
+ expected := time.Local
+ now := Now(tz)
+
+ if now.Location().String() != expected.String() {
+ t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), expected)
+ }
+}
+
+func TestConvertTimeWithNoTimezoneInformation(t *testing.T) {
+ tz := "Canada/Pacific"
+ input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.FixedZone("", 0))
+ output := Convert(tz, input)
+
+ if output.Location().String() != tz {
+ t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
+ }
+
+ hours, minutes, secs := output.Clock()
+ if hours != 14 || minutes != 2 || secs != 3 {
+ t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
+ }
+}
+
+func TestConvertTimeWithDifferentTimezone(t *testing.T) {
+ tz := "Canada/Central"
+ input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.UTC)
+ output := Convert(tz, input)
+
+ if output.Location().String() != tz {
+ t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
+ }
+
+ hours, minutes, secs := output.Clock()
+ if hours != 8 || minutes != 2 || secs != 3 {
+ t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
+ }
+}
+
+func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
+ tz := "Canada/Central"
+ loc, _ := time.LoadLocation(tz)
+ input := time.Date(2018, 3, 1, 14, 2, 3, 0, loc)
+ output := Convert(tz, input)
+
+ if output.Location().String() != tz {
+ t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
+ }
+
+ hours, minutes, secs := output.Clock()
+ if hours != 14 || minutes != 2 || secs != 3 {
+ t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
+ }
+}