erlang time handling (in calendar.erl and erlang.erl)

Håkan Stenholm hakan.stenholm@REDACTED
Mon Jan 31 00:42:49 CET 2005


On 30 jan 2005, at 23.11, Ulf Wiger wrote:

> Den 2005-01-29 22:14:55 skrev Håkan Stenholm 
> <hakan.stenholm@REDACTED>:
>
>> Looking at the code in calendar.erl I would assume that the seconds
>> retrieved with erlang:now() are in Unix time
>> (http://en.wikipedia.org/wiki/Unix_time) - i.e. all days are treated 
>> as
>> if they are 24*60*60 seconds long and leap seconds[1] are handled by
>> extending certain seconds, i.e. reporting 23:59:59 twice instead of
>> 23:59:59 followed by 23:59:60.
>
> The calendar module doesn't rely on erlang:now() for time.
> Instead, it uses erlang:localtime(), erlang:universaltime(),
> and the conversion functions localtime_to_universaltime/1
> and universaltime_to_localtime/1.

The function now_to_datetime/1 and now_to_universal_time/1 pretty much 
appear to be designed to take erlang:now/0 as input, I at least can't 
find any other function that returns {MegaSecs,Secs,Microsecs} as 
output.
See calendar.erl source (R10B-2):

.....
%% now_to_universal_time(Now)
%% now_to_datetime(Now)
%%
%% Convert from now() to UTC.
%%
%% Args: Now = now(); now() = {MegaSec, Sec, MilliSec}, MegaSec = Sec
%% = MilliSec = integer()
%% Returns: {date(), time()}, date() = {Y, M, D}, time() = {H, M, S}.
%%
now_to_datetime({MSec, Sec, _uSec}) ->
     Sec0 = MSec*1000000 + Sec + ?DAYS_FROM_0_TO_1970*?SECONDS_PER_DAY,
     gregorian_seconds_to_datetime(Sec0).

now_to_universal_time(Now) ->
     now_to_datetime(Now).
.....

I know that the remaining functions use erlang:localtime() and 
erlang:universaltime() and similar functions, which all pass time 
information as {Date, Time} tuples (or date-time converted to gregorian 
seconds). But this still poses the problem what kind of seconds 
now_to_datetime/1 and now_to_universal_time/1 receive as they don't 
appear to handle leap seconds.

>
> erlang:now() is to be considered as an internal real-time clock
> which is loosely coupled to the system clock. It will periodically
> sample the system clock and can adjust its own "speed" by +/- 1%
> (if memory serves me) if the "now time" differs from the system
> time. This can happen e.g. if the system clock is adjusted, either
> manually or through an rdate() call. Since timeouts are coupled to
> the erlang:now() clock, big jumps can have rather unpleasant
> consequences.
>
> Some functions, e.g. in snmp, use a combination of localtime()
> and now in order to get sub-second precision. This is because
> there is no time function in Erlang that reports the actual
> system time with high enough precision.
>
> /Uffe
> -- 
> Använder Operas banbrytande e-postklient: http://www.opera.com/m2/
>




More information about the erlang-questions mailing list