| 1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
| 2 |
|
%% |
| 3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
|
%% you may not use this file except in compliance with the License. |
| 5 |
|
%% You may obtain a copy of the License at |
| 6 |
|
%% |
| 7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
%% |
| 9 |
|
%% Unless required by applicable law or agreed to in writing, software |
| 10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
|
%% See the License for the specific language governing permissions and |
| 13 |
|
%% limitations under the License. |
| 14 |
|
|
| 15 |
|
-module(pgmp_calendar). |
| 16 |
|
|
| 17 |
|
|
| 18 |
|
-export([decode/1]). |
| 19 |
|
-export([encode/1]). |
| 20 |
|
-export([epoch/1]). |
| 21 |
|
-export([epoch_date/1]). |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
%% As microseconds since midnight on 2000-01-01 |
| 25 |
|
decode(<<Encoded:64>>) -> |
| 26 |
:-( |
?FUNCTION_NAME(Encoded); |
| 27 |
|
|
| 28 |
|
decode(MicroSincePGEpoch) -> |
| 29 |
404 |
epoch(pg) + MicroSincePGEpoch - epoch(posix). |
| 30 |
|
|
| 31 |
|
encode(MicroSystemTime) -> |
| 32 |
201 |
epoch(posix) + MicroSystemTime - epoch(pg). |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
epoch(System) -> |
| 36 |
1210 |
erlang:convert_time_unit( |
| 37 |
|
calendar:datetime_to_gregorian_seconds(epoch_datetime(System)), |
| 38 |
|
second, |
| 39 |
|
microsecond). |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
epoch_datetime(System) -> |
| 43 |
1210 |
{epoch_date(System), midnight()}. |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
midnight() -> |
| 47 |
1210 |
{0, 0, 0}. |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
epoch_date(pg) -> |
| 51 |
811 |
{2000, 1, 1}; |
| 52 |
|
|
| 53 |
|
epoch_date(posix) -> |
| 54 |
605 |
{1970, 1, 1}. |