Coder Social home page Coder Social logo

go-timezone-local's Introduction

Get the full name of your local time zone (OS setting)

Works on windows, linux and macos

Why?

built-in functionality sometimes won't suffice:

    zone, _ := time.Now().Zone() // try to get my time zone...
    loc, _ := time.LoadLocation(zone)
    fmt.Println(zone, loc) // prints e.g. CEST UTC -> obviously wrong!

localizing a date with obtained loc will cause

panic: time: missing Location in call to Date


Package Usage

go get github.com/thlib/go-timezone-local/tzlocal

See it in action:

Open your project folder
Create a file main.go

package main

import (
    "fmt"
    "time"

    "github.com/thlib/go-timezone-local/tzlocal"
)

func main() {
    tzname, err := tzlocal.RuntimeTZ()
    fmt.Println(tzname, err)

    // example:
    // tzname = "Europe/Berlin"

    // now you can use tzname to properly set up a location:
    loc, _ := time.LoadLocation(tzname)

    d0 := time.Date(2021, 10, 30, 20, 0, 0, 0, loc) // DST active:
    fmt.Println(d0)
    // 2021-10-30 20:00:00 +0200 CEST

    d1 := d0.AddDate(0, 0, 1) // add one day, now DST is inactive:
    fmt.Println(d1)
    // 2021-10-31 20:00:00 +0100 CET
}

Run the following commands:

go mod init example.com/yourpackage
go mod vendor
go run main.go

It should print the go runtime timezone.

For contributors to update the list of time zones on windows

Clone github.com/thlib/go-timezone-local
Change directory to go-timezone-local

cd go-timezone-local
go generate ./...

Credits

All credit goes to colm.anseo and MrFuppes for providing the following answers:

go-timezone-local's People

Contributors

timohuovinen avatar fobersteiner 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.