Allow argument to monnum to be a string, in which case it's parsed as a month name.

This commit is contained in:
Dianne Skoll
2025-05-14 09:57:16 -04:00
parent 398100c6e3
commit c64d939237
4 changed files with 104 additions and 2 deletions

View File

@@ -3714,9 +3714,13 @@ If \fIarg\fR is of \fBDATE\fR or \fBDATETIME\fR type, returns a string
that names the month component of the date. If \fIarg\fR is an
\fBINT\fR from 1 to 12, returns a string that names the month.
.TP
.B monnum(dq_date)
.B monnum(dq_date)\fR or \fBmonnum(s_str)\fR
Returns an \fBINT\fR from 1 to 12, representing the month component of
\fIdate\fR.
\fIdate\fR. If a \fBSTRING\fR is supplied rather than a \fBDATE\fR or
\fBDATETIME\fR, then if \fIstr\fR is a valid month name (or minimum
3-character abbreviation of a month name) then the corresponding month
number is returned. If \fIstr\fR is not a valid month name, then an
error occurs.
.TP
.B moondate(i_phase [,d_date [,t_time]]) \fRor\fB moondate(i_phase, q_datetime)
This function returns the date of the first occurrence of the phase

View File

@@ -723,6 +723,18 @@ static int FDay(func_info *info)
static int FMonnum(func_info *info)
{
int y, m, d, v;
Token tok;
if (ARG(0).type == STR_TYPE) {
/* Convert a month name to a month number */
FindToken(ARG(0).v.str, &tok);
if (tok.type != T_Month) {
return E_BAD_TYPE;
}
RetVal.type = INT_TYPE;
RETVAL = tok.val + 1;
return OK;
}
if (!HASDATE(ARG(0))) return E_BAD_TYPE;
v = DATEPART(ARG(0));

View File

@@ -16299,6 +16299,62 @@ catch(*Type mismatch*, 42) => 42
catch(*Division by zero*, 42) => 42
set m catcherr()
catcherr() => "Number too high"
# Test monnum("string")
set a monnum("January")
monnum("January") => 1
set a monnum("February")
monnum("February") => 2
set a monnum("March")
monnum("March") => 3
set a monnum("April")
monnum("April") => 4
set a monnum("May")
monnum("May") => 5
set a monnum("June")
monnum("June") => 6
set a monnum("July")
monnum("July") => 7
set a monnum("August")
monnum("August") => 8
set a monnum("September")
monnum("September") => 9
set a monnum("October")
monnum("October") => 10
set a monnum("November")
monnum("November") => 11
set a monnum("December")
monnum("December") => 12
set a monnum("Jan")
monnum("Jan") => 1
set a monnum("Feb")
monnum("Feb") => 2
set a monnum("Mar")
monnum("Mar") => 3
set a monnum("Apr")
monnum("Apr") => 4
set a monnum("May")
monnum("May") => 5
set a monnum("Jun")
monnum("Jun") => 6
set a monnum("Jul")
monnum("Jul") => 7
set a monnum("Aug")
monnum("Aug") => 8
set a monnum("Sep")
monnum("Sep") => 9
set a monnum("oct")
monnum("oct") => 10
set a monnum("NOV")
monnum("NOV") => 11
set a monnum("Dec")
monnum("Dec") => 12
set a monnum("not a month")
monnum("not a month") => Type mismatch
../tests/test.rem(1483): monnum(): Type mismatch
DEBUG -x
DEBUG -e
Variable hash table statistics:

View File

@@ -1452,6 +1452,36 @@ set a catch(4/0, catch(4/0, 39))
set m catcherr()
set a catch(1/0, catch("f" * "g", catch($IntMin*88, 42)))
set m catcherr()
# Test monnum("string")
set a monnum("January")
set a monnum("February")
set a monnum("March")
set a monnum("April")
set a monnum("May")
set a monnum("June")
set a monnum("July")
set a monnum("August")
set a monnum("September")
set a monnum("October")
set a monnum("November")
set a monnum("December")
set a monnum("Jan")
set a monnum("Feb")
set a monnum("Mar")
set a monnum("Apr")
set a monnum("May")
set a monnum("Jun")
set a monnum("Jul")
set a monnum("Aug")
set a monnum("Sep")
set a monnum("oct")
set a monnum("NOV")
set a monnum("Dec")
set a monnum("not a month")
DEBUG -x
DEBUG -e