修复死机文件内存泄漏 增加定制时间字符串接口
authorCaisking <3449750381@qq.com>
Fri, 5 Jul 2024 07:30:22 +0000 (15:30 +0800)
committerCaisking <3449750381@qq.com>
Fri, 5 Jul 2024 07:30:22 +0000 (15:30 +0800)
code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp
code/sdk/application/wearable/fd230app/nativeui/sleep/src/SleepModel.cpp
code/sdk/drivers/peripheral/utils/custom_utils.c
code/sdk/drivers/peripheral/utils/custom_utils.h

index 7e8eff9..a73fd48 100644 (file)
@@ -31,7 +31,7 @@
 #define MESSAGE_JSON_FILE "user/userdata/message/ancs.json"
 #define PRINTFDATA 1
 
-extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year);
+extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format);
 namespace OHOS
 {
     MessageData message_;
@@ -120,7 +120,7 @@ namespace OHOS
             uint32 timestamp = cJSON_GetObjectItem(item, "time")->valueint;
             messagedata.timestamp = timestamp;
             char labelTime[20];
-            if (uapi_get_TIME_STR(timestamp, labelTime, '.', false, false) != ERRCODE_SUCC)
+            if (uapi_get_TIME_STR(timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC)
             {
                 messagedata.labelTime = std::string("2024.1.1 00:00");
             }
index b768750..cbb9f5a 100644 (file)
@@ -27,8 +27,9 @@
 #include "wearable_log.h"
 #include "ConfigModel.h"
 #include "heart_blood/health_service/health_service.h"
+#include "custom_utils.h"
 
-extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year);
+extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format);
 extern "C" int get_sleep_time_from_config_model(void);
 extern "C" int get_wake_up_time_from_config_model();
 
@@ -381,7 +382,7 @@ namespace OHOS
     void SleepModel::update_time_info(uint32_t timestamp, std::string &timeStr)
     {
         char labelTime[20];
-        if (uapi_get_TIME_STR(timestamp, labelTime, '.', false, false) == ERRCODE_SUCC)
+        if (uapi_get_TIME_STR(timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC)
         {
             timeStr = std::string(labelTime);
         }
index e6d8da7..63edbb4 100644 (file)
@@ -758,15 +758,17 @@ errcode_t uapi_get_yyyymmddhhmmss_Date(char *date)
     }
     return ERRCODE_SUCC;
 }
+
+
 /**
  * @param {uint32_t} time 时间戳
  * @param {char} *p_time 字符串
- * @param {bool} include_seconds 是否包含秒
- * @param {bool} include_year 是否包含年份
- *  * @param {char} 隔断符
+ * @param {char} date_separator 日期分隔符
+ * @param {char} time_separator 时间分隔符
+ * @param {date_format_t} format 日期格式选项
  * @brief 通过时间戳获取日期
  */
-errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year)
+errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format)
 {
     if (p_time == NULL)
     {
@@ -781,38 +783,38 @@ errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool in
         return ERRCODE_FAIL;
     }
 
-    // 根据需要格式化日期时间字符串,包括时分秒和分隔符
-    if (include_seconds)
+    switch (format)
     {
-        if (include_year)
-        {
-            sprintf(p_time, "%04d%c%02d%c%02d %02d%c%02d", timeinfo->tm_year + 1900, separator, timeinfo->tm_mon + 1, separator,
-                    timeinfo->tm_mday, timeinfo->tm_hour, separator, timeinfo->tm_min);
-        }
-        else
-        {
-            sprintf(p_time, "%02d%c%02d %02d%c%02d", timeinfo->tm_mon + 1, separator,
-                    timeinfo->tm_mday, timeinfo->tm_hour, separator, timeinfo->tm_min);
-        }
-    }
-    else
-    {
-        if (include_year)
-        {
-            sprintf(p_time, "%04d%c%02d%c%02d %02d%c%02d", timeinfo->tm_year + 1900, separator, timeinfo->tm_mon + 1, separator,
-                    timeinfo->tm_mday, timeinfo->tm_hour, separator, timeinfo->tm_min);
-        }
-        else
-        {
-            sprintf(p_time, "%02d%c%02d %02d%c%02d", timeinfo->tm_mon + 1, separator,
-                    timeinfo->tm_mday, timeinfo->tm_hour, separator, timeinfo->tm_min);
-        }
+        case DATE_Y_M_D:
+            sprintf(p_time, "%04d%c%02d%c%02d", timeinfo->tm_year + 1900, date_separator, timeinfo->tm_mon + 1, date_separator, timeinfo->tm_mday);
+            break;
+        case DATE_Y_M_D_H_M:
+            sprintf(p_time, "%04d%c%02d%c%02d %02d%c%02d", timeinfo->tm_year + 1900, date_separator, timeinfo->tm_mon + 1, date_separator,
+                    timeinfo->tm_mday, timeinfo->tm_hour, time_separator, timeinfo->tm_min);
+            break;
+        case DATE_M_D:
+            sprintf(p_time, "%02d%c%02d", timeinfo->tm_mon + 1, date_separator, timeinfo->tm_mday);
+            break;
+        case DATE_H_M_S:
+            sprintf(p_time, "%02d%c%02d%c%02d", timeinfo->tm_hour, time_separator, timeinfo->tm_min, time_separator, timeinfo->tm_sec);
+            break;
+        case DATE_H_M:
+            sprintf(p_time, "%02d%c%02d", timeinfo->tm_hour, time_separator, timeinfo->tm_min);
+            break;
+        case DATE_M_S:
+            sprintf(p_time, "%02d%c%02d", timeinfo->tm_min, time_separator, timeinfo->tm_sec);
+            break;
+        case DATE_M_D_H_M:
+            sprintf(p_time, "%02d%c%02d %02d%c%02d", timeinfo->tm_mon + 1, date_separator, timeinfo->tm_mday, timeinfo->tm_hour, time_separator, timeinfo->tm_min);
+            break;
+        default:
+            sprintf(p_time, "%04d%c%02d%c%02d %02d%c%02d%c%02d", timeinfo->tm_year + 1900, date_separator, timeinfo->tm_mon + 1, date_separator,
+                    timeinfo->tm_mday, timeinfo->tm_hour, time_separator, timeinfo->tm_min, time_separator, timeinfo->tm_sec);
+            break;
     }
 
     return ERRCODE_SUCC;
 }
