diff --git a/Modules/timemodule.c b/Modules/timemodule.c --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -49,18 +49,94 @@ static OSVERSIONINFOEX winver; #endif +/*[clinic input] +module time +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + + +/* This wrapper encapsulates a time_t value that can also be "not set". + Typically, an unset value is understood to refer to the current time + as returned by time(NULL). + + (The maybe prefix refers to the similar Haskell datatype) +*/ +typedef struct { + int is_nothing; + time_t value; +} maybe_time_t; +#define MAYBE_TIME_T_DEFAULT {1, 0} + +/* Return encapsulated value or, if not set, current time */ +time_t unwrap_maybe_time_t(maybe_time_t when) +{ + if(when.is_nothing) + return time(NULL); + else + return when.value; +} + +/* C Converter for argument clinic */ +static int +PyObject_to_maybe_time_t(PyObject *obj, maybe_time_t *when) +{ + if (obj == NULL || obj == Py_None) { + when->is_nothing = 1; + } + else { + if (_PyTime_ObjectToTime_t(obj, &(when->value)) == -1) + return 0; + when->is_nothing = 0; + } + return 1; +} + + +/*[python input] +class maybe_time_t_converter(CConverter): + type = 'maybe_time_t' + converter = 'PyObject_to_maybe_time_t' + + def converter_init(self): + if self.default is None: + self.c_default = 'MAYBE_TIME_T_DEFAULT' +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[clinic input] +module time +time.time + +Return the current time in seconds since the Epoch. + +Fractions of a second may be present if the system clock provides them. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_time__doc__, +"time()\n" +"Return the current time in seconds since the Epoch.\n" +"\n" +"Fractions of a second may be present if the system clock provides them."); + +#define TIME_TIME_METHODDEF \ + {"time", (PyCFunction)time_time, METH_NOARGS, time_time__doc__}, + static PyObject * -time_time(PyObject *self, PyObject *unused) +time_time_impl(PyModuleDef *module); + +static PyObject * +time_time(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return time_time_impl(module); +} + +static PyObject * +time_time_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=9c1b6ed329b95bae3f4543f79d9b313c084093d0]*/ { return floattime(NULL); } -PyDoc_STRVAR(time_doc, -"time() -> floating point number\n\ -\n\ -Return the current time in seconds since the Epoch.\n\ -Fractions of a second may be present if the system clock provides them."); - #if defined(HAVE_CLOCK) #ifndef CLOCKS_PER_SEC @@ -142,31 +218,86 @@ return floatclock(info); } +/*[clinic input] +time.clock + +Return the time since the start of the process or last call + +Return the CPU time or real time since the start of the process or since the +first call to clock(). This has as much precision as the system records. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_clock__doc__, +"clock()\n" +"Return the time since the start of the process or last call\n" +"\n" +"Return the CPU time or real time since the start of the process or since the\n" +"first call to clock(). This has as much precision as the system records."); + +#define TIME_CLOCK_METHODDEF \ + {"clock", (PyCFunction)time_clock, METH_NOARGS, time_clock__doc__}, + static PyObject * -time_clock(PyObject *self, PyObject *unused) +time_clock_impl(PyModuleDef *module); + +static PyObject * +time_clock(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return time_clock_impl(module); +} + +static PyObject * +time_clock_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=f1d11cc2a5cbe42ba42b8457737fb2472e368a2b]*/ + { return pyclock(NULL); } - -PyDoc_STRVAR(clock_doc, -"clock() -> floating point number\n\ -\n\ -Return the CPU time or real time since the start of the process or since\n\ -the first call to clock(). This has as much precision as the system\n\ -records."); -#endif +#endif /* WIN32_PERF_COUNTER || HAVE_CLOCK */ #ifdef HAVE_CLOCK_GETTIME +/*[clinic input] +time.clock_gettime + + clk_id: int + / + +Return the time of the specified clock clk_id +[clinic start generated code]*/ + +PyDoc_STRVAR(time_clock_gettime__doc__, +"clock_gettime(clk_id)\n" +"Return the time of the specified clock clk_id"); + +#define TIME_CLOCK_GETTIME_METHODDEF \ + {"clock_gettime", (PyCFunction)time_clock_gettime, METH_VARARGS, time_clock_gettime__doc__}, + static PyObject * -time_clock_gettime(PyObject *self, PyObject *args) +time_clock_gettime_impl(PyModuleDef *module, int clk_id); + +static PyObject * +time_clock_gettime(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int clk_id; + + if (!PyArg_ParseTuple(args, + "i:clock_gettime", + &clk_id)) + goto exit; + return_value = time_clock_gettime_impl(module, clk_id); + +exit: + return return_value; +} + +static PyObject * +time_clock_gettime_impl(PyModuleDef *module, int clk_id) +/*[clinic end generated code: checksum=cefaa34b15ff90542d1ee0847c40314c254b1d7b]*/ { int ret; - int clk_id; struct timespec tp; - if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) - return NULL; - ret = clock_gettime((clockid_t)clk_id, &tp); if (ret != 0) { PyErr_SetFromErrno(PyExc_IOError); @@ -175,24 +306,53 @@ return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); } -PyDoc_STRVAR(clock_gettime_doc, -"clock_gettime(clk_id) -> floating point number\n\ -\n\ -Return the time of the specified clock clk_id."); + +/*[clinic input] +time.clock_settime + + clk_id: int + obj: object + / + +Set the time of the specified clock clk_id. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_clock_settime__doc__, +"clock_settime(clk_id, obj)\n" +"Set the time of the specified clock clk_id."); + +#define TIME_CLOCK_SETTIME_METHODDEF \ + {"clock_settime", (PyCFunction)time_clock_settime, METH_VARARGS, time_clock_settime__doc__}, static PyObject * -time_clock_settime(PyObject *self, PyObject *args) +time_clock_settime_impl(PyModuleDef *module, int clk_id, PyObject *obj); + +static PyObject * +time_clock_settime(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; int clk_id; PyObject *obj; + + if (!PyArg_ParseTuple(args, + "iO:clock_settime", + &clk_id, &obj)) + goto exit; + return_value = time_clock_settime_impl(module, clk_id, obj); + +exit: + return return_value; +} + +static PyObject * +time_clock_settime_impl(PyModuleDef *module, int clk_id, PyObject *obj) +/*[clinic end generated code: checksum=c2ad16097bd86c6e8926b01f2488dc3c2bf6d3f6]*/ +{ time_t tv_sec; long tv_nsec; struct timespec tp; int ret; - if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj)) - return NULL; - if (_PyTime_ObjectToTimespec(obj, &tv_sec, &tv_nsec) == -1) return NULL; tp.tv_sec = tv_sec; @@ -206,21 +366,49 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(clock_settime_doc, -"clock_settime(clk_id, time)\n\ -\n\ -Set the time of the specified clock clk_id."); + +/*[clinic input] +time.clock_getres + + clk_id: int + / + +Return the resolution (precision) of the specified clock clk_id. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_clock_getres__doc__, +"clock_getres(clk_id)\n" +"Return the resolution (precision) of the specified clock clk_id."); + +#define TIME_CLOCK_GETRES_METHODDEF \ + {"clock_getres", (PyCFunction)time_clock_getres, METH_VARARGS, time_clock_getres__doc__}, static PyObject * -time_clock_getres(PyObject *self, PyObject *args) +time_clock_getres_impl(PyModuleDef *module, int clk_id); + +static PyObject * +time_clock_getres(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int clk_id; + + if (!PyArg_ParseTuple(args, + "i:clock_getres", + &clk_id)) + goto exit; + return_value = time_clock_getres_impl(module, clk_id); + +exit: + return return_value; +} + +static PyObject * +time_clock_getres_impl(PyModuleDef *module, int clk_id) +/*[clinic end generated code: checksum=bbf728de562dbd7d909dc0f6396d8368d98c16c6]*/ { int ret; - int clk_id; struct timespec tp; - if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id)) - return NULL; - ret = clock_getres((clockid_t)clk_id, &tp); if (ret != 0) { PyErr_SetFromErrno(PyExc_IOError); @@ -229,19 +417,51 @@ return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); } - -PyDoc_STRVAR(clock_getres_doc, -"clock_getres(clk_id) -> floating point number\n\ -\n\ -Return the resolution (precision) of the specified clock clk_id."); #endif /* HAVE_CLOCK_GETTIME */ +/*[clinic input] +time.sleep + + secs: double + / + +Delay execution for a given number of seconds. + +The argument may be a floating point number for subsecond precision. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_sleep__doc__, +"sleep(secs)\n" +"Delay execution for a given number of seconds.\n" +"\n" +"The argument may be a floating point number for subsecond precision."); + +#define TIME_SLEEP_METHODDEF \ + {"sleep", (PyCFunction)time_sleep, METH_VARARGS, time_sleep__doc__}, + static PyObject * -time_sleep(PyObject *self, PyObject *args) +time_sleep_impl(PyModuleDef *module, double secs); + +static PyObject * +time_sleep(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; double secs; - if (!PyArg_ParseTuple(args, "d:sleep", &secs)) - return NULL; + + if (!PyArg_ParseTuple(args, + "d:sleep", + &secs)) + goto exit; + return_value = time_sleep_impl(module, secs); + +exit: + return return_value; +} + +static PyObject * +time_sleep_impl(PyModuleDef *module, double secs) +/*[clinic end generated code: checksum=66cd8e8be8e7d3a8d1732ba218de5baf175c22dd]*/ +{ if (secs < 0) { PyErr_SetString(PyExc_ValueError, "sleep length must be non-negative"); @@ -253,11 +473,6 @@ return Py_None; } -PyDoc_STRVAR(sleep_doc, -"sleep(seconds)\n\ -\n\ -Delay execution for a given number of seconds. The argument may be\n\ -a floating point number for subsecond precision."); static PyStructSequence_Field struct_time_type_fields[] = { {"tm_year", "year, for example, 1993"}, @@ -325,38 +540,57 @@ return v; } -/* Parse arg tuple that can contain an optional float-or-None value; - format needs to be "|O:name". - Returns non-zero on success (parallels PyArg_ParseTuple). -*/ -static int -parse_time_t_args(PyObject *args, char *format, time_t *pwhen) + +/*[clinic input] +time.gmtime + + seconds: maybe_time_t=None + / + +Convert seconds since the Epoch to a time tuple expressing UTC. + +When 'seconds' is not passed in, convert the current time instead. If +the platform supports the tm_gmtoff and tm_zone, they are available as +attributes only. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_gmtime__doc__, +"gmtime(seconds=None)\n" +"Convert seconds since the Epoch to a time tuple expressing UTC.\n" +"\n" +"When \'seconds\' is not passed in, convert the current time instead. If\n" +"the platform supports the tm_gmtoff and tm_zone, they are available as\n" +"attributes only."); + +#define TIME_GMTIME_METHODDEF \ + {"gmtime", (PyCFunction)time_gmtime, METH_VARARGS, time_gmtime__doc__}, + +static PyObject * +time_gmtime_impl(PyModuleDef *module, maybe_time_t seconds); + +static PyObject * +time_gmtime(PyModuleDef *module, PyObject *args) { - PyObject *ot = NULL; - time_t whent; + PyObject *return_value = NULL; + maybe_time_t seconds = MAYBE_TIME_T_DEFAULT; - if (!PyArg_ParseTuple(args, format, &ot)) - return 0; - if (ot == NULL || ot == Py_None) { - whent = time(NULL); - } - else { - if (_PyTime_ObjectToTime_t(ot, &whent) == -1) - return 0; - } - *pwhen = whent; - return 1; + if (!PyArg_ParseTuple(args, + "|O&:gmtime", + PyObject_to_maybe_time_t, &seconds)) + goto exit; + return_value = time_gmtime_impl(module, seconds); + +exit: + return return_value; } static PyObject * -time_gmtime(PyObject *self, PyObject *args) +time_gmtime_impl(PyModuleDef *module, maybe_time_t seconds) +/*[clinic end generated code: checksum=391dee65eba13dd6cea14163c6fb8551ba20295e]*/ { - time_t when; struct tm buf, *local; - - if (!parse_time_t_args(args, "|O:gmtime", &when)) - return NULL; - + time_t when = unwrap_maybe_time_t(seconds); + errno = 0; local = gmtime(&when); if (local == NULL) { @@ -370,15 +604,6 @@ return tmtotuple(&buf); } -PyDoc_STRVAR(gmtime_doc, -"gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\ - tm_sec, tm_wday, tm_yday, tm_isdst)\n\ -\n\ -Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\ -GMT). When 'seconds' is not passed in, convert the current time instead.\n\ -\n\ -If the platform supports the tm_gmtoff and tm_zone, they are available as\n\ -attributes only."); static int pylocaltime(time_t *timep, struct tm *result) @@ -400,25 +625,57 @@ return 0; } +/*[clinic input] +time.localtime + + seconds: maybe_time_t=None + / + +Convert seconds since the Epoch to a time tuple expressing local time. + +When 'seconds' is not passed in, convert the current time instead. If +[clinic start generated code]*/ + +PyDoc_STRVAR(time_localtime__doc__, +"localtime(seconds=None)\n" +"Convert seconds since the Epoch to a time tuple expressing local time.\n" +"\n" +"When \'seconds\' is not passed in, convert the current time instead. If"); + +#define TIME_LOCALTIME_METHODDEF \ + {"localtime", (PyCFunction)time_localtime, METH_VARARGS, time_localtime__doc__}, + static PyObject * -time_localtime(PyObject *self, PyObject *args) +time_localtime_impl(PyModuleDef *module, maybe_time_t seconds); + +static PyObject * +time_localtime(PyModuleDef *module, PyObject *args) { - time_t when; + PyObject *return_value = NULL; + maybe_time_t seconds = MAYBE_TIME_T_DEFAULT; + + if (!PyArg_ParseTuple(args, + "|O&:localtime", + PyObject_to_maybe_time_t, &seconds)) + goto exit; + return_value = time_localtime_impl(module, seconds); + +exit: + return return_value; +} + +static PyObject * +time_localtime_impl(PyModuleDef *module, maybe_time_t seconds) +/*[clinic end generated code: checksum=07a9583071a79ece3010bb2492236be2f7c260eb]*/ +{ struct tm buf; + time_t when = unwrap_maybe_time_t(seconds); - if (!parse_time_t_args(args, "|O:localtime", &when)) - return NULL; if (pylocaltime(&when, &buf) == -1) return NULL; return tmtotuple(&buf); } -PyDoc_STRVAR(localtime_doc, -"localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\ - tm_sec,tm_wday,tm_yday,tm_isdst)\n\ -\n\ -Convert seconds since the Epoch to a time tuple expressing local time.\n\ -When 'seconds' is not passed in, convert the current time instead."); /* Convert 9-item tuple to tm structure. Return 1 on success, set * an exception and return 0 on error. @@ -533,26 +790,7 @@ /* wcsftime() doesn't format correctly time zones, see issue #10653 */ # undef HAVE_WCSFTIME #endif -#define STRFTIME_FORMAT_CODES \ -"Commonly used format codes:\n\ -\n\ -%Y Year with century as a decimal number.\n\ -%m Month as a decimal number [01,12].\n\ -%d Day of the month as a decimal number [01,31].\n\ -%H Hour (24-hour clock) as a decimal number [00,23].\n\ -%M Minute as a decimal number [00,59].\n\ -%S Second as a decimal number [00,61].\n\ -%z Time zone offset from UTC.\n\ -%a Locale's abbreviated weekday name.\n\ -%A Locale's full weekday name.\n\ -%b Locale's abbreviated month name.\n\ -%B Locale's full month name.\n\ -%c Locale's appropriate date and time representation.\n\ -%I Hour (12-hour clock) as a decimal number [01,12].\n\ -%p Locale's equivalent of either AM or PM.\n\ -\n\ -Other codes may be available on your platform. See documentation for\n\ -the C library strftime function.\n" + #ifdef HAVE_STRFTIME #ifdef HAVE_WCSFTIME @@ -565,10 +803,95 @@ #define time_strlen strlen #endif +/*[clinic input] +time.strftime + + format_arg: unicode + tup: object=NULL + / + +Convert a time tuple to a string according to a format specification. + +When the time tuple is not present, current time as returned by +localtime() is used. + +Commonly used format codes are: + +%Y Year with century as a decimal number. +%m Month as a decimal number [01,12]. +%d Day of the month as a decimal number [01,31]. +%H Hour (24-hour clock) as a decimal number [00,23]. +%M Minute as a decimal number [00,59]. +%S Second as a decimal number [00,61]. +%z Time zone offset from UTC. +%a Locale's abbreviated weekday name. +%A Locale's full weekday name. +%b Locale's abbreviated month name. +%B Locale's full month name. +%c Locale's appropriate date and time representation. +%I Hour (12-hour clock) as a decimal number [01,12]. +%p Locale's equivalent of either AM or PM. + +For the full list of format codes, see the library reference manual +and the documentation for the C library strftime function on your +platform. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_strftime__doc__, +"strftime(format_arg, tup=None)\n" +"Convert a time tuple to a string according to a format specification.\n" +"\n" +"When the time tuple is not present, current time as returned by\n" +"localtime() is used.\n" +"\n" +"Commonly used format codes are:\n" +"\n" +"%Y Year with century as a decimal number.\n" +"%m Month as a decimal number [01,12].\n" +"%d Day of the month as a decimal number [01,31].\n" +"%H Hour (24-hour clock) as a decimal number [00,23].\n" +"%M Minute as a decimal number [00,59].\n" +"%S Second as a decimal number [00,61].\n" +"%z Time zone offset from UTC.\n" +"%a Locale\'s abbreviated weekday name.\n" +"%A Locale\'s full weekday name.\n" +"%b Locale\'s abbreviated month name.\n" +"%B Locale\'s full month name.\n" +"%c Locale\'s appropriate date and time representation.\n" +"%I Hour (12-hour clock) as a decimal number [01,12].\n" +"%p Locale\'s equivalent of either AM or PM.\n" +"\n" +"For the full list of format codes, see the library reference manual\n" +"and the documentation for the C library strftime function on your\n" +"platform."); + +#define TIME_STRFTIME_METHODDEF \ + {"strftime", (PyCFunction)time_strftime, METH_VARARGS, time_strftime__doc__}, + static PyObject * -time_strftime(PyObject *self, PyObject *args) +time_strftime_impl(PyModuleDef *module, PyObject *format_arg, PyObject *tup); + +static PyObject * +time_strftime(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; + PyObject *format_arg; PyObject *tup = NULL; + + if (!PyArg_ParseTuple(args, + "U|O:strftime", + &format_arg, &tup)) + goto exit; + return_value = time_strftime_impl(module, format_arg, tup); + +exit: + return return_value; +} + +static PyObject * +time_strftime_impl(PyModuleDef *module, PyObject *format_arg, PyObject *tup) +/*[clinic end generated code: checksum=5d11bb181c8a91e54475650d5560132c92b0a4b4]*/ +{ struct tm buf; const time_char *fmt; #ifdef HAVE_WCSFTIME @@ -576,7 +899,6 @@ #else PyObject *format; #endif - PyObject *format_arg; size_t fmtlen, buflen; time_char *outbuf = NULL; size_t i; @@ -584,12 +906,6 @@ memset((void *) &buf, '0円', sizeof(buf)); - /* Will always expect a unicode string to be passed as format. - Given that there's no str type anymore in py3k this seems safe. - */ - if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup)) - return NULL; - if (tup == NULL) { time_t tt = time(NULL); if (pylocaltime(&tt, &buf) == -1) @@ -717,39 +1033,112 @@ #undef time_char #undef format_time -PyDoc_STRVAR(strftime_doc, -"strftime(format[, tuple]) -> string\n\ -\n\ -Convert a time tuple to a string according to a format specification.\n\ -See the library reference manual for formatting codes. When the time tuple\n\ -is not present, current time as returned by localtime() is used.\n\ -\n" STRFTIME_FORMAT_CODES); #endif /* HAVE_STRFTIME */ +/*[clinic input] +time.strptime + + string: str + format: str="%a %b %d %H:%M:%S %Y" + / + +Parse a string to a time tuple according to a format specification. + +For the full list of format codes, see the library reference manual +and the documentation for the C library strftime function on your +platform. + +Commonly used format codes are: + +%Y Year with century as a decimal number. +%m Month as a decimal number [01,12]. +%d Day of the month as a decimal number [01,31]. +%H Hour (24-hour clock) as a decimal number [00,23]. +%M Minute as a decimal number [00,59]. +%S Second as a decimal number [00,61]. +%z Time zone offset from UTC. +%a Locale's abbreviated weekday name. +%A Locale's full weekday name. +%b Locale's abbreviated month name. +%B Locale's full month name. +%c Locale's appropriate date and time representation. +%I Hour (12-hour clock) as a decimal number [01,12]. +%p Locale's equivalent of either AM or PM. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_strptime__doc__, +"strptime(string, format=\'%a %b %d %H:%M:%S %Y\')\n" +"Parse a string to a time tuple according to a format specification.\n" +"\n" +"For the full list of format codes, see the library reference manual\n" +"and the documentation for the C library strftime function on your\n" +"platform.\n" +"\n" +"Commonly used format codes are:\n" +"\n" +"%Y Year with century as a decimal number.\n" +"%m Month as a decimal number [01,12].\n" +"%d Day of the month as a decimal number [01,31].\n" +"%H Hour (24-hour clock) as a decimal number [00,23].\n" +"%M Minute as a decimal number [00,59].\n" +"%S Second as a decimal number [00,61].\n" +"%z Time zone offset from UTC.\n" +"%a Locale\'s abbreviated weekday name.\n" +"%A Locale\'s full weekday name.\n" +"%b Locale\'s abbreviated month name.\n" +"%B Locale\'s full month name.\n" +"%c Locale\'s appropriate date and time representation.\n" +"%I Hour (12-hour clock) as a decimal number [01,12].\n" +"%p Locale\'s equivalent of either AM or PM."); + +#define TIME_STRPTIME_METHODDEF \ + {"strptime", (PyCFunction)time_strptime, METH_VARARGS, time_strptime__doc__}, + static PyObject * -time_strptime(PyObject *self, PyObject *args) +time_strptime_impl(PyModuleDef *module, const char *string, const char *format); + +static PyObject * +time_strptime(PyModuleDef *module, PyObject *args) { - PyObject *strptime_module = PyImport_ImportModuleNoBlock("_strptime"); + PyObject *return_value = NULL; + const char *string; + const char *format = "%a %b %d %H:%M:%S %Y"; + + if (!PyArg_ParseTuple(args, + "s|s:strptime", + &string, &format)) + goto exit; + return_value = time_strptime_impl(module, string, format); + +exit: + return return_value; +} + +static PyObject * +time_strptime_impl(PyModuleDef *module, const char *string, const char *format) +/*[clinic end generated code: checksum=ba1bded6e4a863c94945124ab8c84a26ecb4ddaa]*/ +{ + PyObject *strptime_module = PyImport_ImportModule("_strptime"); PyObject *strptime_result; + PyObject* strptime_args; + _Py_IDENTIFIER(_strptime_time); if (!strptime_module) return NULL; + + strptime_args = Py_BuildValue("ss", string, format); + if (strptime_args == NULL) + return NULL; + strptime_result = _PyObject_CallMethodId(strptime_module, - &PyId__strptime_time, "O", args); + &PyId__strptime_time, "O", + strptime_args); Py_DECREF(strptime_module); return strptime_result; } -PyDoc_STRVAR(strptime_doc, -"strptime(string, format) -> struct_time\n\ -\n\ -Parse a string to a time tuple according to a format specification.\n\ -See the library reference manual for formatting codes (same as\n\ -strftime()).\n\ -\n" STRFTIME_FORMAT_CODES); - static PyObject * _asctime(struct tm *timeptr) { @@ -771,14 +1160,53 @@ 1900 + timeptr->tm_year); } +/*[clinic input] +time.asctime + + tup: object=NULL + / + +Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'. + +When the *tup* is None, current time as returned by localtime() is +used. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_asctime__doc__, +"asctime(tup=None)\n" +"Convert a time tuple to a string, e.g. \'Sat Jun 06 16:26:11 1998\'.\n" +"\n" +"When the *tup* is None, current time as returned by localtime() is\n" +"used."); + +#define TIME_ASCTIME_METHODDEF \ + {"asctime", (PyCFunction)time_asctime, METH_VARARGS, time_asctime__doc__}, + static PyObject * -time_asctime(PyObject *self, PyObject *args) +time_asctime_impl(PyModuleDef *module, PyObject *tup); + +static PyObject * +time_asctime(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; PyObject *tup = NULL; + + if (!PyArg_UnpackTuple(args, "asctime", + 0, 1, + &tup)) + goto exit; + return_value = time_asctime_impl(module, tup); + +exit: + return return_value; +} + +static PyObject * +time_asctime_impl(PyModuleDef *module, PyObject *tup) +/*[clinic end generated code: checksum=45de0db7a5aff7ed82717ad8dd1c5176fa83a3bf]*/ +{ struct tm buf; - if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup)) - return NULL; if (tup == NULL) { time_t tt = time(NULL); if (pylocaltime(&tt, &buf) == -1) @@ -789,31 +1217,60 @@ return _asctime(&buf); } -PyDoc_STRVAR(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."); + +/*[clinic input] +time.ctime + + seconds: maybe_time_t=None + / + +Convert a time in seconds since the Epoch to a string in local time. + +This is equivalent to asctime(localtime(seconds)). When the time tuple is +not present, current time as returned by localtime() is used. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_ctime__doc__, +"ctime(seconds=None)\n" +"Convert a time in seconds since the Epoch to a string in local time.\n" +"\n" +"This is equivalent to asctime(localtime(seconds)). When the time tuple is\n" +"not present, current time as returned by localtime() is used."); + +#define TIME_CTIME_METHODDEF \ + {"ctime", (PyCFunction)time_ctime, METH_VARARGS, time_ctime__doc__}, static PyObject * -time_ctime(PyObject *self, PyObject *args) +time_ctime_impl(PyModuleDef *module, maybe_time_t seconds); + +static PyObject * +time_ctime(PyModuleDef *module, PyObject *args) { - time_t tt; + PyObject *return_value = NULL; + maybe_time_t seconds = MAYBE_TIME_T_DEFAULT; + + if (!PyArg_ParseTuple(args, + "|O&:ctime", + PyObject_to_maybe_time_t, &seconds)) + goto exit; + return_value = time_ctime_impl(module, seconds); + +exit: + return return_value; +} + +static PyObject * +time_ctime_impl(PyModuleDef *module, maybe_time_t seconds) +/*[clinic end generated code: checksum=7401c30d135ddd4133afc58e58f80ba9f773799c]*/ +{ struct tm buf; - if (!parse_time_t_args(args, "|O:ctime", &tt)) - return NULL; - if (pylocaltime(&tt, &buf) == -1) + time_t when = unwrap_maybe_time_t(seconds); + + if (pylocaltime(&when, &buf) == -1) return NULL; return _asctime(&buf); } -PyDoc_STRVAR(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 * @@ -856,8 +1313,54 @@ #ifdef HAVE_WORKING_TZSET static void PyInit_timezone(PyObject *module); +/*[clinic input] +time.tzset + +Initialize the local timezone + +Initialize (or reinitialize) the local timezone to the value stored in +os.environ['TZ']. The TZ environment variable should be specified in standard +Unix timezone format as documented in the tzset man page (eg. 'US/Eastern', +'Europe/Amsterdam'). Unknown timezones will silently fall back to UTC. If the +TZ environment variable is not set, the local timezone is set to the systems +best guess of wallclock time. + +Changing the TZ environment variable without calling tzset *may* change the +local timezone used by methods such as localtime, but this behaviour should +not be relied on. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_tzset__doc__, +"tzset()\n" +"Initialize the local timezone\n" +"\n" +"Initialize (or reinitialize) the local timezone to the value stored in\n" +"os.environ[\'TZ\']. The TZ environment variable should be specified in standard\n" +"Unix timezone format as documented in the tzset man page (eg. \'US/Eastern\',\n" +"\'Europe/Amsterdam\'). Unknown timezones will silently fall back to UTC. If the\n" +"TZ environment variable is not set, the local timezone is set to the systems\n" +"best guess of wallclock time.\n" +"\n" +"Changing the TZ environment variable without calling tzset *may* change the\n" +"local timezone used by methods such as localtime, but this behaviour should\n" +"not be relied on."); + +#define TIME_TZSET_METHODDEF \ + {"tzset", (PyCFunction)time_tzset, METH_NOARGS, time_tzset__doc__}, + static PyObject * -time_tzset(PyObject *self, PyObject *unused) +time_tzset_impl(PyModuleDef *module); + +static PyObject * +time_tzset(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return time_tzset_impl(module); +} + +static PyObject * +time_tzset_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=64412fe1a5576f6ddbc2d6c8f414b641f828b825]*/ + { PyObject* m; @@ -878,18 +1381,6 @@ return Py_None; } -PyDoc_STRVAR(tzset_doc, -"tzset()\n\ -\n\ -Initialize, or reinitialize, the local timezone to the value stored in\n\ -os.environ['TZ']. The TZ environment variable should be specified in\n\ -standard Unix timezone format as documented in the tzset man page\n\ -(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\ -fall back to UTC. If the TZ environment variable is not set, the local\n\ -timezone is set to the systems best guess of wallclock time.\n\ -Changing the TZ environment variable without calling tzset *may* change\n\ -the local timezone used by methods such as localtime, but this behaviour\n\ -should not be relied on."); #endif /* HAVE_WORKING_TZSET */ #if defined(MS_WINDOWS) || defined(__APPLE__) \ @@ -1010,17 +1501,41 @@ #endif } +/*[clinic input] +time.monotonic + +Retrieve timestamp from the system's monotonic clock + +The monotonic clock is guaranteed to never go backwards. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_monotonic__doc__, +"monotonic()\n" +"Retrieve timestamp from the system\'s monotonic clock\n" +"\n" +"The monotonic clock is guaranteed to never go backwards."); + +#define TIME_MONOTONIC_METHODDEF \ + {"monotonic", (PyCFunction)time_monotonic, METH_NOARGS, time_monotonic__doc__}, + static PyObject * -time_monotonic(PyObject *self, PyObject *unused) +time_monotonic_impl(PyModuleDef *module); + +static PyObject * +time_monotonic(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return time_monotonic_impl(module); +} + +static PyObject * +time_monotonic_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=16835fed2074ab76296e5487eb4992277ddba1c7]*/ + { return pymonotonic(NULL); } +#endif /* PYMONOTONIC */ -PyDoc_STRVAR(monotonic_doc, -"monotonic() -> float\n\ -\n\ -Monotonic clock, cannot go backward."); -#endif /* PYMONOTONIC */ static PyObject* perf_counter(_Py_clock_info_t *info) @@ -1056,16 +1571,46 @@ return floattime(info); } +/*[clinic input] +time.perf_counter + +Get current value of system's performance counter. + +The units of this counter are undefined, and the counter may wrap back to zero +at some point. The performance counter may be a per-CPU value, so that +succesive invocations may not be comparable if the process has migrated to a +different CPU between calls. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_perf_counter__doc__, +"perf_counter()\n" +"Get current value of system\'s performance counter.\n" +"\n" +"The units of this counter are undefined, and the counter may wrap back to zero\n" +"at some point. The performance counter may be a per-CPU value, so that\n" +"succesive invocations may not be comparable if the process has migrated to a\n" +"different CPU between calls."); + +#define TIME_PERF_COUNTER_METHODDEF \ + {"perf_counter", (PyCFunction)time_perf_counter, METH_NOARGS, time_perf_counter__doc__}, + static PyObject * -time_perf_counter(PyObject *self, PyObject *unused) +time_perf_counter_impl(PyModuleDef *module); + +static PyObject * +time_perf_counter(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return time_perf_counter_impl(module); +} + +static PyObject * +time_perf_counter_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=0cf52430cd97ebf151edda9ad3a9888c779c819c]*/ + { return perf_counter(NULL); } -PyDoc_STRVAR(perf_counter_doc, -"perf_counter() -> float\n\ -\n\ -Performance counter for benchmarking."); static PyObject* py_process_time(_Py_clock_info_t *info) @@ -1180,28 +1725,82 @@ #endif } +/*[clinic input] +time.process_time + +Get current process time for profiling. + +The proces stime is the sum of the kernel and user-space CPU time. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_process_time__doc__, +"process_time()\n" +"Get current process time for profiling.\n" +"\n" +"The proces stime is the sum of the kernel and user-space CPU time."); + +#define TIME_PROCESS_TIME_METHODDEF \ + {"process_time", (PyCFunction)time_process_time, METH_NOARGS, time_process_time__doc__}, + static PyObject * -time_process_time(PyObject *self, PyObject *unused) +time_process_time_impl(PyModuleDef *module); + +static PyObject * +time_process_time(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return time_process_time_impl(module); +} + +static PyObject * +time_process_time_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=1aa24df2b518aed75dd5c23795ae8f8d57496dae]*/ { return py_process_time(NULL); } -PyDoc_STRVAR(process_time_doc, -"process_time() -> float\n\ -\n\ -Process time for profiling: sum of the kernel and user-space CPU time."); +/*[clinic input] +time.get_clock_info + + name: str + / + +Get information of the specified clock. +[clinic start generated code]*/ + +PyDoc_STRVAR(time_get_clock_info__doc__, +"get_clock_info(name)\n" +"Get information of the specified clock."); + +#define TIME_GET_CLOCK_INFO_METHODDEF \ + {"get_clock_info", (PyCFunction)time_get_clock_info, METH_VARARGS, time_get_clock_info__doc__}, static PyObject * -time_get_clock_info(PyObject *self, PyObject *args) +time_get_clock_info_impl(PyModuleDef *module, const char *name); + +static PyObject * +time_get_clock_info(PyModuleDef *module, PyObject *args) { - char *name; + PyObject *return_value = NULL; + const char *name; + + if (!PyArg_ParseTuple(args, + "s:get_clock_info", + &name)) + goto exit; + return_value = time_get_clock_info_impl(module, name); + +exit: + return return_value; +} + +static PyObject * +time_get_clock_info_impl(PyModuleDef *module, const char *name) +/*[clinic end generated code: checksum=ccf1ecf4b9a25d8bb419a14d345ce61eefba511d]*/ +{ _Py_clock_info_t info; PyObject *obj = NULL, *dict, *ns; - if (!PyArg_ParseTuple(args, "s:get_clock_info", &name)) - return NULL; - #ifdef Py_DEBUG info.implementation = NULL; info.monotonic = -1; @@ -1283,11 +1882,6 @@ return NULL; } -PyDoc_STRVAR(get_clock_info_doc, -"get_clock_info(name: str) -> dict\n\ -\n\ -Get information of the specified clock."); - static void PyInit_timezone(PyObject *m) { /* This code moved from PyInit_time wholesale to allow calling it from @@ -1391,36 +1985,36 @@ static PyMethodDef time_methods[] = { - {"time", time_time, METH_NOARGS, time_doc}, + TIME_TIME_METHODDEF #ifdef PYCLOCK - {"clock", time_clock, METH_NOARGS, clock_doc}, + TIME_CLOCK_METHODDEF #endif #ifdef HAVE_CLOCK_GETTIME - {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, - {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc}, - {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, + TIME_CLOCK_GETTIME_METHODDEF + TIME_CLOCK_SETTIME_METHODDEF + TIME_CLOCK_GETRES_METHODDEF #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}, + TIME_SLEEP_METHODDEF + TIME_GMTIME_METHODDEF + TIME_LOCALTIME_METHODDEF + TIME_ASCTIME_METHODDEF + TIME_CTIME_METHODDEF #ifdef HAVE_MKTIME {"mktime", time_mktime, METH_O, mktime_doc}, #endif #ifdef HAVE_STRFTIME - {"strftime", time_strftime, METH_VARARGS, strftime_doc}, + TIME_STRFTIME_METHODDEF #endif - {"strptime", time_strptime, METH_VARARGS, strptime_doc}, + TIME_STRPTIME_METHODDEF #ifdef HAVE_WORKING_TZSET - {"tzset", time_tzset, METH_NOARGS, tzset_doc}, + TIME_TZSET_METHODDEF #endif #ifdef PYMONOTONIC - {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc}, + TIME_MONOTONIC_METHODDEF #endif - {"process_time", time_process_time, METH_NOARGS, process_time_doc}, - {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc}, - {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc}, + TIME_PROCESS_TIME_METHODDEF + TIME_PERF_COUNTER_METHODDEF + TIME_GET_CLOCK_INFO_METHODDEF {NULL, NULL} /* sentinel */ };

AltStyle によって変換されたページ (->オリジナル) /