Go: time: time.Local is always UTC on Android

Created on 22 May 2017  路  11Comments  路  Source: golang/go

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

Most helpful comment

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
}

All 11 comments

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

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

Yes, create a separate issue and reference this one.

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
}

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.

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

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

Do we have progress on this?

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)...

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.

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

Was this page helpful?
0 / 5 - 0 ratings