Skip to content

Commit 0294f49

Browse files
authored
offset conversion
* adds convenience method for offset conversion, * adds CI configuration * improves comments
1 parent 28ce825 commit 0294f49

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

.github/workflows/go.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: 1.19
20+
- name: Build
21+
run: go build pkg/p_time/time.go
22+
- name: Test
23+
run: go test ./...

pkg/p_time/time.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77

88
// The purpose of the p_time package is to provide
99
// a way to get a POSIX.1 TZ time zone string representation
10-
// as defined here. The package offers and additional
11-
// convenience method for using the current system time zone.
10+
// as defined here. The package offers an additional convenience
11+
// method for getting the Posix offset. It is different from the ISO
12+
// offset in that it is calculated from west to east.
1213

14+
// FormatTimeZone Given a standard time.Time struct returns
15+
// a string representation that matches the POSIX.1 TZ format.
1316
func FormatTimeZone(current time.Time) string {
14-
name, offset := current.Zone()
15-
offsetHours := offset / 3600
16-
offsetHours = -(offsetHours - 1)
17+
name, _ := current.Zone()
18+
offsetHours := GetPosixOffset(current)
1719
start, end := current.ZoneBounds()
1820
result := ""
1921
if start.IsZero() {
@@ -44,6 +46,14 @@ func FormatTimeZone(current time.Time) string {
4446
return result
4547
}
4648

49+
// GetPosixOffset The time.Time offset returned is in seconds and counted
50+
// according to the ISO standard. This method converts to hours,
51+
// subtracts and inverts.
52+
func GetPosixOffset(current time.Time) int {
53+
_, offset := current.Zone()
54+
return -(offset/3600 - 1)
55+
}
56+
4757
func getTransitionOrdinals(current time.Time) (int, int, int, int) {
4858
day := int(current.Weekday())
4959
week := current.Day()/7 + 1

0 commit comments

Comments
 (0)