Skip to content

Commit f7e523b

Browse files
jmdavisdlang-bot
authored andcommitted
Fix Bugzilla issue 24704: Improve error messages for from*String in std.datetime.
The documentation for the from*String functions specifies what they accept, and the fromISO*String functions accept ISO or ISO extended strings, but it is technically the case that there are ISO and ISO extended strings which they do not accept (since the standard specifies what the fields should look like for each string but doesn't specify that all fields must be present or that any code parsing them must accept all variants). So, technically, the error messages which have said that the given strings are not valid ISO or ISO extended strings are not necessarily correct. So, this changes the error messages to remove that ambiguity.
1 parent bbb1566 commit f7e523b

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

std/datetime/date.d

+21-19
Original file line numberDiff line numberDiff line change
@@ -3161,10 +3161,10 @@ public:
31613161

31623162
auto str = strip(isoString);
31633163

3164-
enforce(str.length >= 15, new DateTimeException(format("Invalid ISO String: %s", isoString)));
3164+
enforce!DateTimeException(str.length >= 15, format("Invalid format for DateTime.fromISOString %s", isoString));
31653165
auto t = str.byCodeUnit.countUntil('T');
31663166

3167-
enforce(t != -1, new DateTimeException(format("Invalid ISO String: %s", isoString)));
3167+
enforce!DateTimeException(t != -1, format("Invalid format for DateTime.fromISOString: %s", isoString));
31683168

31693169
immutable date = Date.fromISOString(str[0 .. t]);
31703170
immutable tod = TimeOfDay.fromISOString(str[t+1 .. $]);
@@ -3262,10 +3262,11 @@ public:
32623262

32633263
auto str = strip(isoExtString);
32643264

3265-
enforce(str.length >= 15, new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
3265+
enforce!DateTimeException(str.length >= 15,
3266+
format("Invalid format for DateTime.fromISOExtString: %s", isoExtString));
32663267
auto t = str.byCodeUnit.countUntil('T');
32673268

3268-
enforce(t != -1, new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
3269+
enforce!DateTimeException(t != -1, format("Invalid format for DateTime.fromISOExtString: %s", isoExtString));
32693270

32703271
immutable date = Date.fromISOExtString(str[0 .. t]);
32713272
immutable tod = TimeOfDay.fromISOExtString(str[t+1 .. $]);
@@ -3362,10 +3363,11 @@ public:
33623363

33633364
auto str = strip(simpleString);
33643365

3365-
enforce(str.length >= 15, new DateTimeException(format("Invalid string format: %s", simpleString)));
3366+
enforce!DateTimeException(str.length >= 15,
3367+
format("Invalid format for DateTime.fromSimpleString: %s", simpleString));
33663368
auto t = str.byCodeUnit.countUntil(' ');
33673369

3368-
enforce(t != -1, new DateTimeException(format("Invalid string format: %s", simpleString)));
3370+
enforce!DateTimeException(t != -1, format("Invalid format for DateTime.fromSimpleString: %s", simpleString));
33693371

33703372
immutable date = Date.fromSimpleString(str[0 .. t]);
33713373
immutable tod = TimeOfDay.fromISOExtString(str[t+1 .. $]);
@@ -7628,7 +7630,7 @@ public:
76287630

76297631
auto str = isoString.strip;
76307632

7631-
enforce!DateTimeException(str.length >= 8, text("Invalid ISO String: ", isoString));
7633+
enforce!DateTimeException(str.length >= 8, text("Invalid format for Date.fromISOString: ", isoString));
76327634

76337635
int day, month, year;
76347636
auto yearStr = str[0 .. $ - 4];
@@ -7643,7 +7645,7 @@ public:
76437645
if (yearStr.length > 4)
76447646
{
76457647
enforce!DateTimeException(yearStr.startsWith('-', '+'),
7646-
text("Invalid ISO String: ", isoString));
7648+
text("Invalid format for Date.fromISOString: ", isoString));
76477649
year = to!int(yearStr);
76487650
}
76497651
else
@@ -7653,7 +7655,7 @@ public:
76537655
}
76547656
catch (ConvException)
76557657
{
7656-
throw new DateTimeException(text("Invalid ISO String: ", isoString));
7658+
throw new DateTimeException(text("Invalid format for Date.fromISOString: ", isoString));
76577659
}
76587660

76597661
return Date(year, month, day);
@@ -7774,13 +7776,13 @@ public:
77747776
ubyte month, day;
77757777

77767778
if (str.length < 10 || str[$-3] != '-' || str[$-6] != '-')
7777-
throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString));
7779+
throw new DateTimeException(format("Invalid format for Date.fromISOExtString: %s", isoExtString));
77787780

