@@ -218,6 +218,34 @@ function(ws)
218218 od ;
219219end );
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
222250BindGlobal(" 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
0 commit comments