newsbox/internal/time.go
Adriano Caloiaro 8ae801ae5f
Initial commit
2023-02-15 08:57:48 -08:00

31 lines
800 B
Go

package internal
import (
"fmt"
"time"
)
// TimeInZone returns the current time in the time zone represented as an offset from UTC
func TimeInZone(utcOffset int) time.Time {
loc := ZoneLocation(utcOffset)
t := time.Now()
return t.In(loc)
}
// ZoneName returns the name of the time zone represented as an offset from UTC
func ZoneName(utcOffset int) string {
direction := ""
if utcOffset >= 0 {
direction = "+"
}
return fmt.Sprintf("UTC%s%d", direction, utcOffset)
}
// ZoneLocation returns a Location that represents the time zone with the given offset from UTC
func ZoneLocation(utcOffset int) *time.Location {
// 60 * 60 are the number of minutes and seconds _east_ of UTC
// This number can be positive or negative
return time.FixedZone(ZoneName(utcOffset), utcOffset*60*60)
}