Skip to content

Commit

Permalink
add some time util
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTrustyDev committed Oct 3, 2023
1 parent 37ff764 commit 972a08a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
37 changes: 36 additions & 1 deletion utils/time_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package utils

import "time"
import (
"fmt"
"math"
"time"
)

// NowS returns the current epoch seconds
func NowS() int64 {
Expand All @@ -21,3 +25,34 @@ func DiffS(previous int64) int64 {
func DiffMs(previous int64) int64 {
return NowMs() - previous
}

// GetLocationFromUtcTimezone returns location corresponding to specified UTC-based timezone
func GetLocationFromUtcTimezone(utcTimezone int) *time.Location {
ensureUtcTimezone(utcTimezone)
return time.FixedZone(GetUtcName(utcTimezone), utcTimezone*60*60)
}

// GetUtcName returns naming convention of UTC timezone. Eg: 7 => UTC+0700
func GetUtcName(utcTimezone int) string {
ensureUtcTimezone(utcTimezone)
return fmt.Sprintf("UTC%s", getTimezoneSuffix(utcTimezone))
}

// ensureUtcTimezone will panic if timezone is out of range from -12 to 14
func ensureUtcTimezone(utcTimezone int) {
if utcTimezone < -12 || utcTimezone > 14 {
panic(fmt.Errorf("UTC timezone must be in range -12 to 14"))
}
}

func getTimezoneSuffix(timezone int) string {
if timezone > 9 {
return fmt.Sprintf("+%d00", timezone)
} else if timezone >= 0 {
return fmt.Sprintf("+0%d00", timezone)
} else if timezone >= -9 {
return fmt.Sprintf("-0%d00", int(math.Abs(float64(timezone))))
} else {
return fmt.Sprintf("%d00", timezone)
}
}
78 changes: 78 additions & 0 deletions utils/time_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"github.com/EscanBE/go-lib/test_utils"
"testing"
"time"
)
Expand Down Expand Up @@ -45,3 +46,80 @@ func TestNowS(t *testing.T) {
t.Errorf("NowS() = %v, expected %v +-1!", nowS1, nowS2)
}
}

func TestGetLocationFromUtcTimezone(t *testing.T) {
t.Run("get location for UTC from -12 to 14", func(_ *testing.T) {
for timezone := -12; timezone <= 14; timezone++ {
loc := GetLocationFromUtcTimezone(timezone)
nowUTC := time.Date(2023, 9, 16, 0, 0, 0, 0, time.UTC)
nowWithCustomLoc := nowUTC.In(loc)
diffHours := (nowWithCustomLoc.Hour() + 24*nowWithCustomLoc.Day()) - (nowUTC.Hour() + 24*nowUTC.Day())
if diffHours != timezone {
t.Errorf("Expected diff timezone %d but got %d", timezone, diffHours)
}
}
})

for timezone := -100; timezone <= 100; timezone++ {
wantPanic := timezone < -12 || timezone > 14
t.Run(fmt.Sprintf("timezone %d %s", timezone, func() string {
if wantPanic {
return "should panic"
} else {
return "should not panic"
}
}()), func(_ *testing.T) {
defer test_utils.DeferWantPanicDepends(t, wantPanic)
_ = GetLocationFromUtcTimezone(timezone)
})
}
}

func TestGetUtcName(t *testing.T) {
tests := []struct {
utcTimezone int
want string
}{
{
utcTimezone: -12,
want: "UTC-1200",
},
{
utcTimezone: -1,
want: "UTC-0100",
},
{
utcTimezone: 0,
want: "UTC+0000",
},
{
utcTimezone: 1,
want: "UTC+0100",
},
{
utcTimezone: 14,
want: "UTC+1400",
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := GetUtcName(tt.utcTimezone); got != tt.want {
t.Errorf("GetUtcName() = %v, want %v", got, tt.want)
}
})
}

for timezone := -100; timezone <= 100; timezone++ {
wantPanic := timezone < -12 || timezone > 14
t.Run(fmt.Sprintf("timezone %d %s", timezone, func() string {
if wantPanic {
return "should panic"
} else {
return "should not panic"
}
}()), func(_ *testing.T) {
defer test_utils.DeferWantPanicDepends(t, wantPanic)
_ = GetUtcName(timezone)
})
}
}

0 comments on commit 972a08a

Please sign in to comment.