]> git.earman.xyz Git - sensor-watch.git/commitdiff
fix time zones in simulator
authorjoeycastillo <joeycastillo@utexas.edu>
Mon, 7 Oct 2024 12:36:23 +0000 (08:36 -0400)
committerjoeycastillo <joeycastillo@utexas.edu>
Mon, 7 Oct 2024 12:36:23 +0000 (08:36 -0400)
movement.c
watch-library/simulator/watch/watch_rtc.c

index ee8f6cc6b14a8b1d23eb535503d1f66a45308215..28aec82fd99d90707c26d305d454d1ee9502bc37 100644 (file)
@@ -557,19 +557,6 @@ void app_init(void) {
     _movement_reset_inactivity_countdown();
 
     filesystem_init();
-
-#if __EMSCRIPTEN__
-    /// FIXME: for #SecondMovement: Figure out how to set the time zone automatically! This method no longer works.
-    // int32_t time_zone_offset = EM_ASM_INT({
-    //     return -new Date().getTimezoneOffset();
-    // });
-    // for (int i = 0, count = sizeof(movement_timezone_offsets) / sizeof(movement_timezone_offsets[0]); i < count; i++) {
-    //     if (movement_timezone_offsets[i] == time_zone_offset) {
-    //         movement_state.settings.bit.time_zone = i;
-    //         break;
-    //     }
-    // }
-#endif
 }
 
 void app_wake_from_backup(void) {
@@ -595,6 +582,18 @@ void app_setup(void) {
         // populate the DST offset cache
         _movement_update_dst_offset_cache();
 
+#if __EMSCRIPTEN__
+        int32_t time_zone_offset = EM_ASM_INT({
+            return -new Date().getTimezoneOffset();
+        });
+        for (int i = 0; i < NUM_ZONE_NAMES; i++) {
+            if (movement_get_current_timezone_offset_for_zone(i) == time_zone_offset * 60) {
+                movement_state.settings.bit.time_zone = i;
+                break;
+            }
+        }
+#endif
+
         // set up the 1 minute alarm (for background tasks and low power updates)
         watch_date_time_t alarm_time;
         alarm_time.reg = 0;
index 852bf16eb76b2a93b23af4e78ee16dd46702eddf..f99cb7bcfb1a3c2d6172fe7715339c1a1291d333 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "watch_rtc.h"
 #include "watch_main_loop.h"
+#include "watch_utility.h"
 
 #include <emscripten.h>
 #include <emscripten/html5.h>
@@ -61,6 +62,9 @@ void watch_rtc_set_date_time(watch_date_time_t date_time) {
 
 watch_date_time_t watch_rtc_get_date_time(void) {
     watch_date_time_t retval;
+    int32_t time_zone_offset = EM_ASM_INT({
+        return -new Date().getTimezoneOffset() * 60;
+    });
     retval.reg = EM_ASM_INT({
         const date = new Date(Date.now() + $0);
         return date.getSeconds() |
@@ -70,6 +74,7 @@ watch_date_time_t watch_rtc_get_date_time(void) {
             ((date.getMonth() + 1) << 22) |
             ((date.getFullYear() - 2020) << 26);
     }, time_offset);
+    retval = watch_utility_date_time_convert_zone(retval, time_zone_offset, 0);
     return retval;
 }