SisMaker 4 лет назад
Родитель
Сommit
586abb150b
5 измененных файлов: 911 добавлений и 104 удалений
  1. +118
    -96
      src/utils/rumUtil.erl
  2. +769
    -0
      src/utils/utTime.erl
  3. +21
    -0
      src/utils/utTime.hrl
  4. +2
    -2
      src/watcher/rumHWatcherSrv.erl
  5. +1
    -6
      test/lager_test_function_transform.erl

+ 118
- 96
src/utils/rumUtil.erl Просмотреть файл

@ -26,7 +26,6 @@
, check_hwm/1 , check_hwm/1
, check_hwm/2 , check_hwm/2
, makeInnerSinkName/1 , makeInnerSinkName/1
, otp_version/0
, maybe_flush/2 , maybe_flush/2
, isFileChanged/3 , isFileChanged/3
, get_env/2 , get_env/2
@ -264,86 +263,123 @@ parseRotateDateSpec(_) ->
{error, invalid_date_spec}. {error, invalid_date_spec}.
calcNextRotate(Spec) -> calcNextRotate(Spec) ->
NowDataTime = erlang:localtime(),
Later = calculate_next_rotation(Spec, NowDataTime),
calendar:datetime_to_gregorian_seconds(Later) - calendar:datetime_to_gregorian_seconds(NowDataTime).
calculate_next_rotation([], Now) ->
Now;
calculate_next_rotation([{minute, X} | T], {{_, _, _}, {Hour, Minute, _}} = Now) when Minute < X ->
%% rotation is this hour
NewNow = setelement(2, Now, {Hour, X, 0}),
calculate_next_rotation(T, NewNow);
calculate_next_rotation([{minute, X} | T], Now) ->
%% rotation is next hour
Seconds = calendar:datetime_to_gregorian_seconds(Now) + 3600,
DateTime = calendar:gregorian_seconds_to_datetime(Seconds),
{_, {NewHour, _, _}} = DateTime,
NewNow = setelement(2, DateTime, {NewHour, X, 0}),
calculate_next_rotation(T, NewNow);
calculate_next_rotation([{hour, X} | T], {{_, _, _}, {Hour, _, _}} = Now) when Hour < X ->
%% rotation is today, sometime
NewNow = setelement(2, Now, {X, 0, 0}),
calculate_next_rotation(T, NewNow);
calculate_next_rotation([{hour, X} | T], {{_, _, _}, _} = Now) ->
%% rotation is not today
Seconds = calendar:datetime_to_gregorian_seconds(Now) + 86400,
DateTime = calendar:gregorian_seconds_to_datetime(Seconds),
NewNow = setelement(2, DateTime, {X, 0, 0}),
calculate_next_rotation(T, NewNow);
calculate_next_rotation([{day, Day} | T], {Date, _Time} = Now) ->
DoW = calendar:day_of_the_week(Date),
AdjustedDay = case Day of
{Date, Time} = NowDataTime = erlang:localtime(),
calcNextRotate(Spec, Date, Time, NowDataTime).
calcNextRotate([], Data, Time, NowDataTime) ->
Later = {Data, Time},
calendar:datetime_to_gregorian_seconds(Later) - calendar:datetime_to_gregorian_seconds(NowDataTime);
calcNextRotate([OneSpec | Spec], NDate, NTime, NowDataTime) ->
{NHour, NMinute, _} = NTime,
case OneSpec of
{minute, SMinute} ->
case NMinute < SMinute of
true ->
%% rotation is this hour
calcNextRotate(Spec, NDate, {NHour, SMinute, 0}, NowDataTime);
_ ->
%% rotation is next hour
NexSec = utTime:lDateTimeToSec(NowDataTime) + 3600,
{NNDate, NNTime} = utTime:secToLDateTime(NexSec),
{NewHour, _, _} = NNTime,
calcNextRotate(Spec, NNDate, {NewHour, SMinute, 0}, NowDataTime)
end;
{hour, SHour} ->
case NHour < SHour of
true ->
%% rotation is today, sometime
calcNextRotate(Spec, NDate, {SHour, 0, 0}, NowDataTime);
_ ->
%% rotation is not today
NexSec = utTime:lDateTimeToSec(NowDataTime) + 86400,
{NNDate, _NNTime} = utTime:secToLDateTime(NexSec),
calcNextRotate(Spec, NNDate, {SHour, 0, 0}, NowDataTime)
end;
{day, SDay} ->
CurDay = utTime:weekDay(NDate),
AdjustedDay =
case SDay of
0 -> 7;
_ -> SDay
end,
case AdjustedDay of
CurDay -> %% rotation is today
case calcNextRotate(T, Now) of
{Date, _} = NewNow -> NewNow;
{NewDate, _} ->
%% rotation *isn't* today! rerun the calculation
NewNow = {NewDate, {0, 0, 0}},
calcNextRotate([{day, SDay} | T], NewNow)
end;
Y when Y > CurDay -> %% rotation is later this week
PlusDays = Y - CurDay,
Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays),
{NewDate, _} = calendar:gregorian_seconds_to_datetime(Seconds),
NewNow = {NewDate, {0, 0, 0}},
calcNextRotate(T, NewNow);
Y when Y < CurDay -> %% rotation is next week
PlusDays = ((7 - CurDay) + Y),
Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays),
{NewDate, _} = calendar:gregorian_seconds_to_datetime(Seconds),
NewNow = {NewDate, {0, 0, 0}},
calcNextRotate(T, NewNow)
end;
end;
calcNextRotate([{day, SDay} | T], {Date, _Time} = Now) ->
CurDay = calendar:day_of_the_week(Date),
AdjustedDay = case SDay of
0 -> 7; 0 -> 7;
X -> X X -> X
end, end,
case AdjustedDay of case AdjustedDay of
DoW -> %% rotation is today
case calculate_next_rotation(T, Now) of
CurDay -> %% rotation is today
case calcNextRotate(T, Now) of
{Date, _} = NewNow -> NewNow; {Date, _} = NewNow -> NewNow;
{NewDate, _} -> {NewDate, _} ->
%% rotation *isn't* today! rerun the calculation %% rotation *isn't* today! rerun the calculation
NewNow = {NewDate, {0, 0, 0}}, NewNow = {NewDate, {0, 0, 0}},
calculate_next_rotation([{day, Day} | T], NewNow)
calcNextRotate([{day, SDay} | T], NewNow)
end; end;
Y when Y > DoW -> %% rotation is later this week
PlusDays = Y - DoW,
Y when Y > CurDay -> %% rotation is later this week
PlusDays = Y - CurDay,
Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays), Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays),
{NewDate, _} = calendar:gregorian_seconds_to_datetime(Seconds), {NewDate, _} = calendar:gregorian_seconds_to_datetime(Seconds),
NewNow = {NewDate, {0, 0, 0}}, NewNow = {NewDate, {0, 0, 0}},
calculate_next_rotation(T, NewNow);
Y when Y < DoW -> %% rotation is next week
PlusDays = ((7 - DoW) + Y),
calcNextRotate(T, NewNow);
Y when Y < CurDay -> %% rotation is next week
PlusDays = ((7 - CurDay) + Y),
Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays), Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays),
{NewDate, _} = calendar:gregorian_seconds_to_datetime(Seconds), {NewDate, _} = calendar:gregorian_seconds_to_datetime(Seconds),
NewNow = {NewDate, {0, 0, 0}}, NewNow = {NewDate, {0, 0, 0}},
calculate_next_rotation(T, NewNow)
calcNextRotate(T, NewNow)
end; end;
calculate_next_rotation([{date, last} | T], {{Year, Month, Day}, _} = Now) ->
calcNextRotate([{date, last} | T], {{Year, Month, Day}, _} = Now) ->
Last = calendar:last_day_of_the_month(Year, Month), Last = calendar:last_day_of_the_month(Year, Month),
case Last == Day of case Last == Day of
true -> %% doing rotation today true -> %% doing rotation today
case calculate_next_rotation(T, Now) of
case calcNextRotate(T, Now) of
{{Year, Month, Day}, _} = NewNow -> NewNow; {{Year, Month, Day}, _} = NewNow -> NewNow;
{NewDate, _} -> {NewDate, _} ->
%% rotation *isn't* today! rerun the calculation %% rotation *isn't* today! rerun the calculation
NewNow = {NewDate, {0, 0, 0}}, NewNow = {NewDate, {0, 0, 0}},
calculate_next_rotation([{date, last} | T], NewNow)
calcNextRotate([{date, last} | T], NewNow)
end; end;
false -> false ->
NewNow = setelement(1, Now, {Year, Month, Last}), NewNow = setelement(1, Now, {Year, Month, Last}),
calculate_next_rotation(T, NewNow)
calcNextRotate(T, NewNow)
end; end;
calculate_next_rotation([{date, Date} | T], {{Year, Month, Date}, _} = Now) ->
calcNextRotate([{date, Date} | T], {{Year, Month, Date}, _} = Now) ->
%% rotation is today %% rotation is today
case calculate_next_rotation(T, Now) of
case calcNextRotate(T, Now) of
{{Year, Month, Date}, _} = NewNow -> NewNow; {{Year, Month, Date}, _} = NewNow -> NewNow;
{NewDate, _} -> {NewDate, _} ->
%% rotation *isn't* today! rerun the calculation %% rotation *isn't* today! rerun the calculation
NewNow = setelement(1, Now, NewDate), NewNow = setelement(1, Now, NewDate),
calculate_next_rotation([{date, Date} | T], NewNow)
calcNextRotate([{date, Date} | T], NewNow)
end; end;
calculate_next_rotation([{date, Date} | T], {{Year, Month, Day}, _} = Now) ->
calcNextRotate([{date, Date} | T], {{Year, Month, Day}, _} = Now) ->
PlusDays = case Date of PlusDays = case Date of
X when X < Day -> %% rotation is next month X when X < Day -> %% rotation is next month
Last = calendar:last_day_of_the_month(Year, Month), Last = calendar:last_day_of_the_month(Year, Month),
@ -353,7 +389,7 @@ calculate_next_rotation([{date, Date} | T], {{Year, Month, Day}, _} = Now) ->
end, end,
Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays), Seconds = calendar:datetime_to_gregorian_seconds(Now) + (86400 * PlusDays),
NewNow = calendar:gregorian_seconds_to_datetime(Seconds), NewNow = calendar:gregorian_seconds_to_datetime(Seconds),
calculate_next_rotation(T, NewNow).
calcNextRotate(T, NewNow).
-spec trace_filter(Query :: 'none' | [tuple()]) -> {ok, any()}. -spec trace_filter(Query :: 'none' | [tuple()]) -> {ok, any()}.
trace_filter(Query) -> trace_filter(Query) ->
@ -595,18 +631,6 @@ discard_messages(Second, Filter, Count) ->
makeInnerSinkName(Sink) -> makeInnerSinkName(Sink) ->
binary_to_atom(<<(atom_to_binary(Sink, utf8))/binary, "Event">>). binary_to_atom(<<(atom_to_binary(Sink, utf8))/binary, "Event">>).
-spec otp_version() -> pos_integer().
%% @doc Return the major version of the current Erlang/OTP runtime as an integer.
otp_version() ->
{Vsn, _} = string:to_integer(
case erlang:system_info(otp_release) of
[$R | Rel] ->
Rel;
Rel ->
Rel
end),
Vsn.
maybe_flush(undefined, #rumShaper{} = S) -> maybe_flush(undefined, #rumShaper{} = S) ->
S; S;
maybe_flush(Flag, #rumShaper{} = S) when is_boolean(Flag) -> maybe_flush(Flag, #rumShaper{} = S) when is_boolean(Flag) ->
@ -673,58 +697,58 @@ parse_fail_test() ->
rotation_calculation_test() -> rotation_calculation_test() ->
?assertMatch({{2000, 1, 1}, {13, 0, 0}}, ?assertMatch({{2000, 1, 1}, {13, 0, 0}},
calculate_next_rotation([{minute, 0}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{minute, 0}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 1}, {12, 45, 0}}, ?assertMatch({{2000, 1, 1}, {12, 45, 0}},
calculate_next_rotation([{minute, 45}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{minute, 45}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 2}, {0, 0, 0}}, ?assertMatch({{2000, 1, 2}, {0, 0, 0}},
calculate_next_rotation([{minute, 0}], {{2000, 1, 1}, {23, 45, 43}})),
calcNextRotate([{minute, 0}], {{2000, 1, 1}, {23, 45, 43}})),
?assertMatch({{2000, 1, 2}, {0, 0, 0}}, ?assertMatch({{2000, 1, 2}, {0, 0, 0}},
calculate_next_rotation([{hour, 0}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{hour, 0}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 1}, {16, 0, 0}}, ?assertMatch({{2000, 1, 1}, {16, 0, 0}},
calculate_next_rotation([{hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 2}, {12, 0, 0}}, ?assertMatch({{2000, 1, 2}, {12, 0, 0}},
calculate_next_rotation([{hour, 12}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{hour, 12}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 2, 1}, {12, 0, 0}}, ?assertMatch({{2000, 2, 1}, {12, 0, 0}},
calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{date, 1}, {hour, 12}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 2, 1}, {12, 0, 0}}, ?assertMatch({{2000, 2, 1}, {12, 0, 0}},
calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 15}, {12, 34, 43}})),
calcNextRotate([{date, 1}, {hour, 12}], {{2000, 1, 15}, {12, 34, 43}})),
?assertMatch({{2000, 2, 1}, {12, 0, 0}}, ?assertMatch({{2000, 2, 1}, {12, 0, 0}},
calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 2}, {12, 34, 43}})),
calcNextRotate([{date, 1}, {hour, 12}], {{2000, 1, 2}, {12, 34, 43}})),
?assertMatch({{2000, 2, 1}, {12, 0, 0}}, ?assertMatch({{2000, 2, 1}, {12, 0, 0}},
calculate_next_rotation([{date, 1}, {hour, 12}], {{2000, 1, 31}, {12, 34, 43}})),
calcNextRotate([{date, 1}, {hour, 12}], {{2000, 1, 31}, {12, 34, 43}})),
?assertMatch({{2000, 1, 1}, {16, 0, 0}}, ?assertMatch({{2000, 1, 1}, {16, 0, 0}},
calculate_next_rotation([{date, 1}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{date, 1}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 15}, {16, 0, 0}}, ?assertMatch({{2000, 1, 15}, {16, 0, 0}},
calculate_next_rotation([{date, 15}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{date, 15}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 31}, {16, 0, 0}}, ?assertMatch({{2000, 1, 31}, {16, 0, 0}},
calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{date, last}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 31}, {16, 0, 0}}, ?assertMatch({{2000, 1, 31}, {16, 0, 0}},
calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 31}, {12, 34, 43}})),
calcNextRotate([{date, last}, {hour, 16}], {{2000, 1, 31}, {12, 34, 43}})),
?assertMatch({{2000, 2, 29}, {16, 0, 0}}, ?assertMatch({{2000, 2, 29}, {16, 0, 0}},
calculate_next_rotation([{date, last}, {hour, 16}], {{2000, 1, 31}, {17, 34, 43}})),
calcNextRotate([{date, last}, {hour, 16}], {{2000, 1, 31}, {17, 34, 43}})),
?assertMatch({{2001, 2, 28}, {16, 0, 0}}, ?assertMatch({{2001, 2, 28}, {16, 0, 0}},
calculate_next_rotation([{date, last}, {hour, 16}], {{2001, 1, 31}, {17, 34, 43}})),
calcNextRotate([{date, last}, {hour, 16}], {{2001, 1, 31}, {17, 34, 43}})),
?assertMatch({{2000, 1, 1}, {16, 0, 0}}, ?assertMatch({{2000, 1, 1}, {16, 0, 0}},
calculate_next_rotation([{day, 6}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
calcNextRotate([{day, 6}, {hour, 16}], {{2000, 1, 1}, {12, 34, 43}})),
?assertMatch({{2000, 1, 8}, {16, 0, 0}}, ?assertMatch({{2000, 1, 8}, {16, 0, 0}},
calculate_next_rotation([{day, 6}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
calcNextRotate([{day, 6}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
?assertMatch({{2000, 1, 7}, {16, 0, 0}}, ?assertMatch({{2000, 1, 7}, {16, 0, 0}},
calculate_next_rotation([{day, 5}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
calcNextRotate([{day, 5}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
?assertMatch({{2000, 1, 3}, {16, 0, 0}}, ?assertMatch({{2000, 1, 3}, {16, 0, 0}},
calculate_next_rotation([{day, 1}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
calcNextRotate([{day, 1}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
?assertMatch({{2000, 1, 2}, {16, 0, 0}}, ?assertMatch({{2000, 1, 2}, {16, 0, 0}},
calculate_next_rotation([{day, 0}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
calcNextRotate([{day, 0}, {hour, 16}], {{2000, 1, 1}, {17, 34, 43}})),
?assertMatch({{2000, 1, 9}, {16, 0, 0}}, ?assertMatch({{2000, 1, 9}, {16, 0, 0}},
calculate_next_rotation([{day, 0}, {hour, 16}], {{2000, 1, 2}, {17, 34, 43}})),
calcNextRotate([{day, 0}, {hour, 16}], {{2000, 1, 2}, {17, 34, 43}})),
?assertMatch({{2000, 2, 3}, {16, 0, 0}}, ?assertMatch({{2000, 2, 3}, {16, 0, 0}},
calculate_next_rotation([{day, 4}, {hour, 16}], {{2000, 1, 29}, {17, 34, 43}})),
calcNextRotate([{day, 4}, {hour, 16}], {{2000, 1, 29}, {17, 34, 43}})),
?assertMatch({{2000, 1, 7}, {16, 0, 0}}, ?assertMatch({{2000, 1, 7}, {16, 0, 0}},
calculate_next_rotation([{day, 5}, {hour, 16}], {{2000, 1, 3}, {17, 34, 43}})),
calcNextRotate([{day, 5}, {hour, 16}], {{2000, 1, 3}, {17, 34, 43}})),
?assertMatch({{2000, 1, 3}, {16, 0, 0}}, ?assertMatch({{2000, 1, 3}, {16, 0, 0}},
calculate_next_rotation([{day, 1}, {hour, 16}], {{1999, 12, 28}, {17, 34, 43}})),
calcNextRotate([{day, 1}, {hour, 16}], {{1999, 12, 28}, {17, 34, 43}})),
ok. ok.
check_trace_test() -> check_trace_test() ->
@ -913,16 +937,14 @@ delete_test_dir() ->
delete_test_dir(TestDir) -> delete_test_dir(TestDir) ->
ok = application:unset_env(lager, test_dir), ok = application:unset_env(lager, test_dir),
{OsType, _} = os:type(),
ok = case {OsType, otp_version()} of
{win32, _} ->
application:stop(lager),
do_delete_test_dir(TestDir);
{unix, 15} ->
os:cmd("rm -rf " ++ TestDir);
{unix, _} ->
do_delete_test_dir(TestDir)
end.
ok =
case os:type() of
{win32, _} ->
application:stop(lager),
do_delete_test_dir(TestDir);
{_, _} ->
do_delete_test_dir(TestDir)
end.
do_delete_test_dir(Dir) -> do_delete_test_dir(Dir) ->
ListRet = file:list_dir_all(Dir), ListRet = file:list_dir_all(Dir),

+ 769
- 0
src/utils/utTime.erl Просмотреть файл

@ -0,0 +1,769 @@
-module(utTime).
-include("utTime.hrl").
-import(calendar, [day_of_the_week/1, day_of_the_week/3, iso_week_number/1, date_to_gregorian_days/1]).
-export([
now/0 %%
, nowMs/0 %%
, curDateTime/0 %%
, curDate/0 %%
, curTime/0 %%
, weekDay/0 %%
, weekDay/1 %% Data是星期几
, weekDay/3 %%
, weekCycle/0 %%
, weekCycle/1 %% Date
, secToLDateTime/1 %% datetime()
, secToUDateTime/1 %% datetime()
, lDateTimeToSec/1 %% datetime()
, uDateTimeToSec/1 %% datetime()
, timeZoneDiff/0 %% UTC时区
, countLDay/1 %% 1970Sec
, countUDay/1 %% 1970Sec
, isSameLDay/2 %%
, isSameUDay/2 %%
, countLWeek/1 %% 1970Sec
, countUWeek/1 %% 1970Sec
, isSameLWeek/2 %%
, isSameUWeek/2 %%
, isSameLMonth/2 %%
, isSameUMonth/2 %%
, countLDay/2 %% 1970Sec ZeroOffset为零点偏移时间
, countUDay/2 %% 1970Sec ZeroOffset为零点偏移时间
, isSameLDay/3 %% ZeroOffset为零点偏移时间
, isSameUDay/3 %% ZeroOffset为零点偏移时间
, countLWeek/2 %% 1970Sec ZeroOffset为零点偏移时间
, countUWeek/2 %% 1970Sec ZeroOffset为零点偏移时间
, isSameLWeek/3 %% ZeroOffset为零点偏移时间
, isSameUWeek/3 %% ZeroOffset为零点偏移时间
, isSameLMonth/3 %% ZeroOffset为零点偏移时间
, isSameUMonth/3 %% ZeroOffset为零点偏移时间
, hourBegin/0 %%
, hourBegin/1 %% Sec所在小时开始时间戳
, hourEnd/0 %%
, hourEnd/1 %% Sec所在小时结束时间戳
, dayLBegin/0 %%
, dayLBegin/1 %% Sec所在天开始时间戳
, dayUBegin/0 %% Sec所在天开始时间戳
, dayUBegin/1 %% Sec所在天开始时间戳
, dayLEnd/0 %%
, dayLEnd/1 %% Sec所在天结束时间戳
, dayUEnd/0 %%
, dayUEnd/1 %% Sec所在天结束时间戳
, dayLBeginEnd/0 %%
, dayLBeginEnd/1 %% Sec所在天开始结束时间戳
, dayUBeginEnd/0 %%
, dayUBeginEnd/1 %% Sec所在天开始结束时间戳
, weekLBegin/0 %%
, weekLBegin/1 %% Sec所在周开始时间戳
, weekUBegin/0 %%
, weekUBegin/1 %% Sec所在周的开始时间戳
, weekLEnd/0 %%
, weekLEnd/1 %% Sec所在周的结束时间戳
, weekUEnd/0 %%
, weekUEnd/1 %% Sec所在周的结束时间戳
, weekLBeginEnd/0 %%
, weekLBeginEnd/1 %% Sec所在周的开始结束时间戳
, weekUBeginEnd/0 %%
, weekUBeginEnd/1 %% Sec所在周的开始结束时间戳
, monthLBegin/0 %%
, monthLBegin/1 %% Sec所在月的开始时间戳
, monthUBegin/0 %%
, monthUBegin/1 %% Sec所在月的开始时间戳
, monthLEnd/0 %%
, monthLEnd/1 %% Sec所在月的结束时间戳
, monthUEnd/0 %%
, monthUEnd/1 %% Sec所在月的结束时间戳
, isLeapYear/1 %%
, monthDay/2 %%
, monthSecs/2 %%
, monthLBeginEnd/0 %%
, monthLBeginEnd/1 %% Sec所在月的开始结束时间戳
, monthUBeginEnd/0 %%
, monthUBeginEnd/1 %% Sec所在月的开始结束时间戳
, sWeekName/1 %%
, lWeekName/1 %%
, sMonthName/1 %%
, lMonthName/1 %%
, dateNumber/0 %% version
, dateNumber/1 %% version
, dateNumber/3 %% version
, numberDate/1 %% dateNumber
, secToDayTime/1 %% time()
, diffLDateTimeSec/2 %% datetime()
, diffUDateTimeSec/2 %% datetime()
, diffLDateTimeDayTime/2 %% datetime() daytime()
, diffUDateTimeDayTime/2 %% datetime() daytime()
, diffSecs/2 %% daytime()
, timeToSecs/1 %% time() Sec
, daysInYear/1 %% Date为该年的哪一天
, dateToStr/1 %% Data to Str
, dateToStr/3 %% Data to Str
, dateToStr/2 %% Data to Str
, dateToStr/4 %% Data to Str
, timeToStr/1 %% time to Str
, timeToStr/3 %% time to Str
, timeToStr/2 %% time to Str
, timeToStr/4 %% time to Str
, dateTimeStr/1 %% datetime to Str
]).
%%
-spec now() -> timestamp().
now() ->
erlang:system_time(second).
%%
-spec nowMs() -> timestamp().
nowMs() ->
erlang:system_time(millisecond).
%%
-spec curDateTime() -> datetime().
curDateTime() ->
erlang:localtime().
%%
-spec curDate() -> date().
curDate() ->
erlang:date().
%%
-spec curTime() -> time().
curTime() ->
erlang:time().
%%
-spec weekDay() -> week().
weekDay() ->
day_of_the_week(erlang:date()).
%% Data是星期几
-spec weekDay(Date :: date()) -> week().
weekDay(Date) ->
day_of_the_week(Date).
%%
-spec weekDay(Year :: year(), Month :: month(), Day :: day()) -> week().
weekDay(Year, Month, Day) ->
day_of_the_week(Year, Month, Day).
%%
-spec weekCycle() -> yearWeekCycle().
weekCycle() ->
iso_week_number(erlang:date()).
%% Date
-spec weekCycle(Date :: date()) -> yearWeekCycle().
weekCycle(Date) ->
iso_week_number(Date).
%% datetime()
-spec secToLDateTime(Sec :: timestamp()) -> datetime().
secToLDateTime(Ses) ->
erlang:universaltime_to_localtime(erlang:posixtime_to_universaltime(Ses)).
%% datetime()
-spec secToUDateTime(Sec :: timestamp()) -> datetime().
secToUDateTime(Ses) ->
erlang:posixtime_to_universaltime(Ses).
%% datetime()
-spec lDateTimeToSec(LocalDate :: datetime()) -> timestamp().
lDateTimeToSec(LocalDate) ->
erlang:universaltime_to_posixtime(erlang:localtime_to_universaltime(LocalDate)).
%% datetime()
-spec uDateTimeToSec(UniversalTime :: datetime()) -> timestamp().
uDateTimeToSec(UniversalTime) ->
erlang:universaltime_to_posixtime(UniversalTime).
%% UTC时区
-spec timeZoneDiff() -> timestamp().
timeZoneDiff() ->
Local2001 = erlang:universaltime_to_localtime({{2001, 1, 1}, {0, 0, 0}}),
erlang:universaltime_to_posixtime(Local2001) - 978307200.
%% 1970Sec
-spec countLDay(Sec :: timestamp()) -> integer().
countLDay(Sec) ->
(Sec + timeZoneDiff()) div ?SECS_DAY.
%% 1970Sec
-spec countUDay(Sec :: timestamp()) -> integer().
countUDay(Sec) ->
Sec div ?SECS_DAY.
%%
-spec isSameLDay(Sec1 :: timestamp(), Sec2 :: timestamp()) -> boolean().
isSameLDay(Sec1, Sec2) ->
TimeZoneDiff = timeZoneDiff(),
(Sec1 + TimeZoneDiff) div ?SECS_DAY =:= (Sec2 + TimeZoneDiff) div ?SECS_DAY.
%%
-spec isSameUDay(Sec1 :: timestamp(), Sec2 :: timestamp()) -> boolean().
isSameUDay(Sec1, Sec2) ->
Sec1 div ?SECS_DAY =:= Sec2 div ?SECS_DAY.
%% 1970Sec
-spec countLWeek(Sec :: timestamp()) -> integer().
countLWeek(Sec) ->
(Sec + 259200 + timeZoneDiff()) div ?SECS_WEEK.
%% 1970Sec
-spec countUWeek(Sec :: timestamp()) -> integer().
countUWeek(Sec) ->
(Sec + 259200) div ?SECS_WEEK.
%%
-spec isSameLWeek(Sec1 :: timestamp(), Sec2 :: timestamp()) -> boolean().
isSameLWeek(Sec1, Sec2) ->
TimeZoneDiff = timeZoneDiff(),
(Sec1 + 259200 + TimeZoneDiff) div ?SECS_WEEK =:= (Sec2 + 259200 + TimeZoneDiff) div ?SECS_WEEK.
%%
-spec isSameUWeek(Sec1 :: timestamp(), Sec2 :: timestamp()) -> boolean().
isSameUWeek(Sec1, Sec2) ->
(Sec1 + 259200) div ?SECS_WEEK =:= (Sec2 + 259200) div ?SECS_WEEK.
%%
-spec isSameLMonth(Sec1 :: timestamp(), Sec2 :: timestamp()) -> boolean().
isSameLMonth(Sec1, Sec2) ->
{{Year1, Month1, _Day1}, _Time1} = secToLDateTime(Sec1),
{{Year2, Month2, _Day2}, _Time2} = secToLDateTime(Sec2),
Month1 =:= Month2 andalso Year1 =:= Year2.
%%
-spec isSameUMonth(Sec1 :: timestamp(), Sec2 :: timestamp()) -> boolean().
isSameUMonth(Sec1, Sec2) ->
{{Year1, Month1, _Day1}, _Time1} = secToUDateTime(Sec1),
{{Year2, Month2, _Day2}, _Time2} = secToUDateTime(Sec2),
Month1 =:= Month2 andalso Year1 =:= Year2.
%% 1970Sec ZeroOffset为零点偏移时间
-spec countLDay(Sec :: timestamp(), ZeroOffset :: timestamp()) -> integer().
countLDay(Sec, ZeroOffset) ->
(Sec - ZeroOffset + timeZoneDiff()) div ?SECS_DAY.
%% 1970Sec ZeroOffset为零点偏移时间
-spec countUDay(Sec :: timestamp(), ZeroOffset :: timestamp()) -> integer().
countUDay(Sec, ZeroOffset) ->
(Sec - ZeroOffset) div ?SECS_DAY.
%% ZeroOffset为零点偏移时间
-spec isSameLDay(Sec1 :: timestamp(), Sec2 :: timestamp(), ZeroOffset :: timestamp()) -> boolean().
isSameLDay(Sec1, Sec2, ZeroOffset) ->
TimeZoneDiff = timeZoneDiff(),
(Sec1 - ZeroOffset + TimeZoneDiff) div ?SECS_DAY =:= (Sec2 - ZeroOffset + TimeZoneDiff) div ?SECS_DAY.
%% ZeroOffset为零点偏移时间
-spec isSameUDay(Sec1 :: timestamp(), Sec2 :: timestamp(), ZeroOffset :: timestamp()) -> boolean().
isSameUDay(Sec1, Sec2, ZeroOffset) ->
(Sec1 - ZeroOffset) div ?SECS_DAY =:= (Sec2 - ZeroOffset) div ?SECS_DAY.
%% 1970Sec ZeroOffset为零点偏移时间
-spec countLWeek(Sec :: timestamp(), ZeroOffset :: timestamp()) -> integer().
countLWeek(Sec, ZeroOffset) ->
(Sec - ZeroOffset + 259200 + timeZoneDiff()) div ?SECS_WEEK.
%% 1970Sec ZeroOffset为零点偏移时间
-spec countUWeek(Sec :: timestamp(), ZeroOffset :: timestamp()) -> integer().
countUWeek(Sec, ZeroOffset) ->
(Sec - ZeroOffset + 259200) div ?SECS_WEEK.
%% ZeroOffset为零点偏移时间
-spec isSameLWeek(Sec1 :: timestamp(), Sec2 :: timestamp(), ZeroOffset :: timestamp()) -> boolean().
isSameLWeek(Sec1, Sec2, ZeroOffset) ->
TimeZoneDiff = timeZoneDiff(),
(Sec1 - ZeroOffset + 259200 + TimeZoneDiff) div ?SECS_WEEK =:= (Sec2 - ZeroOffset + 259200 + TimeZoneDiff) div ?SECS_WEEK.
%% ZeroOffset为零点偏移时间
-spec isSameUWeek(Sec1 :: timestamp(), Sec2 :: timestamp(), ZeroOffset :: timestamp()) -> boolean().
isSameUWeek(Sec1, Sec2, ZeroOffset) ->
(Sec1 - ZeroOffset + 259200) div ?SECS_WEEK =:= (Sec2 - ZeroOffset + 259200) div ?SECS_WEEK.
%% ZeroOffset为零点偏移时间
-spec isSameLMonth(Sec1 :: timestamp(), Sec2 :: timestamp(), ZeroOffset :: timestamp()) -> boolean().
isSameLMonth(Sec1, Sec2, ZeroOffset) ->
{{Year1, Month1, _Day1}, _Time1} = secToLDateTime(Sec1 - ZeroOffset),
{{Year2, Month2, _Day2}, _Time2} = secToLDateTime(Sec2 - ZeroOffset),
Month1 =:= Month2 andalso Year1 =:= Year2.
%% ZeroOffset为零点偏移时间
-spec isSameUMonth(Sec1 :: timestamp(), Sec2 :: timestamp(), ZeroOffset :: timestamp()) -> boolean().
isSameUMonth(Sec1, Sec2, ZeroOffset) ->
{{Year1, Month1, _Day1}, _Time1} = secToUDateTime(Sec1 - ZeroOffset),
{{Year2, Month2, _Day2}, _Time2} = secToUDateTime(Sec2 - ZeroOffset),
Month1 =:= Month2 andalso Year1 =:= Year2.
%%
-spec hourBegin() -> timestamp().
hourBegin() ->
Sec = erlang:system_time(second),
Sec - Sec rem ?SECS_HOUR.
%% Sec所在小时开始时间戳
-spec hourBegin(Sec :: timestamp()) -> timestamp().
hourBegin(Sec) ->
Sec - Sec rem ?SECS_HOUR.
%%
-spec hourEnd() -> timestamp().
hourEnd() ->
Sec = erlang:system_time(second),
Sec - Sec rem ?SECS_HOUR + ?SECS_HOUR.
%% Sec所在小时结束时间戳
-spec hourEnd(Sec :: timestamp()) -> timestamp().
hourEnd(Sec) ->
Sec - Sec rem ?SECS_HOUR + ?SECS_HOUR.
%%
-spec dayLBegin() -> timestamp().
dayLBegin() ->
Sec = erlang:system_time(second),
Sec - (Sec + timeZoneDiff()) rem ?SECS_DAY.
%% Sec所在天开始时间戳
-spec dayLBegin(Sec :: timestamp()) -> timestamp().
dayLBegin(Sec) ->
Sec - (Sec + timeZoneDiff()) rem ?SECS_DAY.
%% Sec所在天开始时间戳
-spec dayUBegin() -> timestamp().
dayUBegin() ->
Sec = erlang:system_time(second),
Sec - Sec rem ?SECS_DAY.
%% Sec所在天开始时间戳
-spec dayUBegin(Sec :: timestamp()) -> timestamp().
dayUBegin(Sec) ->
Sec - Sec rem ?SECS_DAY.
%%
-spec dayLEnd() -> timestamp().
dayLEnd() ->
Sec = erlang:system_time(second),
Sec - (Sec + timeZoneDiff()) rem ?SECS_DAY + ?SECS_DAY.
%% Sec所在天结束时间戳
-spec dayLEnd(Sec :: timestamp()) -> timestamp().
dayLEnd(Sec) ->
Sec - (Sec + timeZoneDiff()) rem ?SECS_DAY + ?SECS_DAY.
%%
-spec dayUEnd() -> timestamp().
dayUEnd() ->
Sec = erlang:system_time(second),
Sec - Sec rem ?SECS_DAY + ?SECS_DAY.
%% Sec所在天结束时间戳
-spec dayUEnd(Sec :: timestamp()) -> timestamp().
dayUEnd(Sec) ->
Sec - Sec rem ?SECS_DAY + ?SECS_DAY.
%%
-spec dayLBeginEnd() -> timestamp().
dayLBeginEnd() ->
Sec = erlang:system_time(second),
Begin = Sec - (Sec + timeZoneDiff()) rem ?SECS_DAY,
{Begin, Begin + ?SECS_DAY}.
%% Sec所在天开始结束时间戳
-spec dayLBeginEnd(Sec :: timestamp()) -> timestamp().
dayLBeginEnd(Sec) ->
Begin = Sec - (Sec + timeZoneDiff()) rem ?SECS_DAY,
{Begin, Begin + ?SECS_DAY}.
%%
-spec dayUBeginEnd() -> timestamp().
dayUBeginEnd() ->
Sec = erlang:system_time(second),
Begin = Sec - Sec rem ?SECS_DAY,
{Begin, Begin + ?SECS_DAY}.
%% Sec所在天开始结束时间戳
-spec dayUBeginEnd(Sec :: timestamp()) -> timestamp().
dayUBeginEnd(Sec) ->
Begin = Sec - Sec rem ?SECS_DAY,
{Begin, Begin + ?SECS_DAY}.
%%
-spec weekLBegin() -> timestamp().
weekLBegin() ->
Sec = erlang:system_time(second),
Sec - (Sec - 345600 + timeZoneDiff()) rem ?SECS_WEEK.
%% Sec所在周开始时间戳
-spec weekLBegin(Sec :: timestamp()) -> timestamp().
weekLBegin(Sec) ->
Sec - (Sec - 345600 + timeZoneDiff()) rem ?SECS_WEEK.
%%
-spec weekUBegin() -> timestamp().
weekUBegin() ->
Sec = erlang:system_time(second),
Sec - (Sec - 345600) rem ?SECS_WEEK.
%% Sec所在周的开始时间戳
-spec weekUBegin(Sec :: timestamp()) -> timestamp().
weekUBegin(Sec) ->
Sec - (Sec - 345600) rem ?SECS_WEEK.
%%
-spec weekLEnd() -> timestamp().
weekLEnd() ->
Sec = erlang:system_time(second),
Sec - (Sec - 345600 + timeZoneDiff()) rem ?SECS_WEEK + ?SECS_WEEK.
%% Sec所在周的结束时间戳
-spec weekLEnd(Sec :: timestamp()) -> timestamp().
weekLEnd(Sec) ->
Sec - (Sec - 345600 + timeZoneDiff()) rem ?SECS_WEEK + ?SECS_WEEK.
%%
-spec weekUEnd() -> timestamp().
weekUEnd() ->
Sec = erlang:system_time(second),
Sec - (Sec - 345600) rem ?SECS_WEEK + ?SECS_WEEK.
%% Sec所在周的结束时间戳
-spec weekUEnd(Sec :: timestamp()) -> timestamp().
weekUEnd(Sec) ->
Sec - (Sec - 345600) rem ?SECS_WEEK + ?SECS_WEEK.
%%
-spec weekLBeginEnd() -> timestamp().
weekLBeginEnd() ->
Sec = erlang:system_time(second),
Begin = Sec - (Sec - 345600 + timeZoneDiff()) rem ?SECS_WEEK,
{Begin, Begin + ?SECS_WEEK}.
%% Sec所在周的开始结束时间戳
-spec weekLBeginEnd(Sec :: timestamp()) -> timestamp().
weekLBeginEnd(Sec) ->
Begin = Sec - (Sec - 345600 + timeZoneDiff()) rem ?SECS_WEEK,
{Begin, Begin + ?SECS_WEEK}.
%%
-spec weekUBeginEnd() -> timestamp().
weekUBeginEnd() ->
Sec = erlang:system_time(second),
Begin = Sec - (Sec - 345600) rem ?SECS_WEEK,
{Begin, Begin + ?SECS_WEEK}.
%% Sec所在周的开始结束时间戳
-spec weekUBeginEnd(Sec :: timestamp()) -> timestamp().
weekUBeginEnd(Sec) ->
Begin = Sec - (Sec - 345600) rem ?SECS_WEEK,
{Begin, Begin + ?SECS_WEEK}.
%%
-spec monthLBegin() -> timestamp().
monthLBegin() ->
Sec = erlang:system_time(second),
{{Year, Month, _Day}, _Time} = secToLDateTime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
lDateTimeToSec(MonthStartDateTime).
%% Sec所在月的开始时间戳
-spec monthLBegin(Sec :: timestamp()) -> timestamp().
monthLBegin(Sec) ->
{{Year, Month, _Day}, _Time} = secToLDateTime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
lDateTimeToSec(MonthStartDateTime).
%%
-spec monthUBegin() -> timestamp().
monthUBegin() ->
Sec = erlang:system_time(second),
{{Year, Month, _Day}, _Time} = erlang:posixtime_to_universaltime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
erlang:universaltime_to_posixtime(MonthStartDateTime).
%% Sec所在月的开始时间戳
-spec monthUBegin(Sec :: timestamp()) -> timestamp().
monthUBegin(Sec) ->
{{Year, Month, _Day}, _Time} = erlang:posixtime_to_universaltime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
erlang:universaltime_to_posixtime(MonthStartDateTime).
%%
-spec monthLEnd() -> timestamp().
monthLEnd() ->
Sec = erlang:system_time(second),
{{Year, Month, _Day}, _Time} = secToLDateTime(Sec),
MonthDay = monthDay(Year, Month),
MonthEndDateTime = {{Year, Month, MonthDay}, {23, 59, 59}},
lDateTimeToSec(MonthEndDateTime).
%% Sec所在月的结束时间戳
-spec monthLEnd(Sec :: timestamp()) -> timestamp().
monthLEnd(Sec) ->
{{Year, Month, _Day}, _Time} = secToLDateTime(Sec),
MonthDay = monthDay(Year, Month),
MonthEndDateTime = {{Year, Month, MonthDay}, {23, 59, 59}},
lDateTimeToSec(MonthEndDateTime).
%%
-spec monthUEnd() -> timestamp().
monthUEnd() ->
Sec = erlang:system_time(second),
{{Year, Month, _Day}, _Time} = erlang:posixtime_to_universaltime(Sec),
MonthDay = monthDay(Year, Month),
MonthEndDateTime = {{Year, Month, MonthDay}, {23, 59, 59}},
erlang:universaltime_to_posixtime(MonthEndDateTime).
%% Sec所在月的结束时间戳
-spec monthUEnd(Sec :: timestamp()) -> timestamp().
monthUEnd(Sec) ->
{{Year, Month, _Day}, _Time} = erlang:posixtime_to_universaltime(Sec),
MonthDay = monthDay(Year, Month),
MonthEndDateTime = {{Year, Month, MonthDay}, {23, 59, 59}},
erlang:universaltime_to_posixtime(MonthEndDateTime).
%%
-spec isLeapYear(Yesr :: year()) -> boolean().
isLeapYear(Year) ->
(Year rem 4 =:= 0 andalso Year rem 100 =/= 0) orelse (Year rem 400 =:= 0).
%%
-spec monthDay(year(), month()) -> timestamp().
monthDay(_, 4) -> 30;
monthDay(_, 6) -> 30;
monthDay(_, 9) -> 30;
monthDay(_, 11) -> 30;
monthDay(Y, 2) ->
case isLeapYear(Y) of
true -> 29;
_ -> 28
end;
monthDay(_, _) ->
31.
%%
-spec monthSecs(year(), month()) -> timestamp().
monthSecs(_, 4) -> 2592000;
monthSecs(_, 6) -> 2592000;
monthSecs(_, 9) -> 2592000;
monthSecs(_, 11) -> 2592000;
monthSecs(Y, 2) ->
case isLeapYear(Y) of
true -> 2505600;
_ -> 2419200
end;
monthSecs(_, _) ->
2678400.
%%
-spec monthLBeginEnd() -> timestamp().
monthLBeginEnd() ->
Sec = erlang:system_time(second),
{{Year, Month, _Day}, _Time} = secToLDateTime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
Begin = lDateTimeToSec(MonthStartDateTime),
{Begin, Begin + monthSecs(Year, Month)}.
%% Sec所在月的开始结束时间戳
-spec monthLBeginEnd(Sec :: timestamp()) -> timestamp().
monthLBeginEnd(Sec) ->
{{Year, Month, _Day}, _Time} = secToLDateTime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
Begin = lDateTimeToSec(MonthStartDateTime),
{Begin, Begin + monthSecs(Year, Month)}.
%%
-spec monthUBeginEnd() -> timestamp().
monthUBeginEnd() ->
Sec = erlang:system_time(second),
{{Year, Month, _Day}, _Time} = erlang:posixtime_to_universaltime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
Begin = erlang:universaltime_to_posixtime(MonthStartDateTime),
{Begin, Begin + monthSecs(Year, Month)}.
%% Sec所在月的开始结束时间戳
-spec monthUBeginEnd(Sec :: timestamp()) -> timestamp().
monthUBeginEnd(Sec) ->
{{Year, Month, _Day}, _Time} = erlang:posixtime_to_universaltime(Sec),
MonthStartDateTime = {{Year, Month, 1}, {0, 0, 0}},
Begin = erlang:universaltime_to_posixtime(MonthStartDateTime),
{Begin, Begin + monthSecs(Year, Month)}.
%%
-spec sWeekName(week()) -> string().
sWeekName(1) -> <<"Mon">>;
sWeekName(2) -> <<"Tue">>;
sWeekName(3) -> <<"Wed">>;
sWeekName(4) -> <<"Thu">>;
sWeekName(5) -> <<"Fri">>;
sWeekName(6) -> <<"Sat">>;
sWeekName(7) -> <<"Sun">>.
%%
-spec lWeekName(week()) -> string().
lWeekName(1) -> <<"Monday">>;
lWeekName(2) -> <<"Tuesday">>;
lWeekName(3) -> <<"Wednesday">>;
lWeekName(4) -> <<"Thursday">>;
lWeekName(5) -> <<"Friday">>;
lWeekName(6) -> <<"Saturday">>;
lWeekName(7) -> <<"Sunday">>.
%%
-spec sMonthName(month()) -> string().
sMonthName(1) -> <<"Jan">>;
sMonthName(2) -> <<"Feb">>;
sMonthName(3) -> <<"Mar">>;
sMonthName(4) -> <<"Apr">>;
sMonthName(5) -> <<"May">>;
sMonthName(6) -> <<"Jun">>;
sMonthName(7) -> <<"Jul">>;
sMonthName(8) -> <<"Aug">>;
sMonthName(9) -> <<"Sep">>;
sMonthName(10) -> <<"Oct">>;
sMonthName(11) -> <<"Nov">>;
sMonthName(12) -> <<"Dec">>.
%%
-spec lMonthName(month()) -> string().
lMonthName(1) -> <<"January">>;
lMonthName(2) -> <<"February">>;
lMonthName(3) -> <<"March">>;
lMonthName(4) -> <<"April">>;
lMonthName(5) -> <<"May">>;
lMonthName(6) -> <<"June">>;
lMonthName(7) -> <<"July">>;
lMonthName(8) -> <<"August">>;
lMonthName(9) -> <<"September">>;
lMonthName(10) -> <<"October">>;
lMonthName(11) -> <<"November">>;
lMonthName(12) -> <<"December">>.
%% version
-spec dateNumber() -> integer().
dateNumber() ->
{Year, Month, Day} = erlang:date(),
Year * 10000 + Month * 100 + Day.
%% version
-spec dateNumber(date()) -> integer().
dateNumber({Year, Month, Day}) ->
Year * 10000 + Month * 100 + Day.
%% version
-spec dateNumber(Year :: year(), Month :: month(), Day :: day()) -> integer().
dateNumber(Year, Month, Day) ->
Year * 10000 + Month * 100 + Day.
%% dateNumber
-spec numberDate(DateNumber :: integer()) -> date().
numberDate(DateNumber) ->
Y = DateNumber div 10000,
M = DateNumber rem 10000 div 100,
D = DateNumber rem 100,
{Y, M, D}.
%% time()
-spec secToDayTime(Secs :: timestamp()) -> {Day :: integer(), Time :: time()}.
secToDayTime(Secs) ->
Days0 = Secs div ?SECS_DAY,
Secs0 = Secs rem ?SECS_DAY,
Hour = Secs0 div ?SECS_HOUR,
Secs1 = Secs0 rem ?SECS_HOUR,
Minute = Secs1 div ?SECS_MIN,
Second = Secs1 rem ?SECS_MIN,
{Days0, {Hour, Minute, Second}}.
%% datetime()
-spec diffLDateTimeSec(datetime(), datetime()) -> timestamp().
diffLDateTimeSec(DateTime1, DateTime2) ->
Secs = lDateTimeToSec(DateTime1) - lDateTimeToSec(DateTime2),
erlang:abs(Secs).
%% datetime()
-spec diffUDateTimeSec(datetime(), datetime()) -> timestamp().
diffUDateTimeSec(DateTime1, DateTime2) ->
Secs = uDateTimeToSec(DateTime1) - uDateTimeToSec(DateTime2),
erlang:abs(Secs).
%% datetime() daytime()
-spec diffLDateTimeDayTime(datetime(), datetime()) -> timestamp().
diffLDateTimeDayTime(DateTime1, DateTime2) ->
Secs = lDateTimeToSec(DateTime1) - lDateTimeToSec(DateTime2),
secToDayTime(erlang:abs(Secs)).
%% datetime() daytime()
-spec diffUDateTimeDayTime(datetime(), datetime()) -> timestamp().
diffUDateTimeDayTime(DateTime1, DateTime2) ->
Secs = uDateTimeToSec(DateTime1) - uDateTimeToSec(DateTime2),
secToDayTime(erlang:abs(Secs)).
%% daytime()
-spec diffSecs(timestamp(), timestamp()) -> timestamp().
diffSecs(Sec1, Sec2) ->
secToDayTime(erlang:abs(Sec1 - Sec2)).
%% time() Sec
-spec timeToSecs(time()) -> timestamp().
timeToSecs({H, M, S}) ->
H * ?SECS_HOUR + M * ?SECS_MIN + S.
%% Date为该年的哪一天
-spec daysInYear(date()) -> integer().
daysInYear({Y, _, _} = Date) ->
date_to_gregorian_days(Date) - date_to_gregorian_days({Y, 1, 1}).
%% Data to Str
-spec dateToStr(date()) -> string().
dateToStr({Year, Month, Day}) ->
S = io_lib:format("~B_~2.10.0B_~2.10.0B", [Year, Month, Day]),
lists:flatten(S).
%% Data to Str
-spec dateToStr(year(), month(), day()) -> string().
dateToStr(Year, Month, Day) ->
S = io_lib:format("~B_~2.10.0B_~2.10.0B", [Year, Month, Day]),
lists:flatten(S).
%% Data to Str
-spec dateToStr(date(), string()) -> string().
dateToStr({Year, Month, Day}, Separator) ->
S = io_lib:format("~B~w~2.10.0B~w~2.10.0B", [Year, Separator, Month, Separator, Day]),
lists:flatten(S).
%% Data to Str
-spec dateToStr(year(), month(), day(), string()) -> string().
dateToStr(Year, Month, Day, Separator) ->
S = io_lib:format("~B~w~2.10.0B~w~2.10.0B", [Year, Separator, Month, Separator, Day]),
lists:flatten(S).
%% time to Str
-spec timeToStr(time()) -> string().
timeToStr({Hour, Minute, Second}) ->
S = io_lib:format("~B:~2.10.0B:~2.10.0B", [Hour, Minute, Second]),
lists:flatten(S).
%% time to Str
-spec timeToStr(hour(), minute(), second()) -> string().
timeToStr(Hour, Minute, Second) ->
S = io_lib:format("~B:~2.10.0B:~2.10.0B", [Hour, Minute, Second]),
lists:flatten(S).
%% time to Str
-spec timeToStr(time(), string()) -> string().
timeToStr({Hour, Minute, Second}, Separator) ->
S = io_lib:format("~B~w~2.10.0B~w~2.10.0B", [Hour, Separator, Minute, Separator, Second]),
lists:flatten(S).
%% time to Str
-spec timeToStr(hour(), minute(), second(), string()) -> string().
timeToStr(Hour, Minute, Second, Separator) ->
S = io_lib:format("~B~w~2.10.0B~w~2.10.0B", [Hour, Separator, Minute, Separator, Second]),
lists:flatten(S).
%% datetime to Str
-spec dateTimeStr(datetime()) -> string().
dateTimeStr({{Year, Month, Day}, {Hour, Minute, Second}}) ->
S = io_lib:format("~B_~2.10.0B_~2.10.0B ~B:~2.10.0B:~2.10.0B", [Year, Month, Day, Hour, Minute, Second]),
lists:flatten(S).

+ 21
- 0
src/utils/utTime.hrl Просмотреть файл

@ -0,0 +1,21 @@
-define(SECS_MIN, 60). %%
-define(SECS_HOUR, 3600). %%
-define(SECS_DAY, 86400). %%
-define(SECS_WEEK, 604800). %%
-define(DAYS_NO_LEAP_YEAR, 365). %%
-define(DAYS_LEAP_YEAR, 366). %%
-define(SECS_1970, 62167219200). %% utc 1970
-type year() :: non_neg_integer().
-type month() :: 1..12.
-type day() :: 1..31.
-type hour() :: 0..23.
-type minute() :: 0..59.
-type second() :: 0..59.
-type date() :: {year(), month(), day()}.
-type time() :: {hour(), minute(), second()}.
-type datetime() :: {date(), time()}.
-type timestamp() :: non_neg_integer(). %%
-type week() :: 1..7.
-type weekCycle() :: 1..53. %% 53
-type yearWeekCycle() :: {year(), weekCycle()}.

+ 2
- 2
src/watcher/rumHWatcherSrv.erl Просмотреть файл

@ -69,7 +69,7 @@ handleInfo({gen_event_EXIT, Module, Reason}, #state{module = Module, config = Co
ok ok
end, end,
{noreply, State}; {noreply, State};
handleInfo(reinstall_handler, #state{module = Module, config = Config, sink = Sink}) ->
handleInfo(mReinstallHandler, #state{module = Module, config = Config, sink = Sink}) ->
installHandler(Module, Config, Sink), installHandler(Module, Config, Sink),
kpS; kpS;
handleInfo({reboot, Sink}, _State) -> handleInfo({reboot, Sink}, _State) ->
@ -115,7 +115,7 @@ installHandler(Module, Config, Sink) ->
Error -> Error ->
%% try to reinstall it later %% try to reinstall it later
?INT_LOG(error, "Lager failed to install handler ~p into ~p, retrying later : ~p", [Module, Sink, Error]), ?INT_LOG(error, "Lager failed to install handler ~p into ~p, retrying later : ~p", [Module, Sink, Error]),
erlang:send_after(5000, self(), reinstall_handler),
erlang:send_after(5000, self(), mReinstallHandler),
ok ok
end. end.

+ 1
- 6
test/lager_test_function_transform.erl Просмотреть файл

@ -49,12 +49,7 @@ transform_static() ->
static_result. static_result.
transform_dynamic() -> transform_dynamic() ->
case rumUtil:otp_version() >= 18 of
true ->
erlang:monotonic_time();
false ->
erlang:now()
end.
erlang:now().
not_running_test() -> not_running_test() ->
?assertEqual({error, lager_not_running}, eRum:log(info, self(), "not running")). ?assertEqual({error, lager_not_running}, eRum:log(info, self(), "not running")).

Загрузка…
Отмена
Сохранить