Created
August 24, 2023 05:30
-
-
Save santosh/4fd7137ae76a85e4561f1c86b8526126 to your computer and use it in GitHub Desktop.
Convert from one timezone to another in Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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