Skip to content

Commit

Permalink
android: fix timezone so the BitBox02 shows the correct time
Browse files Browse the repository at this point in the history
On Android, due to a bug in
Go (golang/go#20455), the local timezone is
not correct. It is always UTC.

As a result, when restoring a backup on the BitBox02, the BitBox02
displays the current time in UTC instead of the local time.

This commit contains a workaround to fix the timezone on Android.

Alternative considered: getting the timezone offset in native Android code and sending it
to Go-land via backend.Environment. I did not pursue this as a quick
search didn't turn up an easy way to get the timezone offset on
Android which takes into account daylight-savings etc.
  • Loading branch information
benma committed Oct 4, 2022
1 parent 1b6370c commit 02aac41
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions frontends/android/goserver/goserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ package goserver
import (
"io"
"log"
"os/exec"
"strings"
"sync"
"time"

"github.com/digitalbitbox/bitbox-wallet-app/backend/bridgecommon"
"github.com/digitalbitbox/bitbox-wallet-app/backend/devices/usb"
Expand All @@ -30,6 +33,29 @@ var (
once sync.Once
)

// fixTimezone sets the local timezone on Android. This is a workaround to the bug that on Android,
// time.Local is hard-coded to UTC. See https://github.com/golang/go/issues/20455.
//
// We need the correct timezone to be able to send the `time.Now().Zone()` offset to the BitBox02.
// Without it, the BitBox02 will always display UTC time instad of local time.
//
// This fix is copied from https://github.com/golang/go/issues/20455#issuecomment-342287698.
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
}

func init() {
fixTimezone()
}

// the Go*-named interfaces are implemented in Java for the mobile client. The "Go" prefix is so
// that the code is more readable in Java (interfaces coming from Go-land). The implemented
// interfaces are than translated to implement backend.Environment (see see backendEnvironment
Expand Down

0 comments on commit 02aac41

Please sign in to comment.