Commit Graph

385 Commits

Author SHA1 Message Date
Victor Stinner c29b585fd4
bpo-31784: Implement PEP 564: add time.time_ns() (#3989)
Add new time functions:

* time.clock_gettime_ns()
* time.clock_settime_ns()
* time.monotonic_ns()
* time.perf_counter_ns()
* time.process_time_ns()
* time.time_ns()

Add new _PyTime functions:

* _PyTime_FromTimespec()
* _PyTime_FromNanosecondsObject()
* _PyTime_FromTimeval()

Other changes:

* Add also os.times() tests to test_os.
* pytime_fromtimeval() and pytime_fromtimeval() now return
  _PyTime_MAX or _PyTime_MIN on overflow, rather than undefined
  behaviour
* _PyTime_FromNanoseconds() parameter type changes from long long to
  _PyTime_t
2017-11-02 07:28:27 -07:00
Victor Stinner a64ce973a3
bpo-31917: Add 3 new clock identifiers (#4207)
Add new clock identfiers:

* time.CLOCK_BOOTTIME
* time.CLOCK_PROF
* time.CLOCK_UPTIME
2017-11-02 04:19:19 -07:00
Pablo Galindo 59af94fa61 bpo-31806: Use _PyTime_ROUND_TIMEOUT for the timeout argument parsing in more functions (#4026)
Fix timeout rounding in time.sleep(), threading.Lock.acquire() and
socket.socket.settimeout() to round correctly negative timeouts between -1.0 and
0.0. The functions now block waiting for events as expected. Previously, the
call was incorrectly non-blocking.
2017-10-18 10:13:09 +03:00
Victor Stinner 884d13a55f time.clock() now emits a DeprecationWarning (GH-4020)
bpo-31803: time.clock() and time.get_clock_info('clock') now emit a
DeprecationWarning warning.

Replace time.clock() with time.perf_counter() in tests and demos.

Remove also hasattr(time, 'monotonic') in test_time since time.monotonic()
is now always available since Python 3.5.
2017-10-17 14:46:45 -07:00
Victor Stinner bdaeb7d237 bpo-31773: _PyTime_GetPerfCounter() uses _PyTime_t (GH-3983)
* Rewrite win_perf_counter() to only use integers internally.
* Add _PyTime_MulDiv() which compute "ticks * mul / div"
  in two parts (int part and remaining) to prevent integer overflow.
* Clock frequency is checked at initialization for integer overflow.
* Enhance also pymonotonic() to reduce the precision loss on macOS
  (mach_absolute_time() clock).
2017-10-16 08:44:31 -07:00
Victor Stinner cba9a0c6de bpo-31773: time.perf_counter() uses again double (GH-3964)
time.clock() and time.perf_counter() now use again C double
internally.

Remove also _PyTime_GetWinPerfCounterWithInfo(): use
_PyTime_GetPerfCounterDoubleWithInfo() instead on Windows.
2017-10-12 08:51:56 -07:00
Cheryl Sabella 703ff381ff bpo-28157: Improvements for the time module documentation (GH-928)
* Separated functions and constants descriptions in sections.
* Added a note about the limitations of timezone constants.
* Removed redundant lists from the module docstring.
2017-10-11 16:29:14 +03:00
Victor Stinner a997c7b434 bpo-31415: Add _PyTime_GetPerfCounter() and use it for -X importtime (#3936)
* Add _PyTime_GetPerfCounter()
* Use _PyTime_GetPerfCounter() for -X importtime
2017-10-10 02:51:50 -07:00
pdox e14679c784 closes bpo-31596: Add an interface for pthread_getcpuclockid(3) (#3756) 2017-10-05 00:01:56 -07:00
Oren Milman 1d1d3e9db8 bpo-28261: Fixed err msgs where PyArg_ParseTuple is used to parse normal tuples. (#3119) 2017-08-20 18:35:36 +03:00
Victor Stinner 6e676954de timemodule.c: Cast PyUnicode_AsUTF8() to char* (#1294)
bpo-28769 changed PyUnicode_AsUTF8() return type from const char* to
char* in Python 3.7, but tm_zone field type of the tm structure is
char* on FreeBSD.

Cast PyUnicode_AsUTF8() to char* in gettmarg() to fix the warning:

    Modules/timemodule.c:443:20: warning: assigning to 'char *'
    from 'const char *' discards qualifiers
2017-04-26 13:51:48 +02:00
Victor Stinner 0d659e5614 tmtotuple(): use time_t for gmtoff (#1276)
timegm() return type is time_t, not int. Use time_t to prevent the
following compiler warning on Windows:

timemodule.c: warning C4244: '=': conversion from 'time_t' to 'int',
              possible loss of data
2017-04-25 01:22:42 +02:00
Victor Stinner 8f5cdfa9fc Only define get_zone() and get_gmtoff() if needed (#1193)
Only define the get_zone() and get_gmtoff() private functions in the
time module if these functions are needed to initialize the module.

The change fixes the following warnings on AIX:

Modules/timemodule.c:1175:1: warning: 'get_gmtoff' defined but not used [-Wunused-function]
Modules/timemodule.c:1164:1: warning: 'get_zone' defined but not used [-Wunused-function]
2017-04-20 13:41:09 +02:00
Serhiy Storchaka 228b12edcc Issue #28999: Use Py_RETURN_NONE, Py_RETURN_TRUE and Py_RETURN_FALSE wherever
possible.  Patch is writen with Coccinelle.
2017-01-23 09:47:21 +02:00
Steve Dower 0885519914 Fixes maximum usable length of buffer for formatting time zone in localtime(). 2016-12-14 11:22:14 -08:00
Steve Dower c3c6f71662 Fixes maximum usable length of buffer for formatting time zone in localtime(). 2016-12-14 11:22:05 -08:00
Victor Stinner dbe28d26b4 time_strptime() uses PyObject_Call()
Issue #28915: Use PyObject_Call() to pass a tuple of positional arguments,
instead of relying on _PyObject_CallMethodId() weird behaviour to unpack the
tuple.
2016-12-09 00:38:53 +01:00
Serhiy Storchaka 06515833fe Replaced outdated macros _PyUnicode_AsString and _PyUnicode_AsStringAndSize
with PyUnicode_AsUTF8 and PyUnicode_AsUTF8AndSize.
2016-11-20 09:13:07 +02:00
Alexander Belopolsky 3e7a3cb903 Issue #28148: Stop using localtime() and gmtime() in the time module.
Introduced platform independent _PyTime_localtime API that is similar
to POSIX localtime_r, but available on all platforms.  Patch by Ed
Schouten.
2016-09-28 17:31:35 -04:00
Benjamin Peterson 37098cd584 more granular configure checks for clock_* functions (closes #28081) 2016-09-13 22:55:09 -07:00
Alexander Belopolsky 18f3a9b93c Closes #25283: Make tm_gmtoff and tm_zone available on all platforms. 2016-09-11 22:55:16 -04:00
Victor Stinner 53e22bfea5 Issue #22624: Python 3 requires clock() to build 2016-07-08 17:55:01 +02:00
Serhiy Storchaka ef1585eb9a Issue #25923: Added more const qualifiers to signatures of static and private functions. 2015-12-25 20:01:53 +02:00
Serhiy Storchaka 2d06e84455 Issue #25923: Added the const qualifier to static constant arrays. 2015-12-25 19:53:18 +02:00
Steve Dower 57ab1cdb15 Issue #25092: Fix datetime.strftime() failure when errno was already set to EINVAL. 2015-09-22 14:51:42 -07:00
Steve Dower 97cded934f Issue #25029: MemoryError in test_strptime 2015-09-08 19:12:51 -07:00
Steve Dower e5b5895b5b Issue #24917: time_strftime() buffer over-read. 2015-09-06 19:20:51 -07:00
Larry Hastings 62b24624dd Backing out 09b62202d9b7; the tests fail on Linux, and it needs a re-think. 2015-09-06 00:31:02 -07:00
Steve Dower 373602fa3f Issue #24917: time_strftime() Buffer Over-read. Patch by John Leitch. 2015-09-05 12:16:06 -07:00
Steve Dower d9ef74e3dd Issue 24244: Prevents termination when an invalid format string is encountered on Windows. 2015-05-22 15:08:34 -07:00
Victor Stinner 6aa446cf03 PEP 475: on EINTR, retry the function even if the timeout is equals to zero
Retry:

* signal.sigtimedwait()
* threading.Lock.acquire()
* threading.RLock.acquire()
* time.sleep()
2015-03-30 21:33:51 +02:00
Victor Stinner 869e1778c0 Issue #22117: Replace usage of _PyTime_ROUND_UP with _PyTime_ROUND_CEILING
All these functions only accept positive timeouts, so this change has no effect
in practice.
2015-03-30 03:49:14 +02:00
Victor Stinner ea9c0dd2c2 Issue #22117: Fix usage of _PyTime_AsTimeval()
Add _PyTime_AsTimeval_noraise() function. Call it when it's not possible (or
not useful) to raise a Python exception on overflow.
2015-03-30 02:51:13 +02:00
Victor Stinner 02937aab13 Issue #22117: Add the new _PyTime_ROUND_FLOOR rounding method for the datetime
module. time.clock_settime() now uses this rounding method instead of
_PyTime_ROUND_DOWN to handle correctly dates before 1970.
2015-03-28 05:02:39 +01:00
Victor Stinner b3b4544070 Issue #22117: Use the _PyTime_t API for time.clock_settime()
Remove also the now unused _PyTime_AddDouble() function.
2015-03-28 04:09:41 +01:00
Victor Stinner 95e9cef6f0 Issue #22117: Write unit tests for _PyTime_AsTimeval()
* _PyTime_AsTimeval() now ensures that tv_usec is always positive
* _PyTime_AsTimespec() now ensures that tv_nsec is always positive
* _PyTime_AsTimeval() now returns an integer on overflow instead of raising an
  exception
2015-03-28 01:26:47 +01:00
Victor Stinner a47b881d86 Issue #22117: time.time() now uses the new _PyTime_t API
* Add _PyTime_GetSystemClockWithInfo()
2015-03-27 18:16:17 +01:00
Victor Stinner 4bfb460d88 Issue #22117: time.monotonic() now uses the new _PyTime_t API
* Add _PyTime_FromNanoseconds()
* Add _PyTime_AsSecondsDouble()
* Add unit tests for _PyTime_AsSecondsDouble()
2015-03-27 22:27:24 +01:00
Victor Stinner 992c43fec9 Issue #22117: Fix rounding in _PyTime_FromSecondsObject()
* Rename _PyTime_FromObject() to _PyTime_FromSecondsObject()
* Add _PyTime_AsNanosecondsObject() and _testcapi.pytime_fromsecondsobject()
* Add unit tests
2015-03-27 17:12:45 +01:00
Victor Stinner cb29f0177c Issue #22117: Add a new Python timestamp format _PyTime_t to pytime.h
In practice, _PyTime_t is a number of nanoseconds. Its C type is a 64-bit
signed number. It's integer value is in the range [-2^63; 2^63-1]. In seconds,
the range is around [-292 years; +292 years]. In term of Epoch timestamp
(1970-01-01), it can store a date between 1677-09-21 and 2262-04-11.

The API has a resolution of 1 nanosecond and use integer number. With a
resolution on 1 nanosecond, 64-bit IEEE 754 floating point numbers loose
precision after 194 days. It's not the case with this API. The drawback is
overflow for values outside [-2^63; 2^63-1], but these values are unlikely for
most Python modules, except of the datetime module.

New functions:

- _PyTime_GetMonotonicClock()
- _PyTime_FromObject()
- _PyTime_AsMilliseconds()
- _PyTime_AsTimeval()

This change uses these new functions in time.sleep() to avoid rounding issues.

The new API will be extended step by step, and the old API will be removed step
by step. Currently, some code is duplicated just to be able to move
incrementally, instead of pushing a large change at once.
2015-03-27 13:31:18 +01:00
Victor Stinner 0eac13052c Issue #23646: Fix test_threading on Windows 2015-03-20 03:06:12 +01:00
Victor Stinner 9a8089b32a Issue #23646: Enhance precision of time.sleep() and socket timeout when
interrupted by a signal

Add a new _PyTime_AddDouble() function and remove _PyTime_ADD_SECONDS() macro.
The _PyTime_ADD_SECONDS only supported an integer number of seconds, the
_PyTime_AddDouble() has subsecond resolution.
2015-03-20 01:42:20 +01:00
Victor Stinner 79d68f929d Issue #23646: If time.sleep() is interrupted by a signal, the sleep is now
retried with the recomputed delay, except if the signal handler raises an
exception (PEP 475).

Modify also test_signal to use a monotonic clock instead of the system clock.
2015-03-19 21:54:09 +01:00
Victor Stinner 0c2fd89777 Revert changeset d927047b1d8eb87738676980a24930d053ba2150
Sorry, it was a mistake, the patch is still under review: issue #23646.
2015-03-17 10:49:17 +01:00
Victor Stinner 945c82eea3 test 2015-03-12 16:19:01 +01:00
Steve Dower 65e4cb10d9 Issue #22919: Windows build updated to support VC 14.0 (Visual Studio 2015), which will be used for the official 3.5 release. 2014-11-22 12:54:57 -08:00
Victor Stinner f427a14156 Issue #22592: Drop support of the Borland C compiler to build Python
The distutils module still supports it to build extensions.
2014-10-22 12:33:23 +02:00
Victor Stinner ae58649721 Issue #22043: time.monotonic() is now always available
threading.Lock.acquire(), threading.RLock.acquire() and socket operations now
use a monotonic clock, instead of the system clock, when a timeout is used.
2014-09-02 23:18:25 +02:00
Victor Stinner 67ca33dbf4 Issue #22043: Oops, fix perf_counter() on UNIX if no monotonic clock is
available (unlikely)
2014-08-29 17:00:17 +02:00
Victor Stinner 5488449ba4 Issue #22043: Simplify time.perf_counter() on Windows
QueryPerformanceFrequency() cannot fail on Windows XP and later according to
its documentation: raise an exception on error and drop the fallback to the
system clock.
2014-08-29 16:51:33 +02:00
Victor Stinner 0011124dc2 Issue #22043: _PyTime_Init() now checks if the system clock works.
Other changes:

* The whole _PyTime API is private (not defined if Py_LIMITED_API is set)
* _PyTime_gettimeofday_info() also returns -1 on error
* Simplify PyTime_gettimeofday(): only use clock_gettime(CLOCK_REALTIME) or
  gettimeofday() on UNIX. Don't fallback to ftime() or time() anymore.
2014-08-29 16:31:59 +02:00
Victor Stinner 7efb83393c Issue #22287: On UNIX, _PyTime_gettimeofday() now uses
clock_gettime(CLOCK_REALTIME) if available. As a side effect, Python now
depends on the librt library on Solaris and on Linux (only with glibc older
than 2.17).
2014-08-29 15:41:08 +02:00
Victor Stinner a734af3f39 timemodule.c: Replace PyExc_IOError with PyExc_OSError 2014-07-31 13:07:17 +02:00
Victor Stinner 1ac42614e3 Issue #19748: On AIX, time.mktime() now raises an OverflowError for year
outsize range [1902; 2037].
2014-02-21 09:27:17 +01:00
Victor Stinner 3c1b379ebd Issue #20320: select.select() and select.kqueue.control() now round the timeout
aways from zero, instead of rounding towards zero.

It should make test_asyncio more reliable, especially test_timeout_rounding() test.
2014-02-17 00:02:43 +01:00
Victor Stinner 93965f7a6b Issue #19634: time.strftime("%y") now raises a ValueError on Solaris when given
a year before 1900.
2013-11-23 14:59:33 +01:00
Victor Stinner 136f064b93 Issue #19634: Fix time_strftime() on AIX, format is a wchar_t* not a PyObject* 2013-11-18 02:43:29 +01:00
Victor Stinner 55329f8fbd Issue #19634: time.strftime("%y") now raises a ValueError on AIX when given a
year before 1900.
2013-11-17 23:39:21 +01:00
Tim Golden bbe268f583 Issue13674 Correct crash with strftime %y format under Windows 2013-11-12 12:48:20 +00:00
Tim Golden 6e51b8ff0f Issue13674 Correct crash with strftime %y format under Windows 2013-11-12 12:36:54 +00:00
Victor Stinner 1c8f059019 Issue #18520: Add a new PyStructSequence_InitType2() function, same than
PyStructSequence_InitType() except that it has a return value (0 on success,
-1 on error).

 * PyStructSequence_InitType2() now raises MemoryError on memory allocation failure
 * Fix also some calls to PyDict_SetItemString(): handle error
2013-07-22 22:24:54 +02:00
Victor Stinner 2ff51b83b8 Issue #18408: Fix time.tzset(), detect exception when calling PyInit_timezone() 2013-07-17 21:42:45 +02:00
Victor Stinner 93037498d1 Fix time.mktime() and datetime.datetime.timestamp() on AIX
On AIX, the C function mktime() alwaysd sets tm_wday, even on error. So tm_wday
cannot be used as a sentinel to detect an error, we can only check if the
result is (time_t)-1.
2013-06-25 22:54:35 +02:00
Victor Stinner d7a034bd75 (Merge 3.3) Fix time.strftime("%Y") on AIX: raise a ValueError for year > 9999
time.strtime("%Y") returned "2345" when formatting year 12345.
2013-06-25 02:34:13 +02:00
Victor Stinner 36b82d85a3 Fix time.strftime("%Y") on AIX: raise a ValueError for year > 9999
time.strtime("%Y") returned "2345" when formatting year 12345.
2013-06-25 02:33:53 +02:00
Richard Oudkerk 245bbee0d5 Merge. 2013-04-17 21:24:58 +01:00
Antoine Pitrou cf8a1e51ec - Issue #17782: Fix undefined behaviour on platforms where ``struct timespec``'s "tv_nsec" member is not a C long. 2013-04-17 22:06:44 +02:00
Martin v. Löwis b26a9b10ea Replace WaitForSingleObject with WaitForSingleObjectEx,
for better WinRT compatibility.
2013-01-25 14:25:48 +01:00
Jesus Cea 14c81aba50 #16135: Removal of OS/2 support (Modules/*) 2012-10-05 02:11:36 +02:00
Alexander Belopolsky cf77454908 Issue #9650: List commonly used format codes in time.strftime and time.strptime docsttings. 2012-10-02 18:39:16 -04:00
Alexander Belopolsky c142bba2a7 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
in struct tm, time.struct_time objects returned by time.gmtime(),
time.localtime() and time.strptime() functions now have tm_zone and
tm_gmtoff attributes.  Original patch by Paul Boddie.
2012-06-13 22:15:26 -04:00
Victor Stinner 2b89fdf7eb PEP 418: Rename adjusted attribute to adjustable in time.get_clock_info() result
Fix also its value on Windows and Linux according to its documentation:
"adjustable" indicates if the clock *can be* adjusted, not if it is or was
adjusted.

In most cases, it is not possible to indicate if a clock is or was adjusted.
2012-06-12 22:46:37 +02:00
Victor Stinner bda4b8802c time.get_clock_info() uses a namespace instead of structseq 2012-06-12 22:11:44 +02:00
Alexander Belopolsky d9738242f8 Fixed a typo in time_localtime() 2012-06-12 16:14:17 -04:00
Benjamin Peterson 49a69e4d48 strip is_ prefixes on clock_info fields 2012-05-01 09:38:34 -04:00
Victor Stinner ec89539ccc Issue #14428, #14397: Implement the PEP 418
* Rename time.steady() to time.monotonic()
 * On Windows, time.monotonic() uses GetTickCount/GetTickCount64() instead of
   QueryPerformanceCounter()
 * time.monotonic() uses CLOCK_HIGHRES if available
 * Add time.get_clock_info(), time.perf_counter() and time.process_time()
   functions
2012-04-29 02:41:27 +02:00
Victor Stinner b8d016955a Fix clock_gettime/getres/settime: PyArg_ParseTuple() expects an int
Only use a single #ifdef for the 3 functions.
2012-04-13 23:44:05 +02:00
Victor Stinner 30d79471bb Expose clock_settime() as time.clock_settime() 2012-04-03 00:45:07 +02:00
Victor Stinner 1470f35bc6 Add time.CLOCK_HIGHRES constant, needed on Solaris 2012-04-03 00:31:17 +02:00
Victor Stinner ad95c2d25c time.time() now uses clock_gettime(CLOCK_REALTIME) if available
clock_gettime(CLOCK_REALTIME) has a better resolution than gettimeofday().
time.time() falls back on gettimeofday() (and then on other functions) on
error.
2012-03-28 02:54:15 +02:00
Victor Stinner 74eb6c0e8b Document the fact that mach_timebase_info() cannot fail
And call mach_absolute_time() after mach_timebase_info().
2012-03-28 02:50:46 +02:00
Victor Stinner 8486076dd5 Fix time.steady(strict=True): don't use CLOCK_REALTIME 2012-03-26 22:53:14 +02:00
Victor Stinner 70b2e1e7d9 Issue #14368: _PyTime_gettimeofday() cannot fail
floattime() must not raise an error if the current time is 1970.1.1 at 00:00.
2012-03-26 22:08:02 +02:00
Victor Stinner 071eca3f5c Issue #10278: Add an optional strict argument to time.steady(), False by default 2012-03-15 01:17:09 +01:00
Victor Stinner ec919cc74d Issue #10278: Drop time.monotonic() function, rename time.wallclock() to time.steady()
* On Mac OS X, time.steady() now uses mach_absolute_time(), a monotonic clock
 * Optimistic change: bet that CLOCK_MONOTONIC and CLOCK_REALTIME are available
   when clock_gettime() is available
 * Rewrite time.steady() documentation
2012-03-15 00:58:32 +01:00
Victor Stinner 5d272cc6a2 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
time.ctime(), gmtime(), time.localtime(), datetime.date.fromtimestamp(),
datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp() now
raises an OverflowError, instead of a ValueError, if the timestamp does not fit
in time_t.

datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp() now
round microseconds towards zero instead of rounding to nearest with ties going
away from zero.
2012-03-13 13:35:55 +01:00
Victor Stinner a8ec5ea923 Issue #14104: Implement time.monotonic() on Mac OS X,
patch written by Nicholas Riley.
2012-03-13 00:25:42 +01:00
Stefan Krah 4aea7d3811 Issue #14125: Fix refleak in timemodule.c on Windows. Thanks sbt for pointing
out the location of the problem. MS_WINDOWS currently implies !HAVE_WCSFTIME,
so the addition of !defined(HAVE_WCSFTIME) is for readability.
2012-02-27 16:30:26 +01:00
Victor Stinner 4195b5caea Backout f8409b3d6449: the PEP 410 is not accepted yet 2012-02-08 23:03:19 +01:00
Victor Stinner ccd5715a14 PEP 410 2012-02-08 14:31:50 +01:00
Victor Stinner 8b30201f7d Issue #13846: Add time.monotonic(), monotonic clock. 2012-02-07 23:29:46 +01:00
Victor Stinner 85fdfa85e1 Issue #13847: time.clock() now raises a RuntimeError if the processor time used
is not available or its value cannot be represented
2012-01-27 00:38:48 +01:00
Victor Stinner c1b5d34ede Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
of ValueError on failure. time.ctime() and time.asctime() now raises an
OSError if localtime() failed.
2012-01-27 00:08:48 +01:00
Antoine Pitrou e39ebe45c4 Merge 2012-01-18 02:05:38 +01:00
Victor Stinner 855889b4bf Issue #10278: fix a typo in the doc 2012-01-18 01:57:19 +01:00
Victor Stinner b94b266cfc Close #10278: Add time.wallclock() function, monotonic clock. 2012-01-18 01:50:21 +01:00
Antoine Pitrou 2c085604b7 Fix error handling in timemodule.c 2012-01-18 01:41:44 +01:00
Victor Stinner ab870218e3 Issue #10951: Fix compiler warnings in timemodule.c and unicodeobject.c
Thanks Jérémy Anger for the fix.
2011-12-17 22:39:43 +01:00
Victor Stinner 136ea49b39 Issue #10951: Fix a compiler warning in timemodule.c 2011-12-17 22:37:18 +01:00
Victor Stinner 1b57967b96 Issue #13560: Locale codec functions use the classic "errors" parameter,
instead of surrogateescape

So it would be possible to support more error handlers later.
2011-12-17 05:47:23 +01:00