Skip to content

Commit

Permalink
fix(#23): Fixes the timestamps and gregorian seconds
Browse files Browse the repository at this point in the history
closes #23
  • Loading branch information
josecriane committed Mar 15, 2024
1 parent 0237766 commit 93c195c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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
26 changes: 23 additions & 3 deletions test/ncalendar_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
%% limitations under the License.
-module(ncalendar_SUITE).

-include_lib("stdlib/include/assert.hrl").

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

Expand Down Expand Up @@ -162,13 +164,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 +195,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

0 comments on commit 93c195c

Please sign in to comment.