Do you pre-format your time strings before using TimeSpan.Parse()?

Updated by Brook Jeynes [SSW] 2 years ago. See history

123
<introEmbed body={<> 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: Figure: Bad example - A value greater than 24hours will crash eg. 45:30

ts = TimeSpan.Parse(FormatTimeSpanString(cboMyComboBox.Text))

✅ Figure: Figure: Good example - Using a wrapper method to pre-parse the string containing the TimeSpan value.

We have a program called SSW Code Auditor to check for this rule.

Acknowledgements

Adam Cogan
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star