| « WTF-U's Typing Test | My Tales » |
Generally when Jared has to compare two dates, he'll do something simple like "if (date1 < date2) ..." A contractor no longer under his company's employ had his own unique approach...
public short TheLesserDate(string DateFrom, string DateTo)
{
try
{
string delim = "/";
string[] cons = DateFrom.Split(delim.ToCharArray());
int intDateFrom = Convert.ToInt32(cons[2]) * 10000 +
Convert.ToInt32(cons[1]) * 100 +
Convert.ToInt32(cons[0]);
delim = "/";
cons = DateTo.Split(delim.ToCharArray());
int intDateTo = Convert.ToInt32(cons[2]) * 10000 +
Convert.ToInt32(cons[1]) * 100 +
Convert.ToInt32(cons[0]);
if (intDateFrom < intDateTo)
{
return 1;
}
if (intDateFrom > intDateTo)
{
return 2;
}
return 0;
}
catch(Exception)
{
return 3;
}
}
|
Correct me if I'm wrong, but isn't the code assuming dates will be in the format DAY/MONTH/YEAR ?
So need to a) Make sure locale settings on the deployment server at set to UK/Europe b) Make sure other developers who use this code pass in a string date in UK/EU format. If either condition is not met, then the code won't actually throw any exceptions and will appear to work fine. But it will return inconsistant results, and might not be spotted until much later after deployment. Brillant!!11 |
string delim = "/"; Just in case the delim got bent when all those bits were churning around. |
|
The real WTF is he's returning an int instead of the correct return enum value for comparison operators enum LessThanReturnCodes {Unknown, Lesser, GreaterOrEqual, FileNotFound} |
| « WTF-U's Typing Test | My Tales » |