Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

edgify / persistent   python

Repository URL to install this package:

Version: 4.6.4 

/ _timestamp.c

/*****************************************************************************

  Copyright (c) 2001, 2004 Zope Foundation and Contributors.
  All Rights Reserved.

  This software is subject to the provisions of the Zope Public License,
  Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  FOR A PARTICULAR PURPOSE

 ****************************************************************************/

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "bytesobject.h"
#include <time.h>
#include "_compat.h"


PyObject *TimeStamp_FromDate(int, int, int, int, int, double);
PyObject *TimeStamp_FromString(const char *);

static char TimeStampModule_doc[] =
"A 64-bit TimeStamp used as a ZODB serial number.\n"
"\n"
"$Id$\n";


/* A magic constant having the value 0.000000013969839. When an
   number of seconds between 0 and 59 is *divided* by this number, we get
   a number between 0 (for 0), 71582786 (for 1) and 4223384393 (for 59),
   all of which can be represented in a 32-bit unsigned integer, suitable
   for packing into 4 bytes using `TS_PACK_UINT32_INTO_BYTES`.
   To get (close to) the original seconds back, use
   `TS_UNPACK_UINT32_FROM_BYTES` and *multiply* by this number.
 */
#define TS_SECOND_BYTES_BIAS ((double)((double)60) / ((double)(0x10000)) / ((double)(0x10000)))
#define TS_BASE_YEAR 1900
#define TS_MINUTES_PER_DAY 1440
/* We pretend there are always 31 days in a month; this has us using
   372 days in a year in some calculations */
#define TS_DAYS_PER_MONTH 31
#define TS_MONTHS_PER_YEAR 12
#define TS_MINUTES_PER_MONTH (TS_DAYS_PER_MONTH * TS_MINUTES_PER_DAY)
#define TS_MINUTES_PER_YEAR (TS_MINUTES_PER_MONTH * TS_MONTHS_PER_YEAR)

/* The U suffixes matter on these constants to be sure
   the compiler generates the appropriate instructions when
   optimizations are enabled. On x86_64 GCC, if -fno-wrapv is given
   and -O is used, the compiler might choose to treat these as 32 bit
   signed quantities otherwise, producing incorrect results on
   some corner cases. See
   https://github.com/zopefoundation/persistent/issues/86
*/

/**
 * Given an unsigned int *v*, pack it into the four
 * unsigned char bytes beginning at *bytes*. If *v* is larger
 * than 2^31 (i.e., it doesn't fit in 32 bits), the results will
 * be invalid (the first byte will be 0.)
 *
 * The inverse is `TS_UNPACK_UINT32_FROM_BYTES`. This is a
 * lossy operation and may lose some lower-order precision.
 *
 */
#define TS_PACK_UINT32_INTO_BYTES(v, bytes) do { \
    *(bytes) = v / 0x1000000U;                   \
    *(bytes + 1) = (v % 0x1000000U) / 0x10000U;  \
    *(bytes + 2) = (v % 0x10000U) / 0x100U;      \
    *(bytes + 3) = v % 0x100U;                   \
} while (0)

/**
 * Given a sequence of four unsigned chars beginning at *bytes*
 * as produced by `TS_PACK_UINT32_INTO_BYTES`, return the
 * original unsigned int.
 *
 * Remember this is a lossy operation, and the value you get back
 * may not exactly match the original value. If the original value
 * was greater than 2^31 it will definitely not match.
 */
#define TS_UNPACK_UINT32_FROM_BYTES(bytes) (*(bytes) * 0x1000000U + *(bytes + 1) * 0x10000U + *(bytes + 2) * 0x100U + *(bytes + 3))

typedef struct
{
    PyObject_HEAD
    /*
      The first four bytes of data store the year, month, day, hour, and
      minute as the number of minutes since Jan 1 00:00.

      The final four bytes store the seconds since 00:00 as
      the number of microseconds.

      Both are normalized into those four bytes the same way with
      TS_[UN]PACK_UINT32_INTO|FROM_BYTES.
    */

    unsigned char data[8];
} TimeStamp;

/* The first dimension of the arrays below is non-leapyear / leapyear */

