aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src/calendar.erl
diff options
context:
space:
mode:
authorHans Bolinder <[email protected]>2018-02-22 13:33:40 +0100
committerHans Bolinder <[email protected]>2018-04-21 20:58:03 +0200
commit83515e39e7c321910082fa3ef8a06a659c51f70a (patch)
tree00523177e0474c43b5e4492449523a16adfff1ff /lib/stdlib/src/calendar.erl
parent98cd99ef1eaf32822404daced985258ac4973e14 (diff)
downloadotp-83515e39e7c321910082fa3ef8a06a659c51f70a.tar.gz
otp-83515e39e7c321910082fa3ef8a06a659c51f70a.tar.bz2
otp-83515e39e7c321910082fa3ef8a06a659c51f70a.zip
stdlib: Add system time functions to module calendar
Diffstat (limited to 'lib/stdlib/src/calendar.erl')
-rw-r--r--lib/stdlib/src/calendar.erl23
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/stdlib/src/calendar.erl b/lib/stdlib/src/calendar.erl
index 55a0cfc9a1..2e24e8c133 100644
--- a/lib/stdlib/src/calendar.erl
+++ b/lib/stdlib/src/calendar.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
+%% Copyright Ericsson AB 1996-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
@@ -41,6 +41,8 @@
now_to_universal_time/1,
seconds_to_daystime/1,
seconds_to_time/1,
+ system_time_to_local_time/2,
+ system_time_to_universal_time/2,
time_difference/2,
time_to_seconds/1,
universal_time/0,
@@ -59,6 +61,7 @@
-define(DAYS_PER_100YEARS, 36524).
-define(DAYS_PER_400YEARS, 146097).
-define(DAYS_FROM_0_TO_1970, 719528).
+-define(SECONDS_FROM_0_TO_1970, (?DAYS_FROM_0_TO_1970*?SECONDS_PER_DAY)).
%%----------------------------------------------------------------------
%% Types
@@ -309,7 +312,7 @@ local_time_to_universal_time_dst(DateTime) ->
-spec now_to_datetime(Now) -> datetime1970() when
Now :: erlang:timestamp().
now_to_datetime({MSec, Sec, _uSec}) ->
- Sec0 = MSec*1000000 + Sec + ?DAYS_FROM_0_TO_1970*?SECONDS_PER_DAY,
+ Sec0 = MSec*1000000 + Sec + ?SECONDS_FROM_0_TO_1970,
gregorian_seconds_to_datetime(Sec0).
-spec now_to_universal_time(Now) -> datetime1970() when
@@ -363,6 +366,22 @@ seconds_to_time(Secs) when Secs >= 0, Secs < ?SECONDS_PER_DAY ->
Second = Secs1 rem ?SECONDS_PER_MINUTE,
{Hour, Minute, Second}.
+-spec system_time_to_local_time(Time, TimeUnit) -> datetime() when
+ Time :: integer(),
+ TimeUnit :: erlang:time_unit().
+
+system_time_to_local_time(Time, TimeUnit) ->
+ UniversalDate = system_time_to_universal_time(Time, TimeUnit),
+ erlang:universaltime_to_localtime(UniversalDate).
+
+-spec system_time_to_universal_time(Time, TimeUnit) -> datetime() when
+ Time :: integer(),
+ TimeUnit :: erlang:time_unit().
+
+system_time_to_universal_time(Time, TimeUnit) ->
+ Secs = erlang:convert_time_unit(Time, TimeUnit, second),
+ gregorian_seconds_to_datetime(Secs + ?SECONDS_FROM_0_TO_1970).
+
%% time_difference(T1, T2) = Tdiff
%%
%% Returns the difference between two {Date, Time} structures.