home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Modules / timemodule.c < prev    next >
C/C++ Source or Header  |  1994-02-24  |  9KB  |  417 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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 not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Time module */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29. #include "ceval.h"
  30.  
  31. #include <signal.h>
  32. #include <setjmp.h>
  33.  
  34. #ifndef macintosh
  35. #include <sys/types.h>
  36. #endif
  37.  
  38. #ifdef QUICKWIN
  39. #include <io.h>
  40. #endif
  41.  
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45.  
  46. #ifdef HAVE_SELECT
  47. #include "myselect.h"
  48. #else
  49. #include "mytime.h"
  50. #endif
  51.  
  52. #ifdef HAVE_FTIME
  53. #include <sys/timeb.h>
  54. #endif
  55.  
  56. #ifdef _M_IX86
  57. #include <windows.h>
  58. #define timezone _timezone
  59. #endif
  60.  
  61. /* Forward declarations */
  62. static void floatsleep PROTO((double));
  63. static double floattime PROTO(());
  64.  
  65. static object *
  66. time_time(self, args)
  67.     object *self;
  68.     object *args;
  69. {
  70.     double secs;
  71.     if (!getnoarg(args))
  72.         return NULL;
  73.     secs = floattime();
  74.     if (secs == 0.0) {
  75.         err_errno(IOError);
  76.         return NULL;
  77.     }
  78.     return newfloatobject(secs);
  79. }
  80.  
  81. #ifdef HAVE_CLOCK
  82.  
  83. #ifndef CLOCKS_PER_SEC
  84. #define CLOCKS_PER_SEC 1000000
  85. #endif
  86.  
  87. static object *
  88. time_clock(self, args)
  89.     object *self;
  90.     object *args;
  91. {
  92.     if (!getnoarg(args))
  93.         return NULL;
  94.     return newfloatobject(((double)clock()) / CLOCKS_PER_SEC);
  95. }
  96. #endif /* HAVE_CLOCK */
  97.  
  98. #ifdef SIGINT
  99. static jmp_buf sleep_intr;
  100.  
  101. /* ARGSUSED */
  102. static void
  103. sleep_catcher(sig)
  104.     int sig; /* Not used but required by interface */
  105. {
  106.     longjmp(sleep_intr, 1);
  107. }
  108. #endif /* SIGINT */
  109.  
  110. static object *
  111. time_sleep(self, args)
  112.     object *self;
  113.     object *args;
  114. {
  115.     double secs;
  116. #ifdef SIGINT
  117.     /* We must set the signal handler *after* calling setjmp, to
  118.        avoid a race condition.  Unfortunately some compilers put
  119.        the sigsave variable in a register.  Sometimes auto is
  120.        enough, sometimes static is needed to avoid this (Microsoft
  121.        C 7.0). */
  122. #ifdef WITH_THREAD
  123.     auto
  124. #else
  125.     static
  126. #endif
  127.            RETSIGTYPE (*sigsave)() = 0; /* Initialized to shut lint up */
  128. #endif /* SIGINT */
  129.     if (!getargs(args, "d", &secs))
  130.         return NULL;
  131. #ifdef SIGINT
  132.     BGN_SAVE
  133.     if (setjmp(sleep_intr)) {
  134.         RET_SAVE
  135.         signal(SIGINT, sigsave);
  136.         err_set(KeyboardInterrupt);
  137.         return NULL;
  138.     }
  139.     sigsave = signal(SIGINT, SIG_IGN);
  140.     if (sigsave != (RETSIGTYPE (*)()) SIG_IGN)
  141.         signal(SIGINT, sleep_catcher);
  142. #endif
  143.     floatsleep(secs);
  144. #ifdef SIGINT
  145.     END_SAVE
  146.     signal(SIGINT, sigsave);
  147. #endif
  148.     INCREF(None);
  149.     return None;
  150. }
  151.  
  152. static object *
  153. time_convert(when, function)
  154.     time_t when;
  155.     struct tm * (*function) PROTO((time_t *));
  156. {
  157.     struct tm *p = function(&when);
  158.     return mkvalue("(iiiiiiiii)",
  159.                p->tm_year + 1900,
  160.                p->tm_mon + 1, /* Want January == 1 */
  161.                p->tm_mday,
  162.                p->tm_hour,
  163.                p->tm_min,
  164.                p->tm_sec,
  165.                (p->tm_wday + 6) % 7, /* Want Monday == 0 */
  166.                p->tm_yday + 1, /* Want January, 1 == 1 */
  167.                p->tm_isdst);
  168. }
  169.  
  170. static object *
  171. time_gmtime(self, args)
  172.     object *self;
  173.     object *args;
  174. {
  175.     double when;
  176.     if (!getargs(args, "d", &when))
  177.         return NULL;
  178.     return time_convert((time_t)when, gmtime);
  179. }
  180.  
  181. static object *
  182. time_localtime(self, args)
  183.     object *self;
  184.     object *args;
  185. {
  186.     double when;
  187.     if (!getargs(args, "d", &when))
  188.         return NULL;
  189.     return time_convert((time_t)when, localtime);
  190. }
  191.  
  192. static int
  193. gettmarg(args, p)
  194.     object *args;
  195.     struct tm *p;
  196. {
  197.     if (!getargs(args, "(iiiiiiiii)",
  198.              &p->tm_year,
  199.              &p->tm_mon,
  200.              &p->tm_mday,
  201.              &p->tm_hour,
  202.              &p->tm_min,
  203.              &p->tm_sec,
  204.              &p->tm_wday,
  205.              &p->tm_yday,
  206.              &p->tm_isdst))
  207.         return 0;
  208.     if (p->tm_year >= 1900)
  209.         p->tm_year -= 1900;
  210.     p->tm_mon--;
  211.     p->tm_wday = (p->tm_wday + 1) % 7;
  212.     p->tm_yday--;
  213.     return 1;
  214. }
  215.  
  216. static object *
  217. time_asctime(self, args)
  218.     object *self;
  219.     object *args;
  220. {
  221.     struct tm buf;
  222.     char *p;
  223.     if (!gettmarg(args, &buf))
  224.         return NULL;
  225.     p = asctime(&buf);
  226.     if (p[24] == '\n')
  227.         p[24] = '\0';
  228.     return newstringobject(p);
  229. }
  230.  
  231. static object *
  232. time_ctime(self, args)
  233.     object *self;
  234.     object *args;
  235. {
  236.     double dt;
  237.     time_t tt;
  238.     char *p;
  239.     if (!getargs(args, "d", &dt))
  240.         return NULL;
  241.     tt = dt;
  242.     p = ctime(&tt);
  243.     if (p[24] == '\n')
  244.         p[24] = '\0';
  245.     return newstringobject(p);
  246. }
  247.  
  248. static object *
  249. time_mktime(self, args)
  250.     object *self;
  251.     object *args;
  252. {
  253.     struct tm buf;
  254.     if (!gettmarg(args, &buf))
  255.         return NULL;
  256.     return newintobject((long)mktime(&buf));
  257. }
  258.  
  259. static struct methodlist time_methods[] = {
  260.     {"time",    time_time},
  261. #ifdef HAVE_CLOCK
  262.     {"clock",    time_clock},
  263. #endif
  264.     {"sleep",    time_sleep},
  265.     {"gmtime",    time_gmtime},
  266.     {"localtime",    time_localtime},
  267.     {"asctime",    time_asctime},
  268.     {"ctime",    time_ctime},
  269.     {"mktime",    time_mktime},
  270.     {NULL,        NULL}        /* sentinel */
  271. };
  272.  
  273. void
  274. inittime()
  275. {
  276.     object *m, *d;
  277.     m = initmodule("time", time_methods);
  278.     d = getmoduledict(m);
  279. #ifdef HAVE_TZNAME
  280.     tzset();
  281.     dictinsert(d, "timezone", newintobject((long)timezone));
  282. #ifdef HAVE_ALTZONE
  283.     dictinsert(d, "altzone", newintobject((long)altzone));
  284. #else
  285.     dictinsert(d, "altzone", newintobject((long)timezone-3600));
  286. #endif
  287.     dictinsert(d, "daylight", newintobject((long)daylight));
  288.     dictinsert(d, "tzname", mkvalue("(zz)", tzname[0], tzname[1]));
  289. #else /* !HAVE_TZNAME */
  290. #if HAVE_TM_ZONE
  291.     {
  292. #define YEAR ((time_t)((365 * 24 + 6) * 3600))
  293.         time_t t;
  294.         struct tm *p;
  295.         long winterzone, summerzone;
  296.         char wintername[10], summername[10];
  297.         /* XXX This won't work on the southern hemisphere.
  298.            XXX Anybody got a better idea? */
  299.         t = (time((time_t *)0) / YEAR) * YEAR;
  300.         p = localtime(&t);
  301.         winterzone = -p->tm_gmtoff;
  302.         strncpy(wintername, p->tm_zone ? p->tm_zone : "   ", 9);
  303.         wintername[9] = '\0';
  304.         t += YEAR/2;
  305.         p = localtime(&t);
  306.         summerzone = -p->tm_gmtoff;
  307.         strncpy(summername, p->tm_zone ? p->tm_zone : "   ", 9);
  308.         summername[9] = '\0';
  309.         dictinsert(d, "timezone", newintobject(winterzone));
  310.         dictinsert(d, "altzone", newintobject(summerzone));
  311.         dictinsert(d, "daylight",
  312.                newintobject((long)(winterzone != summerzone)));
  313.         dictinsert(d, "tzname",
  314.                mkvalue("(zz)", wintername, summername));
  315.     }
  316. #endif /* HAVE_TM_ZONE */
  317. #endif /* !HAVE_TZNAME */
  318. }
  319.  
  320.  
  321. /* Implement floattime() for various platforms */
  322.  
  323. static double
  324. floattime()
  325. {
  326.     /* There are three ways to get the time:
  327.        (1) gettimeofday() -- resolution in microseconds
  328.        (2) ftime() -- resolution in milliseconds
  329.        (3) time() -- resolution in seconds
  330.        In all cases the return value is a float in seconds.
  331.        Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  332.        fail, so we fall back on ftime() or time().
  333.        Note: clock resolution does not imply clock accuracy! */
  334. #ifdef HAVE_GETTIMEOFDAY
  335.     {
  336.     struct timeval t;
  337.     if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  338.         return (double)t.tv_sec + t.tv_usec*0.000001;
  339.     }
  340. #endif /* !HAVE_GETTIMEOFDAY */
  341.     {
  342. #ifdef HAVE_FTIME
  343.     struct timeb t;
  344.     ftime(&t);
  345.     return (double)t.time + t.millitm*0.001;
  346. #else /* !HAVE_FTIME */
  347.     time_t secs;
  348.     time(&secs);
  349.     return (double)secs;
  350. #endif /* !HAVE_FTIME */
  351.     }
  352. }
  353.  
  354.  
  355. /* Implement floatsleep() for various platforms */
  356.  
  357.  
  358. static void
  359. floatsleep(secs)
  360.     double secs;
  361. {
  362. #ifdef HAVE_SELECT
  363.     struct timeval t;
  364.     double frac;
  365.     extern double fmod PROTO((double, double));
  366.     extern double floor PROTO((double));
  367.     frac = fmod(secs, 1.0);
  368.     secs = floor(secs);
  369.     t.tv_sec = (long)secs;
  370.     t.tv_usec = (long)(frac*1000000.0);
  371.     (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
  372. #else /* !HAVE_SELECT */
  373. #ifdef macintosh
  374. #define MacTicks    (* (long *)0x16A)
  375.     long deadline;
  376.     deadline = MacTicks + (long)(secs * 60.0);
  377.     while (MacTicks < deadline) {
  378.         if (intrcheck())
  379.             sleep_catcher(SIGINT);
  380.     }
  381. #else /* !macintosh */
  382. #ifdef MSDOS
  383.     struct timeb t1, t2;
  384.     double frac;
  385.     extern double fmod PROTO((double, double));
  386.     extern double floor PROTO((double));
  387.     if (secs <= 0.0)
  388.         return;
  389.     frac = fmod(secs, 1.0);
  390.     secs = floor(secs);
  391.     ftime(&t1);
  392.     t2.time = t1.time + (int)secs;
  393.     t2.millitm = t1.millitm + (int)(frac*1000.0);
  394.     while (t2.millitm >= 1000) {
  395.         t2.time++;
  396.         t2.millitm -= 1000;
  397.     }
  398.     for (;;) {
  399. #ifdef QUICKWIN
  400.         _wyield();
  401. #endif
  402.         ftime(&t1);
  403.         if (t1.time > t2.time ||
  404.             t1.time == t2.time && t1.millitm >= t2.millitm)
  405.             break;
  406.     }
  407. #else /* !MSDOS */
  408. #ifdef _M_IX86
  409.     Sleep((int)(secs*1000));
  410. #else /* _M_IX86 */
  411.     sleep((int)secs);
  412. #endif /* _M_IX86 */
  413. #endif /* !MSDOS */
  414. #endif /* !macintosh */
  415. #endif /* !HAVE_SELECT */
  416. }
  417.