Skip to content

Commit 8ef9c2f

Browse files
committed
Make throwing on invalid input optional in stda.datetime.date.Date.dayOfYear
* Keep throwing on invalid input in the public API * Use assert for Date.this(), where exceptions are not appropriate
1 parent 3f8b5b3 commit 8ef9c2f

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

std/datetime/date.d

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,7 @@ public:
25472547
day = The day of the year to set which day of the year this
25482548
$(LREF DateTime) is on.
25492549
+/
2550-
@property void dayOfYear(int day) @safe pure nothrow @nogc
2550+
@property void dayOfYear(int day) @safe pure
25512551
{
25522552
_date.dayOfYear = day;
25532553
}
@@ -3738,20 +3738,14 @@ public:
37383738
{
37393739
_year = cast(short) years;
37403740

3741-
try
3742-
dayOfYear = day;
3743-
catch (Exception e)
3744-
assert(0, "dayOfYear assignment threw.");
3741+
setDayOfYear(day);
37453742
}
37463743
}
37473744
else if (day <= 0 && -day < daysInLeapYear)
37483745
{
37493746
_year = 0;
37503747

3751-
try
3752-
dayOfYear = (daysInLeapYear + day);
3753-
catch (Exception e)
3754-
assert(0, "dayOfYear assignment threw.");
3748+
setDayOfYear(daysInLeapYear + day);
37553749
}
37563750
else
37573751
{
@@ -3803,10 +3797,7 @@ public:
38033797
_year = cast(short) years;
38043798
immutable newDoY = (yearIsLeapYear(_year) ? daysInLeapYear : daysInYear) + day + 1;
38053799

3806-
try
3807-
dayOfYear = newDoY;
3808-
catch (Exception e)
3809-
assert(0, "dayOfYear assignment threw.");
3800+
setDayOfYear(newDoY);
38103801
}
38113802
}
38123803
}
@@ -6598,12 +6589,26 @@ public:
65986589
$(REF DateTimeException,std,datetime,date) if the given day is an
65996590
invalid day of the year.
66006591
+/
6601-
@property void dayOfYear(int day) @safe pure nothrow @nogc
6592+
@property void dayOfYear(int day) @safe pure
6593+
{
6594+
setDayOfYear!true(day);
6595+
}
6596+
6597+
private void setDayOfYear(bool useExceptions = false)(int day)
66026598
{
66036599
immutable int[] lastDay = isLeapYear ? lastDayLeap : lastDayNonLeap;
66046600

6605-
if (day <= 0 || day > (isLeapYear ? daysInLeapYear : daysInYear))
6606-
assert(0, "Invalid day of the year.");
6601+
bool dayOutOfRange = day <= 0 || day > (isLeapYear ? daysInLeapYear : daysInYear);
6602+
enum errorMsg = "Invalid day of the year.";
6603+
6604+
static if (useExceptions)
6605+
{
6606+
if (dayOutOfRange) throw new DateTimeException(errorMsg);
6607+
}
6608+
else
6609+
{
6610+
assert(!dayOutOfRange, errorMsg);
6611+
}
66076612

66086613
foreach (i; 1 .. lastDay.length)
66096614
{

0 commit comments

Comments
 (0)