home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pytexdoc / ext / source / !Python / Modules / c / timemodule < prev    next >
Encoding:
Text File  |  1996-11-06  |  10.9 KB  |  508 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Time module */
  33.  
  34. #include "allobjects.h"
  35. #include "modsupport.h"
  36. #include "ceval.h"
  37.  
  38. #ifdef HAVE_SELECT
  39. #include "mymath.h"
  40. #endif
  41.  
  42. #ifndef RISCOS
  43. #ifdef macintosh
  44. #include <time.h>
  45. #else
  46. #include <sys/types.h>
  47. #endif
  48. #endif
  49.  
  50. #ifdef QUICKWIN
  51. #include <io.h>
  52. #endif
  53.  
  54. #ifdef HAVE_UNISTD_H
  55. #include <unistd.h>
  56. #endif
  57.  
  58. #ifdef HAVE_SELECT
  59. #include "myselect.h"
  60. #else
  61. #include "mytime.h"
  62. #endif
  63.  
  64. #ifdef HAVE_FTIME
  65. #include <sys/timeb.h>
  66. #endif
  67.  
  68. #ifdef __WATCOMC__
  69. #include <i86.h>
  70. #else
  71. #ifdef MS_WINDOWS
  72. #include <windows.h>
  73. #ifdef MS_WIN16
  74. /* These overrides not needed for Win32 */
  75. #define timezone _timezone
  76. #define tzname _tzname
  77. #define daylight _daylight
  78. #define altzone _altzone
  79. #endif /* MS_WIN16 */
  80. #endif /* MS_WINDOWS */
  81. #endif /* !__WATCOMC__ */
  82.  
  83. /* Forward declarations */
  84. static int floatsleep PROTO((double));
  85. static double floattime PROTO(());
  86.  
  87. static object *
  88. time_time(self, args)
  89.     object *self;
  90.     object *args;
  91. {
  92.     double secs;
  93.     if (!getnoarg(args))
  94.         return NULL;
  95.     secs = floattime();
  96.     if (secs == 0.0) {
  97.         err_errno(IOError);
  98.         return NULL;
  99.     }
  100.     return newfloatobject(secs);
  101. }
  102.  
  103. #ifdef HAVE_CLOCK
  104.  
  105. #ifndef CLOCKS_PER_SEC
  106. #ifdef CLK_TCK
  107. #define CLOCKS_PER_SEC CLK_TCK
  108. #else
  109. #define CLOCKS_PER_SEC 1000000
  110. #endif
  111. #endif
  112.  
  113. static object *
  114. time_clock(self, args)
  115.     object *self;
  116.     object *args;
  117. {
  118.     if (!getnoarg(args))
  119.         return NULL;
  120.     return newfloatobject(((double)clock()) / CLOCKS_PER_SEC);
  121. }
  122. #endif /* HAVE_CLOCK */
  123.  
  124. static object *
  125. time_sleep(self, args)
  126.     object *self;
  127.     object *args;
  128. {
  129.     double secs;
  130.     if (!getargs(args, "d", &secs))
  131.         return NULL;
  132.     BGN_SAVE
  133.     if (floatsleep(secs) != 0) {
  134.         RET_SAVE
  135.         return NULL;
  136.     }
  137.     END_SAVE
  138.     INCREF(None);
  139.     return None;
  140. }
  141.  
  142. static object *
  143. time_convert(when, function)
  144.     time_t when;
  145.     struct tm * (*function) PROTO((const time_t *));
  146. {
  147.     struct tm *p;
  148.     errno = 0;
  149.     p = function(&when);
  150.     if (p == NULL) {
  151. #ifdef EINVAL
  152.         if (errno == NULL)
  153.             errno = EINVAL;
  154. #endif
  155.         return err_errno(IOError);
  156.     }
  157.     return mkvalue("(iiiiiiiii)",
  158.                p->tm_year + 1900,
  159.                p->tm_mon + 1, /* Want January == 1 */
  160.                p->tm_mday,
  161.                p->tm_hour,
  162.                p->tm_min,
  163.                p->tm_sec,
  164.                (p->tm_wday + 6) % 7, /* Want Monday == 0 */
  165.                p->tm_yday + 1, /* Want January, 1 == 1 */
  166.                p->tm_isdst);
  167. }
  168.  
  169. static object *
  170. time_gmtime(self, args)
  171.     object *self;
  172.     object *args;
  173. {
  174.     double when;
  175.     if (!getargs(args, "d", &when))
  176.         return NULL;
  177.     return time_convert((time_t)when, gmtime);
  178. }
  179.  
  180. static object *
  181. time_localtime(self, args)
  182.     object *self;
  183.     object *args;
  184. {
  185.     double when;
  186.     if (!getargs(args, "d", &when))
  187.         return NULL;
  188.     return time_convert((time_t)when, localtime);
  189. }
  190.  
  191. static int
  192. gettmarg(args, p)
  193.     object *args;
  194.     struct tm *p;
  195. {
  196.     if (!getargs(args, "(iiiiiiiii)",
  197.              &p->tm_year,
  198.              &p->tm_mon,
  199.              &p->tm_mday,
  200.              &p->tm_hour,
  201.              &p->tm_min,
  202.              &p->tm_sec,
  203.              &p->tm_wday,
  204.              &p->tm_yday,
  205.              &p->tm_isdst))
  206.         return 0;
  207.     if (p->tm_year >= 1900)
  208.         p->tm_year -= 1900;
  209.     p->tm_mon--;
  210.     p->tm_wday = (p->tm_wday + 1) % 7;
  211.     p->tm_yday--;
  212.     return 1;
  213. }
  214.  
  215. #ifdef HAVE_STRFTIME
  216. static object *
  217. time_strftime(self, args)
  218.     object *self;
  219.     object *args;
  220. {
  221.     struct tm buf;
  222.     const char *fmt;
  223.     char *outbuf = 0;
  224.     int i;
  225.  
  226.     if (!PyArg_ParseTuple(args, "s(iiiiiiiii)",
  227.                   &fmt,
  228.                   &(buf.tm_year),
  229.                   &(buf.tm_mon),
  230.                   &(buf.tm_mday),
  231.                   &(buf.tm_hour),
  232.                   &(buf.tm_min),
  233.                   &(buf.tm_sec),
  234.                   &(buf.tm_wday),
  235.                   &(buf.tm_yday),
  236.                   &(buf.tm_isdst)))
  237.         return NULL;
  238.     if (buf.tm_year >= 1900)
  239.         buf.tm_year -= 1900;
  240.     buf.tm_mon--;
  241.     buf.tm_wday = (buf.tm_wday + 1) % 7;
  242.     buf.tm_yday--;
  243.     /* I hate these functions that presume you know how big the output */
  244.     /* will be ahead of time... */
  245.     for (i = 1024 ; i < 8192 ; i += 1024) {
  246.         outbuf = malloc(i);
  247.         if (outbuf == NULL) {
  248.             return err_nomem();
  249.         }
  250.         if (strftime(outbuf, i-1, fmt, &buf) != 0) {
  251.             object *ret;
  252.             ret = newstringobject(outbuf);
  253.             free(outbuf);
  254.             return ret;
  255.         }
  256.         free(outbuf);
  257.     }
  258.     return err_nomem();
  259. }
  260. #endif /* HAVE_STRFTIME */
  261.  
  262. static object *
  263. time_asctime(self, args)
  264.     object *self;
  265.     object *args;
  266. {
  267.     struct tm buf;
  268.     char *p;
  269.     if (!gettmarg(args, &buf))
  270.         return NULL;
  271.     p = asctime(&buf);
  272.     if (p[24] == '\n')
  273.         p[24] = '\0';
  274.     return newstringobject(p);
  275. }
  276.  
  277. static object *
  278. time_ctime(self, args)
  279.     object *self;
  280.     object *args;
  281. {
  282.     double dt;
  283.     time_t tt;
  284.     char *p;
  285.     if (!getargs(args, "d", &dt))
  286.         return NULL;
  287.     tt = (time_t)dt;
  288.     p = ctime(&tt);
  289.     if (p[24] == '\n')
  290.         p[24] = '\0';
  291.     return newstringobject(p);
  292. }
  293.  
  294. static object *
  295. time_mktime(self, args)
  296.     object *self;
  297.     object *args;
  298. {
  299.     struct tm buf;
  300.     time_t tt;
  301.     tt = time(&tt);
  302.     buf = *localtime(&tt);
  303.     if (!gettmarg(args, &buf))
  304.         return NULL;
  305.     tt = mktime(&buf);
  306.     if (tt == (time_t)(-1)) {
  307.         err_setstr(OverflowError, "mktime argument out of range");
  308.         return NULL;
  309.     }
  310.     return newfloatobject((double)tt);
  311. }
  312.  
  313. static struct methodlist time_methods[] = {
  314.     {"time",    time_time},
  315. #ifdef HAVE_CLOCK
  316.     {"clock",    time_clock},
  317. #endif
  318.     {"sleep",    time_sleep},
  319.     {"gmtime",    time_gmtime},
  320.     {"localtime",    time_localtime},
  321.     {"asctime",    time_asctime},
  322.     {"ctime",    time_ctime},
  323.     {"mktime",    time_mktime},
  324. #ifdef HAVE_STRFTIME
  325.     {"strftime",    time_strftime, 1},
  326. #endif
  327.     {NULL,        NULL}        /* sentinel */
  328. };
  329.  
  330. static void
  331. ins(d, name, v)
  332.     object *d;
  333.     char *name;
  334.     object *v;
  335. {
  336.     if (v == NULL)
  337.         fatal("Can't initialize time module -- NULL value");
  338.     if (dictinsert(d, name, v) != 0)
  339.         fatal("Can't initialize time module -- dictinsert failed");
  340.     DECREF(v);
  341. }
  342.  
  343. void
  344. inittime()
  345. {
  346.     object *m, *d;
  347.     m = initmodule("time", time_methods);
  348.     d = getmoduledict(m);
  349. #ifdef HAVE_TZNAME
  350.     tzset();
  351.     ins(d, "timezone", newintobject((long)timezone));
  352. #ifdef HAVE_ALTZONE
  353.     ins(d, "altzone", newintobject((long)altzone));
  354. #else
  355.     ins(d, "altzone", newintobject((long)timezone-3600));
  356. #endif
  357.     ins(d, "daylight", newintobject((long)daylight));
  358.     ins(d, "tzname", mkvalue("(zz)", tzname[0], tzname[1]));
  359. #else /* !HAVE_TZNAME */
  360. #if HAVE_TM_ZONE
  361.     {
  362. #define YEAR ((time_t)((365 * 24 + 6) * 3600))
  363.         time_t t;
  364.         struct tm *p;
  365.         long winterzone, summerzone;
  366.         char wintername[10], summername[10];
  367.         /* XXX This won't work on the southern hemisphere.
  368.            XXX Anybody got a better idea? */
  369.         t = (time((time_t *)0) / YEAR) * YEAR;
  370.         p = localtime(&t);
  371.         winterzone = -p->tm_gmtoff;
  372.         strncpy(wintername, p->tm_zone ? p->tm_zone : "   ", 9);
  373.         wintername[9] = '\0';
  374.         t += YEAR/2;
  375.         p = localtime(&t);
  376.         summerzone = -p->tm_gmtoff;
  377.         strncpy(summername, p->tm_zone ? p->tm_zone : "   ", 9);
  378.         summername[9] = '\0';
  379.         ins(d, "timezone", newintobject(winterzone));
  380.         ins(d, "altzone", newintobject(summerzone));
  381.         ins(d, "daylight", newintobject((long)(winterzone != summerzone)));
  382.         ins(d, "tzname",  mkvalue("(zz)", wintername, summername));
  383.     }
  384. #endif /* HAVE_TM_ZONE */
  385. #endif /* !HAVE_TZNAME */
  386. }
  387.  
  388.  
  389. /* Implement floattime() for various platforms */
  390.  
  391. static double
  392. floattime()
  393. {
  394.     /* There are three ways to get the time:
  395.        (1) gettimeofday() -- resolution in microseconds
  396.        (2) ftime() -- resolution in milliseconds
  397.        (3) time() -- resolution in seconds
  398.        In all cases the return value is a float in seconds.
  399.        Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  400.        fail, so we fall back on ftime() or time().
  401.        Note: clock resolution does not imply clock accuracy! */
  402. #ifdef HAVE_GETTIMEOFDAY
  403.     {
  404.     struct timeval t;
  405. #ifdef GETTIMEOFDAY_NO_TZ
  406.     if (gettimeofday(&t) == 0)
  407.         return (double)t.tv_sec + t.tv_usec*0.000001;
  408. #else /* !GETTIMEOFDAY_NO_TZ */
  409.     if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  410.         return (double)t.tv_sec + t.tv_usec*0.000001;
  411. #endif /* !GETTIMEOFDAY_NO_TZ */
  412.     }
  413. #endif /* !HAVE_GETTIMEOFDAY */
  414.     {
  415. #ifdef HAVE_FTIME
  416.     struct timeb t;
  417.     ftime(&t);
  418.     return (double)t.time + (double)t.millitm * (double)0.001;
  419. #else /* !HAVE_FTIME */
  420.     time_t secs;
  421.     time(&secs);
  422.     return (double)secs;
  423. #endif /* !HAVE_FTIME */
  424.     }
  425. }
  426.  
  427.  
  428. /* Implement floatsleep() for various platforms.
  429.    When interrupted (or when another error occurs), return -1 and
  430.    set an exception; else return 0. */
  431.  
  432. static int
  433. #ifdef MPW
  434. floatsleep(double secs)
  435. #else
  436. floatsleep(secs)
  437.     double secs;
  438. #endif /* MPW */
  439. {
  440. #ifdef HAVE_SELECT
  441.     struct timeval t;
  442.     double frac;
  443.     frac = fmod(secs, 1.0);
  444.     secs = floor(secs);
  445.     t.tv_sec = (long)secs;
  446.     t.tv_usec = (long)(frac*1000000.0);
  447.     if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
  448.         err_errno(IOError);
  449.         return -1;
  450.     }
  451. #else /* !HAVE_SELECT */
  452. #ifdef macintosh
  453. #define MacTicks    (* (long *)0x16A)
  454.     long deadline;
  455.     deadline = MacTicks + (long)(secs * 60.0);
  456.     while (MacTicks < deadline) {
  457.         if (sigcheck())
  458.             return -1;
  459.     }
  460. #else /* !macintosh */
  461. #ifdef __WATCOMC__
  462.     /* XXX Can't interrupt this sleep */
  463.     delay((int)(secs * 1000 + 0.5));  /* delay() uses milliseconds */
  464. #else /* !__WATCOMC__ */
  465. #ifdef MSDOS
  466.     struct timeb t1, t2;
  467.     double frac;
  468.     extern double fmod PROTO((double, double));
  469.     extern double floor PROTO((double));
  470.     if (secs <= 0.0)
  471.         return;
  472.     frac = fmod(secs, 1.0);
  473.     secs = floor(secs);
  474.     ftime(&t1);
  475.     t2.time = t1.time + (int)secs;
  476.     t2.millitm = t1.millitm + (int)(frac*1000.0);
  477.     while (t2.millitm >= 1000) {
  478.         t2.time++;
  479.         t2.millitm -= 1000;
  480.     }
  481.     for (;;) {
  482. #ifdef QUICKWIN
  483.         _wyield();
  484. #endif
  485.         if (sigcheck())
  486.             return -1;
  487.         ftime(&t1);
  488.         if (t1.time > t2.time ||
  489.             t1.time == t2.time && t1.millitm >= t2.millitm)
  490.             break;
  491.     }
  492. #else /* !MSDOS */
  493. #ifdef MS_WIN32
  494.     /* XXX Can't interrupt this sleep */
  495.     Sleep((int)(secs*1000));
  496. #else /* !MS_WIN32 */
  497.     /* XXX Can't interrupt this sleep */
  498. #ifndef RISCOS
  499.     sleep((int)secs);
  500. #endif
  501. #endif /* !MS_WIN32 */
  502. #endif /* !MSDOS */
  503. #endif /* !__WATCOMC__ */
  504. #endif /* !macintosh */
  505. #endif /* !HAVE_SELECT */
  506.     return 0;
  507. }
  508.