77797781
auto yearStr = str[0 .. $-6];
77807782
auto signAtBegining = cast(bool) yearStr.startsWith('-', '+');
77817783
if ((yearStr.length > 4) != signAtBegining)
77827784
{
7783-
throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString));
7785+
throw new DateTimeException(format("Invalid format for Date.fromISOExtString: %s", isoExtString));
77847786
}
77857787

77867788
try
@@ -7791,7 +7793,7 @@ public:
77917793
}
77927794
catch (ConvException)
77937795
{
7794-
throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString));
7796+
throw new DateTimeException(format("Invalid format for Date.fromISOExtString: %s", isoExtString));
77957797
}
77967798

77977799
return Date(year, month, day);
@@ -7910,7 +7912,7 @@ public:
79107912
auto str = strip(simpleString);
79117913

79127914
if (str.length < 11 || str[$-3] != '-' || str[$-7] != '-')
7913-
throw new DateTimeException(format!"Invalid string format: %s"(simpleString));
7915+
throw new DateTimeException(format!"Invalid format for Date.fromSimpleString: %s"(simpleString));
79147916

79157917
int year;
79167918
uint day;
@@ -7919,7 +7921,7 @@ public:
79197921
auto signAtBegining = cast(bool) yearStr.startsWith('-', '+');
79207922
if ((yearStr.length > 4) != signAtBegining)
79217923
{
7922-
throw new DateTimeException(format!"Invalid string format: %s"(simpleString));
7924+
throw new DateTimeException(format!"Invalid format for Date.fromSimpleString: %s"(simpleString));
79237925
}
79247926

79257927
try
@@ -7929,7 +7931,7 @@ public:
79297931
}
79307932
catch (ConvException)
79317933
{
7932-
throw new DateTimeException(format!"Invalid string format: %s"(simpleString));
7934+
throw new DateTimeException(format!"Invalid format for Date.fromSimpleString: %s"(simpleString));
79337935
}
79347936

79357937
return Date(year, month, day);
@@ -9208,7 +9210,7 @@ public:
92089210
int hours, minutes, seconds;
92099211
auto str = strip(isoString);
92109212

9211-
enforce!DateTimeException(str.length == 6, text("Invalid ISO String: ", isoString));
9213+
enforce!DateTimeException(str.length == 6, text("Invalid format for TimeOfDay.fromISOString: ", isoString));
92129214

92139215
try
92149216
{
@@ -9220,7 +9222,7 @@ public:
92209222
}
92219223
catch (ConvException)
92229224
{
9223-
throw new DateTimeException(text("Invalid ISO String: ", isoString));
9225+
throw new DateTimeException(text("Invalid format for TimeOfDay.fromISOString: ", isoString));
92249226
}
92259227

92269228
return TimeOfDay(hours, minutes, seconds);
@@ -9333,7 +9335,7 @@ public:
93339335
int hours, minutes, seconds;
93349336

93359337
if (str.length != 8 || str[2] != ':' || str[5] != ':')
9336-
throw new DateTimeException(text("Invalid ISO Extended String: ", isoExtString));
9338+
throw new DateTimeException(text("Invalid format for TimeOfDay.fromISOExtString: ", isoExtString));
93379339

