home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / timemodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  20.6 KB  |  834 lines

  1.  
  2. /* Time module */
  3.  
  4. #include "Python.h"
  5. #include "protos.h"
  6.  
  7. #include <ctype.h>
  8.  
  9. #ifdef macintosh
  10. #include <time.h>
  11. #include <OSUtils.h>
  12. #ifdef USE_GUSI2
  13. /* GUSI, the I/O library which has the time() function and such uses the
  14. ** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
  15. ** the ANSI epoch of 1900.
  16. */
  17. #define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
  18. #endif /* USE_GUSI2 */
  19. #else
  20. #include <sys/types.h>
  21. #endif
  22.  
  23. #ifdef _AMIGA
  24. #include <proto/dos.h>
  25. #endif
  26.  
  27. #ifdef QUICKWIN
  28. #include <io.h>
  29. #endif
  30.  
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34.  
  35. #ifdef HAVE_FTIME
  36. #include <sys/timeb.h>
  37. #if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
  38. extern int ftime(struct timeb *);
  39. #endif /* MS_WINDOWS */
  40. #endif /* HAVE_FTIME */
  41.  
  42. #if defined(__WATCOMC__) && !defined(__QNX__)
  43. #include <i86.h>
  44. #else
  45. #ifdef MS_WINDOWS
  46. #include <windows.h>
  47. #ifdef MS_WIN16
  48. /* These overrides not needed for Win32 */
  49. #define timezone _timezone
  50. #define tzname _tzname
  51. #define daylight _daylight
  52. #define altzone _altzone
  53. #endif /* MS_WIN16 */
  54. #endif /* MS_WINDOWS */
  55. #endif /* !__WATCOMC__ || __QNX__ */
  56.  
  57. #if defined(MS_WIN32) && !defined(MS_WIN64)
  58. /* Win32 has better clock replacement
  59.    XXX Win64 does not yet, but might when the platform matures. */
  60. #include <largeint.h>
  61. #undef HAVE_CLOCK /* We have our own version down below */
  62. #endif /* MS_WIN32 && !MS_WIN64 */
  63.  
  64. #if defined(PYCC_VACPP)
  65. #include <sys/time.h>
  66. #endif
  67.  
  68. #ifdef __BEOS__
  69. #include <time.h>
  70. /* For bigtime_t, snooze(). - [cjh] */
  71. #include <support/SupportDefs.h>
  72. #include <kernel/OS.h>
  73. #endif
  74.  
  75. /* Forward declarations */
  76. static int floatsleep(double);
  77. static double floattime(void);
  78.  
  79. /* For Y2K check */
  80. static PyObject *moddict;
  81.  
  82. #ifdef macintosh
  83. /* Our own timezone. We have enough information to deduce whether
  84. ** DST is on currently, but unfortunately we cannot put it to good
  85. ** use because we don't know the rules (and that is needed to have
  86. ** localtime() return correct tm_isdst values for times other than
  87. ** the current time. So, we cop out and only tell the user the current
  88. ** timezone.
  89. */
  90. static long timezone;
  91.  
  92. static void 
  93. initmactimezone(void)
  94. {
  95.     MachineLocation    loc;
  96.     long        delta;
  97.  
  98.     ReadLocation(&loc);
  99.     
  100.     if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
  101.         return;
  102.     
  103.     delta = loc.u.gmtDelta & 0x00FFFFFF;
  104.     
  105.     if (delta & 0x00800000)
  106.         delta |= 0xFF000000;
  107.     
  108.     timezone = -delta;
  109. }
  110. #endif /* macintosh */
  111.  
  112.  
  113. static PyObject *
  114. time_time(PyObject *self, PyObject *args)
  115. {
  116.     double secs;
  117.     if (!PyArg_NoArgs(args))
  118.         return NULL;
  119.     secs = floattime();
  120.     if (secs == 0.0) {
  121.         PyErr_SetFromErrno(PyExc_IOError);
  122.         return NULL;
  123.     }
  124.     return PyFloat_FromDouble(secs);
  125. }
  126.  
  127. static char time_doc[] =
  128. "time() -> floating point number\n\
  129. \n\
  130. Return the current time in seconds since the Epoch.\n\
  131. Fractions of a second may be present if the system clock provides them.";
  132.  
  133. #ifdef HAVE_CLOCK
  134.  
  135. #ifndef CLOCKS_PER_SEC
  136. #ifdef CLK_TCK
  137. #define CLOCKS_PER_SEC CLK_TCK
  138. #else
  139. #define CLOCKS_PER_SEC 1000000
  140. #endif
  141. #endif
  142.  
  143. static PyObject *
  144. time_clock(PyObject *self, PyObject *args)
  145. {
  146.     if (!PyArg_NoArgs(args))
  147.         return NULL;
  148.     return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
  149. }
  150. #endif /* HAVE_CLOCK */
  151.  
  152. #if defined(MS_WIN32) && !defined(MS_WIN64)
  153. /* Due to Mark Hammond */
  154. static PyObject *
  155. time_clock(PyObject *self, PyObject *args)
  156. {
  157.     static LARGE_INTEGER ctrStart;
  158.     static LARGE_INTEGER divisor = {0,0};
  159.     LARGE_INTEGER now, diff, rem;
  160.  
  161.     if (!PyArg_NoArgs(args))
  162.         return NULL;
  163.  
  164.     if (LargeIntegerEqualToZero(divisor)) {
  165.         QueryPerformanceCounter(&ctrStart);
  166.         if (!QueryPerformanceFrequency(&divisor) || 
  167.             LargeIntegerEqualToZero(divisor)) {
  168.                 /* Unlikely to happen - 
  169.                    this works on all intel machines at least! 
  170.                    Revert to clock() */
  171.             return PyFloat_FromDouble(clock());
  172.         }
  173.     }
  174.     QueryPerformanceCounter(&now);
  175.     diff = LargeIntegerSubtract(now, ctrStart);
  176.     diff = LargeIntegerDivide(diff, divisor, &rem);
  177.     /* XXX - we assume both divide results fit in 32 bits.  This is
  178.        true on Intels.  First person who can afford a machine that 
  179.        doesnt deserves to fix it :-)
  180.     */
  181.     return PyFloat_FromDouble((double)diff.LowPart + 
  182.                       ((double)rem.LowPart / (double)divisor.LowPart));
  183. }
  184.  
  185. #define HAVE_CLOCK /* So it gets included in the methods */
  186. #endif /* MS_WIN32 && !MS_WIN64 */
  187.  
  188. #ifdef HAVE_CLOCK
  189. static char clock_doc[] =
  190. "clock() -> floating point number\n\
  191. \n\
  192. Return the CPU time or real time since the start of the process or since\n\
  193. the first call to clock().  This has as much precision as the system records.";
  194. #endif
  195.  
  196. static PyObject *
  197. time_sleep(PyObject *self, PyObject *args)
  198. {
  199.     double secs;
  200.     if (!PyArg_Parse(args, "d", &secs))
  201.         return NULL;
  202.     if (floatsleep(secs) != 0)
  203.         return NULL;
  204.     Py_INCREF(Py_None);
  205.     return Py_None;
  206. }
  207.  
  208. static char sleep_doc[] =
  209. "sleep(seconds)\n\
  210. \n\
  211. Delay execution for a given number of seconds.  The argument may be\n\
  212. a floating point number for subsecond precision.";
  213.  
  214. static PyObject *
  215. tmtotuple(struct tm *p)
  216. {
  217.     return Py_BuildValue("(iiiiiiiii)",
  218.                  p->tm_year + 1900,
  219.                  p->tm_mon + 1,       /* Want January == 1 */
  220.                  p->tm_mday,
  221.                  p->tm_hour,
  222.                  p->tm_min,
  223.                  p->tm_sec,
  224.                  (p->tm_wday + 6) % 7, /* Want Monday == 0 */
  225.                  p->tm_yday + 1,       /* Want January, 1 == 1 */
  226.                  p->tm_isdst);
  227. }
  228.  
  229. static PyObject *
  230. time_convert(time_t when, struct tm * (*function)(const time_t *))
  231. {
  232.     struct tm *p;
  233.     errno = 0;
  234. #if defined(macintosh) && defined(USE_GUSI204)
  235.     when = when + GUSI_TO_MSL_EPOCH;
  236. #endif
  237.     p = function(&when);
  238.     if (p == NULL) {
  239. #ifdef EINVAL
  240.         if (errno == 0)
  241.             errno = EINVAL;
  242. #endif
  243.         return PyErr_SetFromErrno(PyExc_IOError);
  244.     }
  245.     return tmtotuple(p);
  246. }
  247.  
  248. static PyObject *
  249. time_gmtime(PyObject *self, PyObject *args)
  250. {
  251.     double when;
  252.     if (!PyArg_Parse(args, "d", &when))
  253.         return NULL;
  254.     return time_convert((time_t)when, gmtime);
  255. }
  256.  
  257. static char gmtime_doc[] =
  258. "gmtime(seconds) -> tuple\n\
  259. \n\
  260. Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
  261.  
  262. static PyObject *
  263. time_localtime(PyObject *self, PyObject *args)
  264. {
  265.     double when;
  266.     if (!PyArg_Parse(args, "d", &when))
  267.         return NULL;
  268.     return time_convert((time_t)when, localtime);
  269. }
  270.  
  271. static char localtime_doc[] =
  272. "localtime(seconds) -> tuple\n\
  273. Convert seconds since the Epoch to a time tuple expressing local time.";
  274.  
  275. static int
  276. gettmarg(PyObject *args, struct tm *p)
  277. {
  278.     int y;
  279.     memset((void *) p, '\0', sizeof(struct tm));
  280.  
  281.     if (!PyArg_Parse(args, "(iiiiiiiii)",
  282.              &y,
  283.              &p->tm_mon,
  284.              &p->tm_mday,
  285.              &p->tm_hour,
  286.              &p->tm_min,
  287.              &p->tm_sec,
  288.              &p->tm_wday,
  289.              &p->tm_yday,
  290.              &p->tm_isdst))
  291.         return 0;
  292.     if (y < 1900) {
  293.         PyObject *accept = PyDict_GetItemString(moddict,
  294.                             "accept2dyear");
  295.         if (accept == NULL || !PyInt_Check(accept) ||
  296.             PyInt_AsLong(accept) == 0) {
  297.             PyErr_SetString(PyExc_ValueError,
  298.                     "year >= 1900 required");
  299.             return 0;
  300.         }
  301.         if (69 <= y && y <= 99)
  302.             y += 1900;
  303.         else if (0 <= y && y <= 68)
  304.             y += 2000;
  305.         else {
  306.             PyErr_SetString(PyExc_ValueError,
  307.                     "year out of range (00-99, 1900-*)");
  308.             return 0;
  309.         }
  310.     }
  311.     p->tm_year = y - 1900;
  312.     p->tm_mon--;
  313.     p->tm_wday = (p->tm_wday + 1) % 7;
  314.     p->tm_yday--;
  315.     return 1;
  316. }
  317.  
  318. #ifdef HAVE_STRFTIME
  319. static PyObject *
  320. time_strftime(PyObject *self, PyObject *args)
  321. {
  322.     PyObject *tup;
  323.     struct tm buf;
  324.     const char *fmt;
  325.     size_t fmtlen, buflen;
  326.     char *outbuf = 0;
  327.     size_t i;
  328.  
  329.     memset((void *) &buf, '\0', sizeof(buf));
  330.  
  331.     if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup) 
  332.         || !gettmarg(tup, &buf))
  333.         return NULL;
  334.     fmtlen = strlen(fmt);
  335.  
  336.     /* I hate these functions that presume you know how big the output
  337.      * will be ahead of time...
  338.      */
  339.     for (i = 1024; ; i += i) {
  340.         outbuf = malloc(i);
  341.         if (outbuf == NULL) {
  342.             return PyErr_NoMemory();
  343.         }
  344.         buflen = strftime(outbuf, i, fmt, &buf);
  345.         if (buflen > 0 || i >= 256 * fmtlen) {
  346.             /* If the buffer is 256 times as long as the format,
  347.                it's probably not failing for lack of room!
  348.                More likely, the format yields an empty result,
  349.                e.g. an empty format, or %Z when the timezone
  350.                is unknown. */
  351.             PyObject *ret;
  352.             ret = PyString_FromStringAndSize(outbuf, buflen);
  353.             free(outbuf);
  354.             return ret;
  355.         }
  356.         free(outbuf);
  357.     }
  358. }
  359.  
  360. static char strftime_doc[] =
  361. "strftime(format, tuple) -> string\n\
  362. \n\
  363. Convert a time tuple to a string according to a format specification.\n\
  364. See the library reference manual for formatting codes.";
  365. #endif /* HAVE_STRFTIME */
  366.  
  367. #ifdef HAVE_STRPTIME
  368.  
  369. #if 0
  370. /* Enable this if it's not declared in <time.h> */
  371. extern char *strptime(const char *, const char *, struct tm *);
  372. #endif
  373.  
  374. static PyObject *
  375. time_strptime(PyObject *self, PyObject *args)
  376. {
  377.     struct tm tm;
  378.     char *fmt = "%a %b %d %H:%M:%S %Y";
  379.     char *buf;
  380.     char *s;
  381.  
  382.     if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
  383.             return NULL;
  384.     memset((void *) &tm, '\0', sizeof(tm));
  385.     s = strptime(buf, fmt, &tm);
  386.     if (s == NULL) {
  387.         PyErr_SetString(PyExc_ValueError, "format mismatch");
  388.         return NULL;
  389.     }
  390.     while (*s && isspace(*s))
  391.         s++;
  392.     if (*s) {
  393.         PyErr_Format(PyExc_ValueError,
  394.                  "unconverted data remains: '%.400s'", s);
  395.         return NULL;
  396.     }
  397.     return tmtotuple(&tm);
  398. }
  399.  
  400. static char strptime_doc[] =
  401. "strptime(string, format) -> tuple\n\
  402. Parse a string to a time tuple according to a format specification.\n\
  403. See the library reference manual for formatting codes (same as strftime()).";
  404. #endif /* HAVE_STRPTIME */
  405.  
  406. static PyObject *
  407. time_asctime(PyObject *self, PyObject *args)
  408. {
  409.     PyObject *tup;
  410.     struct tm buf;
  411.     char *p;
  412.     if (!PyArg_ParseTuple(args, "O:asctime", &tup))
  413.         return NULL;
  414.     if (!gettmarg(tup, &buf))
  415.         return NULL;
  416.     p = asctime(&buf);
  417.     if (p[24] == '\n')
  418.         p[24] = '\0';
  419.     return PyString_FromString(p);
  420. }
  421.  
  422. static char asctime_doc[] =
  423. "asctime(tuple) -> string\n\
  424. \n\
  425. Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
  426.  
  427. static PyObject *
  428. time_ctime(PyObject *self, PyObject *args)
  429. {
  430.     double dt;
  431.     time_t tt;
  432.     char *p;
  433.     if (!PyArg_Parse(args, "d", &dt))
  434.         return NULL;
  435.     tt = (time_t)dt;
  436. #if defined(macintosh) && defined(USE_GUSI204)
  437.     tt = tt + GUSI_TO_MSL_EPOCH;
  438. #endif
  439.     p = ctime(&tt);
  440.     if (p == NULL) {
  441.         PyErr_SetString(PyExc_ValueError, "unconvertible time");
  442.         return NULL;
  443.     }
  444.     if (p[24] == '\n')
  445.         p[24] = '\0';
  446.     return PyString_FromString(p);
  447. }
  448.  
  449. static char ctime_doc[] =
  450. "ctime(seconds) -> string\n\
  451. \n\
  452. Convert a time in seconds since the Epoch to a string in local time.\n\
  453. This is equivalent to asctime(localtime(seconds)).";
  454.  
  455. #ifdef HAVE_MKTIME
  456. static PyObject *
  457. time_mktime(PyObject *self, PyObject *args)
  458. {
  459.     PyObject *tup;
  460.     struct tm buf;
  461.     time_t tt;
  462.     if (!PyArg_ParseTuple(args, "O:mktime", &tup))
  463.         return NULL;
  464.     tt = time(&tt);
  465.     buf = *localtime(&tt);
  466.     if (!gettmarg(tup, &buf))
  467.         return NULL;
  468.     tt = mktime(&buf);
  469.     if (tt == (time_t)(-1)) {
  470.         PyErr_SetString(PyExc_OverflowError,
  471.                                 "mktime argument out of range");
  472.         return NULL;
  473.     }
  474. #if defined(macintosh) && defined(USE_GUSI2)
  475.     tt = tt - GUSI_TO_MSL_EPOCH;
  476. #endif
  477.     return PyFloat_FromDouble((double)tt);
  478. }
  479.  
  480. static char mktime_doc[] =
  481. "mktime(tuple) -> floating point number\n\
  482. \n\
  483. Convert a time tuple in local time to seconds since the Epoch.";
  484. #endif /* HAVE_MKTIME */
  485.  
  486. static PyMethodDef time_methods[] = {
  487.     {"time",    time_time, METH_OLDARGS, time_doc},
  488. #ifdef HAVE_CLOCK
  489.     {"clock",    time_clock, METH_OLDARGS, clock_doc},
  490. #endif
  491.     {"sleep",    time_sleep, METH_OLDARGS, sleep_doc},
  492.     {"gmtime",    time_gmtime, METH_OLDARGS, gmtime_doc},
  493.     {"localtime",    time_localtime, METH_OLDARGS, localtime_doc},
  494.     {"asctime",    time_asctime, METH_VARARGS, asctime_doc},
  495.     {"ctime",    time_ctime, METH_OLDARGS, ctime_doc},
  496. #ifdef HAVE_MKTIME
  497.     {"mktime",    time_mktime, METH_VARARGS, mktime_doc},
  498. #endif
  499. #ifdef HAVE_STRFTIME
  500.     {"strftime",    time_strftime, METH_VARARGS, strftime_doc},
  501. #endif
  502. #ifdef HAVE_STRPTIME
  503.     {"strptime",    time_strptime, METH_VARARGS, strptime_doc},
  504. #endif
  505.     {NULL,        NULL}        /* sentinel */
  506. };
  507.  
  508. static void
  509. ins(PyObject *d, char *name, PyObject *v)
  510. {
  511.     /* Don't worry too much about errors, they'll be caught by the
  512.      * caller of inittime().
  513.      */
  514.     if (v)
  515.         PyDict_SetItemString(d, name, v);
  516.     Py_XDECREF(v);
  517. }
  518.  
  519.  
  520. static char module_doc[] =
  521. "This module provides various functions to manipulate time values.\n\
  522. \n\
  523. There are two standard representations of time.  One is the number\n\
  524. of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer\n\
  525. or a floating point number (to represent fractions of seconds).\n\
  526. The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
  527. The actual value can be retrieved by calling gmtime(0).\n\
  528. \n\
  529. The other representation is a tuple of 9 integers giving local time.\n\
  530. The tuple items are:\n\
  531.   year (four digits, e.g. 1998)\n\
  532.   month (1-12)\n\
  533.   day (1-31)\n\
  534.   hours (0-23)\n\
  535.   minutes (0-59)\n\
  536.   seconds (0-59)\n\
  537.   weekday (0-6, Monday is 0)\n\
  538.   Julian day (day in the year, 1-366)\n\
  539.   DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
  540. If the DST flag is 0, the time is given in the regular time zone;\n\
  541. if it is 1, the time is given in the DST time zone;\n\
  542. if it is -1, mktime() should guess based on the date and time.\n\
  543. \n\
  544. Variables:\n\
  545. \n\
  546. timezone -- difference in seconds between UTC and local standard time\n\
  547. altzone -- difference in  seconds between UTC and local DST time\n\
  548. daylight -- whether local time should reflect DST\n\
  549. tzname -- tuple of (standard time zone name, DST time zone name)\n\
  550. \n\
  551. Functions:\n\
  552. \n\
  553. time() -- return current time in seconds since the Epoch as a float\n\
  554. clock() -- return CPU time since process start as a float\n\
  555. sleep() -- delay for a number of seconds given as a float\n\
  556. gmtime() -- convert seconds since Epoch to UTC tuple\n\
  557. localtime() -- convert seconds since Epoch to local time tuple\n\
  558. asctime() -- convert time tuple to string\n\
  559. ctime() -- convert time in seconds to string\n\
  560. mktime() -- convert local time tuple to seconds since Epoch\n\
  561. strftime() -- convert time tuple to string according to format specification\n\
  562. strptime() -- parse string to time tuple according to format specification\n\
  563. ";
  564.   
  565.  
  566. DL_EXPORT(void)
  567. inittime(void)
  568. {
  569.     PyObject *m, *d;
  570.     char *p;
  571.     m = Py_InitModule3("time", time_methods, module_doc);
  572.     d = PyModule_GetDict(m);
  573.     /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
  574.     p = getenv("PYTHONY2K");
  575.     ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
  576.     /* Squirrel away the module's dictionary for the y2k check */
  577.     Py_INCREF(d);
  578.     moddict = d;
  579. #if defined(HAVE_TZNAME) && !defined(__GLIBC__)
  580.     tzset();
  581. #ifdef PYOS_OS2
  582.     ins(d, "timezone", PyInt_FromLong((long)_timezone));
  583. #else /* !PYOS_OS2 */
  584.     ins(d, "timezone", PyInt_FromLong((long)timezone));
  585. #endif /* PYOS_OS2 */
  586. #ifdef HAVE_ALTZONE
  587.     ins(d, "altzone", PyInt_FromLong((long)altzone));
  588. #else
  589. #ifdef PYOS_OS2
  590.     ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
  591. #else /* !PYOS_OS2 */
  592.     ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
  593. #endif /* PYOS_OS2 */
  594. #endif
  595.     ins(d, "daylight", PyInt_FromLong((long)daylight));
  596.     ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
  597. #else /* !HAVE_TZNAME || __GLIBC__ */
  598. #ifdef HAVE_TM_ZONE
  599.     {
  600. #define YEAR ((time_t)((365 * 24 + 6) * 3600))
  601.         time_t t;
  602.         struct tm *p;
  603.         long janzone, julyzone;
  604.         char janname[10], julyname[10];
  605.         t = (time((time_t *)0) / YEAR) * YEAR;
  606.         p = localtime(&t);
  607.         janzone = -p->tm_gmtoff;
  608.         strncpy(janname, p->tm_zone ? p->tm_zone : "   ", 9);
  609.         janname[9] = '\0';
  610.         t += YEAR/2;
  611.         p = localtime(&t);
  612.         julyzone = -p->tm_gmtoff;
  613.         strncpy(julyname, p->tm_zone ? p->tm_zone : "   ", 9);
  614.         julyname[9] = '\0';
  615.         
  616.         if( janzone < julyzone ) {
  617.             /* DST is reversed in the southern hemisphere */
  618.             ins(d, "timezone", PyInt_FromLong(julyzone));
  619.             ins(d, "altzone", PyInt_FromLong(janzone));
  620.             ins(d, "daylight",
  621.                 PyInt_FromLong((long)(janzone != julyzone)));
  622.             ins(d, "tzname",
  623.                 Py_BuildValue("(zz)", julyname, janname));
  624.         } else {
  625.             ins(d, "timezone", PyInt_FromLong(janzone));
  626.             ins(d, "altzone", PyInt_FromLong(julyzone));
  627.             ins(d, "daylight",
  628.                 PyInt_FromLong((long)(janzone != julyzone)));
  629.             ins(d, "tzname",
  630.                 Py_BuildValue("(zz)", janname, julyname));
  631.         }
  632.     }
  633. #else
  634. #ifdef macintosh
  635.     /* The only thing we can obtain is the current timezone
  636.     ** (and whether dst is currently _active_, but that is not what
  637.     ** we're looking for:-( )
  638.     */
  639.     initmactimezone();
  640.     ins(d, "timezone", PyInt_FromLong(timezone));
  641.     ins(d, "altzone", PyInt_FromLong(timezone));
  642.     ins(d, "daylight", PyInt_FromLong((long)0));
  643.     ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
  644. #endif /* macintosh */
  645. #endif /* HAVE_TM_ZONE */
  646. #endif /* !HAVE_TZNAME || __GLIBC__ */
  647. }
  648.  
  649.  
  650. /* Implement floattime() for various platforms */
  651.  
  652. static double
  653. floattime(void)
  654. {
  655.     /* There are three ways to get the time:
  656.       (1) gettimeofday() -- resolution in microseconds
  657.       (2) ftime() -- resolution in milliseconds
  658.       (3) time() -- resolution in seconds
  659.       In all cases the return value is a float in seconds.
  660.       Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  661.       fail, so we fall back on ftime() or time().
  662.       Note: clock resolution does not imply clock accuracy! */
  663. #ifdef HAVE_GETTIMEOFDAY
  664.     {
  665.         struct timeval t;
  666. #ifdef GETTIMEOFDAY_NO_TZ
  667.         if (gettimeofday(&t) == 0)
  668.             return (double)t.tv_sec + t.tv_usec*0.000001;
  669. #else /* !GETTIMEOFDAY_NO_TZ */
  670.         if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  671.             return (double)t.tv_sec + t.tv_usec*0.000001;
  672. #endif /* !GETTIMEOFDAY_NO_TZ */
  673.     }
  674. #endif /* !HAVE_GETTIMEOFDAY */
  675.     {
  676. #if defined(HAVE_FTIME)
  677.         struct timeb t;
  678.         ftime(&t);
  679.         return (double)t.time + (double)t.millitm * (double)0.001;
  680. #else /* !HAVE_FTIME */
  681.         time_t secs;
  682.         time(&secs);
  683.         return (double)secs;
  684. #endif /* !HAVE_FTIME */
  685.     }
  686. }
  687.  
  688.  
  689. /* Implement floatsleep() for various platforms.
  690.    When interrupted (or when another error occurs), return -1 and
  691.    set an exception; else return 0. */
  692.  
  693. static int
  694. floatsleep(double secs)
  695. {
  696. /* XXX Should test for MS_WIN32 first! */
  697. #if defined(HAVE_SELECT) && !defined(__BEOS__)
  698.     struct timeval t;
  699.     double frac;
  700. #if defined (AMITCP) || defined(INET225)
  701.     /* check for availability of an Amiga TCP stack for select() */
  702.     if(!checksocketlib())
  703.     {
  704.         /* no bsdsocket.library-- use dos/Delay() */
  705.         PyErr_Clear();
  706.         Delay((long)(secs*50));        /* XXX Can't interrupt this sleep */
  707.         return 0;
  708.     }
  709. #endif
  710.     frac = fmod(secs, 1.0);
  711.     secs = floor(secs);
  712.     t.tv_sec = (long)secs;
  713.     t.tv_usec = (long)(frac*1000000.0);
  714.     Py_BEGIN_ALLOW_THREADS
  715.     if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
  716. #ifdef EINTR
  717.         if (errno != EINTR) {
  718. #else
  719.         if (1) {
  720. #endif
  721.             Py_BLOCK_THREADS
  722.             PyErr_SetFromErrno(PyExc_IOError);
  723.             return -1;
  724.         }
  725.     }
  726.     Py_END_ALLOW_THREADS
  727. #else /* !HAVE_SELECT || __BEOS__ */
  728. #ifdef macintosh
  729. #define MacTicks    (* (long *)0x16A)
  730.     long deadline;
  731.     deadline = MacTicks + (long)(secs * 60.0);
  732.     while (MacTicks < deadline) {
  733.         /* XXX Should call some yielding function here */
  734.         if (PyErr_CheckSignals())
  735.             return -1;
  736.     }
  737. #else /* !macintosh */
  738. #if defined(__WATCOMC__) && !defined(__QNX__)
  739.     /* XXX Can't interrupt this sleep */
  740.     Py_BEGIN_ALLOW_THREADS
  741.     delay((int)(secs * 1000 + 0.5));  /* delay() uses milliseconds */
  742.     Py_END_ALLOW_THREADS
  743. #else /* !__WATCOMC__ || __QNX__ */
  744. #ifdef MSDOS
  745.     struct timeb t1, t2;
  746.     double frac;
  747.     extern double fmod(double, double);
  748.     extern double floor(double);
  749.     if (secs <= 0.0)
  750.         return;
  751.     frac = fmod(secs, 1.0);
  752.     secs = floor(secs);
  753.     ftime(&t1);
  754.     t2.time = t1.time + (int)secs;
  755.     t2.millitm = t1.millitm + (int)(frac*1000.0);
  756.     while (t2.millitm >= 1000) {
  757.         t2.time++;
  758.         t2.millitm -= 1000;
  759.     }
  760.     for (;;) {
  761. #ifdef QUICKWIN
  762.         Py_BEGIN_ALLOW_THREADS
  763.         _wyield();
  764.         Py_END_ALLOW_THREADS
  765. #endif
  766.         if (PyErr_CheckSignals())
  767.             return -1;
  768.         ftime(&t1);
  769.         if (t1.time > t2.time ||
  770.             t1.time == t2.time && t1.millitm >= t2.millitm)
  771.             break;
  772.     }
  773. #else /* !MSDOS */
  774. #ifdef MS_WIN32
  775.     {
  776.         double millisecs = secs * 1000.0;
  777.         if (millisecs > (double)ULONG_MAX) {
  778.             PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
  779.             return -1;
  780.         }
  781.         /* XXX Can't interrupt this sleep */
  782.         Py_BEGIN_ALLOW_THREADS
  783.         Sleep((unsigned long)millisecs);
  784.         Py_END_ALLOW_THREADS
  785.     }
  786. #else /* !MS_WIN32 */
  787. #ifdef PYOS_OS2
  788.     /* This Sleep *IS* Interruptable by Exceptions */
  789.     Py_BEGIN_ALLOW_THREADS
  790.     if (DosSleep(secs * 1000) != NO_ERROR) {
  791.         Py_BLOCK_THREADS
  792.         PyErr_SetFromErrno(PyExc_IOError);
  793.         return -1;
  794.     }
  795.     Py_END_ALLOW_THREADS
  796. #else /* !PYOS_OS2 */
  797. #ifdef __BEOS__
  798.     /* This sleep *CAN BE* interrupted. */
  799.     {
  800.         if( secs <= 0.0 ) {
  801.             return;
  802.         }
  803.         
  804.         Py_BEGIN_ALLOW_THREADS
  805.         /* BeOS snooze() is in microseconds... */
  806.         if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
  807.             Py_BLOCK_THREADS
  808.             PyErr_SetFromErrno( PyExc_IOError );
  809.             return -1;
  810.         }
  811.         Py_END_ALLOW_THREADS
  812.     }
  813. #else /* !__BEOS__ */
  814. #ifdef _AMIGA
  815.     /* XXX Can't interrupt this sleep */
  816.     Py_BEGIN_ALLOW_THREADS
  817.     Delay((long)(secs*50));
  818.     Py_END_ALLOW_THREADS
  819. #else /* !_AMIGA */
  820.     /* XXX Can't interrupt this sleep */
  821.     Py_BEGIN_ALLOW_THREADS
  822.     sleep((int)secs);
  823.     Py_END_ALLOW_THREADS
  824. #endif /* !_AMIGA */
  825. #endif /* !__BEOS__ */
  826. #endif /* !PYOS_OS2 */
  827. #endif /* !MS_WIN32 */
  828. #endif /* !MSDOS */
  829. #endif /* !__WATCOMC__ || __QNX__ */
  830. #endif /* !macintosh */
  831. #endif /* !HAVE_SELECT */
  832.     return 0;
  833. }
  834.