mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-12-03 04:27:44 +08:00
add date_time_from_time
This commit is contained in:
parent
a8bee3ec41
commit
5d23d03651
147
src/base/date_time_format.c
Normal file
147
src/base/date_time_format.c
Normal file
@ -0,0 +1,147 @@
|
||||
/**
|
||||
* File: date_time_format.c
|
||||
* Author: AWTK Develop Team
|
||||
* Brief: date_time_format
|
||||
*
|
||||
* Copyright (c) 2020 - 2020 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* License file for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2020-01-11 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
#include "tkc/utf8.h"
|
||||
#include "base/locale_info.h"
|
||||
#include "base/date_time_format.h"
|
||||
|
||||
static uint32_t count_char(const char* p, char c) {
|
||||
uint32_t nr = 0;
|
||||
|
||||
while (*p++ == c) {
|
||||
nr++;
|
||||
}
|
||||
|
||||
return nr;
|
||||
}
|
||||
|
||||
static wchar_t* translate_wday(wchar_t* str, uint32_t size, uint32_t wday) {
|
||||
return_value_if_fail(wday < 7, NULL);
|
||||
|
||||
static const char* const wdays[] = {
|
||||
"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat",
|
||||
};
|
||||
|
||||
const char* utf8 = locale_info_tr(locale_info(), wdays[wday]);
|
||||
return tk_utf8_to_utf16(utf8, str, size);
|
||||
}
|
||||
|
||||
static wchar_t* translate_month(wchar_t* str, uint32_t size, uint32_t month) {
|
||||
return_value_if_fail(month < 13, NULL);
|
||||
|
||||
static const char* const months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
|
||||
|
||||
const char* utf8 = locale_info_tr(locale_info(), months[month - 1]);
|
||||
return tk_utf8_to_utf16(utf8, str, size);
|
||||
}
|
||||
|
||||
ret_t wstr_format_date_time(wstr_t* str, const char* format, const date_time_t* dt) {
|
||||
wchar_t temp[32];
|
||||
const char* p = format;
|
||||
return_value_if_fail(format != NULL && str != NULL, RET_BAD_PARAMS);
|
||||
|
||||
str->size = 0;
|
||||
memset(temp, 0x00, sizeof(temp));
|
||||
while (*p) {
|
||||
int32_t repeat = count_char(p, *p);
|
||||
|
||||
switch (*p) {
|
||||
case 'Y': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", (dt->year % 100));
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->year);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'M': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->month);
|
||||
} else if (repeat == 3) {
|
||||
translate_month(temp, ARRAY_SIZE(temp), dt->month);
|
||||
wstr_append(str, temp);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->month);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'D': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->day);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->day);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'h': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->hour);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->hour);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'm': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->minute);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->minute);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->second);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->second);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'w': {
|
||||
wstr_push_int(str, "%d", dt->wday);
|
||||
break;
|
||||
}
|
||||
case 'W': {
|
||||
translate_wday(temp, ARRAY_SIZE(temp), dt->wday);
|
||||
wstr_append(str, temp);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
wstr_push(str, *p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
p += repeat;
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t wstr_format_time(wstr_t* str, const char* format, uint64_t time) {
|
||||
date_time_t dt;
|
||||
return_value_if_fail(str != NULL && format != NULL, RET_BAD_PARAMS);
|
||||
|
||||
memset(&dt, 0x00, sizeof(dt));
|
||||
date_time_from_time(&dt, time);
|
||||
|
||||
return wstr_format_date_time(str, format, &dt);
|
||||
}
|
37
src/base/date_time_format.h
Normal file
37
src/base/date_time_format.h
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: date_time_format.h
|
||||
* Author: AWTK Develop Team
|
||||
* Brief: date_time_format
|
||||
*
|
||||
* Copyright (c) 2020 - 2020 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* License file for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2020-01-11 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TK_DATE_TIME_FORMAT_H
|
||||
#define TK_DATE_TIME_FORMAT_H
|
||||
|
||||
#include "tkc/str.h"
|
||||
#include "tkc/wstr.h"
|
||||
#include "tkc/types_def.h"
|
||||
#include "tkc/date_time.h"
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
||||
ret_t wstr_format_time(wstr_t* str, const char* format, uint64_t time);
|
||||
ret_t wstr_format_date_time(wstr_t* str, const char* format, const date_time_t* dt);
|
||||
|
||||
END_C_DECLS
|
||||
|
||||
#endif /*TK_DATE_TIME_FORMAT_H*/
|
@ -67,6 +67,11 @@ static ret_t date_time_set_now_impl(date_time_t* dt) {
|
||||
return RET_NOT_IMPL;
|
||||
}
|
||||
|
||||
static ret_t date_time_from_time_impl(date_time_t* dt, uint64_t timeval) {
|
||||
/*TODO*/
|
||||
return RET_NOT_IMPL;
|
||||
}
|
||||
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
@ -113,6 +118,24 @@ static ret_t date_time_set_now_impl(date_time_t* dt) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t date_time_from_time_impl(date_time_t* dt, uint64_t timeval) {
|
||||
time_t tm = timeval;
|
||||
struct tm* t = localtime(&tm);
|
||||
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
|
||||
|
||||
memset(dt, 0x00, sizeof(date_time_t));
|
||||
|
||||
dt->second = t->tm_sec;
|
||||
dt->minute = t->tm_min;
|
||||
dt->hour = t->tm_hour;
|
||||
dt->day = t->tm_mday;
|
||||
dt->month = t->tm_mon + 1;
|
||||
dt->year = t->tm_year + 1900;
|
||||
dt->wday = t->tm_wday;
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
uint64_t stm_now_ms();
|
||||
@ -122,6 +145,9 @@ uint64_t get_time_ms64() {
|
||||
return stm_now_ms();
|
||||
}
|
||||
|
||||
static const date_time_vtable_t s_date_time_vtable = {
|
||||
date_time_get_now_impl, date_time_set_now_impl, date_time_from_time_impl};
|
||||
|
||||
void sleep_ms(uint32_t ms) {
|
||||
#ifdef WIN32
|
||||
Sleep(ms);
|
||||
@ -140,7 +166,8 @@ ret_t platform_prepare(void) {
|
||||
#ifndef HAS_STD_MALLOC
|
||||
tk_mem_init(s_heap_mem, sizeof(s_heap_mem));
|
||||
#endif /*HAS_STD_MALLOC*/
|
||||
date_time_global_init(date_time_get_now_impl, date_time_set_now_impl);
|
||||
|
||||
date_time_global_init_ex(&s_date_time_vtable);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
@ -24,6 +24,17 @@
|
||||
|
||||
static date_time_get_now_t s_date_time_get_now;
|
||||
static date_time_set_now_t s_date_time_set_now;
|
||||
static date_time_from_time_t s_date_time_from_time;
|
||||
|
||||
ret_t date_time_global_init_ex(const date_time_vtable_t* vt) {
|
||||
return_value_if_fail(vt != NULL, RET_BAD_PARAMS);
|
||||
|
||||
s_date_time_get_now = vt->get_now;
|
||||
s_date_time_set_now = vt->set_now;
|
||||
s_date_time_from_time = vt->from_time;
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t date_time_global_init(date_time_get_now_t get, date_time_set_now_t set) {
|
||||
s_date_time_get_now = get;
|
||||
@ -44,6 +55,13 @@ date_time_t* date_time_create(void) {
|
||||
return date_time_init(dt);
|
||||
}
|
||||
|
||||
ret_t date_time_from_time(date_time_t* dt, uint64_t time) {
|
||||
return_value_if_fail(dt != NULL, RET_BAD_PARAMS);
|
||||
return_value_if_fail(s_date_time_from_time != NULL, RET_BAD_PARAMS);
|
||||
|
||||
return s_date_time_from_time(dt, (time_t)time);
|
||||
}
|
||||
|
||||
date_time_t* date_time_init(date_time_t* dt) {
|
||||
return_value_if_fail(dt != NULL, NULL);
|
||||
|
||||
|
@ -108,6 +108,18 @@ date_time_t* date_time_init(date_time_t* dt);
|
||||
*/
|
||||
ret_t date_time_set(date_time_t* dt);
|
||||
|
||||
/**
|
||||
* @method date_time_from_time
|
||||
* 从time转换而来。
|
||||
*
|
||||
* @annotation ["scriptable"]
|
||||
* @param {date_time_t*} dt date_time对象。
|
||||
* @param {uint64_t} time 时间。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t date_time_from_time(date_time_t* dt, uint64_t time);
|
||||
|
||||
/**
|
||||
* @method date_time_destroy
|
||||
* 销毁date_time对象(一般供脚本语言中使用)。
|
||||
@ -121,22 +133,29 @@ ret_t date_time_destroy(date_time_t* dt);
|
||||
|
||||
typedef ret_t (*date_time_get_now_t)(date_time_t* dt);
|
||||
typedef ret_t (*date_time_set_now_t)(date_time_t* dt);
|
||||
typedef ret_t (*date_time_from_time_t)(date_time_t* dt, uint64_t time);
|
||||
|
||||
typedef struct _date_time_vtable_t {
|
||||
date_time_get_now_t get_now;
|
||||
date_time_set_now_t set_now;
|
||||
date_time_from_time_t from_time;
|
||||
} date_time_vtable_t;
|
||||
|
||||
/**
|
||||
* @method date_time_global_init
|
||||
* @method date_time_global_init_ex
|
||||
* 时间日期全局初始化。
|
||||
*
|
||||
* > 嵌入式平台需要提供并设置获取当前日期和时间的函数,否则相关的功能(如时钟控件)将无法正常工作。
|
||||
*
|
||||
* @param {date_time_get_now_t} get 获取当前日期和时间的函数。
|
||||
* @param {date_time_set_now_t} set 设置当前日期和时间的函数。
|
||||
* @param {date_time_vtable_t} vt 日期和时间的相关函数的实现。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t date_time_global_init(date_time_get_now_t get, date_time_set_now_t set);
|
||||
ret_t date_time_global_init_ex(const date_time_vtable_t* vt);
|
||||
|
||||
/*deprecated*/
|
||||
ret_t date_time_set_impl(date_time_get_now_t date_time_get_now);
|
||||
ret_t date_time_global_init(date_time_get_now_t get, date_time_set_now_t set);
|
||||
END_C_DECLS
|
||||
|
||||
#endif /*TK_DATE_TIME_H*/
|
||||
|
@ -25,123 +25,13 @@
|
||||
#include "base/timer.h"
|
||||
#include "base/locale_info.h"
|
||||
#include "base/widget_vtable.h"
|
||||
#include "base/date_time_format.h"
|
||||
#include "widgets/digit_clock.h"
|
||||
|
||||
#define DATE_TIME_MAX_LEN 127
|
||||
|
||||
static uint32_t count_char(const char* p, char c) {
|
||||
uint32_t nr = 0;
|
||||
while (*p++ == c) {
|
||||
nr++;
|
||||
}
|
||||
|
||||
return nr;
|
||||
}
|
||||
|
||||
static wchar_t* digit_clock_translate_wday(wchar_t* str, uint32_t size, uint32_t wday) {
|
||||
return_value_if_fail(wday < 7, NULL);
|
||||
|
||||
static const char* const wdays[] = {
|
||||
"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat",
|
||||
};
|
||||
|
||||
const char* utf8 = locale_info_tr(locale_info(), wdays[wday]);
|
||||
return tk_utf8_to_utf16(utf8, str, size);
|
||||
}
|
||||
|
||||
static wchar_t* digit_clock_translate_month(wchar_t* str, uint32_t size, uint32_t month) {
|
||||
return_value_if_fail(month < 13, NULL);
|
||||
|
||||
static const char* const months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
|
||||
|
||||
const char* utf8 = locale_info_tr(locale_info(), months[month - 1]);
|
||||
return tk_utf8_to_utf16(utf8, str, size);
|
||||
}
|
||||
|
||||
ret_t digit_clock_format_time(widget_t* widget, const char* format, date_time_t* dt) {
|
||||
wchar_t temp[32];
|
||||
wstr_t* str = NULL;
|
||||
const char* p = format;
|
||||
return_value_if_fail(widget != NULL, RET_BAD_PARAMS);
|
||||
|
||||
str = &(widget->text);
|
||||
|
||||
str->size = 0;
|
||||
memset(temp, 0x00, sizeof(temp));
|
||||
while (*p) {
|
||||
int32_t repeat = count_char(p, *p);
|
||||
|
||||
switch (*p) {
|
||||
case 'Y': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", (dt->year % 100));
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->year);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'M': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->month);
|
||||
} else if (repeat == 3) {
|
||||
digit_clock_translate_month(temp, ARRAY_SIZE(temp), dt->month);
|
||||
wstr_append(str, temp);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->month);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'D': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->day);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->day);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'h': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->hour);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->hour);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'm': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->minute);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->minute);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
if (repeat == 2) {
|
||||
wstr_push_int(str, "%02d", dt->second);
|
||||
} else {
|
||||
wstr_push_int(str, "%d", dt->second);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'w': {
|
||||
wstr_push_int(str, "%d", dt->wday);
|
||||
break;
|
||||
}
|
||||
case 'W': {
|
||||
digit_clock_translate_wday(temp, ARRAY_SIZE(temp), dt->wday);
|
||||
wstr_append(str, temp);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
wstr_push(str, *p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
p += repeat;
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
return wstr_format_date_time(&(widget->text), format, dt);
|
||||
}
|
||||
|
||||
static ret_t digit_clock_update_time(widget_t* widget) {
|
||||
|
Loading…
Reference in New Issue
Block a user