From dd230da77effaf5f2285c44f056a27abe3a6110d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=C5=A0ari=C4=87?= Date: Sat, 22 Oct 2022 10:57:20 +0200 Subject: [PATCH] fixes error due to bad test --- pkg/p_time/time.go | 37 +++++++++++++++++++++++-------------- pkg/p_time/time_test.go | 6 +++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pkg/p_time/time.go b/pkg/p_time/time.go index 4ad1d01..ad144c7 100644 --- a/pkg/p_time/time.go +++ b/pkg/p_time/time.go @@ -7,8 +7,8 @@ import ( // The purpose of the p_time package is to provide // a way to get a POSIX.1 TZ time zone string representation -// as defined in the UNIX Standard. The package offers an additional -// convenience method for getting the Posix offset. It is different +// as defined in the UNIX Standard. The package offers an additional +// convenience method for getting the Posix offset. It is different // from the ISO offset in that it is calculated from west to east. // FormatTimeZone Given a standard time.Time struct returns @@ -18,23 +18,23 @@ func FormatTimeZone(current time.Time) string { offsetHours := GetPosixOffset(current) start, end := current.ZoneBounds() result := "" - + if start.IsZero() { return fmt.Sprintf("%s%d", name, offsetHours) } - - endplus1 := end.Add(time.Hour * 25) - nameOfDST, _ := endplus1.Zone() + endPlus1 := end.Add(time.Hour * 25) + nameOfDST, _ := endPlus1.Zone() firstName := name secondName := nameOfDST if current.IsDST() { firstName = nameOfDST secondName = name + start, end = endPlus1.ZoneBounds() } - m1, w1, d1, h1 := getTransitionOrdinals(start) - m2, w2, d2, h2 := getTransitionOrdinals(end) + m1, w1, d1, h1 := getTransitionOrdinals(end) + m2, w2, d2, h2 := getTransitionOrdinals(start) h1-- h2++ @@ -51,23 +51,32 @@ func FormatTimeZone(current time.Time) string { } // GetPosixOffset The time.Time offset returned is in seconds and counted -// according to the ISO standard. The function converts to hours, -// subtracts and inverts. +// according to the ISO standard. The function converts to hours and inverts. func GetPosixOffset(current time.Time) int { _, offset := current.Zone() - return -(offset/3600 - 1) + + if current.IsDST() { + _, end := current.ZoneBounds() + endPlus1 := end.Add(time.Hour * 25) + _, offset = endPlus1.Zone() + } + + return -(offset / 3600) } func getTransitionOrdinals(current time.Time) (int, int, int, int) { day := int(current.Weekday()) - week := current.Day()/7 + 1 + week := current.Day() / 7 - if current.AddDate(0, 0, 7).Month() != current.Month() && week != 5 { + if current.Day()%7 != 0 { week++ } + if week == 4 { + week = 5 + } month := int(current.Month()) hour := current.Hour() return month, week, day, hour -} +} \ No newline at end of file diff --git a/pkg/p_time/time_test.go b/pkg/p_time/time_test.go index f30902f..c2a65b1 100644 --- a/pkg/p_time/time_test.go +++ b/pkg/p_time/time_test.go @@ -12,7 +12,7 @@ const newyorkZone = "America/New_York" const errorTemplate = "Time zone string for test time %s is not correct. Expected %s but got %s" func TestFormatTimeZoneCET(t *testing.T) { - testFormatTimeZone(t, zagrebZone, expectedCET, 2022, 4, 12) + testFormatTimeZone(t, zagrebZone, expectedCET, 2022, 2, 12) } func TestFormatTimeZoneCETDSTFirst(t *testing.T) { @@ -20,7 +20,7 @@ func TestFormatTimeZoneCETDSTFirst(t *testing.T) { } func TestFormatTimeZoneEST(t *testing.T) { - testFormatTimeZone(t, newyorkZone, expectedEST, 2022, 4, 12) + testFormatTimeZone(t, newyorkZone, expectedEST, 2022, 2, 12) } func TestFormatTimeZoneESTDSTFirst(t *testing.T) { @@ -34,4 +34,4 @@ func testFormatTimeZone(t *testing.T, location, expectedFormat string, year, mon if formatted != expectedFormat { t.Fatalf(errorTemplate, testTime, expectedFormat, formatted) } -} +} \ No newline at end of file