home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / timemodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  10.8 KB  |  504 lines  |  [TEXT/CWIE]

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