mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #23485: Add _PyTime_FromMillisecondsObject() function
This commit is contained in:
		
							parent
							
								
									749a6a85c6
								
							
						
					
					
						commit
						fa09beb150
					
				
					 2 changed files with 25 additions and 6 deletions
				
			
		|  | @ -69,12 +69,18 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec( | ||||||
| /* Create a timestamp from a number of nanoseconds (C long). */ | /* Create a timestamp from a number of nanoseconds (C long). */ | ||||||
| PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns); | PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns); | ||||||
| 
 | 
 | ||||||
| /* Convert a Python float or int to a timetamp.
 | /* Convert a number of seconds (Python float or int) to a timetamp.
 | ||||||
|    Raise an exception and return -1 on error, return 0 on success. */ |    Raise an exception and return -1 on error, return 0 on success. */ | ||||||
| PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t, | PyAPI_FUNC(int) _PyTime_FromSecondsObject(_PyTime_t *t, | ||||||
|     PyObject *obj, |     PyObject *obj, | ||||||
|     _PyTime_round_t round); |     _PyTime_round_t round); | ||||||
| 
 | 
 | ||||||
|  | /* Convert a number of milliseconds (Python float or int, 10^-3) to a timetamp.
 | ||||||
|  |    Raise an exception and return -1 on error, return 0 on success. */ | ||||||
|  | PyAPI_FUNC(int) _PyTime_FromMillisecondsObject(_PyTime_t *t, | ||||||
|  |     PyObject *obj, | ||||||
|  |     _PyTime_round_t round); | ||||||
|  | 
 | ||||||
| /* Convert a timestamp to a number of seconds as a C double. */ | /* Convert a timestamp to a number of seconds as a C double. */ | ||||||
| PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t); | PyAPI_FUNC(double) _PyTime_AsSecondsDouble(_PyTime_t t); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -203,8 +203,9 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise) | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| int | static int | ||||||
| _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) | _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, | ||||||
|  |                    long to_nanoseconds) | ||||||
| { | { | ||||||
|     if (PyFloat_Check(obj)) { |     if (PyFloat_Check(obj)) { | ||||||
|         /* volatile avoids unsafe optimization on float enabled by gcc -O3 */ |         /* volatile avoids unsafe optimization on float enabled by gcc -O3 */ | ||||||
|  | @ -212,7 +213,7 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) | ||||||
| 
 | 
 | ||||||
|         /* convert to a number of nanoseconds */ |         /* convert to a number of nanoseconds */ | ||||||
|         d = PyFloat_AsDouble(obj); |         d = PyFloat_AsDouble(obj); | ||||||
|         d *= 1e9; |         d *= to_nanoseconds; | ||||||
| 
 | 
 | ||||||
|         if (round == _PyTime_ROUND_CEILING) |         if (round == _PyTime_ROUND_CEILING) | ||||||
|             d = ceil(d); |             d = ceil(d); | ||||||
|  | @ -242,8 +243,8 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) | ||||||
|                 _PyTime_overflow(); |                 _PyTime_overflow(); | ||||||
|             return -1; |             return -1; | ||||||
|         } |         } | ||||||
|         *t = sec * SEC_TO_NS; |         *t = sec * to_nanoseconds; | ||||||
|         if (*t / SEC_TO_NS != sec) { |         if (*t / to_nanoseconds != sec) { | ||||||
|             _PyTime_overflow(); |             _PyTime_overflow(); | ||||||
|             return -1; |             return -1; | ||||||
|         } |         } | ||||||
|  | @ -251,6 +252,18 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int | ||||||
|  | _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) | ||||||
|  | { | ||||||
|  |     return _PyTime_FromObject(t, obj, round, SEC_TO_NS); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int | ||||||
|  | _PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) | ||||||
|  | { | ||||||
|  |     return _PyTime_FromObject(t, obj, round, MS_TO_NS); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| double | double | ||||||
| _PyTime_AsSecondsDouble(_PyTime_t t) | _PyTime_AsSecondsDouble(_PyTime_t t) | ||||||
| { | { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner