Skip to content

Commit

Permalink
fixes error due to bad test
Browse files Browse the repository at this point in the history
  • Loading branch information
isaric authored Oct 22, 2022
1 parent 1f55479 commit dd230da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
37 changes: 23 additions & 14 deletions pkg/p_time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
Expand All @@ -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
}
}
6 changes: 3 additions & 3 deletions pkg/p_time/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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) {
testFormatTimeZone(t, zagrebZone, expectedCET, 2022, 7, 12)
}

func TestFormatTimeZoneEST(t *testing.T) {
testFormatTimeZone(t, newyorkZone, expectedEST, 2022, 4, 12)
testFormatTimeZone(t, newyorkZone, expectedEST, 2022, 2, 12)
}

func TestFormatTimeZoneESTDSTFirst(t *testing.T) {
Expand All @@ -34,4 +34,4 @@ func testFormatTimeZone(t *testing.T, location, expectedFormat string, year, mon
if formatted != expectedFormat {
t.Fatalf(errorTemplate, testTime, expectedFormat, formatted)
}
}
}

0 comments on commit dd230da

Please sign in to comment.