93389340
try
93399341
{
@@ -9345,7 +9347,7 @@ public:
93459347
}
93469348
catch (ConvException)
93479349
{
9348-
throw new DateTimeException(text("Invalid ISO Extended String: ", isoExtString));
9350+
throw new DateTimeException(text("Invalid format for TimeOfDay.fromISOExtString: ", isoExtString));
93499351
}
93509352

93519353
return TimeOfDay(hours, minutes, seconds);

std/datetime/systime.d

+11-7
Original file line numberDiff line numberDiff line change
@@ -8879,7 +8879,7 @@ public:
88798879
return retval;
88808880
}
88818881
catch (DateTimeException dte)
8882-
throw new DateTimeException(format("Invalid ISO String: %s", isoString));
8882+
throw new DateTimeException(format("Invalid format for SysTime.fromISOString: %s", isoString));
88838883
}
88848884

88858885
///
@@ -9109,7 +9109,8 @@ public:
91099109
auto str = strip(isoExtString);
91109110

91119111
auto tIndex = str.indexOf('T');
9112-
enforce(tIndex != -1, new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9112+
enforce!DateTimeException(tIndex != -1,
9113+
format("Invalid format for SysTime.fromISOExtString: %s", isoExtString));
91139114

91149115
auto found = str[tIndex + 1 .. $].find('.', 'Z', '+', '-');
91159116
auto dateTimeStr = str[0 .. $ - found[0].length];
@@ -9157,7 +9158,7 @@ public:
91579158
return retval;
91589159
}
91599160
catch (DateTimeException dte)
9160-
throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString));
9161+
throw new DateTimeException(format("Invalid format for SysTime.fromISOExtString: %s", isoExtString));
91619162
}
91629163

91639164
///
@@ -9359,7 +9360,8 @@ public:
93599360
auto str = strip(simpleString);
93609361

93619362
auto spaceIndex = str.indexOf(' ');
9362-
enforce(spaceIndex != -1, new DateTimeException(format("Invalid Simple String: %s", simpleString)));
9363+
enforce!DateTimeException(spaceIndex != -1,
9364+
format("Invalid format for SysTime.fromSimpleString: %s", simpleString));
93639365

93649366
auto found = str[spaceIndex + 1 .. $].find('.', 'Z', '+', '-');
93659367
auto dateTimeStr = str[0 .. $ - found[0].length];
@@ -9407,7 +9409,7 @@ public:
94079409
return retval;
94089410
}
94099411
catch (DateTimeException dte)
9410-
throw new DateTimeException(format("Invalid Simple String: %s", simpleString));
9412+
throw new DateTimeException(format("Invalid format for SysTime.fromSimpleString: %s", simpleString));
94119413
}
94129414

94139415
///
@@ -11170,17 +11172,19 @@ if (isSomeString!S)
1117011172
import std.algorithm.searching : all;
1117111173
import std.ascii : isDigit;
1117211174
import std.conv : to;
11175+
import std.format : format;
1117311176
import std.string : representation;
1117411177

1117511178
if (isoString.empty)
1117611179
return Duration.zero;
1117711180

1117811181
auto str = isoString.representation;
1117911182

11180-
enforce(str[0] == '.', new DateTimeException("Invalid ISO String"));
11183+
enforce!DateTimeException(str[0] == '.', format("Invalid format for fracSecsFromISOString: %s", isoString));
1118111184
str.popFront();
1118211185

11183-
enforce(!str.empty && all!isDigit(str), new DateTimeException("Invalid ISO String"));
11186+
enforce!DateTimeException(!str.empty && all!isDigit(str),
11187+
format("Invalid format for fracSecsFromISOString: %s", isoString));
1118411188

1118511189
dchar[7] fullISOString = void;
1118611190
foreach (i, ref dchar c; fullISOString)

0 commit comments

Comments
 (0)