#include <stdio.h>
#include <windows.h>
#include "time_display.h"
#define MAX_TIME_STR (256) /* Maximum length of time string */
#define MAX_WEEK (7) /* Number of days in a week */
#define MAX_MONTH (12) /* Number of months in a year */
#define TIME_HALFDAY (12) /* Half-day point for AM/PM conversion */
#define TEST_ZERO (0) /* Zero value constant */
#define FORMAT_ERROR (0) /* Error code for format failure */
#define TEST_ONE (1) /* Numeric constant one */
#define TEST_FIVE (5) /* Numeric constant five */
#define CONDITIONONE (1) /* Format index constant 1 */
#define CONDITIONTWO (2) /* Format index constant 2 */
#define CONDITIONTHREE (3) /* Format index constant 3 */
#define CONDITIONFOUR (4) /* Format index constant 4 */
#define CONDITIONFIVE (5) /* Format index constant 5 */
/* Chinese weekday names */
static const U1* u1p_sp_TIME_WEEKZH[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
/* English weekday names */
static const U1* u1p_sp_TIME_WEEKEN[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
/* Japanese weekday abbreviations */
static const U1* u1p_sp_TIME_WEEKJP[] = { "日", "月", "火", "水", "木", "金", "土" };
/* English month names */
static const U1* u1p_sp_TIME_MONTHEN[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
/* Format time according to specified index */
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; /* Return value storage */
U2 u2_t_time_y; /* Year component */
S1 s1_t_time_m; /* Month component */
S1 s1_t_time_d; /* Day component */
S1 s1_t_time_h24; /* Hour in 24-hour format */
S1 s1_t_time_min; /* Minute component */
S1 s1_t_time_sec; /* Second component */
S1 s1_t_time_w; /* Weekday index */
S1 s1_t_time_h12; /* Hour in 12-hour format */
const S1* s1p_t_TIME_AMPM; /* AM/PM indicator */
/* Validate input parameters */
if (!u1p_a_time_bufferone || u4_a_buf_size < (U4)MAX_TIME_STR || !stp_a_time_t)
{
u4_t_format_ret = (U4)FORMAT_ERROR; /* Return error on invalid input */
}
else
{
/* Extract time components from SYSTEMTIME structure */
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;
/* Convert 24-hour to 12-hour format */
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;
}
/* Determine AM/PM indicator */
if (s1_t_time_h24 >= (U1)TIME_HALFDAY)
{
s1p_t_TIME_AMPM = "PM";
}
else
{
s1p_t_TIME_AMPM = "AM";
}
/* Select format based on index */
switch (u1_a_format_index)
{
case (U4)CONDITIONONE: /* Chinese format with weekday */
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: /* Numeric date with Chinese weekday */
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: /* Numeric date with AM/PM */
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: /* English full date format */
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: /* Japanese-style format */
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: /* Handle invalid format index */
u4_t_format_ret = snprintf(u1p_a_time_bufferone, u4_a_buf_size,
"[错误] 无效格式编号:%u", u1_a_format_index);
break;
}
}
return u4_t_format_ret; /* Return formatted string length */
}
/* Display time in specified format */
void vd_g_time_Display_format(U1 u1_a_format_index) {
SYSTEMTIME st_t_format_t; /* System time structure */
GetLocalTime(&st_t_format_t); /* Get current local time */
U1 u1_tp_format_buffer[(U4)MAX_TIME_STR]; /* Format buffer */
/* Format and print time */
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); /* Print with newline */
}
/* Display all available time formats */
void vd_g_time_Display_allformats(void)
{
U1 u1_t_allformats_i; /* Loop counter */
/* Iterate through all format options */
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); /* Display each format */
}
}
/* Write all time formats to log file */
void vd_g_time_Write_allformats_to_file(void)
{
FILE* stp_t_file_fp; /* File pointer */
errno_t s4_t_file_err; /* File error code */
SYSTEMTIME st_t_systemtime_t; /* System time structure */
U1 u1_t_file_i; /* Loop counter */
U1 u1_tp_file_buffer[(U4)MAX_TIME_STR]; /* Format buffer */
S4 s4_t_fclose_result; /* File close result */
/* Open log file in append mode */
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
{
/* No action needed on success */
}
GetLocalTime(&st_t_systemtime_t); /* Get current time */
/* Write all formats to file */
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); /* Write with newline */
}
/* Add separator between entries */
fprintf(stp_t_file_fp, "----------------------------------------\n\n");
/* Close file handle */
s4_t_fclose_result = fclose(stp_t_file_fp);
if ((S4)TEST_ZERO != s4_t_fclose_result)
{
printf("[警告] 文件关闭失败\n"); /* Warn on close failure */
}
else
{
/* No action needed on success */
}
}
/*===================================================================================================================================*/
/* Display Application Opening2 : Post Task */
/* --------------------------------------------------------------------------------------------------------------------------------- */
/* Arguments: U1 u1_contents : Selected Content ID in dspmgr */
/* Return: - */
/*===================================================================================================================================*/按照这个格式帮我注释函数,原来的注释不要删了