-
-
 /**
  * @param {char} *p_time
  * @param {char} 隔断符
@@ -1660,8 +1662,6 @@ int uapi_delete_files(const char *file_path)
 #define  MAX_TAR_READ_BUF  1024
 int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
 {
-    //const char *tarfilePath = "%s/exc_%d.tar";
-    DIR *dir;
     struct dirent *entry;
     mtar_t tar;
     size_t bytesRead = 0;
@@ -1670,14 +1670,15 @@ int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
     long tar_size = 0;
     int ret = 0;
     char *buffer = malloc(MAX_TAR_READ_BUF);
-    if (buffer == NULL) return -1;
+    if (buffer == NULL) 
+        return -1;
     if(access(src_path, F_OK) == -1)
     {
         PRINT("%s does not exist!!!\r\n", src_path);
         return -1;
     }
 
-    dir = opendir(src_path);
+    DIR *dir = opendir(src_path);
     if (dir == NULL) 
     {
         PRINT("unable to open %s\r\n",src_path);
@@ -1689,6 +1690,7 @@ int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
         ret = mkdir(tar_path, 0777); 
         if(ret == -1)
         {
+            closedir(dir);
             PRINT("mkdir %s failed \r\n",tar_path);
             return -1;
         }
@@ -1703,6 +1705,7 @@ int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
     ret = mtar_open(&tar, str, "w");
     if (ret != MTAR_ESUCCESS) 
     {
+        closedir(dir);
         PRINT("unable to open/create %s\r\n",str);
         return -1;
     }
@@ -1717,6 +1720,8 @@ int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
             fp = fopen(filePath, "r");
             if(fp == NULL) 
             {
+                mtar_close(&tar);
+                closedir(dir);
                 PRINT("uable to open %s\r\n", filePath);
                 return -1;
             }
@@ -1741,6 +1746,7 @@ int uapi_tar_file(const char *src_path,const char *tar_path,char *tarfilename)
                     break;
                 }
             }
+            fclose(fp);
         }
     }
     
index 9fbd97a..bef833a 100644 (file)
@@ -80,8 +80,23 @@ void print_current_time(void);
 errcode_t uapi_get_WeekStr(char *p_week);
 /*设置屏幕亮屏*/
 void uapi_set_screen_on_api(void);
+
+/**
+ * @brief 日期格式选项枚举
+ */
+typedef enum {
+    DATE_TIME_DEFULL,   // 年 月 日 时:分:秒
+    DATE_Y_M_D,         // 年 月 日
+    DATE_Y_M_D_H_M,     // 年 月 日 时 分
+    DATE_M_D,           // 月 日
+    DATE_H_M_S,         // 时 分 秒
+    DATE_H_M,           // 时 分
+    DATE_M_S,           // 分 秒
+    DATE_M_D_H_M        // 月 日 时 分
+} date_format_t;
+
 /*通过时间戳解析时间字符串*/
-errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year);
+errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format);
 
 char *uapi_get_local_yyyymmdd_date(void);