Add optional argument to ampm() to specify that hour should be zero-padded to two digits.
All checks were successful
Remind unit tests / tests (push) Successful in 46s

This commit is contained in:
Dianne Skoll
2024-08-22 17:38:54 -04:00
parent 5a3980b5b8
commit 0b28dde9c7
4 changed files with 366 additions and 141 deletions

View File

@@ -2909,18 +2909,25 @@ is supplied, only the date component is used.
Returns the time of "astronomical twilight" on the specified \fIdate\fR. If
\fIdate\fR is omitted, defaults to \fBtoday()\fR.
.TP
.B ampm(tq_time [,s_am [,s_pm]])
.B ampm(tq_time [,s_am [,s_pm [,i_lz]]])
Returns a \fBSTRING\fR that is the result of converting \fItime\fR
(which is either a \fBTIME\fR or a \fBDATETIME\fR object) to "AM/PM"
format. The optional arguments \fIam\fR and \fIpm\fR are the strings
to append in the AM and PM case, respectively; they default to "AM"
and "PM". The function obeys the system variables $DateSep,
$TimeSep and $DateTimeSep when formatting its output. For example:
and "PM". The optional argument \fIlz\fR specifies whether or not
the hour should be padded to two digits with a leading zero. If \fIlz\fR is
zero, then a leading 0 is not added; otherwise, the hour is padded out to
two digits with a leading zero. If not supplied, \fIlz\fR defaults to zero.
.RS
.PP
The function obeys the system variables $DateSep, $TimeSep and
$DateTimeSep when formatting its output. Here are some examples of
its output:
.PP
.nf
ampm(0:22) returns "12:22AM"
ampm(17:45, "am", "pm") returns "5:45pm"
ampm(17:45, "am", "pm", 1) returns "05:45pm"
ampm('2020-03-14@21:34') returns "2020-03-14@9:34PM"
.fi
.PP

View File

@@ -231,7 +231,7 @@ BuiltinFunc Func[] = {
{ "access", 2, 2, 0, FAccess, NULL },
{ "adawn", 0, 1, 0, FADawn, NULL},
{ "adusk", 0, 1, 0, FADusk, NULL},
{ "ampm", 1, 3, 1, FAmpm, NULL },
{ "ampm", 1, 4, 1, FAmpm, NULL },
{ "ansicolor", 1, 5, 1, FAnsicolor, NULL },
{ "args", 1, 1, 0, FArgs, NULL },
{ "asc", 1, 1, 1, FAsc, NULL },
@@ -945,6 +945,8 @@ static int FAmpm(func_info *info)
char const *pm = "PM";
char const *ampm = NULL;
int include_leading_zero = 0;
char outbuf[128];
if (ARG(0).type != DATETIME_TYPE && ARG(0).type != TIME_TYPE) {
@@ -959,6 +961,10 @@ static int FAmpm(func_info *info)
if (Nargs >= 3) {
ASSERT_TYPE(2, STR_TYPE);
pm = ARGSTR(2);
if (Nargs >= 4) {
ASSERT_TYPE(3, INT_TYPE);
include_leading_zero = ARGV(3);
}
}
}
h = TIMEPART(ARG(0)) / 60;
@@ -973,9 +979,17 @@ static int FAmpm(func_info *info)
}
} else {
if (ARG(0).type == DATETIME_TYPE) {
snprintf(outbuf, sizeof(outbuf), "%04d%c%02d%c%02d%c%d%c%02d", yr, DateSep, mo+1, DateSep, da, DateTimeSep, h, TimeSep, m);
if (include_leading_zero) {
snprintf(outbuf, sizeof(outbuf), "%04d%c%02d%c%02d%c%02d%c%02d", yr, DateSep, mo+1, DateSep, da, DateTimeSep, h, TimeSep, m);
} else {
snprintf(outbuf, sizeof(outbuf), "%04d%c%02d%c%02d%c%d%c%02d", yr, DateSep, mo+1, DateSep, da, DateTimeSep, h, TimeSep, m);
}
} else {
snprintf(outbuf, sizeof(outbuf), "%d%c%02d", h, TimeSep, m);
if (include_leading_zero) {
snprintf(outbuf, sizeof(outbuf), "%02d%c%02d", h, TimeSep, m);
} else {
snprintf(outbuf, sizeof(outbuf), "%d%c%02d", h, TimeSep, m);
}
}
}
ampm = am;
@@ -984,9 +998,17 @@ static int FAmpm(func_info *info)
h -= 12;
}
if (ARG(0).type == DATETIME_TYPE) {
snprintf(outbuf, sizeof(outbuf), "%04d%c%02d%c%02d%c%d%c%02d", yr, DateSep, mo+1, DateSep, da, DateTimeSep, h, TimeSep, m);
if (include_leading_zero) {
snprintf(outbuf, sizeof(outbuf), "%04d%c%02d%c%02d%c%02d%c%02d", yr, DateSep, mo+1, DateSep, da, DateTimeSep, h, TimeSep, m);
} else {
snprintf(outbuf, sizeof(outbuf), "%04d%c%02d%c%02d%c%d%c%02d", yr, DateSep, mo+1, DateSep, da, DateTimeSep, h, TimeSep, m);
}
} else {
snprintf(outbuf, sizeof(outbuf), "%d%c%02d", h, TimeSep, m);
if (include_leading_zero) {
snprintf(outbuf, sizeof(outbuf), "%02d%c%02d", h, TimeSep, m);
} else {
snprintf(outbuf, sizeof(outbuf), "%d%c%02d", h, TimeSep, m);
}
}
ampm = pm;
}

File diff suppressed because one or more lines are too long

View File

@@ -708,6 +708,56 @@ set x ampm(21:12) + ""
set x ampm(22:12) + ""
set x ampm(23:12) + ""
set x ampm(0:12,"AM", "PM", 1) + ""
set x ampm(1:12,"AM", "PM", 1) + ""
set x ampm(2:12,"AM", "PM", 1) + ""
set x ampm(3:12,"AM", "PM", 1) + ""
set x ampm(4:12,"AM", "PM", 1) + ""
set x ampm(5:12,"AM", "PM", 1) + ""
set x ampm(6:12,"AM", "PM", 1) + ""
set x ampm(7:12,"AM", "PM", 1) + ""
set x ampm(8:12,"AM", "PM", 1) + ""
set x ampm(9:12,"AM", "PM", 1) + ""
set x ampm(10:12,"AM", "PM", 1) + ""
set x ampm(11:12,"AM", "PM", 1) + ""
set x ampm(12:12,"AM", "PM", 1) + ""
set x ampm(13:12,"AM", "PM", 1) + ""
set x ampm(14:12,"AM", "PM", 1) + ""
set x ampm(15:12,"AM", "PM", 1) + ""
set x ampm(16:12,"AM", "PM", 1) + ""
set x ampm(17:12,"AM", "PM", 1) + ""
set x ampm(18:12,"AM", "PM", 1) + ""
set x ampm(19:12,"AM", "PM", 1) + ""
set x ampm(20:12,"AM", "PM", 1) + ""
set x ampm(21:12,"AM", "PM", 1) + ""
set x ampm(22:12,"AM", "PM", 1) + ""
set x ampm(23:12,"AM", "PM", 1) + ""
set x ampm(0:02,"AM", "PM", 0) + ""
set x ampm(0:02,"AM", "PM", 0) + ""
set x ampm(2:02,"AM", "PM", 0) + ""
set x ampm(3:02,"AM", "PM", 0) + ""
set x ampm(4:02,"AM", "PM", 0) + ""
set x ampm(5:02,"AM", "PM", 0) + ""
set x ampm(6:02,"AM", "PM", 0) + ""
set x ampm(7:02,"AM", "PM", 0) + ""
set x ampm(8:02,"AM", "PM", 0) + ""
set x ampm(9:02,"AM", "PM", 0) + ""
set x ampm(00:02,"AM", "PM", 0) + ""
set x ampm(00:02,"AM", "PM", 0) + ""
set x ampm(02:02,"AM", "PM", 0) + ""
set x ampm(03:02,"AM", "PM", 0) + ""
set x ampm(04:02,"AM", "PM", 0) + ""
set x ampm(05:02,"AM", "PM", 0) + ""
set x ampm(06:02,"AM", "PM", 0) + ""
set x ampm(07:02,"AM", "PM", 0) + ""
set x ampm(08:02,"AM", "PM", 0) + ""
set x ampm(09:02,"AM", "PM", 0) + ""
set x ampm(20:02,"AM", "PM", 0) + ""
set x ampm(20:02,"AM", "PM", 0) + ""
set x ampm(22:02,"AM", "PM", 0) + ""
set x ampm(23:02,"AM", "PM", 0) + ""
# Coerce with am/pm
set x coerce("TIME", "12:45am")
set x coerce("TIME", "12:45")