DateTime: adds AM/PM format support to date and time output

Fixes #2325
This commit is contained in:
李通洲
2026-05-12 22:54:44 +08:00
parent af7e2d47d1
commit d6bd855733
+35 -26
View File
@@ -35,6 +35,7 @@ typedef struct FFDateTimeResult {
char secondPretty[FASTFETCH_STRBUF_DEFAULT_ALLOC]; // 37
char offsetFromUtc[FASTFETCH_STRBUF_DEFAULT_ALLOC];
char timezoneName[FASTFETCH_STRBUF_DEFAULT_ALLOC];
char amPm[FASTFETCH_STRBUF_DEFAULT_ALLOC];
} FFDateTimeResult;
static void printDateTimeFormat(struct tm* tm, const FFModuleArgs* moduleArgs) {
@@ -63,32 +64,39 @@ static void printDateTimeFormat(struct tm* tm, const FFModuleArgs* moduleArgs) {
strftime(result.secondPretty, sizeof(result.secondPretty), "%S", tm);
strftime(result.offsetFromUtc, sizeof(result.offsetFromUtc), "%z", tm);
strftime(result.timezoneName, sizeof(result.timezoneName), "%Z", tm);
strftime(result.amPm, sizeof(result.amPm), "%p", tm);
FF_PRINT_FORMAT_CHECKED(FF_DATETIME_DISPLAY_NAME, 0, moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
FF_ARG(result.year, "year"), // 1
FF_ARG(result.yearShort, "year-short"), // 2
FF_ARG(result.month, "month"), // 3
FF_ARG(result.monthPretty, "month-pretty"), // 4
FF_ARG(result.monthName, "month-name"), // 5
FF_ARG(result.monthNameShort, "month-name-short"), // 6
FF_ARG(result.week, "week"), // 7
FF_ARG(result.weekday, "weekday"), // 8
FF_ARG(result.weekdayShort, "weekday-short"), // 9
FF_ARG(result.dayInYear, "day-in-year"), // 10
FF_ARG(result.dayInMonth, "day-in-month"), // 11
FF_ARG(result.dayInWeek, "day-in-week"), // 12
FF_ARG(result.hour, "hour"), // 13
FF_ARG(result.hourPretty, "hour-pretty"), // 14
FF_ARG(result.hour12, "hour-12"), // 15
FF_ARG(result.hour12Pretty, "hour-12-pretty"), // 16
FF_ARG(result.minute, "minute"), // 17
FF_ARG(result.minutePretty, "minute-pretty"), // 18
FF_ARG(result.second, "second"), // 19
FF_ARG(result.secondPretty, "second-pretty"), // 20
FF_ARG(result.offsetFromUtc, "offset-from-utc"), // 21
FF_ARG(result.timezoneName, "timezone-name"), // 22
FF_ARG(result.dayPretty, "day-pretty"), // 23
}));
FF_PRINT_FORMAT_CHECKED(
FF_DATETIME_DISPLAY_NAME,
0,
moduleArgs,
FF_PRINT_TYPE_DEFAULT,
((FFformatarg[]){
FF_ARG(result.year, "year"), // 1
FF_ARG(result.yearShort, "year-short"), // 2
FF_ARG(result.month, "month"), // 3
FF_ARG(result.monthPretty, "month-pretty"), // 4
FF_ARG(result.monthName, "month-name"), // 5
FF_ARG(result.monthNameShort, "month-name-short"), // 6
FF_ARG(result.week, "week"), // 7
FF_ARG(result.weekday, "weekday"), // 8
FF_ARG(result.weekdayShort, "weekday-short"), // 9
FF_ARG(result.dayInYear, "day-in-year"), // 10
FF_ARG(result.dayInMonth, "day-in-month"), // 11
FF_ARG(result.dayInWeek, "day-in-week"), // 12
FF_ARG(result.hour, "hour"), // 13
FF_ARG(result.hourPretty, "hour-pretty"), // 14
FF_ARG(result.hour12, "hour-12"), // 15
FF_ARG(result.hour12Pretty, "hour-12-pretty"), // 16
FF_ARG(result.minute, "minute"), // 17
FF_ARG(result.minutePretty, "minute-pretty"), // 18
FF_ARG(result.second, "second"), // 19
FF_ARG(result.secondPretty, "second-pretty"), // 20
FF_ARG(result.offsetFromUtc, "offset-from-utc"), // 21
FF_ARG(result.timezoneName, "timezone-name"), // 22
FF_ARG(result.dayPretty, "day-pretty"), // 23
FF_ARG(result.amPm, "am-pm"), // 24
}));
}
bool ffPrintDateTime(FFDateTimeOptions* options) {
@@ -152,7 +160,7 @@ FFModuleBaseInfo ffDateTimeModuleInfo = {
.printModule = (void*) ffPrintDateTime,
.generateJsonResult = (void*) ffGenerateDateTimeJsonResult,
.generateJsonConfig = (void*) ffGenerateDateTimeJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]){
{ "Year", "year" },
{ "Last two digits of year", "year-short" },
{ "Month", "month" },
@@ -176,5 +184,6 @@ FFModuleBaseInfo ffDateTimeModuleInfo = {
{ "Offset from UTC in the ISO 8601 format", "offset-from-utc" },
{ "Locale-dependent timezone name or abbreviation", "timezone-name" },
{ "Day in month with leading zero", "day-pretty" },
{ "AM or PM", "am-pm" },
}))
};