Date Tomfoolery - ParkinT/RubyMotion_Life GitHub Wiki
Thanks to ColinTA on the Google Groups for this.
(main)> feb_28_2012 = 1330473600 # what, you don't have this memorized?
=> 1330473600
(main)> Time.at(feb_28_2012)
=> 2012-02-28 17:00:00 -0700
(main)> Time.at(feb_28_2012).delta(days:1)
=> 2012-02-29 17:00:00 -0700
(main)> Time.at(feb_28_2012).delta(days:2)
=> 2012-03-01 17:00:00 -0700
(main)> Time.at(feb_28_2012).delta(months:1)
=> 2012-03-28 17:00:00 -0600 # look, the time didn't change, event though there was a DST change!
(main)> Time.at(feb_28_2012).delta(years:1)
=> 2013-02-28 17:00:00 -0700
(main)> Time.at(feb_28_2012).delta(days:1, years:1)
=> 2013-02-28 17:00:00 -0700
(main)> Time.at(feb_28_2012).delta(days:1, years:1, months:1)
=> 2013-03-29 17:00:00 -0600
(main)> Time.at(feb_28_2012).delta(days:1, months:11)
=> 2013-01-29 17:00:00 -0700
# what is 1/29/2013 plus two months? easy! march 29, 2013
(main)> Time.at(feb_28_2012).delta(days:1, months:13)
=> 2013-03-29 17:00:00 -0600
# yeah, smart guy? well then what is 1/29/2013 plus ONE month.
# it's feb 28th. trust me. when someone says "see you in a month!"
# they mean "next month", not "in the early part of two months in the future",
# which is where the math will take you if you don't add a "day of month" correction.
(main)> Time.at(feb_28_2012).delta(days:1, months:12)
=> 2013-02-28 17:00:00 -0700
# unfortunately you will end up with stuff like this:
(main)> Time.at(feb_28_2012) == Time.at(feb_28_2012).delta(days:1, months:12).delta(months:-12)
=> true