Skip to content

Instantly share code, notes, and snippets.

@santosh
Created August 24, 2023 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santosh/4fd7137ae76a85e4561f1c86b8526126 to your computer and use it in GitHub Desktop.
Save santosh/4fd7137ae76a85e4561f1c86b8526126 to your computer and use it in GitHub Desktop.
Convert from one timezone to another in Go.
package main
import (
"fmt"
"time"
)
// tsConvert convert timestamp in "YYYY-MM-DDTHH:MM" format from one timezone to another
func tsConvert(ts string, fromTZ string, toTZ string) (string, error) {
la, _ := time.LoadLocation(fromTZ)
jeru, _ := time.LoadLocation(toTZ)
// clean the input
ts = fmt.Sprintf("%s:00", ts)
// parse timestamp in source timezone
laT, _ := time.ParseInLocation("2006-01-02T15:04:05", ts, la)
// time in destination timezone
jeruT := laT.In(jeru)
// return destination time in "YYYY-MM-DDTHH:MM:SS" format
return jeruT.Format("2006-01-02T15:04:05"), nil
}
func main() {
ts := "2021-03-08T19:12"
out, err := tsConvert(ts, "America/Los_Angeles", "Asia/Jerusalem")
if err != nil {
fmt.Printf("error: %s", err)
return
}
fmt.Printf("out: %s", out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment