This type of logic probably has to be implemented yourself. How can Timex tell when it should be 1930 vs. 2030? You on the other hand know your data, so it should be simple enough to wrap the parse function and massage the year to the correct, fully qualified value before you pass it to Timex.
I am not sure how Timex would know if it is supposed to be in 20XX or 19XX.
It would probably be best to update your data (if you own it) to have the full year OR do some hacky thing like:
You can tweak 30 here to whatever your cutoff year is.
@spec parse_date(binary()) :: Date.t()
def parse_date(date) do
[month, day, year] = String.split(date, "/")
year =
case String.to_integer(year) do
value when value >= 100 -> value
value when value >= 30 -> value + 1900
value -> value + 2000
end
Date.new!(year, String.to_integer(month), String.to_integer(day))
end
I assume you actually want a Date and not a NaiveDateTime returned?
Some versions of the %y format handle guessing the century, for instance:
The year within century (0β99). When a century is not otherwise specified, values in the range 69β99 refer to years in the twentieth century (1969β1999); values in the range 00β68 refer to years in the twenty-first century (2000β2068).