Skip to content

Commit e4eedef

Browse files
committed
Add extra validation to DateToStringWithMonth
When adding unit tests to this function I found it lacked validation around edge cases. I refactored this function and now it passes all my unit tests. This function probably never gets fed invalidate data during an actual simulation but this makes the function more robust anyways.
1 parent b6e1d2d commit e4eedef

1 file changed

Lines changed: 48 additions & 37 deletions

File tree

src/EnergyPlus/OutputProcessor.cc

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,65 +3422,76 @@ namespace OutputProcessor {
34223422
// Convert the coded date format into a usable
34233423
// string
34243424

3425-
// Using/Aliasing
3426-
using General::DecodeMonDayHrMin;
3427-
3428-
// Return value
3429-
std::string StringOut;
3425+
if ( codedDate == 0 ) return "-";
34303426

3431-
// Locals
3432-
// ((month*100 + day)*100 + hour)*100 + minute
34333427
static gio::Fmt DateFmt( "(I2.2,'-',A3,'-',I2.2,':',I2.2)" );
34343428

3429+
// ((month*100 + day)*100 + hour)*100 + minute
34353430
int Month; // month in integer format (1-12)
34363431
int Day; // day in integer format (1-31)
34373432
int Hour; // hour in integer format (1-24)
34383433
int Minute; // minute in integer format (0:59)
3439-
std::string monthName;
34403434

3441-
if ( codedDate != 0 ) {
3442-
monthName = "";
3443-
DecodeMonDayHrMin( codedDate, Month, Day, Hour, Minute );
3444-
--Hour;
3445-
if ( Minute == 60 ) {
3446-
++Hour;
3447-
Minute = 0;
3448-
}
3449-
if ( Month == 1 ) {
3435+
General::DecodeMonDayHrMin( codedDate, Month, Day, Hour, Minute );
3436+
3437+
if ( Month < 1 || Month > 12 ) return "-";
3438+
if ( Day < 1 || Day > 31 ) return "-";
3439+
if ( Hour < 1 || Hour > 24 ) return "-";
3440+
if ( Minute < 0 || Minute > 60 ) return "-";
3441+
3442+
--Hour;
3443+
if ( Minute == 60 ) {
3444+
++Hour;
3445+
Minute = 0;
3446+
}
3447+
3448+
std::string monthName;
3449+
switch ( Month ) {
3450+
case 1:
34503451
monthName = "JAN";
3451-
} else if ( Month == 2 ) {
3452+
break;
3453+
case 2:
34523454
monthName = "FEB";
3453-
} else if ( Month == 3 ) {
3455+
break;
3456+
case 3:
34543457
monthName = "MAR";
3455-
} else if ( Month == 4 ) {
3458+
break;
3459+
case 4:
34563460
monthName = "APR";
3457-
} else if ( Month == 5 ) {
3461+
break;
3462+
case 5:
34583463
monthName = "MAY";
3459-
} else if ( Month == 6 ) {
3464+
break;
3465+
case 6:
34603466
monthName = "JUN";
3461-
} else if ( Month == 7 ) {
3467+
break;
3468+
case 7:
34623469
monthName = "JUL";
3463-
} else if ( Month == 8 ) {
3470+
break;
3471+
case 8:
34643472
monthName = "AUG";
3465-
} else if ( Month == 9 ) {
3473+
break;
3474+
case 9:
34663475
monthName = "SEP";
3467-
} else if ( Month == 10 ) {
3476+
break;
3477+
case 10:
34683478
monthName = "OCT";
3469-
} else if ( Month == 11 ) {
3479+
break;
3480+
case 11:
34703481
monthName = "NOV";
3471-
} else if ( Month == 12 ) {
3482+
break;
3483+
case 12:
34723484
monthName = "DEC";
3473-
} else {
3474-
monthName = "***";
3475-
}
3476-
gio::write( StringOut, DateFmt ) << Day << monthName << Hour << Minute;
3477-
if ( has( StringOut, "*" ) ) {
3478-
StringOut = "-";
3479-
}
3480-
} else { // codeddate = 0
3481-
StringOut = "-";
3485+
break;
3486+
default:
3487+
assert( false );
3488+
monthName = "Should't get here!!";
3489+
break;
34823490
}
34833491

3492+
std::string StringOut;
3493+
gio::write( StringOut, DateFmt ) << Day << monthName << Hour << Minute;
3494+
34843495
return StringOut;
34853496
}
34863497

0 commit comments

Comments
 (0)