📄 timemodule.c
字号:
return PyString_FromString(p);
}
const static char asctime_doc[] =
"asctime([tuple]) -> string\n\
\n\
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
When the time tuple is not present, current time as returned by localtime()\n\
is used.";
static PyObject *
time_ctime(PyObject *self, PyObject *args)
{
double dt;
time_t tt;
char *p;
if (PyTuple_Size(args) == 0)
tt = time(NULL);
else {
if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
return NULL;
tt = (time_t)dt;
}
#if defined(macintosh) && defined(USE_GUSI204)
tt = tt + GUSI_TO_MSL_EPOCH;
#endif
p = ctime(&tt);
if (p == NULL) {
PyErr_SetString(PyExc_ValueError, "unconvertible time");
return NULL;
}
if (p[24] == '\n')
p[24] = '\0';
return PyString_FromString(p);
}
#ifdef SYMBIAN
static long
symbian_get_timezone()
{
time_t tt;
tt = time(&tt);
tt = -e32_UTC_offset();
#if SERIES60_VERSION>=30
if (e32_daylight_saving_on()) {
tt += 3600;
}
#endif
return (long)tt;
}
static long
symbian_get_altzone()
{
time_t tt;
tt = time(&tt);
tt = -(e32_UTC_offset());
#if SERIES60_VERSION<=28
tt -= 3600;
#else
if (!e32_daylight_saving_on()) {
tt -= 3600;
}
#endif
return (long)tt;
}
extern long e32_daylight_saving(void);
static long
symbian_get_daylight()
{
return (long)e32_daylight_saving();
}
#endif // SYMBIAN
const static char ctime_doc[] =
"ctime(seconds) -> string\n\
\n\
Convert a time in seconds since the Epoch to a string in local time.\n\
This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
not present, current time as returned by localtime() is used.";
#ifdef HAVE_MKTIME
static PyObject *
time_mktime(PyObject *self, PyObject *args)
{
PyObject *tup;
struct tm buf;
time_t tt;
if (!PyArg_ParseTuple(args, "O:mktime", &tup))
return NULL;
tt = time(&tt);
buf = *localtime(&tt);
if (!gettmarg(tup, &buf))
return NULL;
tt = mktime(&buf);
#if SERIES60_VERSION>28
tt -= e32_UTC_offset();
#endif
if (tt == (time_t)(-1)) {
PyErr_SetString(PyExc_OverflowError,
"mktime argument out of range");
return NULL;
}
#if defined(macintosh) && defined(USE_GUSI211)
tt = tt - GUSI_TO_MSL_EPOCH;
#endif
return PyFloat_FromDouble((double)tt);
}
const static char mktime_doc[] =
"mktime(tuple) -> floating point number\n\
\n\
Convert a time tuple in local time to seconds since the Epoch.";
#endif /* HAVE_MKTIME */
const static PyMethodDef time_methods[] = {
{"time", time_time, METH_VARARGS, time_doc},
#ifdef HAVE_CLOCK
{"clock", time_clock, METH_VARARGS, clock_doc},
#endif
{"sleep", time_sleep, METH_VARARGS, sleep_doc},
{"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
{"localtime", time_localtime, METH_VARARGS, localtime_doc},
{"asctime", time_asctime, METH_VARARGS, asctime_doc},
{"ctime", time_ctime, METH_VARARGS, ctime_doc},
#ifdef HAVE_MKTIME
{"mktime", time_mktime, METH_VARARGS, mktime_doc},
#endif
#ifdef HAVE_STRFTIME
{"strftime", time_strftime, METH_VARARGS, strftime_doc},
#endif
#ifdef HAVE_STRPTIME
{"strptime", time_strptime, METH_VARARGS, strptime_doc},
#endif
{NULL, NULL} /* sentinel */
};
static void
ins(PyObject *d, char *name, PyObject *v)
{
/* Don't worry too much about errors, they'll be caught by the
* caller of inittime().
*/
if (v)
PyDict_SetItemString(d, name, v);
Py_XDECREF(v);
}
const static char module_doc[] =
"This module provides various functions to manipulate time values.\n\
\n\
There are two standard representations of time. One is the number\n\
of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
or a floating point number (to represent fractions of seconds).\n\
The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
The actual value can be retrieved by calling gmtime(0).\n\
\n\
The other representation is a tuple of 9 integers giving local time.\n\
The tuple items are:\n\
year (four digits, e.g. 1998)\n\
month (1-12)\n\
day (1-31)\n\
hours (0-23)\n\
minutes (0-59)\n\
seconds (0-59)\n\
weekday (0-6, Monday is 0)\n\
Julian day (day in the year, 1-366)\n\
DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
If the DST flag is 0, the time is given in the regular time zone;\n\
if it is 1, the time is given in the DST time zone;\n\
if it is -1, mktime() should guess based on the date and time.\n\
\n\
Variables:\n\
\n\
timezone -- difference in seconds between UTC and local standard time\n\
altzone -- difference in seconds between UTC and local DST time\n\
daylight -- whether local time should reflect DST\n\
tzname -- tuple of (standard time zone name, DST time zone name)\n\
\n\
Functions:\n\
\n\
time() -- return current time in seconds since the Epoch as a float\n\
clock() -- return CPU time since process start as a float\n\
sleep() -- delay for a number of seconds given as a float\n\
gmtime() -- convert seconds since Epoch to UTC tuple\n\
localtime() -- convert seconds since Epoch to local time tuple\n\
asctime() -- convert time tuple to string\n\
ctime() -- convert time in seconds to string\n\
mktime() -- convert local time tuple to seconds since Epoch\n\
strftime() -- convert time tuple to string according to format specification\n\
strptime() -- parse string to time tuple according to format specification\n\
";
DL_EXPORT(void)
inittime(void)
{
PyObject *m, *d;
char *p;
m = Py_InitModule3("time", time_methods, module_doc);
d = PyModule_GetDict(m);
/* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
p = Py_GETENV("PYTHONY2K");
ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
/* Squirrel away the module's dictionary for the y2k check */
Py_INCREF(d);
moddict = d;
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
tzset();
#ifdef PYOS_OS2
ins(d, "timezone", PyInt_FromLong((long)_timezone));
#else /* !PYOS_OS2 */
ins(d, "timezone", PyInt_FromLong((long)timezone));
#endif /* PYOS_OS2 */
#ifdef HAVE_ALTZONE
ins(d, "altzone", PyInt_FromLong((long)altzone));
#else
#ifdef PYOS_OS2
ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
#else /* !PYOS_OS2 */
ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
#endif /* PYOS_OS2 */
#endif
ins(d, "daylight", PyInt_FromLong((long)daylight));
ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
#ifdef HAVE_TM_ZONE
{
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
time_t t;
struct tm *p;
long janzone, julyzone;
char janname[10], julyname[10];
t = (time((time_t *)0) / YEAR) * YEAR;
p = localtime(&t);
janzone = -p->tm_gmtoff;
strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
janname[9] = '\0';
t += YEAR/2;
p = localtime(&t);
julyzone = -p->tm_gmtoff;
strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
julyname[9] = '\0';
if( janzone < julyzone ) {
/* DST is reversed in the southern hemisphere */
ins(d, "timezone", PyInt_FromLong(julyzone));
ins(d, "altzone", PyInt_FromLong(janzone));
ins(d, "daylight",
PyInt_FromLong((long)(janzone != julyzone)));
ins(d, "tzname",
Py_BuildValue("(zz)", julyname, janname));
} else {
ins(d, "timezone", PyInt_FromLong(janzone));
ins(d, "altzone", PyInt_FromLong(julyzone));
ins(d, "daylight",
PyInt_FromLong((long)(janzone != julyzone)));
ins(d, "tzname",
Py_BuildValue("(zz)", janname, julyname));
}
}
#else
#ifdef macintosh
/* The only thing we can obtain is the current timezone
** (and whether dst is currently _active_, but that is not what
** we're looking for:-( )
*/
initmactimezone();
ins(d, "timezone", PyInt_FromLong(timezone));
ins(d, "altzone", PyInt_FromLong(timezone));
ins(d, "daylight", PyInt_FromLong((long)0));
ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
#endif /* macintosh */
#endif /* HAVE_TM_ZONE */
#ifdef __CYGWIN__
tzset();
ins(d, "timezone", PyInt_FromLong(_timezone));
ins(d, "altzone", PyInt_FromLong(_timezone));
ins(d, "daylight", PyInt_FromLong(_daylight));
ins(d, "tzname", Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#endif /* __CYGWIN__ */
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
#ifdef SYMBIAN
ins(d, "timezone", PyInt_FromLong(symbian_get_timezone()));
ins(d, "altzone", PyInt_FromLong(symbian_get_altzone()));
ins(d, "daylight", PyInt_FromLong(symbian_get_daylight()));
#endif
PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
PyDict_SetItemString(d, "struct_time", (PyObject*) &StructTimeType);
}
/* Implement floattime() for various platforms */
static double
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 */
{
#if defined(HAVE_FTIME)
struct timeb t;
ftime(&t);
return (double)t.time + (double)t.millitm * (double)0.001;
#else /* !HAVE_FTIME */
time_t secs;
time(&secs);
#ifdef SYMBIAN
return (double)(secs);
#else
return (double)secs;
#endif
#endif /* !HAVE_FTIME */
}
}
/* Implement floatsleep() for various platforms.
When interrupted (or when another error occurs), return -1 and
set an exception; else return 0. */
static int
floatsleep(double secs)
{
/* XXX Should test for MS_WIN32 first! */
#if defined(HAVE_SELECT) && !defined(__BEOS__)
struct timeval t;
double frac;
frac = fmod(secs, 1.0);
secs = floor(secs);
t.tv_sec = (long)secs;
t.tv_usec = (long)(frac*1000000.0);
Py_BEGIN_ALLOW_THREADS
if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
#ifdef EINTR
if (errno != EINTR) {
#else
if (1) {
#endif
Py_BLOCK_THREADS
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
}
Py_END_ALLOW_THREADS
#else /* !HAVE_SELECT || __BEOS__ */
#ifdef macintosh
#define MacTicks (* (long *)0x16A)
long deadline;
deadline = MacTicks + (long)(secs * 60.0);
while (MacTicks < deadline) {
/* XXX Should call some yielding function here */
if (PyErr_CheckSignals())
return -1;
}
#else /* !macintosh */
#if defined(__WATCOMC__) && !defined(__QNX__)
/* XXX Can't interrupt this sleep */
Py_BEGIN_ALLOW_THREADS
delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Py_END_ALLOW_THREADS
#else /* !__WATCOMC__ || __QNX__ */
#ifdef MSDOS
struct timeb t1, t2;
double frac;
extern double fmod(double, double);
extern double floor(double);
if (secs <= 0.0)
return;
frac = fmod(secs, 1.0);
secs = floor(secs);
ftime(&t1);
t2.time = t1.time + (int)secs;
t2.millitm = t1.millitm + (int)(frac*1000.0);
while (t2.millitm >= 1000) {
t2.time++;
t2.millitm -= 1000;
}
for (;;) {
#ifdef QUICKWIN
Py_BEGIN_ALLOW_THREADS
_wyield();
Py_END_ALLOW_THREADS
#endif
if (PyErr_CheckSignals())
return -1;
ftime(&t1);
if (t1.time > t2.time ||
t1.time == t2.time && t1.millitm >= t2.millitm)
break;
}
#else /* !MSDOS */
#ifdef MS_WIN32
{
double millisecs = secs * 1000.0;
if (millisecs > (double)ULONG_MAX) {
PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
return -1;
}
/* XXX Can't interrupt this sleep */
Py_BEGIN_ALLOW_THREADS
Sleep((unsigned long)millisecs);
Py_END_ALLOW_THREADS
}
#else /* !MS_WIN32 */
#ifdef PYOS_OS2
/* This Sleep *IS* Interruptable by Exceptions */
Py_BEGIN_ALLOW_THREADS
if (DosSleep(secs * 1000) != NO_ERROR) {
Py_BLOCK_THREADS
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
Py_END_ALLOW_THREADS
#else /* !PYOS_OS2 */
#ifdef __BEOS__
/* This sleep *CAN BE* interrupted. */
{
if( secs <= 0.0 ) {
return;
}
Py_BEGIN_ALLOW_THREADS
/* BeOS snooze() is in microseconds... */
if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Py_BLOCK_THREADS
PyErr_SetFromErrno( PyExc_IOError );
return -1;
}
Py_END_ALLOW_THREADS
}
#else /* !__BEOS__ */
#ifdef RISCOS
if (secs <= 0.0)
return 0;
Py_BEGIN_ALLOW_THREADS
/* This sleep *CAN BE* interrupted. */
if ( sleep(secs) )
return -1;
Py_END_ALLOW_THREADS
#else /* !RISCOS */
/* XXX Can't interrupt this sleep */
Py_BEGIN_ALLOW_THREADS
#ifdef SYMBIAN
/* Workaround for Symbian estdlib bug. sleep() is broken and crashes for >=2148 second sleeps.
* See PyS60 bug [ 1446275 ] 231:time.sleep fails for >=2148 second sleep.
*/
{
int delay_left;
for (delay_left=secs; delay_left>2000; delay_left-=2000)
sleep(2000);
sleep(delay_left);
}
#else
sleep((int)secs);
#endif
Py_END_ALLOW_THREADS
#endif /* !RISCOS */
#endif /* !__BEOS__ */
#endif /* !PYOS_OS2 */
#endif /* !MS_WIN32 */
#endif /* !MSDOS */
#endif /* !__WATCOMC__ || __QNX__ */
#endif /* !macintosh */
#endif /* !HAVE_SELECT */
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -