Skip to content

Commit

Permalink
Merge pull request #21 from tkuchiki/v0.2.0
Browse files Browse the repository at this point in the history
V0.2.0
  • Loading branch information
tkuchiki authored Jun 29, 2020
2 parents f58f9e3 + 6091dc9 commit 2a169ba
Show file tree
Hide file tree
Showing 12 changed files with 14,619 additions and 1,595 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
push:

jobs:
test:
runs-on: ubuntu-18.04
strategy:
matrix:
go: [ '1.14', '1.13', '1.12' ]
steps:
- uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}
id: go
- run: mkdir -p src/github.com/tkuchiki
- uses: actions/checkout@v2
with:
path: src/github.com/tkuchiki/go-timezone
- run: go test -v
working-directory: /home/runner/work/go-timezone/go-timezone/src/github.com/tkuchiki/go-timezone
env:
GOPATH: /home/runner/work/go-timezone/go-timezone
110 changes: 7 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,113 +1,17 @@
# go-timezone
Timezone utility for Golang

## Example
Timezone utility for Go

### Code
[![GoDocWidget]][GoDocReference]

```go
package main
[GoDocWidget]:https://godoc.org/github.com/tkuchiki/go-timezone?status.svg
[GoDocReference]:https://godoc.org/github.com/tkuchiki/go-timezone

import (
"fmt"
"github.com/tkuchiki/go-timezone"
"time"
)
![Test](https://github.com/tkuchiki/go-timezone/workflows/Test/badge.svg)

func main() {
offset, err := timezone.GetOffset("JST")
fmt.Println(offset, err)
## Data source

offset, err = timezone.GetOffset("hogehoge")
fmt.Println(offset, err)

var zones []string
zones, err = timezone.GetTimezones("UTC")
fmt.Println(zones, err)

zones, err = timezone.GetTimezones("foobar")
fmt.Println(zones, err)

now := time.Now()

fmt.Println("## current timezone")
fmt.Println(now)

var jst time.Time
loc, _ := time.LoadLocation("UTC")
utc := now.In(loc)

jst, _ = timezone.FixedTimezone(utc, "")

fmt.Println("## UTC")
fmt.Println(utc)
fmt.Println("## UTC -> current timezone")
fmt.Println(jst)

var est time.Time
est, _ = timezone.FixedTimezone(now, "America/New_York")
fmt.Println("## current timezone -> EDT")
fmt.Println(est)

offset, _ = timezone.GetOffset("EST")
zone, _ := timezone.GetTimezoneAbbreviation("America/New_York")
fmt.Println("## DST is not considered")
fmt.Println(offset)
fmt.Println(zone)

offset, _ = timezone.GetOffset("EST", true)
zone, _ = timezone.GetTimezoneAbbreviation("America/New_York", true)
fmt.Println("## DST is considered")
fmt.Println(offset)
fmt.Println(zone)
}
```

### Result

```console
# current timezone = UTC
$ TZ=UTC go run /path/to/main.go
32400 <nil>
0 Invalid short timezone: hogehoge
[Antarctica/Troll Etc/UTC Etc/Universal Etc/Zulu UTC Universal Zulu] <nil>
[] Invalid short timezone: foobar
## current timezone
2018-03-15 00:07:01.921041165 +0000 UTC m=+0.000669042
## UTC
2018-03-15 00:07:01.921041165 +0000 UTC
## UTC -> current timezone
2018-03-15 00:07:01.921041165 +0000 UTC
## current timezone -> EDT
2018-03-14 20:07:01.921041165 -0400 EDT
## DST is not considered
-18000
EST
## DST is considered
-14400
EDT

# current timezone = JST
$ TZ=Asia/Tokyo go run /path/to/main.go
32400 <nil>
0 Invalid short timezone: hogehoge
[Antarctica/Troll Etc/UTC Etc/Universal Etc/Zulu UTC Universal Zulu] <nil>
[] Invalid short timezone: foobar
## current timezone
2018-03-15 09:08:58.410680998 +0900 JST m=+0.000536318
## UTC
2018-03-15 00:08:58.410680998 +0000 UTC
## UTC -> current timezone
2018-03-15 09:08:58.410680998 +0900 JST
## current timezone -> EDT
2018-03-14 20:08:58.410680998 -0400 EDT
## DST is not considered
-18000
EST
## DST is considered
-14400
EDT
```
https://github.com/tkuchiki/timezones

# Contributors

Expand Down
122 changes: 122 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package timezone_test

import (
"fmt"
"time"

"github.com/tkuchiki/go-timezone"
)

func ExampleTimezone_GetTzAbbreviationInfo() {
tz := timezone.New()
tzAbbrInfos, _ := tz.GetTzAbbreviationInfo("EET")

fmt.Println(tzAbbrInfos[0].Name())
fmt.Println(tzAbbrInfos[0].Offset())
fmt.Println(tzAbbrInfos[0].OffsetHHMM())
// Output:
// Eastern European Time/Eastern European Standard Time
// 7200
// +02:00
}

func ExampleTimezone_GetTzAbbreviationInfo_ambiguousTimezoneAbbreviationsError() {
tz := timezone.New()
tzAbbrInfos, _ := tz.GetTzAbbreviationInfo("BST")

fmt.Println(tzAbbrInfos[0].Name())
fmt.Println(tzAbbrInfos[0].Offset())
fmt.Println(tzAbbrInfos[0].OffsetHHMM())
fmt.Println(tzAbbrInfos[1].Name())
fmt.Println(tzAbbrInfos[1].Offset())
fmt.Println(tzAbbrInfos[1].OffsetHHMM())
fmt.Println(tzAbbrInfos[2].Name())
fmt.Println(tzAbbrInfos[2].Offset())
fmt.Println(tzAbbrInfos[2].OffsetHHMM())
// Output:
// Bolivia Summer Time
// -12756
// -03:27
// British Summer Time
// 3600
// +01:00
// Bougainville Standard Time
// 39600
// +11:00
}

func ExampleTimezone_GetTzAbbreviationInfoByTZName() {
tz := timezone.New()
tzAbbrInfo, _ := tz.GetTzAbbreviationInfoByTZName("BST", "British Summer Time")

fmt.Println(tzAbbrInfo.Name())
fmt.Println(tzAbbrInfo.Offset())
fmt.Println(tzAbbrInfo.OffsetHHMM())
// Output:
// British Summer Time
// 3600
// +01:00
}

func ExampleTimezone_GetTimezones() {
tz := timezone.New()
timezones, _ := tz.GetTimezones("UTC")

fmt.Println(timezones)
// Output:
// [Etc/UCT Etc/UTC Etc/Universal Etc/Zulu UCT UTC Universal Zulu]
}

func ExampleTimezone_FixedTimezone() {
tz := timezone.New()

_time := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
fixedTime, _ := tz.FixedTimezone(_time, "Europe/Belgrade")

fmt.Println(fixedTime)
// Output:
// 2020-01-01 01:00:00 +0100 CET
}

func ExampleTimezone_GetTzInfo() {
tz := timezone.New()
tzInfo, _ := tz.GetTzInfo("Europe/London")

fmt.Println(tzInfo.LongStandard())
fmt.Println(tzInfo.ShortStandard())
fmt.Println(tzInfo.StandardOffset())
fmt.Println(tzInfo.StandardOffsetHHMM())
fmt.Println(tzInfo.LongDaylight())
fmt.Println(tzInfo.ShortDaylight())
fmt.Println(tzInfo.DaylightOffset())
fmt.Println(tzInfo.DaylightOffsetHHMM())
// Output:
// Greenwich Mean Time
// GMT
// 0
// +00:00
// British Summer Time
// BST
// 3600
// +01:00
}

func ExampleTimezone_GetTimezoneAbbreviation() {
tz := timezone.New()

abbr, _ := tz.GetTimezoneAbbreviation("Europe/London")

fmt.Println(abbr)
// Output:
// GMT
}

func ExampleTimezone_GetTimezoneAbbreviation_dst() {
tz := timezone.New()

abbr, _ := tz.GetTimezoneAbbreviation("Europe/London", true)

fmt.Println(abbr)
// Output:
// BST
}
Loading

0 comments on commit 2a169ba

Please sign in to comment.