修复时间存储以及更新问题
authorCaisking <Casiking@zhihuishixun.com>
Fri, 23 May 2025 09:32:18 +0000 (17:32 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Fri, 23 May 2025 09:32:18 +0000 (17:32 +0800)
code/sdk/application/brandy/brandy_standard/thread_pri_init.c
code/sdk/middleware/chips/brandy/exception/chip_system_time.c
code/sdk/middleware/chips/brandy/exception/chip_system_time.h
code/sdk/zhihui/services/ble_service/bleservice/bleEvent.c
code/sdk/zhihui/utils/custom_runtime.c
code/sdk/zhihui/utils/custom_utils.c

index 3170c7b..20ad523 100644 (file)
@@ -468,8 +468,8 @@ static void thread_main(const void *unused)
     ConfigModelInit();\r
 //     uapi_runtime_log_service_init();\r
     init_btaddr_btname();\r
-//     uapi_calendar_init();\r
-//     uapi_calendar_recover_date();\r
+    uapi_calendar_init();\r
+    uapi_calendar_recover_date();\r
 // #endif\r
 #if DSP_EXIST == YES\r
     audio_init();\r
index ecef3bb..e3ce9be 100644 (file)
@@ -8,68 +8,44 @@
 
 #include "chip_system_time.h"
 
-#define SECONDS_PER_MINUTE 60 /* second per minute */
-#define MINUTES_PER_HOUR 60   /* second per minute */
-#define MSEC_PER_SEC 1000
-#define USEC_PER_MSEC 1000
-#define TZ_ADDRESS 0x5702C014
-#define MAX_TIMEZONE_HOURS 24
-#define TIME_MAX 64
-#define ONE_DAY_MIN 1440
-#define BEIJING_TIMEZONE 480
-#define MIN_INTERVAL 15
-#define CUSTOM_PER_HOUR (60 / MIN_INTERVAL)
+#define SECONDS_PER_MINUTE      60 /* seconds per minute */
+#define MINUTES_PER_HOUR        60   /* minutes per hour */
+#define MINUTES_PER_QUARTER     15   /* seconds per quarter */
+#define QUARTERS_PER_HOUR       4   /* quarters per hour */
+#define MSEC_PER_SEC            1000
+#define USEC_PER_MSEC           1000
+#define TZ_ADDRESS              0x5702C014
+#define MAX_TIMEZONE_HOURS      24
+#define TIME_MAX                64
 
 void print_time(calendar_t *date)
 {
     printf("CALLENDAR:year = %d, month = %d, day = %d, hour = %d, minute = %d, second = %d \r\n",
-           date->year,
-           date->mon,
-           date->day,
-           date->hour,
-           date->min,
-           date->sec);
+        date->year,
+        date->mon,
+        date->day,
+        date->hour,
+        date->min,
+        date->sec);
 }
 
 void print_time_2(struct tm *date)
 {
     printf("TM:year = %d, month = %d, day = %d, hour = %d, minute = %d, second = %d \r\n",
-           date->tm_year,
-           date->tm_mon,
-           date->tm_mday,
-           date->tm_hour,
-           date->tm_min,
-           date->tm_sec);
+        date->tm_year + 1900,
+        date->tm_mon,
+        date->tm_mday,
+        date->tm_hour,
+        date->tm_min,
+        date->tm_sec);
 }
-
-static uint16_t get_timezone(void)
-{
-    return readw(TZ_ADDRESS) & 0x00FF;
-}
-
-static void set_timezone(uint16_t timezone)
-{
-    uint16_t tz_zone = readw(TZ_ADDRESS) & 0xFF00;
-    tz_zone = tz_zone | timezone;
-    writew(TZ_ADDRESS, tz_zone);
-}
-
-int get_timezone_min(void)
+/**
+ * return quarters for timezone 
+*/
+uint16_t uapi_get_timezone(void)
 {
-    uint16_t sdk_zone = get_timezone();
-    if (sdk_zone > (CUSTOM_PER_HOUR * MAX_TIMEZONE_HOURS))
-        return BEIJING_TIMEZONE;
-    int zone = 0;
-    // 东边 1-12H, 西边 13-24H
-    if (sdk_zone > (12 * CUSTOM_PER_HOUR)) // 西边
-    {
-        zone = (sdk_zone - (12 * CUSTOM_PER_HOUR)) * MIN_INTERVAL * (-1);
-    }
-    else // 东边
-    {
-        zone = sdk_zone * MIN_INTERVAL;
-    }
-    return zone;
+    uint16_t timezone_quarters = readw(TZ_ADDRESS) & 0x00FF;
+    return timezone_quarters ;
 }
 
 void uapi_set_timezone(uint16_t timezone_quarters)
