I have the below code:
package main
import (
"fmt"
"time"
)
func main() {
loc := "Jan 06"
date := "2020-12-31 16:00:00 +0000 UTC"
layout := "2006-01-02 15:04:05 -0700 MST"
t, err := time.Parse(layout, date)
if err != nil {
fmt.Print("Error")
}
fmt.Println(t)
GetFormattedDate(t, loc)
}
func GetFormattedDate(date time.Time, layout string) (string, error) {
fmt.Println("\n\n====================================================")
fmt.Println("time.Now(): ", time.Now())
fmt.Println("time.Now().Location(): ", time.Now().Location())
fmt.Println("Converting: ", date)
configLocale := "Singapore"
fmt.Println("configLocale: ", configLocale)
loc, err := time.LoadLocation(configLocale)
fmt.Println("loc: ", loc)
if err != nil {
return "", err
}
date = date.In(loc)
fmt.Println("date.In(loc): ", date)
fmt.Println("layout: ", layout)
converted := fmt.Sprintf(date.Format(layout))
fmt.Println("converted fmt: ", converted)
fmt.Println("\n\n==================================================")
return fmt.Sprintf(date.Format(layout)), nil
}
When I run it locally I get:
====================================================
time.Now(): 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
time.Now().Location(): Local
Converting: 2020-12-31 16:00:00 +0000 UTC
configLocale: Singapore
loc: Singapore
date.In(loc): 2021-01-01 00:00:00 +0800 +08 // This is the issue
layout: Jan 06
converted fmt: Jan 21
==================================================
But when I run the same code on my linux box server, I get:
time.Now(): 2020-10-24 09:04:22.256288497 +0000 UTC m=+252.011109438
time.Now().Location(): UTC
Converting: 2020-12-31 16:00:00 +0000 UTC
msg":"configLocale: Singapore
msg":"loc: Singapore
date.In(loc): 2020-12-31 16:00:00 +0000 +0000 // This is the issue
layout: Jan 06
converted fmt: Dec 20
What I am trying to do:
I am trying to convert: 2020-12-31 16:00:00 +0000 UTC into a locale specific date, locale being Singapore. My layout is Jan 06
For one machine this date converts to Dec 20 and for another it converts to Jan 21
Specifically when I dig deeper, I feel the method date = date.In(loc) returns different result on different machines despite setting location to Singapore and passing it explicitly, as is evident in the logs.
However on the server commandline:
/ # zdump Singapore
Singapore Mon Oct 26 02:10:18 2020 +08
Any suggestions how do I solve this issue?