Skip to content
Merged
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
49 changes: 25 additions & 24 deletions std/datetime/date.d
Original file line number Diff line number Diff line change
Expand Up @@ -7687,37 +7687,37 @@ public:
static Date fromSimpleString(S)(in S simpleString) @safe pure
if (isSomeString!(S))
{
import std.algorithm.searching : all, startsWith;
import std.ascii : isDigit;
import std.conv : to;
import std.exception : enforce;
import std.algorithm.searching : startsWith;
import std.conv : to, ConvException;
import std.format : format;
import std.string : strip;

auto dstr = to!dstring(strip(simpleString));

enforce(dstr.length >= 11, new DateTimeException(format("Invalid string format: %s", simpleString)));
auto str = strip(simpleString);

auto day = dstr[$-2 .. $];
auto month = monthFromString(to!string(dstr[$-6 .. $-3]));
auto year = dstr[0 .. $-7];
if (str.length < 11 || str[$-3] != '-' || str[$-7] != '-')
throw new DateTimeException(format!"Invalid string format: %s"(simpleString));

enforce(dstr[$-3] == '-', new DateTimeException(format("Invalid string format: %s", simpleString)));
enforce(dstr[$-7] == '-', new DateTimeException(format("Invalid string format: %s", simpleString)));
enforce(all!isDigit(day), new DateTimeException(format("Invalid string format: %s", simpleString)));
int year;
uint day;
auto month = monthFromString(str[$ - 6 .. $ - 3]);
auto yearStr = str[0 .. $ - 7];
auto signAtBegining = cast(bool) yearStr.startsWith('-', '+');
if ((yearStr.length > 4) != signAtBegining)
{
throw new DateTimeException(format!"Invalid string format: %s"(simpleString));
}

if (year.length > 4)
try
{
enforce(year.startsWith('-', '+'),
new DateTimeException(format("Invalid string format: %s", simpleString)));
enforce(all!isDigit(year[1..$]),
new DateTimeException(format("Invalid string format: %s", simpleString)));
day = to!uint(str[$ - 2 .. $]);
year = to!int(yearStr);
}
catch (ConvException)
{
throw new DateTimeException(format!"Invalid string format: %s"(simpleString));
}
else
enforce(all!isDigit(year),
new DateTimeException(format("Invalid string format: %s", simpleString)));

return Date(to!short(year), month, to!ubyte(day));
return Date(year, month, day);
}

///
Expand Down Expand Up @@ -10244,7 +10244,8 @@ string monthToString(Month month) @safe pure
$(REF DateTimeException,std,datetime,date) if the given month is not a
valid month string.
+/
Month monthFromString(string monthStr) @safe pure
Month monthFromString(T)(T monthStr) @safe pure
if (isSomeString!T)
{
import std.format : format;
switch (monthStr)
Expand Down Expand Up @@ -10274,7 +10275,7 @@ Month monthFromString(string monthStr) @safe pure
case "Dec":
return Month.dec;
default:
throw new DateTimeException(format("Invalid month %s", monthStr));
throw new DateTimeException(format!"Invalid month %s"(monthStr));
}
}

Expand Down