Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tmm1 opened this issue May 22, 2017 · 11 comments
Open

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

tmm1 opened this issue May 22, 2017 · 11 comments
Milestone

Comments

@tmm1
Copy link
Contributor

@tmm1 tmm1 commented May 22, 2017

initLocal() in zoneinfo_android.go is hard-coded to UTC:

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

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 (golang/mobile@730f563), but that predates the availability of tzdata on android (80b31c0).

cc @eliasnaur @crawshaw #13581 #10857

@bradfitz bradfitz added this to the Unreleased milestone May 22, 2017
@gopherbot
Copy link

@gopherbot gopherbot commented May 22, 2017

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

@gwik
Copy link
Contributor

@gwik gwik commented Jun 26, 2017

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

@eliasnaur
Copy link
Contributor

@eliasnaur eliasnaur commented Jun 26, 2017

Yes, create a separate issue and reference this one.

@tmm1
Copy link
Contributor Author

@tmm1 tmm1 commented Nov 6, 2017

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
Copy link
Contributor Author

@tmm1 tmm1 commented Apr 6, 2018

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
Copy link
Contributor

@eliasnaur eliasnaur commented Apr 6, 2018

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

@tmm1
Copy link
Contributor Author

@tmm1 tmm1 commented Apr 6, 2018

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
Copy link

@uudashr uudashr commented Jan 20, 2019

Do we have progress on this?

@grantbow
Copy link

@grantbow grantbow commented Jun 21, 2019

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
Copy link
Contributor

@eliasnaur eliasnaur commented Jun 21, 2019

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
Copy link

@huangdiv huangdiv commented Dec 30, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.