diff --git a/docs/changes.md b/docs/changes.md index e15fffa6fd..771a030b59 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,8 @@ # 最新动态 +2023/12/09 + * 增加函数 date\_time\_parse\_date/date\_time\_parse\_time/date\_time\_parse\_date\_time + 2023/12/07 * csv file object 支持通过 MVVM 来查询。 * 修复生成资源c文件的资源主题名字和图片信息错误的问题(感谢智明提供补丁) diff --git a/src/tkc/date_time.c b/src/tkc/date_time.c index 98283f2e3f..49439d06d4 100644 --- a/src/tkc/date_time.c +++ b/src/tkc/date_time.c @@ -20,6 +20,7 @@ */ #include "tkc/mem.h" +#include "tkc/utils.h" #include "tkc/date_time.h" static date_time_get_now_t s_date_time_get_now; @@ -221,3 +222,63 @@ ret_t date_time_set_second(date_time_t* dt, uint32_t second) { return RET_OK; } + +ret_t date_time_parse_time(date_time_t* dt, const char* str) { + int32_t hour = 0; + int32_t minute = 0; + int32_t second = 0; + int32_t n = 0; + return_value_if_fail(dt != NULL && str != NULL, RET_BAD_PARAMS); + + n = tk_sscanf(str, "%d:%d:%d", &hour, &minute, &second); + if (n >= 2) { + return date_time_set_hour(dt, hour) || date_time_set_minute(dt, minute) || + date_time_set_second(dt, second); + } else { + return RET_BAD_PARAMS; + } +} + +ret_t date_time_parse_date(date_time_t* dt, const char* str) { + int32_t year = 0; + int32_t month = 0; + int32_t day = 0; + int32_t n = 0; + return_value_if_fail(dt != NULL && str != NULL, RET_BAD_PARAMS); + + if(strchr(str, '/') != NULL) { + n = tk_sscanf(str, "%d/%d/%d", &year, &month, &day); + } else { + n = tk_sscanf(str, "%d-%d-%d", &year, &month, &day); + } + if (n == 3) { + return date_time_set_year(dt, year) || date_time_set_month(dt, month) || + date_time_set_day(dt, day); + } else { + return RET_BAD_PARAMS; + } +} + +ret_t date_time_parse_date_time(date_time_t* dt, const char* str) { + int32_t year = 0; + int32_t month = 0; + int32_t day = 0; + int32_t hour = 0; + int32_t minute = 0; + int32_t second = 0; + int32_t n = 0; + return_value_if_fail(dt != NULL && str != NULL, RET_BAD_PARAMS); + + if(strchr(str, '/') != NULL) { + n = tk_sscanf(str, "%d/%d/%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second); + } else { + n = tk_sscanf(str, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second); + } + if (n >= 5) { + return date_time_set_year(dt, year) || date_time_set_month(dt, month) || + date_time_set_day(dt, day) || date_time_set_hour(dt, hour) || + date_time_set_minute(dt, minute) || date_time_set_second(dt, second); + } else { + return RET_BAD_PARAMS; + } +} \ No newline at end of file diff --git a/src/tkc/date_time.h b/src/tkc/date_time.h index 2f09e6432e..caf0c75b4f 100644 --- a/src/tkc/date_time.h +++ b/src/tkc/date_time.h @@ -97,6 +97,33 @@ date_time_t* date_time_create(void); */ date_time_t* date_time_init(date_time_t* dt); +/** + * @method date_time_parse_time + * 解析时间。 + * @param {date_time_t*} dt date_time对象。 + * @param {const char*} str 时间字符串。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t date_time_parse_time(date_time_t* dt, const char* str); + +/** + * @method date_time_parse_date + * 解析日期。 + * @param {date_time_t*} dt date_time对象。 + * @param {const char*} str 日期字符串。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t date_time_parse_date(date_time_t* dt, const char* str); + +/** + * @method date_time_parse_date_time + * 解析日期和时间。 + * @param {date_time_t*} dt date_time对象。 + * @param {const char*} str 日期和时间字符串。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t date_time_parse_date_time(date_time_t* dt, const char* str); + /** * @method date_time_set_year * 设置年。 diff --git a/tests/date_time_test.cc b/tests/date_time_test.cc index bc1b561a2c..659f8ffd2a 100644 --- a/tests/date_time_test.cc +++ b/tests/date_time_test.cc @@ -242,3 +242,78 @@ TEST(DateTime, fscript_object) { TK_OBJECT_UNREF(obj); } + +TEST(DateTime, test_parse_time) { + date_time_t dt; + memset(&dt, 0x00, sizeof(dt)); + ASSERT_EQ(date_time_parse_time(&dt, "01:02:03"), RET_OK); + ASSERT_EQ(dt.year, 0); + ASSERT_EQ(dt.month, 0); + ASSERT_EQ(dt.day, 0); + ASSERT_EQ(dt.hour, 1); + ASSERT_EQ(dt.minute, 2); + ASSERT_EQ(dt.second, 3); + + ASSERT_EQ(date_time_parse_time(&dt, "01:02"), RET_OK); + ASSERT_EQ(dt.year, 0); + ASSERT_EQ(dt.month, 0); + ASSERT_EQ(dt.day, 0); + ASSERT_EQ(dt.hour, 1); + ASSERT_EQ(dt.minute, 2); + ASSERT_EQ(dt.second, 0); + + ASSERT_NE(date_time_parse_time(&dt, "01"), RET_OK); +} + +TEST(DateTime, test_parse_date) { + date_time_t dt; + memset(&dt, 0x00, sizeof(dt)); + ASSERT_EQ(date_time_parse_date(&dt, "2020-01-02"), RET_OK); + ASSERT_EQ(dt.year, 2020); + ASSERT_EQ(dt.month, 1); + ASSERT_EQ(dt.day, 2); + ASSERT_EQ(dt.hour, 0); + ASSERT_EQ(dt.minute, 0); + ASSERT_EQ(dt.second, 0); + + ASSERT_EQ(date_time_parse_date(&dt, "2020-01-02 01:02:03"), RET_OK); + ASSERT_EQ(dt.year, 2020); + ASSERT_EQ(dt.month, 1); + ASSERT_EQ(dt.day, 2); + ASSERT_EQ(dt.hour, 0); + ASSERT_EQ(dt.minute, 0); + ASSERT_EQ(dt.second, 0); +} + +TEST(DateTime, test_parse_date_time) { + date_time_t dt; + memset(&dt, 0x00, sizeof(dt)); + ASSERT_EQ(date_time_parse_date_time(&dt, "2020-01-02 01:02:03"), RET_OK); + ASSERT_EQ(dt.year, 2020); + ASSERT_EQ(dt.month, 1); + ASSERT_EQ(dt.day, 2); + ASSERT_EQ(dt.hour, 1); + ASSERT_EQ(dt.minute, 2); + ASSERT_EQ(dt.second, 3); + + memset(&dt, 0x00, sizeof(dt)); + ASSERT_EQ(date_time_parse_date_time(&dt, "2020-01-02 01:02"), RET_OK); + ASSERT_EQ(dt.year, 2020); + ASSERT_EQ(dt.month, 1); + ASSERT_EQ(dt.day, 2); + ASSERT_EQ(dt.hour, 1); + ASSERT_EQ(dt.minute, 2); + ASSERT_EQ(dt.second, 0); + + memset(&dt, 0x00, sizeof(dt)); + ASSERT_NE(date_time_parse_date_time(&dt, "2020-01-02 01"), RET_OK); + + memset(&dt, 0x00, sizeof(dt)); + ASSERT_NE(date_time_parse_date_time(&dt, "2020-01-02"), RET_OK); + + ASSERT_NE(date_time_parse_date_time(&dt, "2020-01"), RET_OK); + ASSERT_NE(date_time_parse_date_time(&dt, "2020"), RET_OK); + ASSERT_EQ(date_time_parse_date_time(&dt, "2020-01-02 01:02:03.123"), RET_OK); + ASSERT_EQ(date_time_parse_date_time(&dt, "2020-01-02 01:02:03.123456"), RET_OK); +} +