static char month_len[2][12] =
{
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

static short joff[2][12] =
{
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};

static double gmoff=0;


static int
leap(int year)
{
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

static int
days_in_month(int year, int month)
{
    return month_len[leap(year)][month];
}

static double
TimeStamp_yad(int y)
{
    double d, s;

    y -= TS_BASE_YEAR;

    d = (y - 1) * 365;
    if (y > 0) {
        s = 1.0;
    y -= 1;
    } else {
    s = -1.0;
    y = -y;
    }
    return d + s * (y / 4 - y / 100 + (y + 300) / 400);
}

static double
TimeStamp_abst(int y, int mo, int d, int m, int s)
{
    return (TimeStamp_yad(y) + joff[leap(y)][mo] + d) * 86400 + m * 60 + s;
}

static int
TimeStamp_init_gmoff(void)
{
    struct tm *t;
    time_t z=0;

    t = gmtime(&z);
    if (t == NULL)
    {
        PyErr_SetString(PyExc_SystemError, "gmtime failed");
        return -1;
    }

    gmoff = TimeStamp_abst(t->tm_year + TS_BASE_YEAR, t->tm_mon, t->tm_mday - 1,
               t->tm_hour * 60 + t->tm_min, t->tm_sec);

    return 0;
}

static void
TimeStamp_dealloc(TimeStamp *ts)
{
    PyObject_Del(ts);
}

static PyObject*
TimeStamp_richcompare(TimeStamp *self, TimeStamp *other, int op)
{
    PyObject *result = NULL;
    int cmp;

    if (Py_TYPE(other) != Py_TYPE(self))
    {
        result = Py_NotImplemented;
    }
    else
    {
        cmp = memcmp(self->data, other->data, 8);
        switch (op) {
        case Py_LT:
            result = (cmp < 0) ? Py_True : Py_False;
            break;
        case Py_LE:
            result = (cmp <= 0) ? Py_True : Py_False;
            break;
        case Py_EQ:
            result = (cmp == 0) ? Py_True : Py_False;
            break;
        case Py_NE:
            result = (cmp != 0) ? Py_True : Py_False;
            break;
        case Py_GT:
            result = (cmp > 0) ? Py_True : Py_False;
            break;
        case Py_GE:
            result = (cmp >= 0) ? Py_True : Py_False;
            break;
        }
    }

    Py_XINCREF(result);
    return result;
}


#ifdef PY3K
static Py_hash_t
#else
static long
#endif
TimeStamp_hash(TimeStamp *self)
{
    register unsigned char *p = (unsigned char *)self->data;
    register int len = 8;
    register long x = *p << 7;
    while (--len >= 0)
        x = (1000003*x) ^ *p++;
    x ^= 8;
    if (x == -1)
        x = -2;
    return x;
}

typedef struct
{
    /* TODO:  reverse-engineer what's in these things and comment them */
    int y;
    int m;
    int d;
    int mi;
} TimeStampParts;


static void
TimeStamp_unpack(TimeStamp *self, TimeStampParts *p)
{
    unsigned int minutes_since_base;

    minutes_since_base = TS_UNPACK_UINT32_FROM_BYTES(self->data);
    p->y = minutes_since_base / TS_MINUTES_PER_YEAR + TS_BASE_YEAR;
    p->m = (minutes_since_base % TS_MINUTES_PER_YEAR) / TS_MINUTES_PER_MONTH + 1;
    p->d = (minutes_since_base % TS_MINUTES_PER_MONTH) / TS_MINUTES_PER_DAY + 1;
    p->mi = minutes_since_base % TS_MINUTES_PER_DAY;
}

static double
TimeStamp_sec(TimeStamp *self)
{
    unsigned int v;

    v = TS_UNPACK_UINT32_FROM_BYTES(self->data +4);
    return TS_SECOND_BYTES_BIAS * v;
}

static PyObject *
TimeStamp_year(TimeStamp *self)
{
    TimeStampParts p;
    TimeStamp_unpack(self, &p);
    return INT_FROM_LONG(p.y);
}

static PyObject *
TimeStamp_month(TimeStamp *self)
{
    TimeStampParts p;
    TimeStamp_unpack(self, &p);
    return INT_FROM_LONG(p.m);
}

static PyObject *
TimeStamp_day(TimeStamp *self)
{
    TimeStampParts p;
    TimeStamp_unpack(self, &p);
    return INT_FROM_LONG(p.d);
}

static PyObject *
TimeStamp_hour(TimeStamp *self)
{
    TimeStampParts p;
    TimeStamp_unpack(self, &p);
    return INT_FROM_LONG(p.mi / 60);
}

static PyObject *
TimeStamp_minute(TimeStamp *self)
{
    TimeStampParts p;
    TimeStamp_unpack(self, &p);
    return INT_FROM_LONG(p.mi % 60);
}

static PyObject *
TimeStamp_second(TimeStamp *self)
{
    return PyFloat_FromDouble(TimeStamp_sec(self));
}

static PyObject *
TimeStamp_timeTime(TimeStamp *self)
{
    TimeStampParts p;
    TimeStamp_unpack(self, &p);
    return PyFloat_FromDouble(TimeStamp_abst(p.y, p.m - 1, p.d - 1, p.mi, 0)
                  + TimeStamp_sec(self) - gmoff);
}

static PyObject *
TimeStamp_raw(TimeStamp *self)
{
    return PyBytes_FromStringAndSize((const char*)self->data, 8);
}

static PyObject *
TimeStamp_repr(TimeStamp *self)
{
    PyObject *raw, *result;
    raw = TimeStamp_raw(self);
    result = PyObject_Repr(raw);
    Py_DECREF(raw);
    return result;
}

static PyObject *
TimeStamp_str(TimeStamp *self)
{
    char buf[128];
    TimeStampParts p;
Loading ...