Skip to content

Instantly share code, notes, and snippets.

@matthusby
Created June 25, 2012 23:49
Show Gist options
  • Save matthusby/2992158 to your computer and use it in GitHub Desktop.
Save matthusby/2992158 to your computer and use it in GitHub Desktop.
-module(general_lib).
-compile(export_all).
-define(SECRET, "SOMETEXTHERE").
time_as_string() ->
{MegaSeconds, Seconds, MicroSeconds} = erlang:now(),
integer_to_list(MegaSeconds) ++ integer_to_list(Seconds) ++ integer_to_list(MicroSeconds).
time_as_seconds() ->
time_as_seconds(erlang:now()).
time_as_seconds(Time) ->
calendar:datetime_to_gregorian_seconds(calendar:now_to_local_time(Time)).
local_time_as_seconds(Time) ->
calendar:datetime_to_gregorian_seconds(Time).
to_upper(S) -> lists:map(fun char_to_upper/1, S).
to_lower(S) -> lists:map(fun char_to_lower/1, S).
char_to_upper(C) when C >= $a, C =< $z -> C bxor $\s;
char_to_upper(C) -> C.
char_to_lower(C) when C >= $A, C =< $Z -> C bxor $\s;
char_to_lower(C) -> C.
% Generates a random binary UUID.
uuid() ->
to_string(generate_uuid()).
generate_uuid() ->
v4(crypto:rand_uniform(1, round(math:pow(2, 48))) - 1, crypto:rand_uniform(1, round(math:pow(2, 12))) - 1, crypto:rand_uniform(1, round(math:pow(2, 32))) - 1, crypto:rand_uniform(1, round(math:pow(2, 30))) - 1).
v4(R1, R2, R3, R4) ->
<<R1:48, 4:4, R2:12, 2:2, R3:32, R4: 30>>.
% Generate a random string of given length
generate_random_string(Length) ->
generate_random_string(Length, "abcdefghijkmnpqrstuvwxyz23456789").
generate_random_string(Length, AllowedChars) ->
lists:foldl(fun(_, Acc) ->
[lists:nth(random:uniform(length(AllowedChars)), AllowedChars)] ++ Acc end, [], lists:seq(1, Length)).
% Returns a string representation of a binary UUID.
to_string(U) ->
lists:flatten(io_lib:format("~8.16.0b-~4.16.0b-~4.16.0b-~2.16.0b~2.16.0b-~12.16.0b", get_parts(U))).
% Returns the 32, 16, 16, 8, 8, 48 parts of a binary UUID.
get_parts(<<TL:32, TM:16, THV:16, CSR:8, CSL:8, N:48>>) ->
[TL, TM, THV, CSR, CSL, N].
% Converts a UUID string in the format of 550e8400-e29b-41d4-a716-446655440000
% (with or without the dashes) to binary.
to_binary(U)->
convert(lists:filter(fun(Elem) -> Elem /= $- end, U), []).
% Converts a list of pairs of hex characters (00-ff) to bytes.
convert([], Acc)->
list_to_binary(lists:reverse(Acc));
convert([X, Y | Tail], Acc)->
{ok, [Byte], _} = io_lib:fread("~16u", [X, Y]),
convert(Tail, [Byte | Acc]).
% Use this with models to always get the string version
to_list(Binary) when is_binary(Binary) ->
binary_to_list(Binary);
to_list(Val) ->
Val.
to_float(Float) when erlang:is_float(Float) ->
Float;
to_float(Float) ->
% well there is no is_string so I guess this one just goes last
string:to_float(Float).
floor(X) ->
T = erlang:trunc(X),
case (X - T) of
Neg when Neg < 0 -> T - 1;
Pos when Pos > 0 -> T;
_ -> T
end.
ceiling(X) ->
T = erlang:trunc(X),
case (X - T) of
Neg when Neg < 0 -> T;
Pos when Pos > 0 -> T + 1;
_ -> T
end.
% Be careful with this, it is O(N)
index_of(Item, List) -> index_of(Item, List, 1).
index_of(_, [], _) -> not_found;
index_of(Item, [Item|_], Index) -> Index;
index_of(Item, [_|Tl], Index) -> index_of(Item, Tl, Index+1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment