[Tutor] python time

Alan Gauld alan.gauld at btinternet.com
Fri Nov 27 22:10:20 CET 2009


"spir" <denis.spir at free.fr> wrote

> So, python  uses C's gettimeofday() func when available 

It's not C's function, it's a Unix system call. 
It's been part of Unix since BSD 4.2

> ftime() (millisecond), 

Also Unix and predates BSD 4.2...

> else it has only plain second precision using time(). 

Which is an ANSI C function.

> But there is no matching system call AFAIK except for time 
> (via datetime in unix-like systems). 

time() is the lowest common denominator supported by any ANSI C system.
It returns a tm struct:

struct tm{
    int tm_sec;
    int tm_min;
    // etc up to
    int tm_yday;
    int tm_isdst;
}

So only seconds need to be supported for ANSI compliance.

gettimeofday() is the Unix system call that returns microseconds.

But to confuse things, on Unix there is also the time() library function
(not system call - subtle difference!) which returns a timeval structure:

struct timeval{
    long tv_sec;
    long tv_usec;
}

So even time() on Unix returns microsecs.

But on early DOS it only returns seconds.
(I may be wrong but I think time() on DOS 6 returned milliseconds?)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list