I want to create a DateTime instance that lies 20 minutes and 10 seconds in the future.
I tried around with Time and DateTime in irb, but can’t seem to figure out a way that really makes sense. I can only add days to DateTime objects and only add seconds to the Time objects.
Isn’t there a better way than to always convert the time I want to add into seconds?
A
Timeis a number of seconds since an epoch whereas aDateTimeis a number of days since an epoch which is why adding1to aDateTimeadds a whole day. You can however add fractions of a day, for exampleWill add 10 seconds to
d(since there are 86400 seconds in a day).If you are using Rails, Active Support adds some helper methods and you can do
Which will do the right thing is
dis aDateTimeor aTime. You can use Active Support on its own, and these days you can pull in just the bits you need. I seem to recall that this stuff is inactivesupport/duration. I believe there are a few other gems that offer help with time handling too.