Do you pre-format your time strings before using TimeSpan.Parse()?
26/04/2018 8:03 AM by
TimeSpan.Parse() constructs a Timespan from a time indicated by a specified string. The acceptable parameters for this function are in the format "d.hh:mm" where "d" is the number of days (it is optional), "hh" is hours and is between 0 and 23 and "mm" is minutes and is between 0 and 59. If you try to pass, as a parameter, as a string such as "45:30" (meaning 45 hours and 30 minutes), TimeSpan.Parse() function will crash. (The exact exception received is: "System.OverflowException: TimeSpan overflowed because duration is too long".) Therefore it is recommended that you should always pre-parse the time string before passing it to the "TimeSpan.Parse()" function. This pre-parsing is done by the FormatTimeSpanString( ) function. This function will format the input string correctly. Therefore, a time string of value "45:30" will be converted to "1.21:30" (meaning 1 day, 21 hours and 30 minutes). This format is perfectly acceptable for TimeSpan.Parse() function and it will not crash.
ts = TimeSpan.Parse(cboMyComboBox.Text)Figure: Bad code because a value greater than 24hours will crash eg. 45:30
ts = TimeSpan.Parse(FormatTimeSpanString(cboMyComboBox.Text))
Figure: Good code because we are using a wrapper method to pre-parse the string containing the
TimeSpan value. (Look it up in CodeBase)
We have a program called SSW Code Auditor to check for this rule.
Do you feel this rule needs an update?