mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
_PyTime_AsMicroseconds() rounding. Add also unit tests.
This commit is contained in:
parent
067bbba7a4
commit
62d1c70eff
3 changed files with 123 additions and 17 deletions
|
@ -19,6 +19,10 @@
|
|||
#define MS_TO_NS (MS_TO_US * US_TO_NS)
|
||||
#define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
|
||||
|
||||
/* Conversion from nanoseconds */
|
||||
#define NS_TO_MS (1000 * 1000)
|
||||
#define NS_TO_US (1000)
|
||||
|
||||
static void
|
||||
error_time_t_overflow(void)
|
||||
{
|
||||
|
@ -288,33 +292,29 @@ _PyTime_AsNanosecondsObject(_PyTime_t t)
|
|||
}
|
||||
|
||||
static _PyTime_t
|
||||
_PyTime_Multiply(_PyTime_t t, unsigned int multiply, _PyTime_round_t round)
|
||||
_PyTime_Divide(_PyTime_t t, _PyTime_t k, _PyTime_round_t round)
|
||||
{
|
||||
_PyTime_t k;
|
||||
if (multiply < SEC_TO_NS) {
|
||||
k = SEC_TO_NS / multiply;
|
||||
if (round == _PyTime_ROUND_CEILING)
|
||||
assert(k > 1);
|
||||
if (round == _PyTime_ROUND_CEILING) {
|
||||
if (t >= 0)
|
||||
return (t + k - 1) / k;
|
||||
else
|
||||
return t / k;
|
||||
}
|
||||
else {
|
||||
k = multiply / SEC_TO_NS;
|
||||
return t * k;
|
||||
return (t - (k - 1)) / k;
|
||||
}
|
||||
else
|
||||
return t / k;
|
||||
}
|
||||
|
||||
_PyTime_t
|
||||
_PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
|
||||
{
|
||||
return _PyTime_Multiply(t, 1000, round);
|
||||
return _PyTime_Divide(t, NS_TO_MS, round);
|
||||
}
|
||||
|
||||
/* FIXME: write unit tests */
|
||||
_PyTime_t
|
||||
_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
|
||||
{
|
||||
return _PyTime_Multiply(t, 1000 * 1000, round);
|
||||
return _PyTime_Divide(t, NS_TO_US, round);
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue