Packages

otpbp

7.6.2
7.18.0 7.16.1 7.15.0 7.14.0 7.13.0 7.12.0 7.11.0 7.10.2 7.9.3 7.9.2 7.9.1 7.8.7 7.8.6 7.8.5 7.8.4 7.7.1 7.7.0 7.6.3 7.6.2 7.6.1 7.6.0 7.5.1 7.5.0 7.4.0 7.3.4 7.3.3 7.3.2 7.3.1 7.3.0 7.2.0 7.1.1 7.1.0 7.0.4 7.0.3 7.0.2 7.0.1 6.26.0 6.25.0 6.24.0 6.23.0 6.22.2 6.21.3 6.21.2 6.21.1 6.20.2 6.19.1 6.19.0 6.18.3 6.18.2 6.18.1 6.18.0 6.17.1 6.17.0 6.16.0 6.15.4 6.15.3 6.15.2 6.15.1 6.15.0 6.14.0 6.13.5 6.13.4 6.13.3 6.13.2 6.13.1 6.13.0 6.12.0 6.11.2 6.11.1 6.10.0 6.9.2 6.9.1 6.9.0 6.8.0 6.7.1 6.7.0 6.6.4 6.6.3 6.6.2 6.6.1 6.6.0 6.5.0 6.4.0 6.3.3 6.3.2 6.3.1 6.3.0 6.2.1 6.2.0 6.1.0 6.0.0 5.39.0 5.38.0 5.37.0 5.36.0 5.35.2 5.34.3 5.34.2 5.34.1 5.33.1 5.32.1 5.32.0 5.31.3 5.31.2 5.31.1 5.31.0 5.30.1 5.30.0 5.29.0 5.28.4 5.28.3 5.28.2 5.28.1 5.28.0 5.27.0 5.26.5 5.26.4 5.26.3 5.26.2 5.26.1 5.26.0 5.25.0 5.24.2 5.24.1 5.23.0 5.22.2 5.22.1 5.22.0 5.21.0 5.20.1 5.20.0 5.19.4 5.19.3 5.19.2 5.19.1 5.19.0 5.18.0 5.17.0 5.16.3 5.16.1 5.16.0 5.15.1 5.15.0 5.14.0 5.13.1 5.13.0 5.12.1 5.12.0 5.11.2 5.11.1 5.10.1 5.10.0 5.9.0 5.8.0 5.7.0 5.6.1 5.6.0 5.5.0 5.4.2 5.4.1 5.4.0 5.3.0 5.2.2 5.2.1 5.2.0 5.1.2 5.1.1 4.79.0 4.78.0 4.77.0 4.76.2 4.75.3 4.75.2 4.75.1 4.74.0 4.73.1 4.73.0 4.72.3 4.72.2 4.72.1 4.72.0 4.71.1 4.71.0 4.70.0 4.69.4 4.69.3 4.69.2 4.69.1 4.69.0 4.68.0 4.67.5 4.67.4 4.67.3 4.67.2 4.67.0 4.66.0 4.65.2 4.65.1 4.64.0 4.63.2 4.63.1 4.63.0 4.62.0 4.61.1 4.61.0 4.60.4 4.60.3 4.60.2 4.60.1 4.60.0 4.59.0 4.58.0 4.57.2 4.57.1 4.57.0 4.56.2 4.56.1 4.56.0 4.55.0 4.54.0 4.53.0 4.52.1 4.52.0 4.51.3 4.51.2 4.51.0 4.50.0 4.49.3 4.49.2

OTP backports

Current section

Files

Jump to
otpbp src otpbp_calendar.erl
Raw

src/otpbp_calendar.erl

