修改日期格式接口缺陷

This commit is contained in:
shaozhimin 2023-11-03 14:32:14 +08:00
parent 3dcf39ad2e
commit 3b9598bb08
2 changed files with 35 additions and 43 deletions

View File

@ -2405,6 +2405,7 @@ char* kdk_system_longformat_transform(struct tm *ptr)
textdomain (GETTEXT_PACKAGE);
char *homeDir = NULL;
char value[32] = "\0";
struct tm stPtr;
char *tvalue = (char *)malloc(sizeof(char) * 64);
if(!tvalue)
{
@ -2423,9 +2424,10 @@ char* kdk_system_longformat_transform(struct tm *ptr)
}
sprintf(path, "%s/.config/kydate/dateformat.conf", filename);
int ptr_year = ptr->tm_year;
ptr->tm_year = ptr->tm_year - 1900;
stPtr.tm_year = ptr_year - 1900;
int ptr_mon = ptr->tm_mon;
ptr->tm_mon = ptr->tm_mon - 1;
stPtr.tm_mon = ptr_mon - 1;
stPtr.tm_mday = ptr->tm_mday;
if (access(path, F_OK) == 0)
{
@ -2479,7 +2481,7 @@ char* kdk_system_longformat_transform(struct tm *ptr)
}
free(mon);
}else{
strftime(tmp, sizeof(tmp), gettext("%Y_year%m_mon%d_day"), ptr);
strftime(tmp, sizeof(tmp), gettext("%Y_year%m_mon%d_day"), &stPtr);
}
}
@ -2496,6 +2498,7 @@ char* kdk_system_shortformat_transform(struct tm *ptr)
char tmp[64];
char canonical_filename[PATH_MAX] = "\0";
char filename[PATH_MAX] = "\0";
struct tm stPrt;
char *env_time = getenv("LC_TIME");
if(!env_time)
@ -2510,9 +2513,11 @@ char* kdk_system_shortformat_transform(struct tm *ptr)
}
sprintf(path, "%s/.config/kydate/dateformat.conf", filename);
int ptr_year = ptr->tm_year;
ptr->tm_year = ptr->tm_year - 1900;
stPrt.tm_year = ptr_year - 1900;
int ptr_mon = ptr->tm_mon;
ptr->tm_mon = ptr->tm_mon - 1;
stPrt.tm_mon = ptr_mon - 1;
stPrt.tm_mday = ptr->tm_mday;
if (access(path, F_OK) == 0)
{
@ -2584,29 +2589,29 @@ char* kdk_system_shortformat_transform(struct tm *ptr)
else if(strstr(value, "yyyy-MM-dd"))
{
strftime(tmp, sizeof(tmp), "%Y-%m-%d", ptr);
strftime(tmp, sizeof(tmp), "%Y-%m-%d", &stPrt);
}
else if (strstr(value, "MM-dd-yyyy"))
{
strftime(tmp, sizeof(tmp), "%m-%d-%Y", ptr);
strftime(tmp, sizeof(tmp), "%m-%d-%Y", &stPrt);
}
else if(strstr(value, "yyyy/MM/dd"))
{
strftime(tmp, sizeof(tmp), "%Y/%m/%d", ptr);
strftime(tmp, sizeof(tmp), "%Y/%m/%d", &stPrt);
}
else if (strstr(value, "MM/dd/yyyy"))
{
strftime(tmp, sizeof(tmp), "%m/%d/%Y", ptr);
strftime(tmp, sizeof(tmp), "%m/%d/%Y", &stPrt);
}
else if(strstr(value, "yyyy.MM.dd"))
{
strftime(tmp, sizeof(tmp), "%Y.%m.%d", ptr);
strftime(tmp, sizeof(tmp), "%Y.%m.%d", &stPrt);
}
else if (strstr(value, "MM.dd.yyyy"))
{
strftime(tmp, sizeof(tmp), "%m.%d.%Y", ptr);
strftime(tmp, sizeof(tmp), "%m.%d.%Y", &stPrt);
}
g_key_file_free(config);

View File

@ -98,7 +98,7 @@ int main()
// free(timt);
int ret = kdk_system_set_long_dateformat("2230年3月4日");
ret = kdk_system_set_short_dateformat("22/5/3");
ret = kdk_system_set_short_dateformat("2203-05-03");
char *ld = kdk_system_get_longformat_date();
printf("long res = %s\n", ld);
@ -116,44 +116,31 @@ int main()
printf("shortformat = %s\n", ss);
free(ss);
struct tm ptr;
ptr.tm_year = 2322;
ptr.tm_mon = 9;
ptr.tm_mday = 3;
char *lt = kdk_system_longformat_transform(&ptr);
printf("longformat_transform = %s\n", lt);
free(lt);
struct tm ptr;
ptr.tm_year = 2022;
ptr.tm_mon = 3;
ptr.tm_mday = 2;
char *st = kdk_system_shortformat_transform(&ptr);
printf("shortformat_transform = %s\n", st);
free(st);
kdk_logn_dateinfo *info = kdk_system_login_lock_dateinfo("szm");
printf("date = %s,time = %s,week = %s\n",info->date,info->time,info->week);
kdk_free_logn_dateinfo(info);
ptr.tm_hour = 19;
ptr.tm_min = 3;
ptr.tm_sec = 2;
kdk_timeinfo *ti = kdk_system_timeformat_transform(&ptr);
printf("time = %s, timesec = %s\n", ti->time, ti->timesec);
kdk_free_timeinfo(ti);
ptr.tm_year = 2023;
ptr.tm_mon = 10;
ptr.tm_mday = 22;
ptr.tm_mon = 11;
ptr.tm_mday = 3;
ptr.tm_hour = 14;
ptr.tm_min = 53;
ptr.tm_sec = 21;
char *absDate = kdk_system_tran_absolute_date(&ptr);
printf("absDate = %s\n", absDate);
free(absDate);
free(absDate);
printf("year = %d, mon = %d, day = %d\n", ptr.tm_year, ptr.tm_mon, ptr.tm_mday);
char *st = kdk_system_shortformat_transform(&ptr);
printf("shortformat_transform = %s\n", st);
free(st);
char *lt = kdk_system_longformat_transform(&ptr);
printf("longformat_transform = %s\n", lt);
free(lt);
kdk_timeinfo *ti = kdk_system_timeformat_transform(&ptr);
printf("time = %s, timesec = %s\n", ti->time, ti->timesec);
kdk_free_timeinfo(ti);
// free(tvalue);
return 0;