But the 20
from 23
to 2023
is implicit.
That makes only sense to be common practice if that is commonly known . . .
Probably you responded to my initial posting. Please look at my second posting.
There, I showed an example interface where the user has to provide the information to disambiguate. For example, you specify 2000 as the center and then “23” will become unambiguously 2023. I didn’t ask for an interface that tries to guess what the central year is.
and even then, you run into things like the Y2K-Bug.
Sorry I failed to understand what problem you see for our case (converting a two-digit year to a real year). Perhaps you allude to the year-0 problem? (What’s the year before 1 AD? Is it the year 0 or is it 1 BC?). You would have to change the algorithm if you use a calendar where 1 BC precedes 1 AD.
But if you are veryvery sure, you could ass a "20"
upfront every of your strings that starts with "23"
To use such a dataset as I have, you need to be sure where the central year is in any case. You need to know whether “49” means 1949 or 2049. Once you know the central year, then you write a program like this:
function convert_2digit_year(year2d, center)
@assert(0 <= year2d <= 99)
# For example, center = 2000
year_offset = (year2d < 50) ? center : (center - 100)
return year2d + year_offset
end
If the central year is set to be 2000, “23” will be unambiguously 2023, “49” will be unambigously 2049, and “51” unambiguously 1951 (because 1951 is closer to 2000 than 2051 is to 2000). In this example, I have decided that “50” means “1950”. If you know that “50” means “2050”, then you change year2d < 50
to year2d <= 50
.
This problem is so common when you handle date-related, real-world data that I thought this little trick may be already implemented somewhere in the Dates
package.
Since there seems to be no such a thing, I would put my little utility function in my “kitchen sink” module.