@@ -86,7 +62,7 @@ void save_time(void)
     uapi_calendar_set_timestamp(time_sec);
 }
 
-uint32_t get_current_timestamp(void) // 0时区timestamp
+uint32_t get_current_timestamp(void)
 {
     uint32_t timestamp = 0;
     struct timeval current_time = {0};
@@ -99,26 +75,22 @@ uint32_t get_current_timestamp(void) // 0时区timestamp
     return timestamp;
 }
 
-uint32_t get_local_timestamp(void) // 本地时区timestamp
+uint32_t get_local_timestamp(void)
 {
-    uint32_t timestamp = get_current_timestamp() + get_timezone_min() * SECONDS_PER_MINUTE;
+    uint32_t timestamp = get_current_timestamp() + uapi_get_timezone() * MINUTES_PER_QUARTER * SECONDS_PER_MINUTE;
     return timestamp;
 }
 
-void get_local_time(struct tm *tm)
+void get_current_time(struct timeval *tv, struct tm *tm)
 {
-    uint32_t timestamp = get_current_timestamp();
-    localtime_r(&timestamp, tm);
-    tm->tm_year = tm->tm_year + 1900,
-    tm->tm_mon = tm->tm_mon + 1;
-}
-
-void get_current_time(struct tm *tm)
-{
-    uint32_t timestamp = get_current_timestamp() - (get_timezone_min() * 60);
-    localtime_r(&timestamp, tm);
-    tm->tm_year = tm->tm_year + 1900,
+    char timebuf[TIME_MAX] = {0};
+    if (gettimeofday(tv, NULL)) {
+        return;
+    }
+    localtime_r(&tv->tv_sec, tm);
     tm->tm_mon = tm->tm_mon + 1;
+    printf("get_current_time tv_tv_sec = %d \r\n", tv->tv_sec);
+    print_time_2(tm);
 }
 
 void restore_time(void)
@@ -130,59 +102,83 @@ void restore_time(void)
     uapi_calendar_get_timestamp(&sec);
     printf("callendar timestamp = %lld \r\n", sec);
     sec = sec / MSEC_PER_SEC;
-    // 东边 1-12H, 西边 13-24H, 需在这里转换成 -1到-12
-    uint16_t timezone = get_timezone();
-    int timezone_trans = timezone > (12 * CUSTOM_PER_HOUR) ? ((timezone - (12 * CUSTOM_PER_HOUR)) * (-1)) : timezone;
-    timezone_trans *= MIN_INTERVAL;
-    sync_time(sec, timezone_trans);
+    sync_time(sec, uapi_get_timezone());
 }
-
-uint32_t sync_time(uint32_t timestamp, int timezone)
+uint32_t sync_time_utc(uint32_t timestamp, uint32_t timezone_quarters)
 {
-    printf("sync time timestamp %d, timezone %d \r\n", timestamp, timezone);
+    printf("sync time utc timestamp %d, timezone_in_quarters %d \r\n", timestamp, timezone_quarters);
     uint32_t seconds;
-    if (timezone > ONE_DAY_MIN) {
+    if (timezone_quarters > (MAX_TIMEZONE_HOURS * QUARTERS_PER_HOUR)) {
         return ERRCODE_FAIL;
     }
     if (timestamp == 0) {
         return ERRCODE_FAIL;
+    } 
+#if 0
+    else if (timestamp <= (timezone_quarters * MINUTES_PER_QUARTER * SECONDS_PER_MINUTE)) {
+        seconds = 0;
     } else {
-        seconds = timestamp - (timezone * SECONDS_PER_MINUTE);
+        seconds = timestamp - (timezone_quarters * MINUTES_PER_QUARTER * SECONDS_PER_MINUTE);
     }
-
-    // 东边1-12H, 西边13-24H
-    uint32_t timezone_trans = (timezone / MIN_INTERVAL) < 0 ? abs(timezone / MIN_INTERVAL) + (12 * CUSTOM_PER_HOUR) : (timezone / MIN_INTERVAL);
-    set_timezone(timezone_trans);
+#else
+    seconds = timestamp;
+#endif
+    uapi_set_timezone(timezone_quarters);
     struct timeval tv = {0};
     struct timezone tzone = {0};
     tv.tv_sec = seconds;
     tv.tv_usec = 0;
     tzone.tz_dsttime = 0;
-    tzone.tz_minuteswest = timezone;
+    tzone.tz_minuteswest = timezone_quarters * MINUTES_PER_QUARTER;
     return settimeofday(&tv, &tzone);
 }
 
-void test_bandy_time(void)
+uint32_t sync_time(uint32_t timestamp, uint32_t timezone_quarters)
 {
-    struct tm tm;
-    uint32_t time = 1716044461; // 2024-5-18 15:1:1
-    printf("--------SYNC TIME----------\r\n");
-    get_current_timestamp();
-    get_current_time(&tm);
-    sync_time(time, 480);
-    get_current_timestamp();
-    get_current_time(&tm);
-    printf("--------SAVE TIME----------\r\n");
-    get_current_timestamp();
-    get_current_time(&tm);
-    save_time();
-    get_current_timestamp();
-    get_current_time(&tm);
-    printf("--------RESTORE TIME----------\r\n");
-    get_current_timestamp();
-    get_current_time(&tm);
-    restore_time();
-    get_current_timestamp();
-    get_current_time(&tm);
+    printf("sync time timestamp %d, timezone_in_quarters %d \r\n", timestamp, timezone_quarters);
+    uint32_t seconds;
+    if (timezone_quarters > (MAX_TIMEZONE_HOURS * QUARTERS_PER_HOUR)) {
+        return ERRCODE_FAIL;
+    }
+    if (timestamp == 0) {
+        return ERRCODE_FAIL;
+    } else if (timestamp <= (timezone_quarters * MINUTES_PER_QUARTER * SECONDS_PER_MINUTE)) {
+        seconds = 0;
+    } else {
+        seconds = timestamp - (timezone_quarters * MINUTES_PER_QUARTER * SECONDS_PER_MINUTE);
+    }
+    uapi_set_timezone(timezone_quarters);
+    struct timeval tv = {0};
+    struct timezone tzone = {0};
+    tv.tv_sec = seconds;
+    tv.tv_usec = 0;
+    tzone.tz_dsttime = 0;
+    tzone.tz_minuteswest = timezone_quarters * MINUTES_PER_QUARTER;
+    return settimeofday(&tv, &tzone);
 }
-#endif
+
+// void test_bandy_time(uint32_t time)
+// {
+//     struct tm tm;
+//     struct timeval tv;
+//     uint32_t time = 1716044461;  // 2024-5-18 15:1:1  网站转换:2024-05-18 23:01:01
+//     printf("--------SYNC TIME----------\r\n");
+//     get_current_timestamp();
+//     get_current_time(&tv, &tm);
+//     sync_time(time, 8*QUARTERS_PER_HOUR);
+//     get_current_timestamp();
+//     get_current_time(&tv, &tm);
+//     printf("--------SAVE TIME----------\r\n");
+//     get_current_timestamp();
+//     get_current_time(&tv, &tm);
+//     save_time();
+//     get_current_timestamp();
+//     get_current_time(&tv, &tm);
+//     printf("--------RESTORE TIME----------\r\n");
+//     get_current_timestamp();
+//     get_current_time(&tv, &tm);
+//     restore_time();
+//     get_current_timestamp();
+//     get_current_time(&tv, &tm);
+// }
+#endif
\ No newline at end of file
index 66eb436..a5c58ad 100644 (file)
@@ -7,11 +7,6 @@
 #ifndef CHIP_SYSTEM_TIME_H
 #define CHIP_SYSTEM_TIME_H
 
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
 #ifndef BUILD_APPLICATION_SSB
 #include <stdio.h>
 #include <stdint.h>
@@ -43,9 +38,7 @@ uint32_t get_current_timestamp(void);
  * @brief  Get the current system time as a timeval struct and a tm
  * @return void.
  */
-void get_current_time(struct tm *tm);
-
-void get_local_time(struct tm *tm);
+void get_current_time(struct timeval *tv, struct tm *tm);
 
 /**
  * @brief  Restore the system time from the saved non-volatile
@@ -53,32 +46,29 @@ void get_local_time(struct tm *tm);
  */
 void restore_time(void);
 
-/**
- * @brief  get timezone minute
- * @return timezone minute.
- */
-int get_timezone_min(void);
-
 /**
  * @brief  Synchronize the system time with the given timestamp and timezone
  * @return void.
  */
-uint32_t sync_time(uint32_t timestamp, int timezone);
-
+uint32_t sync_time(uint32_t timestamp, uint32_t timezone_in_quarters);
+/**
+ * @brief  Synchronize the system time with the given utc timestamp and timezone
+ *  means that , do not need remove timezone difference when set time.
+ * @return void.
+*/
+uint32_t sync_time_utc(uint32_t timestamp, uint32_t timezone_quarters);
 /**
  * @brief  use for test
  * @return void.
  */
-void test_bandy_time(void);
+void test_bandy_time(uint32_t timestamp);
 
 /**
  * @brief  Get the current system time as a local timestamp with timezone offset
  * @return void.
  */
 uint32_t get_local_timestamp(void);
-#endif
 
-#ifdef __cplusplus
-}
-#endif
+
 #endif
