I dug into the template argument of LocalDatePattern.Create for the first time today and stumbled across two behaviors I found fairly surprising:
- The default template is fixed at 1 Jan 2000, regardless of the current date.
- Century rollover happens at year of century 31.
The consequence of this is that if, on 1 Jan 2031, we run
LocalDatePattern.CreateWithInvariantCulture("MM/dd/yy").Parse("01/01/31").Value
we will get
Thursday, January 1, 1931
which is fairly unintuitive to me.
I'm wondering if anyone else finds this suprising, and if so, if there's something we can do, either code or documentation, to nudge users into not firing this footgun.
When I'm writing APIs dealing with two digit years I usually require the user to pass in a "reference year", and then I choose the century to be that which when combined with the year of century is as close as possible to the reference year. That forces the user to explicitly think about how to determine the appropriate century, and is easy to implement in the two most common cases I see, where a two digit year is assumed to be close to the current time, or close to the time at which the data with the two digit year in it was created.
It's not a great fit with the existing parsing methods though, since the same methods are used whether you're dealing with 2 digit or 4 digit years.
Thoughts?
-- Scott