Code Monkey home page Code Monkey logo

go-timezone's Introduction

go-timezone

Timezone utility for Golang

Example

Code

package main

import (
    "fmt"
    "github.com/tkuchiki/go-timezone"
    "time"
)

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

	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

# 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

Contributors

go-timezone's People

Contributors

tkuchiki avatar alex-tan avatar kkavchakht avatar scottleedavis avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.