1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
defmodule Util do
def to_naive_date_time(naive = %NaiveDateTime{}), do: naive
def to_naive_date_time(datetime = %DateTime{}), do: DateTime.to_naive(datetime)
def to_naive_date_time(timestamp) when is_integer(timestamp) do
timestamp
|> to_date_time()
|> to_naive_date_time()
end
def to_date_time(naive_or_timestamp, timezone \\ "Europe/Paris")
def to_date_time(date = %DateTime{}, timezone) do
DateTime.shift_zone!(data, timezone, Tzdata.TimeZoneDatabase)
end
def to_date_time(naive = %NaiveDateTime{}, timezone) do
DateTime.from_naive!(naive, timezone, Tzdata.TimeZoneDatabase)
end
# todo: this is wrong.
def to_date_time(timestamp, timezone) when is_integer(timestamp) do
timestamp
|> DateTime.from_unix!(:millisecond)
|> DateTime.shift_zone!(timezone, Tzdata.TimeZoneDatabase)
end
def plusminus(number) when number > 0, do: "+#{number}"
def plusminus(0), do: "0"
def plusminus(number) when number < 0, do: "#{number}"
def float_paparse(float) when is_float(float), do: {float, ""}
def float_paparse(int) when is_integer(int), do: {(int+0.0), ""}
def float_paparse(string) when is_binary(string) do
string
|> String.replace(",", ".")
|> Float.parse()
end
def ets_mutate_select_each(ets, table, spec \\ [{:"$1", [], [:"$1"]}], fun) do
ets.safe_fixtable(table, true)
first = ets.select(table, spec, 1)
do_ets_mutate_select_each(ets, table, fun, first)
after
ets.safe_fixtable(table, false)
end
defp do_ets_mutate_select_each(_, _, _, :'$end_of_table') do
:ok
end
defp do_ets_mutate_select_each(ets, table, fun, {objs, continuation}) do
for obj <- objs, do: fun.(table, obj)
do_ets_mutate_select_each(ets, table, fun, ets.select(continuation))
end
def ets_mutate_each(ets, table, fun) do
ets.safe_fixtable(table, true)
first = ets.first(table)
do_ets_mutate_each(ets, table, fun, first)
after
ets.safe_fixtable(table, false)
end
defp do_ets_mutate_each(ets, table, fun, key) do
case ets.lookup(table, key) do
[elem] -> fun.(table, elem)
_ -> nil
end
do_ets_mutate_each(ets, table, fun, ets.next(table, key))
end
end
|