Skip to content

Instantly share code, notes, and snippets.

@asaaki
Forked from zaphar/date_util.erl
Last active December 28, 2015 13:19
Show Gist options
  • Save asaaki/7506975 to your computer and use it in GitHub Desktop.
Save asaaki/7506975 to your computer and use it in GitHub Desktop.
-module(date_util).
-compile(export_all).
epoch() ->
now_to_seconds(now())
.
epoch_hires() ->
now_to_seconds_hires(now())
.
now_to_seconds({Mega, Sec, _}) ->
(Mega * 1000000) + Sec
.
now_to_milliseconds({Mega, Sec, Micro}) ->
now_to_seconds({Mega, Sec, Micro}) * 1000
.
now_to_seconds_hires({Mega, Sec, Micro}) ->
now_to_seconds({Mega, Sec, Micro}) + (Micro / 1000000)
.
now_to_milliseconds_hires({Mega, Sec, Micro}) ->
now_to_seconds_hires({Mega, Sec, Micro}) * 1000
.
epoch_gregorian_seconds() ->
calendar:datetime_to_gregorian_seconds({{1970,1,1}, {0,0,0}})
.
now_to_gregorian_seconds() ->
epoch_to_gregorian_seconds(now())
.
epoch_to_gregorian_seconds({Mega, Sec, Micro}) ->
epoch_to_gregorian_seconds(now_to_seconds({Mega, Sec, Micro}));
epoch_to_gregorian_seconds(Now) ->
EpochSecs = epoch_gregorian_seconds()
, Now + EpochSecs
.
gregorian_seconds_to_epoch(Secs) ->
EpochSecs = epoch_gregorian_seconds()
, Secs - EpochSecs
.
date_to_epoch(Date) ->
datetime_to_epoch({Date, {0,0,0} })
.
datetime_to_epoch({Date, Time}) ->
gregorian_seconds_to_epoch(
calendar:datetime_to_gregorian_seconds({Date, Time}))
.
is_older_by(T1, T2, {days, N}) ->
N1 = day_difference(T1, T2)
, case N1 of
N2 when (-N < N2) ->
true;
_ ->
false
end
.
is_sooner_by(T1, T2, {days, N}) ->
case day_difference(T1, T2) of
N1 when N > N1 ->
true;
_ ->
false
end
.
is_time_older_than({Date, Time}, Mark) ->
is_time_older_than(calendar:datetime_to_gregorian_seconds({Date, Time})
, Mark);
is_time_older_than(Time, {DateMark, TimeMark}) ->
is_time_older_than(Time
, calendar:datetime_to_gregorian_seconds({DateMark, TimeMark}));
is_time_older_than(Time, Mark) when is_integer(Time), is_integer(Mark) ->
Time < Mark
.
day_difference({D1, _}, D2) ->
day_difference(D1, D2);
day_difference(D1, {D2, _}) ->
day_difference(D1, D2);
day_difference(D1, D2) ->
Days1 = calendar:date_to_gregorian_days(D1)
, Days2 = calendar:date_to_gregorian_days(D2)
, Days1 - Days2
.
is_time_sooner_than({Date, Time}, Mark) ->
is_time_sooner_than(calendar:datetime_to_gregorian_seconds({Date, Time})
, Mark);
is_time_sooner_than(Time, {DateMark, TimeMark}) ->
is_time_sooner_than(Time
, calendar:datetime_to_gregorian_seconds({DateMark, TimeMark}));
is_time_sooner_than(Time, Mark) when is_integer(Time), is_integer(Mark) ->
Time > Mark
.
subtract(Date, {days, N}) ->
New = calendar:date_to_gregorian_days(Date) - N
, calendar:gregorian_days_to_date(New)
.
add(Date, {days, N}) ->
New = calendar:date_to_gregorian_days(Date) + N
, calendar:gregorian_days_to_date(New)
.
defmodule DateUtil do
def now, do: now_to_seconds
def epoch, do: now_to_seconds
def timestamp, do: now_to_seconds
def now_hires, do: now_to_seconds_hires
def epoch_hires, do: now_to_seconds_hires
def timestamp_hires, do: now_to_seconds_hires
def today, do: today_utc
def today_utc, do: datetime_to_date(:calendar.universal_time)
def today_local, do: datetime_to_date(:calendar.local_time)
def datetime_to_date({date, time}), do: :calendar.gregorian_days_to_date(:calendar.date_to_gregorian_days(date))
def date_to_datetime(date), do: {date, {0,0,0}}
def now_to_seconds, do: now_to_seconds :erlang.now
def now_to_seconds({mega, sec, _}), do: mega * 1000000 + sec
def now_to_milliseconds({mega, sec, micro}), do: now_to_seconds({mega, sec, micro}) * 1000
def now_to_seconds_hires , do: now_to_seconds_hires :erlang.now
def now_to_seconds_hires({mega, sec, micro}), do: now_to_seconds({mega, sec, micro}) + (micro / 1000000)
def now_to_milliseconds_hires({mega, sec, micro}), do: now_to_seconds_hires({mega, sec, micro}) * 1000
def epoch_gregorian_seconds, do: :calendar.datetime_to_gregorian_seconds({{1970,1,1}, {0,0,0}})
def now_to_gregorian_seconds, do: epoch_to_gregorian_seconds :erlang.now
def epoch_to_gregorian_seconds({mega, sec, micro}), do: epoch_to_gregorian_seconds(now_to_seconds({mega, sec, micro}))
def epoch_to_gregorian_seconds(now), do: now + epoch_gregorian_seconds
def gregorian_seconds_to_epoch(secs), do: secs - epoch_gregorian_seconds
def date_to_epoch(date), do: datetime_to_epoch {date, {0,0,0} }
def datetime_to_epoch({date, time}), do: gregorian_seconds_to_epoch(:calendar.datetime_to_gregorian_seconds({date, time}))
def is_older_by(t1, t2, [days: n]), do: (n2 = day_difference(t1, t2); -n < n2)
def is_sooner_by(t1, t2, [days: n]), do: (n1 = day_difference(t1, t2); n > n1)
def day_difference({d1, _}, d2), do: day_difference(d1, d2)
def day_difference(d1, {d2, _}), do: day_difference(d1, d2)
def day_difference(d1, d2), do: :calendar.date_to_gregorian_days(d1) - :calendar.date_to_gregorian_days(d2)
def is_time_older_than({date, time}, mark), do: is_time_older_than(:calendar.datetime_to_gregorian_seconds({date, time}), mark)
def is_time_older_than(time, {dateMark, timeMark}), do: is_time_older_than(time, :calendar.datetime_to_gregorian_seconds({dateMark, timeMark}))
def is_time_older_than(time, mark) when is_integer(time) and is_integer(mark), do: time < mark
def is_time_sooner_than({date, time}, mark), do: is_time_sooner_than(:calendar.datetime_to_gregorian_seconds({date, time}), mark)
def is_time_sooner_than(time, {dateMark, timeMark}), do: is_time_sooner_than(time, :calendar.datetime_to_gregorian_seconds({dateMark, timeMark}))
def is_time_sooner_than(time, mark) when is_integer(time) and is_integer(mark), do: time > mark
def subtract({date, time}, [days: n]), do: { subtract(date, [days: n]), time }
def subtract(date, [days: n]), do: :calendar.gregorian_days_to_date(:calendar.date_to_gregorian_days(date) - n)
def add({date, time}, [days: n]), do: { add(date, [days: n]), time }
def add(date, [days: n]), do: :calendar.gregorian_days_to_date(:calendar.date_to_gregorian_days(date) + n)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment