Add and subtract time from a DateTime structure in Erlang
I was trying to add times in Erlang, but couldn't find an existing function, so I wrote my own.
Some languages, like Ruby, give you 12 ways to do the same thing. Other languages, like Erlang, make it tough to find 1 way to do something.
Awhile back, I was trying to add a period of time to an existing DateTime
value (in {{Y,M,D},{H,m,s}}
format), but I couldn't find a function (such as in the Calendar
module) that allowed me to manipulate a DateTime
value directly.
If you're looking too, the Calendar
module can be used to convert a DateTime
to seconds, which makes it easier to add seconds, minutes, hours, etc.
To make life easier, I created a function to add additional time to (or subtract time from) an existing DateTime
:
-type datetime() :: {{non_neg_integer(), pos_integer(), pos_integer()},
{non_neg_integer(), non_neg_integer(), non_neg_integer()}}.
-type timespan() :: {integer(), integer(), integer()}.
-spec add_time_to_datetime(datetime(), timespan()) -> datetime().
add_time_to_datetime(Date, {Hour, Min, Sec}) ->
DateInSeconds = calendar:datetime_to_gregorian_seconds(Date),
NewDateInSeconds = DateInSeconds + (Hour * 60 * 60) + (Min * 60) + Sec,
calendar:gregorian_seconds_to_datetime(NewDateInSeconds).
This is trivial to write, but then it's trivial for them to just include too. Add it to the things I miss in a modern framework like .NET.
Spread the Word