Skip to content

Commit

Permalink
add date_time_parse_xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Dec 9, 2023
1 parent 28412f1 commit f87edb2
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -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文件的资源主题名字和图片信息错误的问题(感谢智明提供补丁)
Expand Down
61 changes: 61 additions & 0 deletions src/tkc/date_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
27 changes: 27 additions & 0 deletions src/tkc/date_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
* 设置年。
Expand Down
75 changes: 75 additions & 0 deletions tests/date_time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit f87edb2

Please sign in to comment.