Skip to content

Commit c44eb4f

Browse files
authored
Add AUTODOC_ParseDate (#294)
Using code formerly in AUTODOC_FormatDate
1 parent 5a1aa06 commit c44eb4f

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

gap/ToolFunctions.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ DeclareGlobalFunction( "AUTODOC_Diff" );
1717
DeclareGlobalFunction( "AUTODOC_TestWorkSheet" );
1818

1919
DeclareGlobalFunction( "AUTODOC_FormatDate" );
20+
DeclareGlobalFunction( "AUTODOC_ParseDate" );

gap/ToolFunctions.gi

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,34 @@ function(ws)
218218
od;
219219
end);
220220

221+
# Parse a date given as a string. Currently only supports the two formats
222+
# allowed in PackageInfo.g, namely "DD/MM/YYYY" or "YYYY-MM-DD". Returns a
223+
# record with entries `year`, `month`, `day` bound to the corresponding
224+
# integers extracted from the input string.
225+
#
226+
# Returns `fail` if the input could not be parsed.
227+
InstallGlobalFunction( AUTODOC_ParseDate,
228+
function(date)
229+
local day, month, year;
230+
if Length(date) <> 10 then
231+
return fail;
232+
fi;
233+
if date{[3,6]} = "//" then
234+
day := Int(date{[1,2]});
235+
month := Int(date{[4,5]});
236+
year := Int(date{[7..10]});
237+
elif date{[5,8]} = "--" then
238+
day := Int(date{[9,10]});
239+
month := Int(date{[6,7]});
240+
year := Int(date{[1..4]});
241+
else
242+
return fail;
243+
fi;
244+
if day = fail or month = fail or year = fail then
245+
return fail;
246+
fi;
247+
return rec( day := day, month := month, year := year );
248+
end);
221249

222250
BindGlobal("AUTODOC_months", MakeImmutable([
223251
"January", "February", "March",
@@ -226,7 +254,6 @@ BindGlobal("AUTODOC_months", MakeImmutable([
226254
"October", "November", "December"
227255
]));
228256

229-
230257
# Format a date into a human readable string; a date may consist of only
231258
# a year; or a year and a month; or a year, month and day. Dates are
232259
# formatted as "2019", resp. "February 2019" resp. "5 February 2019".
@@ -243,24 +270,7 @@ function(arg)
243270
if Length(arg) = 1 and IsRecord(arg[1]) then
244271
date := ShallowCopy(arg[1]);
245272
elif Length(arg) = 1 and IsString(arg[1]) then
246-
if Length(arg[1]) = 10 then
247-
date := arg[1];
248-
if date{[3,6]} = "//" then
249-
date := rec(
250-
day := Int(date{[1,2]}),
251-
month := Int(date{[4,5]}),
252-
year := Int(date{[7..10]}),
253-
);
254-
elif date{[5,8]} = "--" then
255-
date := rec(
256-
year := Int(date{[1..4]}),
257-
month := Int(date{[6,7]}),
258-
day := Int(date{[9,10]}),
259-
);
260-
else
261-
Unbind(date);
262-
fi;
263-
fi;
273+
date := AUTODOC_ParseDate(arg[1]);
264274
elif Length(arg) in [1..3] then
265275
date := rec();
266276
date.year := arg[1];
@@ -271,7 +281,7 @@ function(arg)
271281
date.day := arg[3];
272282
fi;
273283
fi;
274-
if not IsBound(date) then
284+
if not IsBound(date) or date = fail then
275285
Error("Invalid arguments");
276286
fi;
277287

tst/misc.tst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ gap> AUTODOC_SetIfMissing(r, "foo", 2);
1313
gap> r;
1414
rec( foo := 1 )
1515

16+
#
17+
# AUTODOC_ParseDate
18+
#
19+
gap> AUTODOC_ParseDate("2019-03-01");
20+
rec( day := 1, month := 3, year := 2019 )
21+
gap> AUTODOC_ParseDate("01/03/2019");
22+
rec( day := 1, month := 3, year := 2019 )
23+
gap> AUTODOC_ParseDate("01.03.2019");
24+
fail
25+
1626
#
1727
# AUTODOC_FormatDate
1828
#

0 commit comments

Comments
 (0)