优化时间戳获取时间字符串函数
authorCaisking <3449750381@qq.com>
Mon, 1 Jul 2024 08:23:23 +0000 (16:23 +0800)
committerCaisking <3449750381@qq.com>
Mon, 1 Jul 2024 08:23:23 +0000 (16:23 +0800)
code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp
code/sdk/drivers/peripheral/utils/custom_utils.c
code/sdk/drivers/peripheral/utils/custom_utils.h

index 0e6db03..7e8eff9 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);
+extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year);
 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) != ERRCODE_SUCC)
+            if (uapi_get_TIME_STR(timestamp, labelTime, '.', false, false) != ERRCODE_SUCC)
             {
                 messagedata.labelTime = std::string("2024.1.1 00:00");
             }
index 235d878..c96ac6f 100644 (file)
@@ -790,11 +790,12 @@ errcode_t uapi_get_yyyymmddhhmmss_Date(char *date)
 /**
  * @param {uint32_t} time 时间戳
  * @param {char} *p_time 字符串
- * @param {bool} *include_seconds 是否包含秒
- * @param {char} 隔断符
+ * @param {bool} include_seconds 是否包含秒
+ * @param {bool} include_year 是否包含年份
+ *  * @param {char} 隔断符
  * @brief 通过时间戳获取日期
  */
-errcode_t uapi_get_TIME_STR(uint32_t time,char *p_time, char separator, bool include_seconds)
+errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year)
 {
     if (p_time == NULL)
     {
@@ -812,18 +813,35 @@ errcode_t uapi_get_TIME_STR(uint32_t time,char *p_time, char separator, bool inc
     // 根据需要格式化日期时间字符串,包括时分秒和分隔符
     if (include_seconds)
     {
-        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);
+        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
     {
-        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);
+        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);
+        }
     }
 
     return ERRCODE_SUCC;
 }
 
+
 /**
  * @param {char} *p_time
  * @param {char} 隔断符
index 10b0903..e62f483 100644 (file)
@@ -67,14 +67,11 @@ errcode_t uapi_set_time_from_cat1(char* cat1time);
 
 /**获取日期*/
 errcode_t uapi_get_yyyymmddhhmmss_Date(char *date);
-
-/*获取日期*/
-errcode_t uapi_get_YYMMDD_Time(char *p_time, char separator);
-
 /*获取日期扩展版*/
 errcode_t uapi_get_YYMMDD_Time_extend(char *p_time, char separator,uint8_t day);
 
-
+/*获取日期*/
+errcode_t uapi_get_YYMMDD_Time(char *p_time, char separator);
 /*获取时间*/
 errcode_t uapi_get_HHMMSS_Time(char *p_time, char separator, bool include_seconds);
 /*获取星期*/
@@ -82,7 +79,7 @@ errcode_t uapi_get_WeekStr(char *p_week);
 /*设置屏幕亮屏*/
 void uapi_set_screen_on_api(void);
 /*通过时间戳解析时间字符串*/
-errcode_t uapi_get_TIME_STR(uint32_t time,char *p_time, char separator, bool include_seconds);
+errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char separator, bool include_seconds, bool include_year);
 
 char *uapi_get_local_yyyymmdd_date(void);