I am reading Phobos docs. Sometimes I can't understand the logic of some methods.
Adds the given number of years or months to this Date. A negative number will subtract. The difference between rolling and adding is that rolling does not affect larger units.
Maybe is Phobos is not good considered, maybe I do not understand where it's can be helpful.
If I am adding to 2013-07-01 for example 200 days I am expecting to get 2014 year, but not 2013.
Could anybody explain the logic?
roll
is also present in Java.... the only time I remember ever using it in either language though was implementing a little date picker widget. There's separate fields for each thing: month, day, year, and you want the user to be able to spin through days without it incrementing month, since they are separate fields. (Suppose it is set to the 30th and they want to select the 1st. The quickest way might be to just hit the up arrow and let it roll over.)
There might be other uses, I just don't remember them right now. Even when I was implementing ical support a while ago (sadly, proprietary), I never used the roll method, but there might be potential for it there. ical is a standard for recurring events on calendars and there's some tricky things in there, and a few different ways you might implement it.
In the comment, you also ask why you can't add
days. The reason is that adding days is pretty trivial and covered with the opBinary
:
datetime += 5.days; // works
That does NOT work for months though, because months have a variable duration. That's where the add
method comes in to help: suppose it is January 30 and you add one month. You might just add 31 days, since January has 31 days in it. That would land you on March 2 (I think, doing this in my head)... but Jan 30 + 1 month might also need to be Feb 28. Or maybe Feb 29. Suppose you are paying a monthly bill due at the end of the month, the number of days between those due dates will change.
The add
method handles this and gives you the option to fall on the last day or to spill over into the next month.
Though, I'd kinda argue days don't have a static length either.... consider the DST transition. (Or leap seconds, but handling them would be nuts). Maybe we can ask JMD.