Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gap/ToolFunctions.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ DeclareGlobalFunction( "AUTODOC_Diff" );
DeclareGlobalFunction( "AUTODOC_TestWorkSheet" );

DeclareGlobalFunction( "AUTODOC_FormatDate" );
DeclareGlobalFunction( "AUTODOC_ParseDate" );
50 changes: 30 additions & 20 deletions gap/ToolFunctions.gi
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,34 @@ function(ws)
od;
end);

# Parse a date given as a string. Currently only supports the two formats
# allowed in PackageInfo.g, namely "DD/MM/YYYY" or "YYYY-MM-DD". Returns a
# record with entries `year`, `month`, `day` bound to the corresponding
# integers extracted from the input string.
#
# Returns `fail` if the input could not be parsed.
InstallGlobalFunction( AUTODOC_ParseDate,
function(date)
local day, month, year;
if Length(date) <> 10 then
return fail;
fi;
if date{[3,6]} = "//" then
day := Int(date{[1,2]});
month := Int(date{[4,5]});
year := Int(date{[7..10]});
elif date{[5,8]} = "--" then
day := Int(date{[9,10]});
month := Int(date{[6,7]});
year := Int(date{[1..4]});
else
return fail;
fi;
if day = fail or month = fail or year = fail then
return fail;
fi;
return rec( day := day, month := month, year := year );
end);

BindGlobal("AUTODOC_months", MakeImmutable([
"January", "February", "March",
Expand All @@ -226,7 +254,6 @@ BindGlobal("AUTODOC_months", MakeImmutable([
"October", "November", "December"
]));


# Format a date into a human readable string; a date may consist of only
# a year; or a year and a month; or a year, month and day. Dates are
# formatted as "2019", resp. "February 2019" resp. "5 February 2019".
Expand All @@ -243,24 +270,7 @@ function(arg)
if Length(arg) = 1 and IsRecord(arg[1]) then
date := ShallowCopy(arg[1]);
elif Length(arg) = 1 and IsString(arg[1]) then
if Length(arg[1]) = 10 then
date := arg[1];
if date{[3,6]} = "//" then
date := rec(
day := Int(date{[1,2]}),
month := Int(date{[4,5]}),
year := Int(date{[7..10]}),
);
elif date{[5,8]} = "--" then
date := rec(
year := Int(date{[1..4]}),
month := Int(date{[6,7]}),
day := Int(date{[9,10]}),
);
else
Unbind(date);
fi;
fi;
date := AUTODOC_ParseDate(arg[1]);
elif Length(arg) in [1..3] then
date := rec();
date.year := arg[1];
Expand All @@ -271,7 +281,7 @@ function(arg)
date.day := arg[3];
fi;
fi;
if not IsBound(date) then
if not IsBound(date) or date = fail then
Error("Invalid arguments");
fi;

Expand Down
10 changes: 10 additions & 0 deletions tst/misc.tst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ gap> AUTODOC_SetIfMissing(r, "foo", 2);
gap> r;
rec( foo := 1 )

#
# AUTODOC_ParseDate
#
gap> AUTODOC_ParseDate("2019-03-01");
rec( day := 1, month := 3, year := 2019 )
gap> AUTODOC_ParseDate("01/03/2019");
rec( day := 1, month := 3, year := 2019 )
gap> AUTODOC_ParseDate("01.03.2019");
fail

#
# AUTODOC_FormatDate
#
Expand Down