summaryrefslogtreecommitdiffstats
path: root/Python/pytime.c
blob: 8679bcc2cdeb0ab8dc7f5a2c2e1ab60a0e52fef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "Python.h"
#ifdef MS_WINDOWS
#include <windows.h>
#endif

#if defined(__APPLE__) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
  /*
   * _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter
   * might fail on some platforms. This fallback is unwanted on MacOSX because
   * that makes it impossible to use a binary build on OSX 10.4 on earlier
   * releases of the OS. Therefore claim we don't support ftime.
   */
# undef HAVE_FTIME
#endif

#if defined(HAVE_FTIME) && !defined(MS_WINDOWS)
#include <sys/timeb.h>
extern int ftime(struct timeb *);
#endif

#define MICROSECONDS    1000000

void
_PyTime_get(_PyTime_t *ts)
{
#ifdef MS_WINDOWS
    FILETIME system_time;
    ULARGE_INTEGER large;
    ULONGLONG value;

    GetSystemTimeAsFileTime(&system_time);
    large.u.LowPart = system_time.dwLowDateTime;
    large.u.HighPart = system_time.dwHighDateTime;
    /* 116,444,736,000,000,000: number of 100 ns between
       the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
       days). */
    value = large.QuadPart - 116444736000000000;
    ts->seconds = 0;
    ts->numerator = value;
    ts->denominator = (_PyTime_fraction_t)10000000;
#else

#ifdef HAVE_GETTIMEOFDAY
    struct timeval tv;
    int err;
#endif
#if defined(HAVE_FTIME)
    struct timeb t;
#endif

    /* 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 in a timeval struct.
      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
#ifdef GETTIMEOFDAY_NO_TZ
    err = gettimeofday(&tv);
#else /* !GETTIMEOFDAY_NO_TZ */
    err = gettimeofday(&tv, (struct timezone *)NULL);
#endif /* !GETTIMEOFDAY_NO_TZ */
    if (err == 0)
    {
        ts->seconds = tv.tv_sec;
        ts->numerator = tv.tv_usec;
        ts->denominator = MICROSECONDS;
        return;
    }
#endif /* !HAVE_GETTIMEOFDAY */

#if defined(HAVE_FTIME)
    ftime(&t);
    ts->seconds = t.time;
    ts->numerator = t.millitm;
    ts->denominator = 1000;
#else /* !HAVE_FTIME */
    ts->seconds = time(NULL);
    ts->numerator = 0;
    ts->denominator = 1;
#endif /* !HAVE_FTIME */

#endif /* MS_WINDOWS */
}

void
_PyTime_gettimeofday(_PyTime_timeval *tv)
{
    _PyTime_t ts;
    _PyTime_fraction_t k;
    time_t sec;

    _PyTime_get(&ts);
    tv->tv_sec = ts.seconds;
    if (ts.numerator) {
        if (ts.numerator > ts.denominator) {
            sec = Py_SAFE_DOWNCAST(ts.numerator / ts.denominator,
                                   _PyTime_fraction_t, time_t);
            /* ignore integer overflow because _PyTime_gettimeofday() has
               no return value */
            tv->tv_sec += sec;
            ts.numerator = ts.numerator % ts.denominator;
        }
        if (MICROSECONDS >= ts.denominator) {
            k = (_PyTime_fraction_t)MICROSECONDS / ts.denominator;
            tv->tv_usec = (long)(ts.numerator * k);
        }
        else {
            k = ts.denominator / (_PyTime_fraction_t)MICROSECONDS;
            tv->tv_usec = (long)(ts.numerator / k);
        }
    }
    else {
        tv->tv_usec = 0;
    }
}

static PyObject*
_PyLong_FromTime_t(time_t value)
{
#if SIZEOF_TIME_T <= SIZEOF_LONG
    return PyLong_FromLong(value);
#else
    assert(sizeof(time_t) <= sizeof(PY_LONG_LONG));
    return PyLong_FromLongLong(value);
#endif
}

#if defined(HAVE_LONG_LONG)
#  define _PyLong_FromTimeFraction_t PyLong_FromLongLong
#else
#  define _PyLong_FromTimeFraction_t PyLong_FromSize_t
#endif

/* Convert a timestamp to a PyFloat object */
static PyObject*
_PyTime_AsFloat(_PyTime_t *ts)
{
    double d;
    d = (double)ts->seconds;
    d += (double)ts->numerator / (double)ts->denominator;
    return PyFloat_FromDouble(d);
}

/* Convert a timestamp to a PyLong object */
static PyObject*
_PyTime_AsLong(_PyTime_t *ts)
{
    PyObject *a, *b, *c;

    a = _PyLong_FromTime_t(ts->seconds);
    if (a == NULL)
        return NULL;
    b = _PyLong_FromTimeFraction_t(ts->numerator / ts->denominator);
    if (b == NULL)
    {
        Py_DECREF(a);
        return NULL;
    }
    c = PyNumber_Add(a, b);
    Py_DECREF(a);
    Py_DECREF(b);
    return c;
}

