% coding: utf-8
% Copyright 2008 Grégory Burri
%
% This file is part of Euphorik.
%
% Euphorik is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Euphorik is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Euphorik. If not, see .
%
% Module avec plein de bordel utile à l'intérieur.
% @author G.Burri
-module(euphorik_common).
-export([
ceiling/1,
delta_date_ms/2,
delta_date_minute/2,
serialize_ip/1,
unserialize_ip/1
]).
% Arrondi un float à l'entier supérieur.
% Bizarre, cette fonction n'existe pas dans la stdlib.
% Pas utilisé mais bon ca me fait de la peine de l'enlever.
ceiling(X) ->
T = trunc(X),
case (X - T) of
Neg when Neg < 0 -> T;
Pos when Pos > 0 -> T + 1;
_ -> T
end.
% Retourne la difference entre deux timestamp (erlang:now()) en miliseconde
delta_date_ms(D1, D2) ->
1000000000 * abs(element(1, D1) - element(1, D2)) + 1000 * abs(element(2, D1) - element(2, D2)) + trunc(abs(element(3, D1) - element(3, D2)) / 1000).
% Retourne la différence entre deux timestamp (erlang:now) en minutes
delta_date_minute(D1, D2) ->
trunc(delta_date_ms(D1, D2) / 1000 / 60).
serialize_ip(undefined) ->
"";
serialize_ip(IP) ->
lists:flatten(io_lib:format("~w.~w.~w.~w", tuple_to_list(IP))).
unserialize_ip(IP) ->
case io_lib:fread("~d.~d.~d.~d", IP) of
{ok, [A, B, C, D], []} -> {A, B, C, D};
_ -> erreur
end.