A couple days ago my manager told me about the client complaining that our software stops working since 13th. Magic date isn't it?
Introduction: at my work i have responsibility to support one legacy application. It was written a long time ago and has all accompanying features: copy-paste, spaghetti code ... From time to time i need to fix some bugs in it.
This time it looks like the application don't show the list of main business objects at the screen. I checked all settings and permissions at the server, searched through all logs, there was no sign of any error. The application still don't show these objects.
OK, i thought, let's look at the code. I found the function that shows the objects, it was something like this:
The client worked at Windows OS with English culture where the date format is MM-dd-YYYY, so when the date becomes later than 12th it can't be parsed back because the month value can't be greater than 12.
That's the story.
This is the fixed code:
Introduction: at my work i have responsibility to support one legacy application. It was written a long time ago and has all accompanying features: copy-paste, spaghetti code ... From time to time i need to fix some bugs in it.
This time it looks like the application don't show the list of main business objects at the screen. I checked all settings and permissions at the server, searched through all logs, there was no sign of any error. The application still don't show these objects.
OK, i thought, let's look at the code. I found the function that shows the objects, it was something like this:
if (someCondition)
{
//show the objects
}
This condition checks the date of last connection to server that was stored in local db as a string. This is the code:1: var dateString = this[LocalConstantSettingsType.LastUpdateTime];
2: if (this[LocalConstantSettingsType.LastUpdateTime] != null && !string.IsNullOrEmpty(this[LocalConstantSettingsType.LastUpdateTime]))
3: result = DateTime.Parse(this[LocalConstantSettingsType.LastUpdateTime]);
4: else
5: result = null;
Do you see this ugly code at the line 3? The problem that the value is stored in this way: string DateTimeFormat = "dd-MM-yyyy HH:mm";
this[LocalConstantSettingsType.LastUpdateTime] = value.Value.ToString(DateTimeFormat);
We have fixed pattern for storing DateTime value and Culture specific code for parsing it back.The client worked at Windows OS with English culture where the date format is MM-dd-YYYY, so when the date becomes later than 12th it can't be parsed back because the month value can't be greater than 12.
That's the story.
This is the fixed code:
DateTime result;
var dateString = this[LocalConstantSettingsType.LastUpdateTime];
if (string.IsNullOrEmpty(dateString) || !DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return null;
}
return result;
.....
this[LocalConstantSettingsType.LastUpdateTime] = value.Value.ToString(CultureInfo.InvariantCulture);
Комментариев нет:
Отправить комментарий