Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#23): Fixes the timestamps and gregorian seconds #24

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ncalendar_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ datetimezone_to_datetime({Datetime, _Subseconds, Timezone}) ->
-spec datetimezone_to_gregorian_seconds(Datetimezone) -> Result when
Datetimezone :: ncalendar_format:datetimezone(),
Result :: ncalendar:gregorian_seconds().
datetimezone_to_gregorian_seconds({Datetime, _Subseconds, Timezone}) ->
calendar:datetime_to_gregorian_seconds(Datetime) + timezone_diff(Timezone).
datetimezone_to_gregorian_seconds({Datetime, _Subseconds, _Timezone}) ->
calendar:datetime_to_gregorian_seconds(Datetime).

-spec datetimezone_to_timestamp(Datetimezone) -> Result when
Datetimezone :: ncalendar_format:datetimezone(),
Result :: erlang:timestamp().
datetimezone_to_timestamp({Datetime, {millisecond, Milliseconds}, Timezone}) ->
GregorianSeconds = calendar:datetime_to_gregorian_seconds(Datetime) + timezone_diff(Timezone),
datetimezone_to_timestamp({Datetime, {millisecond, Milliseconds}, _Timezone}) ->
GregorianSeconds = calendar:datetime_to_gregorian_seconds(Datetime),
Secs = GregorianSeconds - ?JANUARY_1ST_1970,
MicroSecs = Milliseconds * 1000,
{Secs div 1000000, Secs rem 1000000, MicroSecs}.
Expand Down
27 changes: 24 additions & 3 deletions test/ncalendar_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
%% limitations under the License.
-module(ncalendar_SUITE).

%%% INCLUDE FILES
-include_lib("stdlib/include/assert.hrl").
josecriane marked this conversation as resolved.
Show resolved Hide resolved

%%% EXTERNAL EXPORTS
-compile([export_all, nowarn_export_all]).

Expand Down Expand Up @@ -162,13 +165,25 @@ convert_without_tz(_Conf) ->
datetime(_Conf) ->
Datetime = calendar:universal_time(),
Bin = ncalendar:from_datetime(iso8601, Datetime),
Datetime = ncalendar:to_datetime(iso8601, Bin).
Datetime = ncalendar:to_datetime(iso8601, Bin),

ISO8601UTC = <<"20140519T100000Z">>,
ISO8601 = <<"20140519T100000+1000">>,
UTC = ncalendar:to_datetime(iso8601, ISO8601UTC),
Zone = ncalendar:to_datetime(iso8601, ISO8601),
?assertEqual(UTC, Zone).

gregorian_seconds(_Conf) ->
Datetime = calendar:universal_time(),
GregorianSeconds = calendar:datetime_to_gregorian_seconds(Datetime),
Bin = ncalendar:from_gregorian_seconds(iso8601, GregorianSeconds),
GregorianSeconds = ncalendar:to_gregorian_seconds(iso8601, Bin).
GregorianSeconds = ncalendar:to_gregorian_seconds(iso8601, Bin),

ISO8601UTC = <<"20140519T100000Z">>,
ISO8601 = <<"20140519T100000+1000">>,
UTC = ncalendar:to_gregorian_seconds(iso8601, ISO8601UTC),
Zone = ncalendar:to_gregorian_seconds(iso8601, ISO8601),
?assertNotEqual(UTC, Zone).

now(_Conf) ->
DateTime = calendar:now_to_datetime(erlang:timestamp()),
Expand All @@ -181,7 +196,13 @@ timestamp(_Conf) ->
Timestamp = erlang:timestamp(),
Bin = ncalendar:from_timestamp(iso8601, Timestamp),
{MSecs, Secs, _MicroSecs1} = Timestamp,
{MSecs, Secs, 0} = ncalendar:to_timestamp(iso8601, Bin).
{MSecs, Secs, 0} = ncalendar:to_timestamp(iso8601, Bin),

ISO8601UTC = <<"20140519T100000Z">>,
ISO8601 = <<"20140519T100000+1000">>,
UTC = ncalendar:to_timestamp(iso8601, ISO8601UTC),
Zone = ncalendar:to_timestamp(iso8601, ISO8601),
?assertNotEqual(UTC, Zone).

shift_timezone(_Conf) ->
ISO8601 = <<"20140519T100000+1000">>,
Expand Down
Loading