2 digit year parsing as we approach 2031

34 views
Skip to first unread message

Scott Stephens

unread,
Nov 3, 2022, 11:29:14 AM11/3/22
to Noda Time
I dug into the template argument of LocalDatePattern.Create for the first time today and stumbled across two behaviors I found fairly surprising: 
  1. The default template is fixed at 1 Jan 2000, regardless of the current date.
  2. 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

    

Jon Skeet

unread,
Nov 3, 2022, 12:16:30 PM11/3/22
to noda...@googlegroups.com
Please could you file this as an issue on https://github.com/nodatime/nodatime? That's probably a simpler space to explore options.

I will say that I want to stick to "the current date doesn't change things" - I really hate the idea of code changing its behavior based on the current date. (We do that in one place at the moment - only if we don't have a system time zone ID.)

--

---
You received this message because you are subscribed to the Google Groups "Noda Time" group.
To unsubscribe from this group and stop receiving emails from it, send an email to noda-time+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/noda-time/0da69333-0a1d-4ef3-bb87-7095c9e7bddbn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages