I am using Timex library to do date stuff in my project. One of my tests requires to calculate a date exactly 18 years a go from today. What if today is 29 February 2016?
2016 is a leap year but 1998 is not. So there is no 29 February 2016.
Subtract 18 from the year. Test if the year is leap?. If it’s not and today.day == 29 and today.month == 02 then create a new date with day = 01 and month = 03. So from 29-February-2016 we derive 01-March-1998.
But still I am curious how this can be achieved using Elixir’s libraries instead of my simplistic idea. Be it Timex or built in.
you could argure that since leap day is the last day of February, it should ‘jump back’ to 28 February if you end up in a non-leap year. For many applications (such as e. g. monthly recurring timers) this makes more sense.
But other than that, your simple approach is great.
… until you look too far back, and come across calendar ambiguities such as for instance the greatly varying moments at which the different European countries decided to switch from the Julian to the Gregorian calendar.