fscript support date_time_format

This commit is contained in:
lixianjing 2023-12-06 08:51:26 +08:00
parent 6a791f4c47
commit e620aaa1cf
7 changed files with 277 additions and 9 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2023/12/06
* fscript 增加 date\_time\_format
2023/12/05
* 完善fscript部分数学函数。
* 修复上一个高亮对话框影响下一个普通窗口播放窗口动画的问题(感谢智明提供补丁)

View File

@ -216,6 +216,41 @@ var dt = date_time_create();
var year = date_time_get_prop(dt, "year");
```
### 12.date\_time\_format
> 格式化日期对象为字符串。
----------------------------
> 格式规则:
* Y 代表年(完整显示)
* M 代表月(1-12)
* D 代表日(1-31)
* h 代表时(0-23)
* H 代表时(0-11)
* m 代表分(0-59)
* s 代表秒(0-59)
* YY 代表年(只显示末两位)
* MM 代表月(01-12)
* DD 代表日(01-31)
* hh 代表时(00-23)
* HH 代表时(00-11)
* mm 代表分(00-59)
* ss 代表秒(00-59)
#### 原型
```js
date_time_format(dt, format) => str
date_time_format(epoch_time, format) => str
```
#### 示例
```js
print(date_time_format(0, "Y-M-D h:m:s")); // => "1970-1-1 0:0:0"
print(date_time_format(0, "YY-MM-DD hh:mm:ss")); // => "70-01-01 00:00:00"
```
### 更多示例
```js

View File

@ -68,6 +68,31 @@ static ret_t func_date_time_set(fscript_t* fscript, fscript_args_t* args, value_
return RET_OK;
}
static ret_t func_date_time_format(fscript_t* fscript, fscript_args_t* args, value_t* result) {
str_t str;
uint64_t t;
char buff[64] = {0};
date_time_t* dt = NULL;
const char* format = NULL;
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
format = value_str(args->args + 1);
if (args->args[0].type == VALUE_TYPE_OBJECT) {
dt = get_date_time(fscript, args);
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
t = date_time_to_time(dt);
} else {
t = value_uint64(args->args);
}
str_attach(&str, buff, sizeof(buff));
tk_date_time_format(t, format, &str);
value_dup_str(result, buff);
return RET_OK;
}
static ret_t func_time_now_us(fscript_t* fscript, fscript_args_t* args, value_t* result) {
value_set_uint64(result, time_now_us());
return RET_OK;
@ -140,6 +165,7 @@ FACTORY_TABLE_ENTRY("time_now_s", func_time_now_s)
FACTORY_TABLE_ENTRY("time_now", func_time_now_s)
FACTORY_TABLE_ENTRY("is_leap_year", func_year_is_leap)
FACTORY_TABLE_ENTRY("get_days_of_month", func_get_days_of_month)
FACTORY_TABLE_ENTRY("date_time_format", func_date_time_format)
/*主要给AWBLOCK使用*/
FACTORY_TABLE_ENTRY("date_time_set_prop", func_date_time_set_prop)

View File

@ -1991,14 +1991,6 @@ int tk_sscanf(const char* str, const char* format, ...) {
}
#endif /*HAS_NO_VSSCANF*/
/**
* @method tk_levelize
* value转换成level
* levels为"0-20;21-40;41-60;61-80;81-100"value为502
* @param {const char*} levels
* @param {int32_t} value
* @return {int32_t} level
*/
int32_t tk_levelize(const char* levels, int32_t value) {
int32_t level = 0;
int32_t start = 0;
@ -2028,3 +2020,101 @@ int32_t tk_levelize(const char* levels, int32_t value) {
return level;
}
#include "tkc/date_time.h"
uint32_t tk_count_char(const char* str, char c) {
uint32_t nr = 0;
return_value_if_fail(str != NULL, 0);
while (*str++ == c) {
nr++;
if (*str == '\0') {
break;
}
}
return nr;
}
ret_t tk_date_time_format(uint64_t time, const char* format, str_t* result) {
date_time_t dt;
wchar_t temp[32];
const char* p = format;
return_value_if_fail(format != NULL && result != NULL, RET_BAD_PARAMS);
str_clear(result);
date_time_init(&dt);
date_time_from_time(&dt, time);
memset(temp, 0x00, sizeof(temp));
while (*p) {
int32_t repeat = tk_count_char(p, *p);
switch (*p) {
case 'Y': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.year % 100);
} else {
str_append_format(result, 32, "%d", dt.year);
}
break;
}
case 'M': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.month);
} else {
str_append_format(result, 32, "%d", dt.month);
}
break;
}
case 'D': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.day);
} else {
str_append_format(result, 32, "%d", dt.day);
}
break;
}
case 'h': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.hour);
} else {
str_append_format(result, 32, "%d", dt.hour);
}
break;
}
case 'H': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.hour % 12);
} else {
str_append_format(result, 32, "%d", dt.hour % 12);
}
break;
}
case 'm': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.minute);
} else {
str_append_format(result, 32, "%d", dt.minute);
}
break;
}
case 's': {
if (repeat == 2) {
str_append_format(result, 32, "%02d", dt.second);
} else {
str_append_format(result, 32, "%d", dt.second);
}
break;
}
default: {
str_append_with_len(result, p, repeat);
break;
}
}
p += repeat;
}
return RET_OK;
}

View File

@ -1062,9 +1062,50 @@ int tk_sscanf_simple(const char* str, const char* format, ...);
* @param {const char*} levels
* @param {int32_t} value
* @return {int32_t} level
*/
*/
int32_t tk_levelize(const char* levels, int32_t value);
/**
* @method tk_count_char
*
* @param {const char*} str
* @param {char} c
* @return {uint32_t}
*/
uint32_t tk_count_char(const char* str, char c);
/**
* @method tk_date_time_format
*
*
* * Y ()
* * M (1-12)
* * D (1-31)
* * h (0-23)
* * H (0-11)
* * m (0-59)
* * s (0-59)
* * YY ()
* * MM (01-12)
* * DD (01-31)
* * hh (00-23)
* * HH (00-11)
* * mm (00-59)
* * ss (00-59)
*
* 2018/11/12 9:10:20
* * "Y/M/D""2018/11/12"
* * "Y-M-D""2018-11-12"
* * "Y-M-D h:m:s""2018-11-12 9:10:20"
* * "Y-M-D hh:mm:ss""2018-11-12 09:10:20"
*
* @param {uint64_t} time
* @param {const char*} format
* @param {str_t*} result
* @return {ret_t} RET_OK表示成功
*/
ret_t tk_date_time_format(uint64_t time, const char* format, str_t* result);
#define TK_STRDUP(str) ((str) != NULL) ? tk_strdup(str) : NULL
#define TK_STRNDUP(str, len) ((str) != NULL) ? tk_strndup(str, len) : NULL

View File

@ -0,0 +1,43 @@
#include "tkc/fscript.h"
#include "tkc/object_default.h"
#include "gtest/gtest.h"
TEST(FExpr, date_time_format1) {
value_t v;
tk_object_t* obj = object_default_create();
fscript_eval(obj, "date_time_format(0, \"Y\")", &v);
ASSERT_STREQ(value_str(&v), "1970");
value_reset(&v);
fscript_eval(obj, "date_time_format(0, \"Y-M-D\")", &v);
ASSERT_STREQ(value_str(&v), "1970-1-1");
value_reset(&v);
fscript_eval(obj, "date_time_format(0, \"Y-M-D h:m:s\")", &v);
ASSERT_STREQ(value_str(&v), "1970-1-1 0:0:0");
value_reset(&v);
TK_OBJECT_UNREF(obj);
}
TEST(FExpr, date_time_format2) {
value_t v;
tk_object_t* obj = object_default_create();
fscript_eval(obj, "var dt = date_time_create(); date_time_from_time(dt, 61); date_time_format(dt, \"Y\")", &v);
ASSERT_STREQ(value_str(&v), "1970");
value_reset(&v);
fscript_eval(obj, "var dt = date_time_create(); date_time_from_time(dt, 61); date_time_format(dt, \"Y-M-D\")", &v);
ASSERT_STREQ(value_str(&v), "1970-1-1");
value_reset(&v);
fscript_eval(obj, "var dt = date_time_create(); date_time_from_time(dt, 61); date_time_format(dt, \"Y-M-D h:m:s\")", &v);
ASSERT_STREQ(value_str(&v), "1970-1-1 0:1:1");
value_reset(&v);
TK_OBJECT_UNREF(obj);
}

View File

@ -1192,3 +1192,33 @@ TEST(Utils, levelize3) {
ASSERT_EQ(tk_levelize("0-20;21-40", 30), 1);
ASSERT_EQ(tk_levelize("0-20;21-40", 100), 1);
}
TEST(Utils, tk_date_time_format) {
str_t str;
char buff[128];
str_attach(&str, buff, sizeof(buff));
ASSERT_EQ(tk_date_time_format(0, "", &str), RET_OK);
ASSERT_STREQ(str.str, "");
ASSERT_EQ(tk_date_time_format(0, "Y", &str), RET_OK);
ASSERT_STREQ(str.str, "1970");
ASSERT_EQ(tk_date_time_format(0, "Y-M-D", &str), RET_OK);
ASSERT_STREQ(str.str, "1970-1-1");
ASSERT_EQ(tk_date_time_format(0, "Y-M-D h:m:s", &str), RET_OK);
ASSERT_STREQ(str.str, "1970-1-1 0:0:0");
ASSERT_EQ(tk_date_time_format(0, "YY", &str), RET_OK);
ASSERT_STREQ(str.str, "70");
ASSERT_EQ(tk_date_time_format(0, "YY-MM-DD", &str), RET_OK);
ASSERT_STREQ(str.str, "70-01-01");
ASSERT_EQ(tk_date_time_format(0, "YY-MM-DD hh:mm:ss", &str), RET_OK);
ASSERT_STREQ(str.str, "70-01-01 00:00:00");
ASSERT_EQ(tk_date_time_format(3765, "YY-MM-DD hh:mm:ss", &str), RET_OK);
ASSERT_STREQ(str.str, "70-01-01 01:02:45");
}