Beispiel #1
0
static PyObject *
typecast_MXDATE_cast(const char *str, Py_ssize_t len, PyObject *curs)
{
    int n, y=0, m=0, d=0;
    int hh=0, mm=0, ss=0, us=0, tz=0;
    const char *tp = NULL;

    if (str == NULL) { Py_RETURN_NONE; }

    Dprintf("typecast_MXDATE_cast: s = %s", str);

    /* check for infinity */
    if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) {
        if (str[0] == '-') {
            return mxDateTime.DateTime_FromDateAndTime(-999998,1,1, 0,0,0);
        }
        else {
            return mxDateTime.DateTime_FromDateAndTime(999999,12,31, 0,0,0);
        }
    }

    n = typecast_parse_date(str, &tp, &len, &y, &m, &d);
    Dprintf("typecast_MXDATE_cast: tp = %p n = %d,"
            " len = " FORMAT_CODE_PY_SSIZE_T ","
            " y = %d, m = %d, d = %d", tp, n, len, y, m, d);
    if (n != 3) {
        PyErr_SetString(DataError, "unable to parse date");
        return NULL;
    }

    if (len > 0) {
        n = typecast_parse_time(tp, NULL, &len, &hh, &mm, &ss, &us, &tz);
        Dprintf("typecast_MXDATE_cast: n = %d,"
            " len = " FORMAT_CODE_PY_SSIZE_T ","
            " hh = %d, mm = %d, ss = %d, us = %d, tz = %d",
            n, len, hh, mm, ss, us, tz);
        if (n != 0 && (n < 3 || n > 6)) {
            PyErr_SetString(DataError, "unable to parse time");
            return NULL;
        }
    }

    Dprintf("typecast_MXDATE_cast: fractionary seconds: %lf",
        (double)ss + (double)us/(double)1000000.0);
    return mxDateTime.DateTime_FromDateAndTime(y, m, d, hh, mm,
        (double)ss + (double)us/(double)1000000.0);
}
static PyObject *
typecast_PYDATE_cast(const char *str, Py_ssize_t len, PyObject *curs)
{
    PyObject* obj = NULL;
    int n, y=0, m=0, d=0;

    if (str == NULL) {Py_INCREF(Py_None); return Py_None;}

    if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) {
        if (str[0] == '-') {
            obj = PyObject_GetAttrString(
                (PyObject*)PyDateTimeAPI->DateType, "min");
        }
        else {
            obj = PyObject_GetAttrString(
                (PyObject*)PyDateTimeAPI->DateType, "max");
        }
    }

    else {
        n = typecast_parse_date(str, NULL, &len, &y, &m, &d);
        Dprintf("typecast_PYDATE_cast: "
                "n = %d, len = " FORMAT_CODE_PY_SSIZE_T ", "
                "y = %d, m = %d, d = %d",
                 n, len, y, m, d);
        if (n != 3) {
            PyErr_SetString(DataError, "unable to parse date");
            return NULL;
        }
        else {
            if (y > 9999) y = 9999;
            obj = PyObject_CallFunction(
                (PyObject*)PyDateTimeAPI->DateType, "iii", y, m, d);
        }
    }
    return obj;
}
static PyObject *
typecast_PYDATETIME_cast(const char *str, Py_ssize_t len, PyObject *curs)
{
    PyObject* obj = NULL;
    PyObject *tzinfo = NULL;
    PyObject *tzinfo_factory;
    int n, y=0, m=0, d=0;
    int hh=0, mm=0, ss=0, us=0, tz=0;
    const char *tp = NULL;

    if (str == NULL) {Py_INCREF(Py_None); return Py_None;}

    /* check for infinity */
    if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) {
        if (str[0] == '-') {
            obj = PyObject_GetAttrString(
                (PyObject*)PyDateTimeAPI->DateTimeType, "min");
        }
        else {
            obj = PyObject_GetAttrString(
                (PyObject*)PyDateTimeAPI->DateTimeType, "max");
        }
    }

    else {
        Dprintf("typecast_PYDATETIME_cast: s = %s", str);
        n = typecast_parse_date(str, &tp, &len, &y, &m, &d);
        Dprintf("typecast_PYDATE_cast: tp = %p "
                "n = %d, len = " FORMAT_CODE_PY_SSIZE_T ","
                " y = %d, m = %d, d = %d",
                 tp, n, len, y, m, d);
        if (n != 3) {
            PyErr_SetString(DataError, "unable to parse date");
            return NULL;
        }

        if (len > 0) {
            n = typecast_parse_time(tp, NULL, &len, &hh, &mm, &ss, &us, &tz);
            Dprintf("typecast_PYDATETIME_cast: n = %d,"
                " len = " FORMAT_CODE_PY_SSIZE_T ","
                " hh = %d, mm = %d, ss = %d, us = %d, tz = %d",
                n, len, hh, mm, ss, us, tz);
            if (n < 3 || n > 6) {
                PyErr_SetString(DataError, "unable to parse time");
                return NULL;
            }
        }

        if (ss > 59) {
            mm += 1;
            ss -= 60;
        }
        if (y > 9999)
            y = 9999;

        tzinfo_factory = ((cursorObject *)curs)->tzinfo_factory;
        if (n >= 5 && tzinfo_factory != Py_None) {
            /* we have a time zone, calculate minutes and create
               appropriate tzinfo object calling the factory */
            Dprintf("typecast_PYDATETIME_cast: UTC offset = %ds", tz);

            /* The datetime module requires that time zone offsets be
               a whole number of minutes, so truncate the seconds to the
               closest minute. */
            // printf("%d %d %d\n", tz, tzmin, round(tz / 60.0));
            tzinfo = PyObject_CallFunction(tzinfo_factory, "i",
                (int)round(tz / 60.0));
        } else {
            Py_INCREF(Py_None);
            tzinfo = Py_None;
        }
        if (tzinfo != NULL) {
            obj = PyObject_CallFunction(
                (PyObject*)PyDateTimeAPI->DateTimeType, "iiiiiiiO",
                y, m, d, hh, mm, ss, us, tzinfo);
            Dprintf("typecast_PYDATETIME_cast: tzinfo: %p, refcnt = "
                FORMAT_CODE_PY_SSIZE_T,
                tzinfo, Py_REFCNT(tzinfo)
              );
            Py_DECREF(tzinfo);
        }
    }
    return obj;
}