golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.62k stars 17.61k forks source link

time: time.Local is always UTC on Android #20455

Open tmm1 opened 7 years ago

tmm1 commented 7 years ago

initLocal() in zoneinfo_android.go is hard-coded to UTC: https://github.com/golang/go/blob/bc495c5751201854366b422e5a642ac55b42414a/src/time/zoneinfo_android.go#L43-L46

func initLocal() {
    // TODO(elias.naur): getprop persist.sys.timezone
    localLoc = *UTC
}

x/mobile currently uses a hack to set time.Local for gomobile android apps (https://github.com/golang/mobile/commit/730f563fbc4684e73445e98ea49d9f2b5b09a58c), but that predates the availability of tzdata on android (80b31c05e6ae37c09162406590b9e3b99f0fff9b).

cc @eliasnaur @crawshaw #13581 #10857

gopherbot commented 7 years ago

CL https://golang.org/cl/43754 mentions this issue.

gwik commented 7 years ago

Same issue on iOS, should I create a separate issue ?

eliasnaur commented 7 years ago

Yes, create a separate issue and reference this one.

tmm1 commented 6 years ago

Since I wasn't able to write a patch to fix this in golang, I ended up calling this from init() in my app instead:

func fixTimezone() {
    out, err := exec.Command("/system/bin/getprop", "persist.sys.timezone").Output()
    if err != nil {
        return
    }
    z, err := time.LoadLocation(strings.TrimSpace(string(out)))
    if err != nil {
        return
    }
    time.Local = z
}
tmm1 commented 6 years ago

I found out there's a C api available for fetching properties, which should make it much simpler to support Android timezones from golang directly (via cgo): __system_property_get in <sys/system_properties.h>

My previous attempt in CL43754 was abandoned because it was too hard to call os/exec from the time package.

eliasnaur commented 6 years ago

Good catch. If you mail a CL before the end of this month, it should be eligible for Go 1.11.

tmm1 commented 6 years ago

Good catch. If you mail a CL before the end of this month, it should be eligible for Go 1.11.

I don't plan to submit any CLs, but others should feel free to do so.

You will need to first get a decision one way or another about whether the time package is allowed to depend on cgo, as originally proposed in #20797

uudashr commented 5 years ago

Do we have progress on this?

grantbow commented 5 years ago

zoneinfo_android.go at line 24 appears to show this is still a problem. It took me a while to understand this was the problem I encountered. I found a way to work around it. In case anyone else is interested I set export TZ="$(getprop persist.sys.timezone)" in my bashrc and in my golang program (edited a bit for brevity) tz := os.Getenv("TZ") then loc, _ = time.LoadLocation(tz) and finally ...time.Time.In(loc)...

eliasnaur commented 5 years ago

CL 43754 previously proposed as a fix for this issue also ran getprop. If you're feeling adventurous, you could attempt to fix that CL by converting calls to os/exec with calls to syscall as suggested in the comments for the change.

huangdiv commented 4 years ago

It is till a problem when I run hugo in Termux on my android device, any progress?