From 0237766de898145bbd55bb26eef8917535f341ca Mon Sep 17 00:00:00 2001 From: Javier Garea Date: Sat, 27 May 2023 10:22:20 +0200 Subject: [PATCH] feat: docs and utils (#21) --- .gitignore | 1 + rebar.config | 9 ++- src/ncalendar.app.src | 2 +- src/ncalendar.erl | 106 ++++++++++++++++++++++++++++------ src/ncalendar.hrl | 61 ------------------- src/ncalendar_format.erl | 28 +++++++-- src/ncalendar_http_date.erl | 12 +++- src/ncalendar_imf_fixdate.erl | 12 +++- src/ncalendar_iso8601.erl | 45 +++++++++++---- src/ncalendar_util.erl | 43 +++++++------- 10 files changed, 198 insertions(+), 121 deletions(-) diff --git a/.gitignore b/.gitignore index 1ebdb70..78774d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ _build/ _checkouts/ _gen/ +doc/ *.dump *.crashdump diff --git a/rebar.config b/rebar.config index f6c6d0a..726684e 100644 --- a/rebar.config +++ b/rebar.config @@ -3,7 +3,8 @@ ]}. {project_plugins, [ - {erlfmt, {git, "git@github.com:WhatsApp/erlfmt.git", {branch, "main"}}} + {erlfmt, {git, "git@github.com:WhatsApp/erlfmt.git", {branch, "main"}}}, + rebar3_ex_doc ]}. {erlfmt, [write]}. @@ -25,6 +26,9 @@ ]}, {test, [ {ct, "--spec test/conf/test.spec --cover --readable true"} + ]}, + {doc, [ + {ex_doc, "-f html"} ]} ]}. @@ -32,5 +36,6 @@ {cover_enabled, true}. {xref_ignores, [ - ncalendar + ncalendar, + {ncalendar_iso8601, format_timezone, 1} ]}. diff --git a/src/ncalendar.app.src b/src/ncalendar.app.src index 2988e08..ab457f7 100644 --- a/src/ncalendar.app.src +++ b/src/ncalendar.app.src @@ -1,7 +1,7 @@ {application, ncalendar, [ {description, "An OTP library for the manipulation of a binary representation of dates and times"}, - {vsn, "0.2.0"}, + {vsn, "0.3.0"}, {registered, []}, {applications, [kernel, stdlib]}, {env, []} diff --git a/src/ncalendar.erl b/src/ncalendar.erl index a66afd3..b3fd96d 100644 --- a/src/ncalendar.erl +++ b/src/ncalendar.erl @@ -48,12 +48,61 @@ ]). %%% TYPES +-type format() :: iso8601 | http_date | imf_fixdate. +-type gregorian_seconds() :: non_neg_integer(). +-type opts() :: map(). +-type timezone() :: + undefined + | -1200 + | -1100 + | -1000 + | -0930 + | -0900 + | -0800 + | -0700 + | -0600 + | -0500 + | -0430 + | -0400 + | -0330 + | -0300 + | -0230 + | -0200 + | -0100 + | +0000 + | +0100 + | +0200 + | +0300 + | +0330 + | +0400 + | +0430 + | +0500 + | +0530 + | +0545 + | +0600 + | +0630 + | +0700 + | +0730 + | +0800 + | +0900 + | +0930 + | +1000 + | +1030 + | +1100 + | +1130 + | +1200 + | +1245 + | +1300 + | +1345 + | +1400. +-type timezone_alias() :: binary(). +-type posix_time() :: non_neg_integer(). +-type value() :: binary(). + +%%% EXPORT TYPES -export_type([ - datetime/0, format/0, - gregorian_seconds/0, opts/0, - timestamp/0, timezone/0, timezone_alias/0, value/0 @@ -70,6 +119,7 @@ To :: format(), Value :: value(), Result :: value(). +%% @equiv convert(From, To, Value, #{}) convert(From, To, Value) -> convert(From, To, Value, #{}). @@ -79,6 +129,7 @@ convert(From, To, Value) -> Value :: value(), Opts :: opts(), Result :: value(). +%% @doc Converts a binary representation of a datetime from one format to another. convert(From, To, Value, Opts) -> ModFrom = mod(From), ModTo = mod(To), @@ -86,16 +137,18 @@ convert(From, To, Value, Opts) -> -spec from_datetime(Format, Datetime) -> Result when Format :: format(), - Datetime :: datetime(), + Datetime :: calendar:datetime(), Result :: value(). +%% @equiv from_datetime(Format, Datetime, #{}) from_datetime(Format, Datetime) -> from_datetime(Format, Datetime, #{}). -spec from_datetime(Format, Datetime, Opts) -> Result when Format :: format(), - Datetime :: datetime(), + Datetime :: calendar:datetime(), Opts :: opts(), Result :: value(). +%% @doc Converts a calendar:datetime() value to a binary representation in the specified format. from_datetime(Format, Datetime, Opts) -> Datetimezone = {Datetime, {millisecond, 0}, +0000}, Mod = mod(Format), @@ -105,6 +158,7 @@ from_datetime(Format, Datetime, Opts) -> Format :: format(), GregorianSeconds :: gregorian_seconds(), Result :: value(). +%% @equiv from_gregorian_seconds(Format, GregorianSeconds, #{}) from_gregorian_seconds(Format, GregorianSeconds) -> from_gregorian_seconds(Format, GregorianSeconds, #{}). @@ -113,40 +167,45 @@ from_gregorian_seconds(Format, GregorianSeconds) -> GregorianSeconds :: gregorian_seconds(), Opts :: opts(), Result :: value(). +%% @doc Converts the given amout of gregorian seconds to a binary representation in the specified format. from_gregorian_seconds(Format, GregorianSeconds, Opts) -> Datetime = calendar:gregorian_seconds_to_datetime(GregorianSeconds), Datetimezone = {Datetime, {millisecond, 0}, +0000}, Mod = mod(Format), Mod:from_datetimezone(Datetimezone, Opts). --spec from_posix_time(Format, UnixTime) -> Result when +-spec from_posix_time(Format, PosixTime) -> Result when Format :: format(), - UnixTime :: posix_time(), + PosixTime :: posix_time(), Result :: value(). -from_posix_time(Format, UnixTime) -> - from_posix_time(Format, UnixTime, #{}). +%% @equiv from_posix_time(Format, PosixTime, #{}) +from_posix_time(Format, PosixTime) -> + from_posix_time(Format, PosixTime, #{}). --spec from_posix_time(Format, UnixTime, Opts) -> Result when +-spec from_posix_time(Format, PosixTime, Opts) -> Result when Format :: format(), - UnixTime :: posix_time(), + PosixTime :: posix_time(), Opts :: opts(), Result :: value(). -from_posix_time(Format, UnixTime, Opts) -> - GregorianSeconds = UnixTime + ?EPOCH_DELTA, +%% @doc Converts the given POSIX time (in seconds) to a binary representation in the specified format. +from_posix_time(Format, PosixTime, Opts) -> + GregorianSeconds = PosixTime + ?EPOCH_DELTA, from_gregorian_seconds(Format, GregorianSeconds, Opts). -spec from_timestamp(Format, Timestamp) -> Result when Format :: format(), - Timestamp :: timestamp(), + Timestamp :: erlang:timestamp(), Result :: value(). +%% @equiv from_timestamp(Format, Timestamp, #{}) from_timestamp(Format, Timestamp) -> from_timestamp(Format, Timestamp, #{}). -spec from_timestamp(Format, Timestamp, Opts) -> Result when Format :: format(), - Timestamp :: timestamp(), + Timestamp :: erlang:timestamp(), Opts :: opts(), Result :: value(). +%% @doc Converts the given erlang:timestamp value to a binary representation in the specified format. from_timestamp(Format, Timestamp, Opts) -> Milliseconds = ncalendar_util:timestamp_to_milliseconds(Timestamp), Datetimezone = ncalendar_util:milliseconds_to_datetimezone(Milliseconds, +0000), @@ -157,6 +216,7 @@ from_timestamp(Format, Timestamp, Opts) -> Format :: format(), Value :: value(), Result :: boolean(). +%% @equiv is_valid(Format, Value, #{}) is_valid(Format, Value) -> is_valid(Format, Value, #{}). @@ -165,6 +225,7 @@ is_valid(Format, Value) -> Value :: value(), Opts :: opts(), Result :: boolean(). +%% @doc Checks if the given binary representation of a datetime is valid for the specified format. is_valid(Format, Value, Opts) -> Mod = mod(Format), Mod:is_valid(Value, Opts). @@ -172,6 +233,7 @@ is_valid(Format, Value, Opts) -> -spec now(Format) -> Result when Format :: format(), Result :: value(). +%% @equiv now(Format, +0000) now(Format) -> now(Format, +0000). @@ -179,6 +241,7 @@ now(Format) -> Format :: format(), Timezone :: timezone(), Result :: value(). +%% @equiv now(Format, Timezone, #{}) now(Format, Timezone) -> now(Format, Timezone, #{}). @@ -187,6 +250,7 @@ now(Format, Timezone) -> Timezone :: timezone(), Opts :: opts(), Result :: value(). +%% @doc Returns the current datetime in the specified format. now(Format, Timezone, Opts) -> Datetimezone = ncalendar_util:milliseconds_to_datetimezone( erlang:system_time(millisecond) + (?EPOCH_DELTA * 1000), @@ -199,6 +263,7 @@ now(Format, Timezone, Opts) -> Format :: format(), Value :: value(), Result :: timezone(). +%% @doc Returns the timezone of the given binary representation of a datetime. timezone(Format, Value) -> Mod = mod(Format), {_Date, _Time, Timezone} = Mod:to_datetimezone(Value), @@ -207,7 +272,8 @@ timezone(Format, Value) -> -spec to_datetime(Format, Value) -> Result when Format :: format(), Value :: value(), - Result :: datetime(). + Result :: calendar:datetime(). +%% @doc Converts the given binary representation of a datetime to a calendar:datetime() value. to_datetime(Format, Value) -> Mod = mod(Format), Datetimezone = Mod:to_datetimezone(Value), @@ -217,6 +283,7 @@ to_datetime(Format, Value) -> Format :: format(), Value :: value(), Result :: gregorian_seconds(). +%% @doc Converts the given binary representation of a datetime to gregorian seconds. to_gregorian_seconds(Format, Value) -> Mod = mod(Format), Datetimezone = Mod:to_datetimezone(Value), @@ -226,6 +293,7 @@ to_gregorian_seconds(Format, Value) -> Format :: format(), Value :: value(), Result :: posix_time(). +%% @doc Converts the given binary representation of a datetime to POSIX time (in seconds). to_posix_time(Format, Value) -> GregorianSeconds = to_gregorian_seconds(Format, Value), GregorianSeconds - ?EPOCH_DELTA. @@ -233,7 +301,8 @@ to_posix_time(Format, Value) -> -spec to_timestamp(Format, Value) -> Result when Format :: format(), Value :: value(), - Result :: timestamp(). + Result :: erlang:timestamp(). +%% @doc Converts the given binary representation of a datetime to a erlang:timestamp() value. to_timestamp(Format, Value) -> Mod = mod(Format), Datetimezone = Mod:to_datetimezone(Value), @@ -244,6 +313,7 @@ to_timestamp(Format, Value) -> %%%----------------------------------------------------------------------------- -spec timezones() -> Result when Result :: [timezone()]. +%% @doc Returns a list of all supported timezones. timezones() -> ?TIMEZONES. @@ -252,6 +322,7 @@ timezones() -> Value :: value(), Timezone :: timezone(), Result :: value(). +%% @equiv shift_timezone(Format, Value, Timezone, #{}) shift_timezone(Format, Value, Timezone) -> shift_timezone(Format, Value, Timezone, #{}). @@ -261,6 +332,7 @@ shift_timezone(Format, Value, Timezone) -> Timezone :: timezone(), Opts :: opts(), Result :: value(). +%% @doc Shifts the timezone of the given binary representation of a datetime to the specified timezone. shift_timezone(Format, Value, Timezone, Opts) -> case timezone(Format, Value) of undefined -> diff --git a/src/ncalendar.hrl b/src/ncalendar.hrl index c5c18da..9120229 100644 --- a/src/ncalendar.hrl +++ b/src/ncalendar.hrl @@ -14,67 +14,6 @@ -ifndef(ncalendar). -define(ncalendar, true). -%%% TYPES --type date() :: calendar:date(). --type datetime() :: calendar:datetime(). -% Datetime is: UTC time | local time (if no timezone is specified). --type datetimezone() :: {datetime(), sub_seconds(), timezone() | timezone_alias()}. --type format() :: iso8601. --type gregorian_seconds() :: non_neg_integer(). --type milliseconds() :: non_neg_integer(). --type opts() :: map(). --type sub_seconds() :: {millisecond, milliseconds()}. --type time() :: calendar:time(). --type timestamp() :: erlang:timestamp(). --type timezone() :: - undefined - | -1200 - | -1100 - | -1000 - | -0930 - | -0900 - | -0800 - | -0700 - | -0600 - | -0500 - | -0430 - | -0400 - | -0330 - | -0300 - | -0230 - | -0200 - | -0100 - | +0000 - | +0100 - | +0200 - | +0300 - | +0330 - | +0400 - | +0430 - | +0500 - | +0530 - | +0545 - | +0600 - | +0630 - | +0700 - | +0730 - | +0800 - | +0900 - | +0930 - | +1000 - | +1030 - | +1100 - | +1130 - | +1200 - | +1245 - | +1300 - | +1345 - | +1400. -%% <<"Z">> --type timezone_alias() :: binary(). --type posix_time() :: non_neg_integer(). --type value() :: binary(). - %%% MACROS -define(TIMEZONES, [ -1200, diff --git a/src/ncalendar_format.erl b/src/ncalendar_format.erl index 0e0ea05..1004c57 100644 --- a/src/ncalendar_format.erl +++ b/src/ncalendar_format.erl @@ -11,10 +11,25 @@ %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% See the License for the specific language governing permissions and %% limitations under the License +% +%% @private +%% @doc Behaviour for ncalendar format modules. -module(ncalendar_format). -%%% INCLUDE FILES --include("ncalendar.hrl"). +%%% TYPES +-type datetimezone() :: { + calendar:datetime(), sub_seconds(), ncalendar:timezone() | ncalendar:timezone_alias() +}. +% Datetime is: UTC time | local time (if no timezone is specified). +-type milliseconds() :: non_neg_integer(). +-type sub_seconds() :: {millisecond, milliseconds()}. + +%%% EXPORT TYPES +-export_type([ + datetimezone/0, + milliseconds/0, + sub_seconds/0 +]). %%%----------------------------------------------------------------------------- %%% BEHAVIOUR CALLBACKS @@ -22,13 +37,16 @@ -callback from_datetimezone(Datetimezone, Opts) -> Result when Datetimezone :: datetimezone(), Opts :: map(), - Result :: value(). + Result :: ncalendar:value(). +% Converts a datetimezone() value to a binary representation of the implementer format. -callback is_valid(Value, Opts) -> Result when - Value :: value(), + Value :: ncalendar:value(), Opts :: map(), Result :: boolean(). +% Checks if a value is a valid datetime in the implementer format. -callback to_datetimezone(Value) -> Result when - Value :: value(), + Value :: ncalendar:value(), Result :: datetimezone(). +% Converts a binary representation of the implementer format to a datetimezone() value. diff --git a/src/ncalendar_http_date.erl b/src/ncalendar_http_date.erl index d1eada9..bd8d851 100644 --- a/src/ncalendar_http_date.erl +++ b/src/ncalendar_http_date.erl @@ -11,12 +11,14 @@ %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% See the License for the specific language governing permissions and %% limitations under the License +% +%% @doc ncalendar's HTTP Date format module. -module(ncalendar_http_date). %%% BEHAVIOURS -behaviour(ncalendar_format). -%%% EXTERNAL EXPORTS +%%% FORMAT EXPORTS -export([ from_datetimezone/2, is_valid/2, @@ -39,8 +41,10 @@ -define(GMT_TIMEZONE, "+0000"). %%%----------------------------------------------------------------------------- -%%% EXTERNAL EXPORTS +%%% FORMAT EXPORTS %%%----------------------------------------------------------------------------- +%% @private +%% @doc Converts a datetimezone() value to an HTTP Date binary. from_datetimezone({Datetime, Subseconds, <<"GMT">>}, Opts) -> from_datetimezone({Datetime, Subseconds, +0000}, Opts); from_datetimezone({{{Year, Month, Day}, {Hour, Min, Sec}}, _Subseconds, +0000}, _Opts) -> @@ -65,6 +69,8 @@ from_datetimezone({{{Year, Month, Day}, {Hour, Min, Sec}}, _Subseconds, +0000}, from_datetimezone({Datetime, Subseconds, _TZ}, Opts) -> from_datetimezone({Datetime, Subseconds, +0000}, Opts). +%% @private +%% @doc Checks if a value is a valid HTTP Date datetime. is_valid(Value, Opts) when is_binary(Value) -> is_valid(erlang:binary_to_list(Value), Opts); is_valid( @@ -118,6 +124,8 @@ is_valid( is_valid(_Value, _Opts) -> false. +%% @private +%% @doc Converts an HTTP Date binary to a datetimezone() value. to_datetimezone(Value) when is_binary(Value) -> to_datetimezone(erlang:binary_to_list(Value)); to_datetimezone( diff --git a/src/ncalendar_imf_fixdate.erl b/src/ncalendar_imf_fixdate.erl index 686963d..fc74b57 100644 --- a/src/ncalendar_imf_fixdate.erl +++ b/src/ncalendar_imf_fixdate.erl @@ -11,12 +11,14 @@ %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% See the License for the specific language governing permissions and %% limitations under the License +% +%% @doc ncalendar's imf-fixdate format module. -module(ncalendar_imf_fixdate). %%% BEHAVIOURS -behaviour(ncalendar_format). -%%% EXTERNAL EXPORTS +%%% FORMAT EXPORTS -export([ from_datetimezone/2, is_valid/2, @@ -38,8 +40,10 @@ -define(DECEMBER, "Dec"). %%%----------------------------------------------------------------------------- -%%% EXTERNAL EXPORTS +%%% FORMAT EXPORTS %%%----------------------------------------------------------------------------- +%% @private +%% @doc Converts a datetimezone() value to an imf-fixdate binary. from_datetimezone({Datetime, Subseconds, <<"GMT">>}, Opts) -> from_datetimezone({Datetime, Subseconds, +0000}, Opts); from_datetimezone({_Datetime, _Subseconds, Timezone} = Datetimezone, _Opts) -> @@ -64,6 +68,8 @@ from_datetimezone({_Datetime, _Subseconds, Timezone} = Datetimezone, _Opts) -> format_timezone(Timezone) ]). +%% @private +%% @doc Checks if a value is a valid imf-fixdate datetime. is_valid(Value, Opts) when is_binary(Value) -> is_valid(erlang:binary_to_list(Value), Opts); is_valid( @@ -118,6 +124,8 @@ is_valid( is_valid(_Value, _Opts) -> false. +%% @private +%% @doc Converts an imf-fixdate binary to a datetimezone() value. to_datetimezone(Value) when is_binary(Value) -> to_datetimezone(erlang:binary_to_list(Value)); to_datetimezone( diff --git a/src/ncalendar_iso8601.erl b/src/ncalendar_iso8601.erl index 3013349..544aa05 100644 --- a/src/ncalendar_iso8601.erl +++ b/src/ncalendar_iso8601.erl @@ -11,30 +11,42 @@ %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% See the License for the specific language governing permissions and %% limitations under the License +% +%% @doc ncalendar's ISO 8601 format module. -module(ncalendar_iso8601). %%% BEHAVIOURS -behaviour(ncalendar_format). -%%% EXTERNAL EXPORTS +%%% FORMAT EXPORTS -export([ from_datetimezone/2, is_valid/2, to_datetimezone/1 ]). -%% TYPES +%%% EXTERNAL EXPORTS +-export([ + format_timezone/1 +]). + +%%% TYPES -type opts() :: #{precision => precision(), extended => boolean()}. +% Options for the iso8601 format. -type precision() :: millisecond. +% Subsecond time precision level. +%%% EXPORT TYPES -export_type([ opts/0, precision/0 ]). %%%----------------------------------------------------------------------------- -%%% EXTERNAL EXPORTS +%%% FORMAT EXPORTS %%%----------------------------------------------------------------------------- +%% @private +%% @doc Converts a datetimezone() value to an iso8601 binary. from_datetimezone({Datetime, Subseconds, <<"Z">>}, Opts) -> from_datetimezone({Datetime, Subseconds, +0000}, Opts); %% Extended Milliseconds @@ -106,6 +118,8 @@ from_datetimezone({_Datetime, _Subseconds, Timezone} = Datetimezone, _Opts) -> format_timezone(Timezone) ]). +%% @private +%% @doc Checks if a value is a valid iso8601 datetime. is_valid(Value, Opts) when is_binary(Value) -> is_valid(erlang:binary_to_list(Value), Opts); %% Extended Milliseconds @@ -256,6 +270,8 @@ is_valid([Y1, Y2, Y3, Y4, Mo1, Mo2, D1, D2, $T, H1, H2, Mi1, Mi2, S1, S2 | TZ], is_valid(_Value, _Opts) -> false. +%% @private +%% @doc Converts an iso8601 binary to a datetimezone() value. to_datetimezone(Value) when is_binary(Value) -> to_datetimezone(erlang:binary_to_list(Value)); %% Extended Milliseconds @@ -322,6 +338,21 @@ to_datetimezone([Y1, Y2, Y3, Y4, Mo1, Mo2, D1, D2, $T, H1, H2, Mi1, Mi2, S1, S2 to_datetimezone(Value) -> erlang:throw({error, ncalendar_iso8601, {unrecognized_value, Value}}). +%%%----------------------------------------------------------------------------- +%%% EXTERNAL EXPORTS +%%%----------------------------------------------------------------------------- +-spec format_timezone(Timezone) -> Binary when + Timezone :: ncalendar:timezone(), + Binary :: binary(). +%% @doc Formats a timezone to a binary in iso8601 format. +format_timezone(Timezone) -> + case ncalendar_util:is_valid_timezone(Timezone) of + true -> + do_format_timezone(Timezone); + _False -> + erlang:throw({error, ncalendar_iso8601, {unsupported_timezone, Timezone}}) + end. + %%%----------------------------------------------------------------------------- %%% INTERNAL FUNCTIONS %%%----------------------------------------------------------------------------- @@ -346,14 +377,6 @@ do_format_timezone(Val) when Val > -1000 -> do_format_timezone(Val) -> erlang:integer_to_binary(Val). -format_timezone(Timezone) -> - case ncalendar_util:is_valid_timezone(Timezone) of - true -> - do_format_timezone(Timezone); - _False -> - erlang:throw({error, ncalendar_iso8601, {unsupported_timezone, Timezone}}) - end. - resolve_timezone_alias("Z") -> "+0000"; resolve_timezone_alias([]) -> diff --git a/src/ncalendar_util.erl b/src/ncalendar_util.erl index 2c69e71..1abd26b 100644 --- a/src/ncalendar_util.erl +++ b/src/ncalendar_util.erl @@ -11,6 +11,9 @@ %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %% See the License for the specific language governing permissions and %% limitations under the License +% +%% @private +%% @doc Utility functions for ncalendar. -module(ncalendar_util). %%% INCLUDE FILES @@ -40,10 +43,10 @@ %%% EXTERNAL EXPORTS %%%----------------------------------------------------------------------------- -spec datetime_to_datetimezone(Datetime, Subseconds, Timezone) -> Result when - Datetime :: datetime(), - Subseconds :: sub_seconds(), - Timezone :: timezone(), - Result :: datetimezone(). + Datetime :: calendar:datetime(), + Subseconds :: ncalendar_format:sub_seconds(), + Timezone :: ncalendar:timezone(), + Result :: ncalendar_format:datetimezone(). datetime_to_datetimezone(Datetime, Subseconds, +0000) -> {Datetime, Subseconds, +0000}; datetime_to_datetimezone(RawDatetime, Subseconds, Timezone) -> @@ -53,8 +56,8 @@ datetime_to_datetimezone(RawDatetime, Subseconds, Timezone) -> {Datetime, Subseconds, Timezone}. -spec datetimezone_to_datetime(Datetimezone) -> Result when - Datetimezone :: datetimezone(), - Result :: datetime(). + Datetimezone :: ncalendar_format:datetimezone(), + Result :: calendar:datetime(). datetimezone_to_datetime({Datetime, _Subseconds, +0000}) -> Datetime; datetimezone_to_datetime({Datetime, _Subseconds, Timezone}) -> @@ -62,14 +65,14 @@ datetimezone_to_datetime({Datetime, _Subseconds, Timezone}) -> calendar:gregorian_seconds_to_datetime(LocalSeconds). -spec datetimezone_to_gregorian_seconds(Datetimezone) -> Result when - Datetimezone :: datetimezone(), - Result :: gregorian_seconds(). + Datetimezone :: ncalendar_format:datetimezone(), + Result :: ncalendar:gregorian_seconds(). datetimezone_to_gregorian_seconds({Datetime, _Subseconds, Timezone}) -> calendar:datetime_to_gregorian_seconds(Datetime) + timezone_diff(Timezone). -spec datetimezone_to_timestamp(Datetimezone) -> Result when - Datetimezone :: datetimezone(), - Result :: timestamp(). + Datetimezone :: ncalendar_format:datetimezone(), + Result :: erlang:timestamp(). datetimezone_to_timestamp({Datetime, {millisecond, Milliseconds}, Timezone}) -> GregorianSeconds = calendar:datetime_to_gregorian_seconds(Datetime) + timezone_diff(Timezone), Secs = GregorianSeconds - ?JANUARY_1ST_1970, @@ -77,13 +80,13 @@ datetimezone_to_timestamp({Datetime, {millisecond, Milliseconds}, Timezone}) -> {Secs div 1000000, Secs rem 1000000, MicroSecs}. -spec is_valid_date(Date) -> Result when - Date :: date(), + Date :: calendar:date(), Result :: boolean(). is_valid_date({Year, Month, Day}) -> calendar:valid_date(Year, Month, Day). -spec is_valid_time(Time) -> Result when - Time :: time(), + Time :: calendar:time(), Result :: boolean(). is_valid_time({H, M, S}) when H >= 0, H < 24, M >= 0, M < 60, S >= 0, S < 60 -> true; @@ -91,7 +94,7 @@ is_valid_time(_Time) -> false. -spec is_valid_timezone(Timezone) -> Result when - Timezone :: timezone(), + Timezone :: ncalendar:timezone(), Result :: boolean(). is_valid_timezone(undefined) -> true; @@ -100,8 +103,8 @@ is_valid_timezone(Timezone) -> -spec milliseconds_to_datetimezone(Milliseconds, Timezone) -> Result when Milliseconds :: non_neg_integer(), - Timezone :: timezone(), - Result :: datetimezone(). + Timezone :: ncalendar:timezone(), + Result :: ncalendar_format:datetimezone(). milliseconds_to_datetimezone(Milliseconds, TimeZone) -> GregorianSeconds = Milliseconds div 1000, RestMilliseconds = Milliseconds - GregorianSeconds * 1000, @@ -133,7 +136,7 @@ pad(4, N) -> [$0, $0, $0 | erlang:integer_to_list(N)]. -spec timestamp_to_milliseconds(Timestamp) -> Result when - Timestamp :: timestamp(), + Timestamp :: erlang:timestamp(), Result :: non_neg_integer(). timestamp_to_milliseconds({MSecs, Secs, MicroSecs}) -> MilliSecs = MicroSecs div 1000, @@ -144,7 +147,7 @@ timestamp_to_milliseconds({MSecs, Secs, MicroSecs}) -> YearList :: list(), MonthList :: list(), DayList :: list(), - Result :: date(). + Result :: calendar:date(). to_date(YearList, MonthList, DayList) -> Year = erlang:list_to_integer(YearList), Month = erlang:list_to_integer(MonthList), @@ -155,7 +158,7 @@ to_date(YearList, MonthList, DayList) -> HourList :: list(), MinList :: list(), SecList :: list(), - Result :: time(). + Result :: calendar:time(). to_time(HourList, MinList, SecList) -> Hour = erlang:list_to_integer(HourList), Min = erlang:list_to_integer(MinList), @@ -164,7 +167,7 @@ to_time(HourList, MinList, SecList) -> -spec to_timezone(TimezoneList) -> Result when TimezoneList :: list(), - Result :: timezone(). + Result :: ncalendar:timezone(). to_timezone([]) -> undefined; to_timezone(TimezoneList) -> @@ -174,7 +177,7 @@ to_timezone(TimezoneList) -> %%% INTERNAL FUNCTIONS %%%----------------------------------------------------------------------------- -spec timezone_diff(Timezone) -> Result when - Timezone :: timezone(), + Timezone :: ncalendar:timezone(), Result :: integer(). timezone_diff(undefined) -> 0;