|
|
@ -14,8 +14,10 @@ |
|
|
|
, nowMs/0 |
|
|
|
, msToBinStr/0 |
|
|
|
, msToBinStr/1 |
|
|
|
, parseRotateDateSpec/1 |
|
|
|
, parseRotateSpec/1 |
|
|
|
, calcNextRotate/1 |
|
|
|
, calcNextRotate/2 |
|
|
|
, calcNextRotateT/2 |
|
|
|
, validate_trace/1 |
|
|
|
, check_traces/4 |
|
|
|
, is_loggable/3 |
|
|
@ -204,159 +206,157 @@ i3b(Num) -> |
|
|
|
integer_to_binary(Num) |
|
|
|
end. |
|
|
|
|
|
|
|
parseRotateHourSpec([], Res) -> |
|
|
|
{ok, Res}; |
|
|
|
parseRotateHourSpec([$H, M1, M2], Res) -> |
|
|
|
case list_to_integer([M1, M2]) of |
|
|
|
X when X >= 0, X =< 59 -> |
|
|
|
{ok, Res ++ [{minute, X}]}; |
|
|
|
_ -> |
|
|
|
{error, invalid_date_spec} |
|
|
|
end; |
|
|
|
parseRotateHourSpec([$H, M], Res) when M >= $0, M =< $9 -> |
|
|
|
{ok, Res ++ [{minute, M - $0}]}; |
|
|
|
parseRotateHourSpec(_, _) -> |
|
|
|
%% last parse hour |
|
|
|
parseRotateHourSpec([], DayOrMonth, Hour, Minute) -> |
|
|
|
{DayOrMonth, Hour, Minute}; |
|
|
|
parseRotateHourSpec([$H, M1, M2], DayOrMonth, Hour, _Minute) when M1 >= $0, M1 =< $9, M2 >= $0, M2 =< $9 -> |
|
|
|
Min = list_to_integer([M1, M2]), |
|
|
|
?IIF(Hour >= 0 andalso Hour =< 59, {DayOrMonth, Hour, Min}, {error, invalid_date_spec}); |
|
|
|
parseRotateHourSpec([$H, M], DayOrMonth, Hour, _Minute) when M >= $0, M =< $9 -> |
|
|
|
{DayOrMonth, Hour, M - $0}; |
|
|
|
parseRotateHourSpec(_, _DayOrMonth, _Hour, _Minute) -> |
|
|
|
{error, invalid_date_spec}. |
|
|
|
|
|
|
|
%% Default to 00:00:00 rotation |
|
|
|
parseRotateDaySpec([], Res) -> |
|
|
|
{ok, Res ++ [{hour, 0}]}; |
|
|
|
parseRotateDaySpec([$D, D1, D2 | T], Res) -> |
|
|
|
case list_to_integer([D1, D2]) of |
|
|
|
X when X >= 0, X =< 23 -> |
|
|
|
parseRotateHourSpec(T, Res ++ [{hour, X}]); |
|
|
|
_ -> |
|
|
|
{error, invalid_date_spec} |
|
|
|
end; |
|
|
|
parseRotateDaySpec([$D, D | T], Res) when D >= $0, D =< $9 -> |
|
|
|
parseRotateHourSpec(T, Res ++ [{hour, D - $0}]); |
|
|
|
parseRotateDaySpec(X, Res) -> |
|
|
|
parseRotateHourSpec(X, Res). |
|
|
|
|
|
|
|
parseRotateDateSpec([$$, $W, W | T]) when W >= $0, W =< $6 -> |
|
|
|
Week = W - $0, |
|
|
|
parseRotateDaySpec(T, [{day, Week}]); |
|
|
|
parseRotateDateSpec([$$, $M, L | T]) when L == $L; L == $l -> |
|
|
|
%% last day in month. |
|
|
|
parseRotateDaySpec(T, [{date, last}]); |
|
|
|
parseRotateDateSpec([$$, $M, M1, M2 | [$D | _] = T]) -> |
|
|
|
case list_to_integer([M1, M2]) of |
|
|
|
X when X >= 1, X =< 31 -> |
|
|
|
parseRotateDaySpec(T, [{date, X}]); |
|
|
|
_ -> |
|
|
|
{error, invalid_date_spec} |
|
|
|
end; |
|
|
|
parseRotateDateSpec([$$, $M, M | [$D | _] = T]) -> |
|
|
|
parseRotateDaySpec(T, [{date, M - $0}]); |
|
|
|
parseRotateDateSpec([$$, $M, M1, M2]) -> |
|
|
|
case list_to_integer([M1, M2]) of |
|
|
|
X when X >= 1, X =< 31 -> |
|
|
|
{ok, [{date, X}, {hour, 0}]}; |
|
|
|
_ -> |
|
|
|
{error, invalid_date_spec} |
|
|
|
end; |
|
|
|
parseRotateDateSpec([$$, $M, M]) -> |
|
|
|
{ok, [{date, M - $0}, {hour, 0}]}; |
|
|
|
parseRotateDateSpec([$$ | X]) when X /= [] -> |
|
|
|
parseRotateDaySpec(X, []); |
|
|
|
parseRotateDateSpec(_) -> |
|
|
|
%% second parse day Default to 00:00:00 rotation |
|
|
|
parseRotateDaySpec([], DayOrMonth, Hour, Minute) -> |
|
|
|
{DayOrMonth, Hour, Minute}; |
|
|
|
parseRotateDaySpec([$D, D1, D2 | T], DayOrMonth, _Hour, _Minute) when D1 > $0, D1 < $9, D2 > $0, D2 < $9 -> |
|
|
|
Day = list_to_integer([D1, D1]), |
|
|
|
?IIF(Day >= 0 andalso Day =< 23, parseRotateHourSpec(T, DayOrMonth, Day, 0), {error, invalid_date_spec}); |
|
|
|
parseRotateDaySpec([$D, D | T], DayOrMonth, _Hour, _Minute) when D >= $0, D =< $9 -> |
|
|
|
parseRotateHourSpec(T, DayOrMonth, D - $0, 0); |
|
|
|
parseRotateDaySpec(T, DayOrMonth, Hour, Minute) -> |
|
|
|
parseRotateHourSpec(T, DayOrMonth, Hour, Minute). |
|
|
|
|
|
|
|
%% first parse date or week |
|
|
|
parseRotateDateSpec([$$, $W, W | T], _DayOrMonth, _Hour, _Minute) when W >= $1, W =< $7 -> |
|
|
|
parseRotateDaySpec(T, {day, W - $0}, 0, 0); |
|
|
|
parseRotateDateSpec([$$, $M, L | T], _DayOrMonth, _Hour, _Minute) when L == $L; L == $l -> |
|
|
|
parseRotateDaySpec(T, {date, last}, 0, 0); |
|
|
|
parseRotateDateSpec([$$, $M, M1, M2 | T], _DayOrMonth, _Hour, _Minute) when M1 >= $0, M1 =< $9, M2 >= $0, M2 =< $9 -> |
|
|
|
Date = list_to_integer([M1, M2]), |
|
|
|
?IIF(Date >= 1 andalso Date =< 31, parseRotateDaySpec(T, {date, Date}, 0, 0), {error, invalid_date_spec}); |
|
|
|
parseRotateDateSpec([$$, $M, M | T], _DayOrMonth, _Hour, _Minute) when M >= $1, M =< $9 -> |
|
|
|
parseRotateDaySpec(T, {date, M - $0}, 0, 0); |
|
|
|
parseRotateDateSpec([$$ | T], DayOrMonth, Hour, Minute) -> |
|
|
|
parseRotateDaySpec(T, DayOrMonth, Hour, Minute); |
|
|
|
parseRotateDateSpec(_, _DayOrMonth, _Hour, _Minute) -> |
|
|
|
{error, invalid_date_spec}. |
|
|
|
|
|
|
|
parseRotateSpec(Spec) -> |
|
|
|
case parseRotateDateSpec(Spec, undefined, undefined, undefined) of |
|
|
|
{error, _} = ErrRet -> |
|
|
|
ErrRet; |
|
|
|
{undefined, undefined, undefined} -> |
|
|
|
{error, undefined}; |
|
|
|
STuple -> |
|
|
|
{ok, STuple} |
|
|
|
end. |
|
|
|
|
|
|
|
calcNextRotate(Spec) -> |
|
|
|
{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) -> |
|
|
|
{NYear, NMonth, NDay} = NDate, {NHour, NMinute, _} = NTime, |
|
|
|
case OneSpec of |
|
|
|
{minute, SMinute} -> |
|
|
|
case NMinute < SMinute of |
|
|
|
NextTime = calcNextRotate(Spec, Date, Time), |
|
|
|
(rumTime:lDateTimeToSec(NextTime) - rumTime:lDateTimeToSec(NowDataTime)) * 1000. |
|
|
|
calcNextRotate(Spec, NowDataTime) -> |
|
|
|
{Date, Time} = NowDataTime, |
|
|
|
NextTime = calcNextRotate(Spec, Date, Time), |
|
|
|
(rumTime:lDateTimeToSec(NextTime) - rumTime:lDateTimeToSec(NowDataTime)) * 1000. |
|
|
|
|
|
|
|
calcNextRotateT(Spec, NowDataTime) -> |
|
|
|
{Date, Time} = NowDataTime, |
|
|
|
calcNextRotate(Spec, Date, Time). |
|
|
|
|
|
|
|
calcNextRotate(Spec, CurDate, CurTime) -> |
|
|
|
{CurYear, CurMonth, CurDay} = CurDate, {CurHour, CurMinute, _} = CurTime, |
|
|
|
case Spec of |
|
|
|
{undefined, undefined, SMinute} -> |
|
|
|
case CurMinute < SMinute of |
|
|
|
true -> |
|
|
|
%% rotation is this hour |
|
|
|
calcNextRotate(Spec, NDate, {NHour, SMinute, 0}, NowDataTime); |
|
|
|
{CurDate, {CurHour, SMinute, 0}}; |
|
|
|
_ -> |
|
|
|
%% rotation is next hour |
|
|
|
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + 3600, |
|
|
|
{NNDate, NNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
{NewHour, _, _} = NNTime, |
|
|
|
calcNextRotate(Spec, NNDate, {NewHour, SMinute, 0}, NowDataTime) |
|
|
|
NexSec = rumTime:lDateTimeToSec({CurDate, {CurHour, SMinute, 0}}) + 3600, |
|
|
|
rumTime:secToLDateTime(NexSec) |
|
|
|
end; |
|
|
|
{hour, SHour} -> |
|
|
|
case NHour < SHour of |
|
|
|
{undefined, SHour, SMinute} -> |
|
|
|
case CurTime < {SHour, SMinute, 0} of |
|
|
|
true -> |
|
|
|
%% rotation is today, sometime |
|
|
|
calcNextRotate(Spec, NDate, {SHour, NMinute, 0}, NowDataTime); |
|
|
|
%% rotation is this day |
|
|
|
{CurDate, {SHour, SMinute, 0}}; |
|
|
|
_ -> |
|
|
|
%% rotation is not today |
|
|
|
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + 86400, |
|
|
|
{NNDate, _NNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
calcNextRotate(Spec, NNDate, {SHour, NMinute, 0}, NowDataTime) |
|
|
|
%% rotation is next day |
|
|
|
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + 86400, |
|
|
|
rumTime:secToLDateTime(NexSec) |
|
|
|
end; |
|
|
|
{day, SDay} -> |
|
|
|
CurDay = rumTime:weekDay(NDate), |
|
|
|
AdjustedDay = ?IIF(SDay == 0, 7, SDay), |
|
|
|
{{day, SDay}, SHour, SMinute} -> |
|
|
|
CurDay = rumTime:weekDay(CurDate), |
|
|
|
if |
|
|
|
AdjustedDay > CurDay -> |
|
|
|
PlusDays = AdjustedDay - CurDay, |
|
|
|
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + (86400 * PlusDays), |
|
|
|
{NewNDate, NewNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
calcNextRotate(Spec, NewNDate, NewNTime, NowDataTime); |
|
|
|
AdjustedDay < CurDay -> |
|
|
|
PlusDays = ((7 - CurDay) + AdjustedDay), |
|
|
|
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + (86400 * PlusDays), |
|
|
|
{NewNDate, NewNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
calcNextRotate(Spec, NewNDate, NewNTime, NowDataTime); |
|
|
|
CurDay < SDay -> |
|
|
|
%% rotation is this week |
|
|
|
DiffDays = SDay - CurDay, |
|
|
|
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + (86400 * DiffDays), |
|
|
|
rumTime:secToLDateTime(NexSec); |
|
|
|
CurDay > SDay -> |
|
|
|
%% rotation is next week |
|
|
|
DiffDays = ((7 - CurDay) + SDay), |
|
|
|
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + (86400 * DiffDays), |
|
|
|
rumTime:secToLDateTime(NexSec); |
|
|
|
true -> |
|
|
|
case {NDate, NTime} > NowDataTime of |
|
|
|
case CurTime < {SHour, SMinute, 0} of |
|
|
|
true -> |
|
|
|
calcNextRotate(Spec, NDate, NTime, NowDataTime); |
|
|
|
%% rotation is this week |
|
|
|
{CurDate, {SHour, SMinute, 0}}; |
|
|
|
_ -> |
|
|
|
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + (86400 * 7), |
|
|
|
{NewNDate, NewNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
calcNextRotate(Spec, NewNDate, NewNTime, NowDataTime) |
|
|
|
%% rotation is next week |
|
|
|
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + (86400 * 7), |
|
|
|
rumTime:secToLDateTime(NexSec) |
|
|
|
end |
|
|
|
end; |
|
|
|
{date, last} -> |
|
|
|
LastDay = rumTime:monthDay(NYear, NMonth), |
|
|
|
case LastDay == NDay of |
|
|
|
{{date, last}, SHour, SMinute} -> |
|
|
|
CurMonthDay = rumTime:monthDay(CurYear, CurMonth), |
|
|
|
case CurMonthDay == CurDay of |
|
|
|
true -> |
|
|
|
case {NDate, NTime} > NowDataTime of |
|
|
|
case CurTime < {SHour, SMinute, 0} of |
|
|
|
true -> |
|
|
|
calcNextRotate(Spec, NDate, NTime, NowDataTime); |
|
|
|
%% rotation is this last month day |
|
|
|
{CurDate, {SHour, SMinute, 0}}; |
|
|
|
_ -> |
|
|
|
NexSec = rumTime:lDateTimeToSec({NDate, {23, 59, 59}}) + 1, %% 下个月1号凌晨 |
|
|
|
%% rotation is next last month day |
|
|
|
NexSec = rumTime:lDateTimeToSec({CurDate, {23, 59, 59}}) + 1, %% 下个月1号凌晨 |
|
|
|
{NewNDate, _NewNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
{NewNYear, NewNMonth, _} = NewNDate, |
|
|
|
NewNDay = rumTime:monthDay(NewNYear, NewNMonth), |
|
|
|
calcNextRotate(Spec, {NewNYear, NewNMonth, NewNDay}, NTime, NowDataTime) |
|
|
|
NewMonthDay = rumTime:monthDay(NewNYear, NewNMonth), |
|
|
|
{{NewNYear, NewNMonth, NewMonthDay}, {SHour, SMinute, 0}} |
|
|
|
end; |
|
|
|
_ -> |
|
|
|
calcNextRotate(Spec, {NYear, NMonth, LastDay}, NTime, NowDataTime) |
|
|
|
%% rotation is this last month day |
|
|
|
{{CurYear, CurMonth, CurMonthDay}, {SHour, SMinute, 0}} |
|
|
|
end; |
|
|
|
{date, SDate} -> |
|
|
|
{{date, SDate}, SHour, SMinute} -> |
|
|
|
if |
|
|
|
SDate < NDay -> |
|
|
|
LastDay = rumTime:monthDay(NYear, NMonth), |
|
|
|
NexSec = calendar:lDateTimeToSec({{NYear, NMonth, LastDay}, {23, 59, 59}}) + 1, |
|
|
|
CurDay < SDate -> |
|
|
|
%% rotation is this month day |
|
|
|
{{CurYear, CurMonth, SDate}, {SHour, SMinute, 0}}; |
|
|
|
CurDay > SDate -> |
|
|
|
%% rotation is next month day |
|
|
|
LastDay = rumTime:monthDay(CurYear, CurMonth), |
|
|
|
NexSec = rumTime:lDateTimeToSec({{CurYear, CurMonth, LastDay}, {23, 59, 59}}) + 1, |
|
|
|
{NewNDate, _NewNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
{NewNYear, NewNMonth, _} = NewNDate, |
|
|
|
calcNextRotate(Spec, {NewNYear, NewNMonth, SDate}, NTime, NowDataTime); |
|
|
|
SDate > NDay -> |
|
|
|
calcNextRotate(Spec, {NYear, NMonth, SDate}, NTime, NowDataTime); |
|
|
|
{{NewNYear, NewNMonth, SDate}, {SHour, SMinute, 0}}; |
|
|
|
true -> |
|
|
|
case {NDate, NTime} > NowDataTime of |
|
|
|
case CurTime < {SHour, SMinute, 0} of |
|
|
|
true -> |
|
|
|
calcNextRotate(Spec, NDate, NTime, NowDataTime); |
|
|
|
%% rotation is this month day |
|
|
|
{CurDate, {SHour, SMinute, 0}}; |
|
|
|
_ -> |
|
|
|
LastDay = rumTime:monthDay(NYear, NMonth), |
|
|
|
NexSec = calendar:lDateTimeToSec({{NYear, NMonth, LastDay}, {23, 59, 59}}) + 1, |
|
|
|
%% rotation is next month day |
|
|
|
LastDay = rumTime:monthDay(CurYear, CurMonth), |
|
|
|
NexSec = rumTime:lDateTimeToSec({{CurYear, CurMonth, LastDay}, {23, 59, 59}}) + 1, |
|
|
|
{NewNDate, _NewNTime} = rumTime:secToLDateTime(NexSec), |
|
|
|
{NewNYear, NewNMonth, _} = NewNDate, |
|
|
|
calcNextRotate(Spec, {NewNYear, NewNMonth, SDate}, NTime, NowDataTime) |
|
|
|
{{NewNYear, NewNMonth, SDate}, {SHour, SMinute, 0}} |
|
|
|
end |
|
|
|
end |
|
|
|
end. |
|
|
|