Sunday, October 14, 2012

Dealing with time intervals

Sometimes in a configuration file you want to express something should happen periodically or after a delay. The problem is that Erlang like most languages wants to see an integer number of milliseconds, while I want to write something that will make sense. If I see 5400000  in a config file I have no idea what that means. On the other hand if I see {90, minute} then it is very clear that  this is a time value.

Here is a brief function that will let you specify time as seconds, minutes, hours or days.


-module(time_interval).
-export([get_interval/1]).
-define(SECOND, 1000).
-define(MINUTE, 60 * ?SECOND).
-define(HOUR, 60 * ?MINUTE).
-define(DAY, 24 * ?HOUR).
get_interval({Time, second}) ->
Time * ?SECOND;
get_interval({Time, minute}) ->
Time * ?MINUTE;
get_interval({Time, hour}) ->
Time * ?HOUR;
get_interval({Time, day}) ->
Time * ?DAY.

No comments:

Post a Comment