aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src
diff options
context:
space:
mode:
authorHans Bolinder <[email protected]>2018-04-26 15:21:41 +0200
committerHans Bolinder <[email protected]>2018-04-26 15:21:41 +0200
commitbfde922a6308a7f0956cf688ab38cbdbffbba41d (patch)
tree84eacd924ce5babf063ff81a2d6910a11de23511 /lib/stdlib/src
parent9779a7fe2dd318f9b1e5a2d4b3b4e413200d0f9c (diff)
parent83515e39e7c321910082fa3ef8a06a659c51f70a (diff)
downloadotp-bfde922a6308a7f0956cf688ab38cbdbffbba41d.tar.gz
otp-bfde922a6308a7f0956cf688ab38cbdbffbba41d.tar.bz2
otp-bfde922a6308a7f0956cf688ab38cbdbffbba41d.zip
Merge branch 'hasse/stdlib/calendar_systemtime/OTP-13413'
* hasse/stdlib/calendar_systemtime/OTP-13413: stdlib: Add system time functions to module calendar
Diffstat (limited to 'lib/stdlib/src')
-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.