[Tutor] python time

spir denis.spir at free.fr
Sat Nov 28 00:07:13 CET 2009


"Alan Gauld" <alan.gauld at btinternet.com> wrote:

Thank you, Alan.

> "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...

I am confused here. That's what I first thought (there _must_ be a way to get time more precise that seconds!). But on my system (ubuntu 9.10) I cannot find the proper manner to use these system calls. Even from the command-line directly. Certainly missing something _really_ obvious.

As for the code in timemodule.c, I read:

floattime(void)
{
	/* There are three ways to get the time:
	  (1) gettimeofday() -- resolution in microseconds
	  (2) ftime() -- resolution in milliseconds
	  (3) time() -- resolution in seconds
	  In all cases the return value is a float in seconds.
	  Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
	  fail, so we fall back on ftime() or time().
	  Note: clock resolution does not imply clock accuracy! */
#ifdef HAVE_GETTIMEOFDAY
	{
		struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
		if (gettimeofday(&t) == 0)
			return (double)t.tv_sec + t.tv_usec*0.000001;
#else /* !GETTIMEOFDAY_NO_TZ */
		if (gettimeofday(&t, (struct timezone *)NULL) == 0)
			return (double)t.tv_sec + t.tv_usec*0.000001;
#endif /* !GETTIMEOFDAY_NO_TZ */
	}

#endif /* !HAVE_GETTIMEOFDAY */
...

This let me think gettimeofday() and ftime() are C routines, but they may ne wrappers around system calls.

> 
> > 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.

Right. This is precisely the reason why some languages (eg lua) don't provide any way to get time in ms or us. And also the source of my original post on the topic.

> 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,
 
So, I still have the issue of not beeing smart enough to access one of these systems-provided features. Would someone be nice & write an example call to use eg Unix's gettimeofday directly from python or from the command-line? (without using python's time module, indeed)

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/



More information about the Tutor mailing list