+#endif
\ No newline at end of file
index f8577cd..d2c02a7 100644 (file)
@@ -1646,7 +1646,7 @@ static uint8_t jsonCheck_time(uint8_t *buf)//json 格式解析
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"timezone in quarters %d\n",timezone);\r
     uapi_set_timezone(timezone); //要在uaip_sntp_set_time_utc 之前执行才有效\r
     /********/\r
-      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"time: %d\n",timestamp );\r
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"time: %d\n",timestamp );\r
 \r
     uaip_sntp_set_time_utc(timestamp);\r
 \r
index 90d316f..e83c51c 100644 (file)
@@ -314,11 +314,9 @@ void uapi_dev_runtime_set_time_hour_show_model(uint8_t hour_type)
 //关机前,把runtime数据同步到config文件里
 void uapi_dev_runtime_sync_runtime_env_to_config(void)
 {
-    #ifdef FD230_650
     uint16_t timezone_quarters=uapi_get_timezone();//uapi_get_timezon一直未定义故暂时屏蔽
     cm_set_timezone_quarters(timezone_quarters);
     cm_set_hour_show_model(g_runtime.time_hour_show_model);
-    #endif
 }
 
 void uapi_dev_runtime_set_ancs_clientid(uint16_t clientid)
index fae5f02..8c60dad 100644 (file)
@@ -737,18 +737,18 @@ errcode_t uapi_get_timestampe_ss_extend(uint32_t *tm,int year, int month, int da
 
 errcode_t uaip_set_time_utc_zone(uint32_t seconds,int16_t zone_quarters)
 {    
-    // return sync_time_utc(seconds,zone_quarters);
-    return 0;
+    return sync_time_utc(seconds,zone_quarters);
+    // return 0;
 }
 
 errcode_t uaip_sntp_set_time_utc(uint32_t seconds)
 {
 
 
-    int zone_quarters = 0;//uapi_get_timezone();
+    int zone_quarters = uapi_get_timezone();
    
-    //  = sync_time_utc(seconds,zone_quarters);
-    errcode_t ret=0;
+    errcode_t ret = sync_time_utc(seconds,zone_quarters);
+
 
     time_t rawtime;
     struct tm timeinfo;