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 bf91c80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@
- Render number of blocks scanned and percentage progress using fixed-width digits for a more stable UI
- Transaction details: show fiat value at time of transaction
- Android: more modern look by changing the status bar color to white while the app is running
- Android: fix time shown on BitBox02 when restoring a backup (it was shown in UTC instead of local time)
- Fix update balance after transaction sent
- Fix missing utxos update after new transaction is sent
- Add attestation check on device setting
Expand Down
26 changes: 26 additions & 0 deletions frontends/android/goserver/goserver.go
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 bf91c80

Please sign in to comment.