The Daily WTF
Follow
CodeSOD: Negative Days
The .NET source code for the TimeSpan struct contains a comment explaining its functionality. A TimeSpan represents a duration that can be either positive or negative. Internally, it's stored as a number of milliseconds. This representation works well for units like hours and days but becomes imprecise for longer periods. For example, months can vary in length from 28 to 31 days. Similarly, a year can have 365 or 366 days, and a decade can contain one to three leap years. These inconsistencies are why the TimeSpan struct does not offer methods for years or months. The "364 days" mentioned for a year is a minor error, a "fat-finger typo," as the author calls it, that doesn't affect the code's behavior. This comment has remained in the code for twelve years without alteration. The TimeSpan class itself, being essentially a millisecond wrapper, has also seen no changes. More complex date calculations like adding months or years are handled by other date-time objects. While not a major issue, the comment and the class's history are noteworthy. The TimeSpan class also reveals deprecated constructors and compile-time support for older .NET versions, including Silverlight. Silverlight, a defunct technology, highlights the class's long-standing presence.