/* Convert a timestamp to a decimal.Decimal object */
static PyObject*
_PyTime_AsDecimal(_PyTime_t *ts)
{
    static PyObject* module = NULL;
    static PyObject* decimal = NULL;
    static PyObject* exponent_context = NULL;
    static PyObject* context = NULL;
    /* exponent cache, dictionary of:
       int (denominator) => Decimal (1/denominator) */
    static PyObject* exponent_cache = NULL;
    PyObject *t = NULL;
    PyObject *key, *exponent, *quantized;
    _Py_IDENTIFIER(quantize);
    _Py_IDENTIFIER(__truediv__);

    if (!module) {
        module = PyImport_ImportModuleNoBlock("decimal");
        if (module == NULL)
            return NULL;
    }

    if (!decimal) {
        decimal = PyObject_GetAttrString(module, "Decimal");
        if (decimal == NULL)
            return NULL;
    }

    if (context == NULL)
    {
        /* Use 12 decimal digits to store 10,000 years in seconds + 9
           decimal digits for the floating part in nanoseconds + 1 decimal
           digit to round correctly.

           context = decimal.Context(22, rounding=decimal.ROUND_HALF_EVEN)
           exponent_context = decimal.Context(1, rounding=decimal.ROUND_HALF_EVEN)
        */
        PyObject *context_class, *rounding;
        context_class = PyObject_GetAttrString(module, "Context");
        if (context_class == NULL)
            return NULL;
        rounding = PyObject_GetAttrString(module, "ROUND_HALF_EVEN");
        if (rounding == NULL)
        {
            Py_DECREF(context_class);
            return NULL;
        }
        context = PyObject_CallFunction(context_class, "iO", 22, rounding);
        if (context == NULL)
        {
            Py_DECREF(context_class);
            Py_DECREF(rounding);
            return NULL;
        }

        exponent_context = PyObject_CallFunction(context_class, "iO", 1, rounding);
        Py_DECREF(context_class);
        Py_DECREF(rounding);
        if (exponent_context == NULL)
        {
            Py_CLEAR(context);
            return NULL;
        }
    }

    /* t = decimal.Decimal(value) */
    if (ts->seconds) {
        PyObject *f = _PyLong_FromTime_t(ts->seconds);
        t = PyObject_CallFunction(decimal, "O", f);
        Py_CLEAR(f);
    }
    else {
        t = PyObject_CallFunction(decimal, "iO", 0, context);
    }
    if (t == NULL)
        return NULL;

    if (ts->numerator)
    {
        /* t += decimal.Decimal(numerator, ctx) / decimal.Decimal(denominator, ctx) */
        PyObject *a, *b, *c, *d, *x;

        x = _PyLong_FromTimeFraction_t(ts->numerator);
        if (x == NULL)
            goto error;
        a = PyObject_CallFunction(decimal, "OO", x, context);
        Py_CLEAR(x);
        if (a == NULL)
            goto error;

        x = _PyLong_FromTimeFraction_t(ts->denominator);
        if (x == NULL)
        {
            Py_DECREF(a);
            goto error;
        }
        b = PyObject_CallFunction(decimal, "OO", x, context);
        Py_CLEAR(x);
        if (b == NULL)
        {
            Py_DECREF(a);
            goto error;
        }

        c = _PyObject_CallMethodId(a, &PyId___truediv__, "OO",
                                   b, context);
        Py_DECREF(a);
        Py_DECREF(b);
        if (c == NULL)
            goto error;

        d = PyNumber_Add(t, c);
        Py_DECREF(c);
        if (d == NULL)
            goto error;
        Py_DECREF(t);
        t = d;
    }

    if (exponent_cache == NULL) {
        exponent_cache = PyDict_New();
        if (exponent_cache == NULL)
            goto error;
    }

    key = _PyLong_FromTimeFraction_t(ts->denominator);
    if (key == NULL)
        goto error;
    exponent = PyDict_GetItem(exponent_cache, key);
    if (exponent == NULL) {
        /* exponent = decimal.Decimal(1) / decimal.Decimal(resolution) */
        PyObject *one, *denominator;

        one = PyObject_CallFunction(decimal, "i", 1);
        if (one == NULL) {
            Py_DECREF(key);
            goto error;
        }

        denominator = PyObject_CallFunction(decimal, "O", key);
        if (denominator == NULL) {
            Py_DECREF(key);
            Py_DECREF(one);
            goto error;
        }

        exponent = _PyObject_CallMethodId(one, &PyId___truediv__, "OO",
                                          denominator, exponent_context);
        Py_DECREF(one);
        Py_DECREF(denominator);
        if (exponent == NULL) {
            Py_DECREF(key);
            goto error;
        }

        if (PyDict_SetItem(exponent_cache, key, exponent) < 0) {
            Py_DECREF(key);
            Py_DECREF(exponent);
            goto error;
        }
        Py_DECREF(key);
    }

    /* t = t.quantize(exponent, None, context) */
    quantized = _PyObject_CallMethodId(t, &PyId_quantize, "OOO",
                                       exponent, Py_None, context);
    if (quantized == NULL)
        goto error;
    Py_DECREF(t);
    t = quantized;

    return t;

error:
    Py_XDECREF(t);
    return NULL;
}

PyObject*
_PyTime_Convert(_PyTime_t *ts, PyObject *format)
{
    assert(ts->denominator != 0);

    if (format == NULL || (PyTypeObject *)format == &PyFloat_Type)
        return _PyTime_AsFloat(ts);
    if ((PyTypeObject *)format == &PyLong_Type)
        return _PyTime_AsLong(ts);

    if (PyType_Check(format))
    {
        PyObject *module, *name;
        _Py_IDENTIFIER(__name__);
        _Py_IDENTIFIER(__module__);

        module = _PyObject_GetAttrId(format, &PyId___module__);
        name = _PyObject_GetAttrId(format, &PyId___name__);
        if (module != NULL && PyUnicode_Check(module)
            && name != NULL && PyUnicode_Check(name))
        {
            if (PyUnicode_CompareWithASCIIString(module, "decimal") == 0
                && PyUnicode_CompareWithASCIIString(name, "Decimal") == 0)
                return _PyTime_AsDecimal(ts);
        }
        else
            PyErr_Clear();
    }

    PyErr_Format(PyExc_ValueError, "Unknown timestamp format: %R", format);
    return NULL;
}

void
_PyTime_Init()
{
    /* Do nothing.  Needed to force linking. */
}