#include <stdio.h>
#include <windows.h>
#include "time_display.h"
#define MAX_TIME_STR (256) // 时间字符串最大长度
#define MAX_WEEK (7)
#define MAX_MONTH (12)
#define TIME_HALFDAY (12)
#define TEST_ZERO (0)
#define FORMAT_ERROR (0)
#define TEST_ONE (1)
#define TEST_FIVE (5)
#define CONDITIONONE (1)
#define CONDITIONTWO (2)
#define CONDITIONTHREE (3)
#define CONDITIONFOUR (4)
#define CONDITIONFIVE (5)
static const U1* u1p_sp_TIME_WEEKZH[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
static const U1* u1p_sp_TIME_WEEKEN[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
static const U1* u1p_sp_TIME_WEEKJP[] = { "日", "月", "火", "水", "木", "金", "土" };
static const U1* u1p_sp_TIME_MONTHEN[] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
static U4 u4_s_time_Format(U1 u1_a_format_index, SYSTEMTIME* stp_a_time_t, U1* u1p_a_time_bufferone, size_t u4_a_buf_size)
{
U4 u4_t_format_ret ; // 默认返回错误值
U2 u2_t_time_y;
S1 s1_t_time_m;
S1 s1_t_time_d;
S1 s1_t_time_h24;
S1 s1_t_time_min;
S1 s1_t_time_sec;
S1 s1_t_time_w;
S1 s1_t_time_h12;
const S1* s1p_t_TIME_AMPM;
if (!u1p_a_time_bufferone || u4_a_buf_size < (U4)MAX_TIME_STR || !stp_a_time_t)
{
u4_t_format_ret= (U4)FORMAT_ERROR; // 直接返回错误
}
else
{
// 时间变量提取
u2_t_time_y = (U2)stp_a_time_t->wYear;
s1_t_time_m = (S1)stp_a_time_t->wMonth;
s1_t_time_d = (S1)stp_a_time_t->wDay;
s1_t_time_h24 = (S1)stp_a_time_t->wHour;
s1_t_time_min = (S1)stp_a_time_t->wMinute;
s1_t_time_sec = (S1)stp_a_time_t->wSecond;
s1_t_time_w = (S1)stp_a_time_t->wDayOfWeek;
if ((U1)TEST_ZERO == s1_t_time_h24 % (U1)TIME_HALFDAY)
{
s1_t_time_h12 = (U1)TIME_HALFDAY;
}
else
{
s1_t_time_h12 = s1_t_time_h24 % (U1)TIME_HALFDAY;
}
if (s1_t_time_h24 >= (U1)TIME_HALFDAY)
{
s1p_t_TIME_AMPM = "PM";
}
else
{
s1p_t_TIME_AMPM = "AM";
}
// 格式选择
switch (u1_a_format_index)
{
case (U4)CONDITIONONE:
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"格式1:%u年%u月%u日 %s %u点%u分%u秒",
u2_t_time_y, s1_t_time_m, s1_t_time_d,
u1p_sp_TIME_WEEKZH[s1_t_time_w],
s1_t_time_h24, s1_t_time_min, s1_t_time_sec);
break;
case (U4)CONDITIONTWO:
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"格式2:%u/%u/%u %s %02u:%02u:%02u",
u2_t_time_y, s1_t_time_m, s1_t_time_d,
u1p_sp_TIME_WEEKZH[s1_t_time_w],
s1_t_time_h24, s1_t_time_min, s1_t_time_sec);
break;
case (U4)CONDITIONTHREE:
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"格式3:%u/%u/%u %s %u:%02u:%02u(%s)",
u2_t_time_y, s1_t_time_m, s1_t_time_d,
u1p_sp_TIME_WEEKZH[s1_t_time_w],
s1_t_time_h24, s1_t_time_min, s1_t_time_sec, s1p_t_TIME_AMPM);
break;
case (U4)CONDITIONFOUR:
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"格式4:%s, %s %u, %u %02u:%02u:%02u",
u1p_sp_TIME_WEEKEN[s1_t_time_w],
u1p_sp_TIME_MONTHEN[s1_t_time_m - 1],
s1_t_time_d, u2_t_time_y,
s1_t_time_h24, s1_t_time_min, s1_t_time_sec);
break;
case (U4)CONDITIONFIVE:
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"格式5:%u年%u月%u日(%s) %u:%02u:%02u(%s)",
u2_t_time_y, s1_t_time_m, s1_t_time_d,
u1p_sp_TIME_WEEKJP[s1_t_time_w],
s1_t_time_h12, s1_t_time_min, s1_t_time_sec, s1p_t_TIME_AMPM);
break;
default:
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"[错误] 无效格式编号:%u", u1_a_format_index);
break;
}
}
return u4_t_format_ret;
}
// 显示指定格式的时间(使用核心格式化函数)
void vd_g_time_Display_format(U1 u1_a_format_index) {
SYSTEMTIME st_t_format_t;
GetLocalTime(&st_t_format_t);
U1 u1_tp_format_buffer[(U4)MAX_TIME_STR];
u4_s_time_Format(u1_a_format_index, &st_t_format_t, u1_tp_format_buffer, sizeof(u1_tp_format_buffer));
printf("%s\n", u1_tp_format_buffer); // 添加换行符
}
// 显示所有格式的时间
void vd_g_time_Display_allformats(void)
{
U1 u1_t_allformats_i;
for (u1_t_allformats_i = (U1)TEST_ONE; u1_t_allformats_i <= (U1)TEST_FIVE; ++u1_t_allformats_i)
{
vd_g_time_Display_format(u1_t_allformats_i);
}
}
// 写入所有格式到文件(使用核心格式化函数)
void vd_g_time_Write_allformats_to_file(void)
{
FILE* stp_t_file_fp;
errno_t s4_t_file_err;
SYSTEMTIME st_t_systemtime_t;
U1 u1_t_file_i;
U1 u1_tp_file_buffer[(U4)MAX_TIME_STR];
S4 s4_t_fclose_result;
// 正确使用 fopen_s 需要三个参数:文件指针地址、文件名、打开模式
s4_t_file_err = fopen_s(&stp_t_file_fp, "time_log.txt", "a");
if ((S4)TEST_ONE!= s4_t_file_err || NULL == stp_t_file_fp) {
printf("[错误] 无法打开 time_log.txt,错误码:%d\n", s4_t_file_err);
return;
}
else
{
/*do nothing*/
}
GetLocalTime(&st_t_systemtime_t);
for (u1_t_file_i = 1; u1_t_file_i <= 5; ++u1_t_file_i) {
u4_s_time_Format(u1_t_file_i, &st_t_systemtime_t, u1_tp_file_buffer, sizeof(u1_tp_file_buffer));
fprintf(stp_t_file_fp, "%s\n", u1_tp_file_buffer); // 添加换行符
}
fprintf(stp_t_file_fp, "----------------------------------------\n\n"); // 增强分隔符
s4_t_fclose_result = fclose(stp_t_file_fp);
if ((S4)TEST_ZERO != s4_t_fclose_result)
{
printf("[警告] 文件关闭失败\n");
}
else
{
/*do nothing*/
}
}添加英文注释,用/* */单行注释
最新发布