-module(otpbp_calendar).
-compile([{parse_transform, otpbp_pt}]).
-ifndef(HAVE_calendar__system_time_to_universal_time_2).
% OTP 21.0
-export([system_time_to_universal_time/2]).
-endif.
-ifndef(HAVE_calendar__system_time_to_local_time_2).
% OTP 21.0
-export([system_time_to_local_time/2]).
-endif.
-ifndef(HAVE_calendar__system_time_to_rfc3339_1).
% OTP 21.0
-export([system_time_to_rfc3339/1]).
-endif.
-ifndef(HAVE_calendar__system_time_to_rfc3339_2).
% OTP 21.0
-export([system_time_to_rfc3339/2]).
-endif.
-ifndef(HAVE_calendar__rfc3339_to_system_time_1).
% OTP 21.0
-export([rfc3339_to_system_time/1]).
-endif.
-ifndef(HAVE_calendar__rfc3339_to_system_time_2).
% OTP 21.0
-export([rfc3339_to_system_time/2]).
-endif.
-ifndef(HAVE_calendar__system_time_to_local_time_2).
-ifdef(HAVE_calendar__system_time_to_universal_time_2).
-import(calendar, [system_time_to_universal_time/2]).
-endif.
-endif.
-define(SECONDS_PER_DAY, 86400).
-define(DAYS_FROM_0_TO_1970, 719528).
-define(SECONDS_FROM_0_TO_1970, (?DAYS_FROM_0_TO_1970 * ?SECONDS_PER_DAY)).
-define(DAYS_FROM_0_TO_10000, 2932897).
-define(SECONDS_FROM_0_TO_10000, (?DAYS_FROM_0_TO_10000 * ?SECONDS_PER_DAY)).
-ifndef(HAVE_calendar__system_time_to_universal_time_2).
system_time_to_universal_time(Time, Unit) ->
calendar:gregorian_seconds_to_datetime(erlang:convert_time_unit(Time, Unit, second) + ?SECONDS_FROM_0_TO_1970).
-endif.
-ifndef(HAVE_calendar__system_time_to_local_time_2).
system_time_to_local_time(Time, Unit) -> erlang:universaltime_to_localtime(system_time_to_universal_time(Time, Unit)).
-endif.
-ifndef(HAVE_calendar__system_time_to_rfc3339_1).
-ifndef(HAVE_calendar__system_time_to_rfc3339_2).
system_time_to_rfc3339(Time) -> calendar:system_time_to_rfc3339(Time, []).
-else.
system_time_to_rfc3339(Time) ->
AdjustmentSecs = local_offset(Time, second),
case Time + AdjustmentSecs of
Secs when Secs >= -?SECONDS_FROM_0_TO_1970, Secs < ?SECONDS_FROM_0_TO_10000 ->
{{Year, Month, Day}, {Hour, Min, Sec}} = system_time_to_datetime(Secs),
lists:flatten(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0B~s",
[Year, Month, Day, Hour, Min, Sec, offset(AdjustmentSecs)]));
_ -> error({badarg, [Time]})
end.
-ifndef(NEED_local_offset_2).
-define(NEED_local_offset_2, true).
-endif.
-ifndef(NEED_system_time_to_datetime_1).
-define(NEED_system_time_to_datetime_1, true).
-endif.
-endif.
-endif.
-ifndef(HAVE_calendar__system_time_to_rfc3339_2).
system_time_to_rfc3339(Time, Options) ->
case proplists:get_value(unit, Options, second) of
native ->
system_time_to_rfc3339(erlang:convert_time_unit(Time, native, millisecond), Options, millisecond,
case proplists:get_value(offset, Options, "") of
OffsetOpt when is_integer(OffsetOpt) ->
erlang:convert_time_unit(OffsetOpt, native, millisecond);
OffsetOpt -> OffsetOpt
end);
Unit -> system_time_to_rfc3339(Time, Options, Unit, proplists:get_value(offset, Options, ""))
end.
system_time_to_rfc3339(Time, Options, Unit, OffsetOption) ->
AdjustmentSecs = offset_adjustment(Time, Unit, OffsetOption),
AdjustedTime = Time + erlang:convert_time_unit(AdjustmentSecs, second, Unit),
Factor = factor(Unit),
case AdjustedTime div Factor of
Secs when Secs >= -?SECONDS_FROM_0_TO_1970, Secs < ?SECONDS_FROM_0_TO_10000 ->
{{Year, Month, Day}, {Hour, Min, Sec}} = system_time_to_datetime(Secs),
L = io_lib:format("~4..0B-~2..0B-~2..0B~c~2..0B:~2..0B:~2..0B~s~s",
[Year, Month, Day, proplists:get_value(time_designator, Options, $T),
Hour, Min, Sec, fraction_str(Factor, AdjustedTime),
offset(OffsetOption, AdjustmentSecs)]),
case proplists:get_value(return, Options, string) of
string -> lists:flatten(L);
binary -> list_to_binary(L)
end;
_ -> error({badarg, [Time, Options]})
end.
-ifndef(NEED_factor_1).
-define(NEED_factor_1, true).
-endif.
-ifndef(NEED_system_time_to_datetime_1).
-define(NEED_system_time_to_datetime_1, true).
-endif.
offset_adjustment(Time, Unit, "") -> local_offset(Time, Unit);
offset_adjustment(_Time, _Unit, OffsetString) when is_list(OffsetString) ->
offset_string_adjustment(OffsetString);
offset_adjustment(_Time, Unit, Offset) when is_integer(Offset) -> erlang:convert_time_unit(Offset, Unit, second).
-ifndef(NEED_local_offset_2).
-define(NEED_local_offset_2, true).
-endif.
offset(OffsetOption, Secs) when OffsetOption =:= ""; is_integer(OffsetOption) -> offset(Secs);
offset(OffsetOption, _Secs) -> OffsetOption.
offset(Secs) when Secs < 0 -> offset_(-Secs, $-);
offset(Secs) -> offset_(Secs, $+).
offset_(Secs, Sign) -> io_lib:format("~c~2..0B:~2..0B", [Sign, Secs div 3600, (Secs rem 3600) div 60]).
-ifndef(NEED_offset_string_adjustment_1).
-define(NEED_offset_string_adjustment_1, true).
-endif.
fraction_str(1, _Time) -> "";
fraction_str(Factor, Time) -> io_lib:format(".~*..0B", [byte_size(integer_to_binary(Factor)) - 1, abs(Time rem Factor)]).
-endif.
-ifndef(HAVE_calendar__rfc3339_to_system_time_1).
-ifdef(HAVE_calendar__rfc3339_to_system_time_2).
rfc3339_to_system_time(DateTimeString) -> calendar:rfc3339_to_system_time(DateTimeString, []).
-else.
rfc3339_to_system_time(DateTimeString) ->
{Time, TimeStr} = datetime_str(DateTimeString),
case Time - offset_string_adjustment(lists:dropwhile(fun is_fraction_char/1, TimeStr)) of
Secs when Secs >= -?SECONDS_FROM_0_TO_1970, Secs < ?SECONDS_FROM_0_TO_10000 -> Secs;
_ -> error({badarg, [Time]})
end.
-ifndef(NEED_datetime_str_1).
-define(NEED_datetime_str_1, true).
-endif.
-ifndef(NEED_offset_string_adjustment_1).
-define(NEED_offset_string_adjustment_1, true).
-endif.
-ifndef(NEED_is_fraction_char_1).
-define(NEED_is_fraction_char_1, true).
-endif.
-endif.
-endif.
-ifndef(HAVE_calendar__rfc3339_to_system_time_2).
rfc3339_to_system_time(DateTimeString, Options) ->
{Time, TimeStr} = datetime_str(DateTimeString),
{FractionStr, UtcOffset} = lists:splitwith(fun is_fraction_char/1, TimeStr),
case Time - offset_string_adjustment(UtcOffset) of
Secs when Secs >= -?SECONDS_FROM_0_TO_1970, Secs < ?SECONDS_FROM_0_TO_10000 ->
Unit = proplists:get_value(unit, Options, second),
ScaledEpoch = erlang:convert_time_unit(Secs, second, Unit),
ScaledEpoch + copy_sign(fraction(Unit, FractionStr), ScaledEpoch);
_ -> error({badarg, [DateTimeString, Options]})
end.
-ifndef(NEED_datetime_str_1).
-define(NEED_datetime_str_1, true).
-endif.
-ifndef(NEED_offset_string_adjustment_1).
-define(NEED_offset_string_adjustment_1, true).
-endif.
-ifndef(NEED_is_fraction_char_1).
-define(NEED_is_fraction_char_1, true).
-endif.
copy_sign(N1, N2) when N2 < 0 -> -N1;
copy_sign(N1, _N2) -> N1.
fraction(Unit, FractionStr) when Unit =:= second; FractionStr =:= "" -> 0;
fraction(Unit, FractionStr) -> round(factor(Unit) * list_to_float([$0|FractionStr])).
-ifndef(NEED_factor_1).
-define(NEED_factor_1, true).
-endif.
-endif.
-ifdef(NEED_local_offset_2).
local_offset(SystemTime, Unit) ->
UniversalTime = calendar:system_time_to_universal_time(SystemTime, Unit),
calendar:datetime_to_gregorian_seconds(erlang:universaltime_to_localtime(UniversalTime)) -
calendar:datetime_to_gregorian_seconds(UniversalTime).
-endif.
-ifdef(NEED_factor_1).
factor(Unit) -> erlang:convert_time_unit(1, second, Unit).
-endif.
-ifdef(NEED_offset_string_adjustment_1).
offset_string_adjustment(TZ) when TZ =:= "Z"; TZ =:= "z" -> 0;
offset_string_adjustment([Sign, H1, H2]) -> offset_string_adjustment(Sign, H1, H2, 0);
offset_string_adjustment([Sign, H1, H2, $:, M1, M2]) ->
offset_string_adjustment(Sign, H1, H2, list_to_integer([M1, M2]) * 60);
offset_string_adjustment(TZ) -> error({badmatch, TZ}).
offset_string_adjustment(Sign, H1, H2, Min) -> offset_string_adjustment(Sign, 3600 * list_to_integer([H1, H2]) + Min).
offset_string_adjustment($-, Adjustment) -> -Adjustment;
offset_string_adjustment($+, Adjustment) -> Adjustment.
-endif.
-ifdef(NEED_datetime_str_1).
datetime_str([Y1, Y2, Y3, Y4, $-, Mon1, Mon2, $-, D1, D2, _T, H1, H2, $:, Min1, Min2, $:, S1, S2|TimeStr]) ->
{datetime_str(Y1, Y2, Y3, Y4, Mon1, Mon2, D1, D2, H1, H2, Min1, Min2, S1, S2), TimeStr};
datetime_str(<<Y1, Y2, Y3, Y4, $-, Mon1, Mon2, $-, D1, D2, _T, H1, H2, $:, Min1, Min2, $:, S1, S2, TimeStr/binary>>) ->
{datetime_str(Y1, Y2, Y3, Y4, Mon1, Mon2, D1, D2, H1, H2, Min1, Min2, S1, S2), binary_to_list(TimeStr)}.
datetime_str(Y1, Y2, Y3, Y4, Mon1, Mon2, D1, D2, H1, H2, Min1, Min2, S1, S2) ->
calendar:datetime_to_gregorian_seconds({{(Y1 - $0) * 1000 + (Y2 - $0) * 100 + (Y3 - $0) * 10 + (Y4 - $0),
(Mon1 - $0) * 10 + (Mon2 - $0),
(D1 - $0) * 10 + (D2 - $0)},
{(H1 - $0) * 10 + (H2 - $0),
(Min1 - $0) * 10 + (Min2 - $0),
(S1 - $0) * 10 + (S2 - $0)}}) - ?SECONDS_FROM_0_TO_1970.
-endif.
-ifdef(NEED_is_fraction_char_1).
is_fraction_char(C) -> C =:= $. orelse C >= $0 andalso C =< $9.
-endif.
-ifdef(NEED_system_time_to_datetime_1).
system_time_to_datetime(Seconds) -> calendar:gregorian_seconds_to_datetime(Seconds + ?SECONDS_FROM_0_TO_1970).
-endif.