Exemplo n.º 1
0
static PyObject* GetDataTimestamp(Cursor* cur, Py_ssize_t iCol)
{
    TIMESTAMP_STRUCT value;

    SQLLEN cbFetched = 0;
    SQLRETURN ret;

    Py_BEGIN_ALLOW_THREADS
    ret = SQLGetData(cur->hstmt, (SQLUSMALLINT)(iCol+1), SQL_C_TYPE_TIMESTAMP, &value, sizeof(value), &cbFetched);
    Py_END_ALLOW_THREADS
    if (!SQL_SUCCEEDED(ret))
        return RaiseErrorFromHandle("SQLGetData", cur->cnxn->hdbc, cur->hstmt);

    if (cbFetched == SQL_NULL_DATA)
        Py_RETURN_NONE;

    switch (cur->colinfos[iCol].sql_type)
    {
    case SQL_TYPE_TIME:
    {
        int micros = (int)(value.fraction / 1000); // nanos --> micros
        return PyTime_FromTime(value.hour, value.minute, value.second, micros);
    }

    case SQL_TYPE_DATE:
        return PyDate_FromDate(value.year, value.month, value.day);
    }

    int micros = (int)(value.fraction / 1000); // nanos --> micros
    return PyDateTime_FromDateAndTime(value.year, value.month, value.day, value.hour, value.minute, value.second, micros);
}
/**
  Convert a DATE MySQL value to Python datetime.date.

  Convert a DATETIME MySQL value to Python datetime.date. When a date
  can be parsed, but it is invalid, None is returned.

  Raises ValueError when the date is not for format %d-%d-%d.

  @param    data        string to be converted

  @return   datetime.date object.
    @retval PyDate  OK
    @retval None    Invalid date
    @retval NULL    Exception
*/
PyObject*
mytopy_date(const char *data)
{
    int year= 0, month= 0, day= 0;

    PyDateTime_IMPORT;

#pragma warning(push)
// sscanf data comes from MySQL and is fixed
#pragma warning(disable: 4996)
    if (3 == sscanf(data, "%d-%d-%d", &year, &month, &day))
#pragma warning(pop)
    {
        // Invalid dates are returned as None instead of raising ValueError
        if (!is_valid_date(year, month, day))
        {
            Py_RETURN_NONE;
        }
        return PyDate_FromDate(year, month, day);
    }

    PyErr_SetString(PyExc_ValueError,
                    "Received incorrect DATE value from MySQL server");
    return NULL;
}
static PyObject *meth_QDate_toPyDate(PyObject *sipSelf, PyObject *sipArgs)
{
    PyObject *sipParseErr = NULL;

    {
        QDate *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QDate, &sipCpp))
        {
            PyObject * sipRes = 0;

#line 117 "/home/tsheasha/GUC/Bachelors/android-python27/python-build/PyQt-x11-gpl-4.8/sip/QtCore/qdatetime.sip"
        if (!PyDateTimeAPI)
            PyDateTime_IMPORT;
        
        // Convert to a Python date object.
        sipRes = PyDate_FromDate(sipCpp->year(), sipCpp->month(), sipCpp->day());
#line 74 "sipQtCoreQDate.cpp"

            return sipRes;
        }
    }

    /* Raise an exception if the arguments couldn't be parsed. */
    sipNoMethod(sipParseErr, sipName_QDate, sipName_toPyDate, NULL);

    return NULL;
}
Exemplo n.º 4
0
static void *PyDateTimeToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
  PyObject *obj = (PyObject *) _obj;
  PyObject *date, *ord, *utcoffset;
  int y, m, d, h, mn, s, days;

  utcoffset = PyObject_CallMethod(obj, "utcoffset", NULL);
  if(utcoffset != Py_None){
    obj = PyNumber_Subtract(obj, utcoffset);
  }

  y = PyDateTime_GET_YEAR(obj);
  m = PyDateTime_GET_MONTH(obj);
  d = PyDateTime_GET_DAY(obj);
  h = PyDateTime_DATE_GET_HOUR(obj);
  mn = PyDateTime_DATE_GET_MINUTE(obj);
  s = PyDateTime_DATE_GET_SECOND(obj);

  date = PyDate_FromDate(y, m, 1);
  ord = PyObject_CallMethod(date, "toordinal", NULL);
  days = PyInt_AS_LONG(ord) - EPOCH_ORD + d - 1;
  Py_DECREF(date);
  Py_DECREF(ord);
  *( (JSINT64 *) outValue) = (((JSINT64) ((days * 24 + h) * 60 + mn)) * 60 + s);
  return NULL;
}
Exemplo n.º 5
0
static PyObject *meth_QDate_toPyDate(PyObject *sipSelf, PyObject *sipArgs)
{
    PyObject *sipParseErr = NULL;

    {
        const QDate *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QDate, &sipCpp))
        {
            PyObject * sipRes = 0;

#line 99 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\sip/QtCore/qdatetime.sip"
        if (!PyDateTimeAPI)
            PyDateTime_IMPORT;
        
        // Convert to a Python date object.
        sipRes = PyDate_FromDate(sipCpp->year(), sipCpp->month(), sipCpp->day());
#line 63 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\QtCore/sipQtCoreQDate.cpp"

            return sipRes;
        }
    }

    /* Raise an exception if the arguments couldn't be parsed. */
    sipNoMethod(sipParseErr, sipName_QDate, sipName_toPyDate, doc_QDate_toPyDate);

    return NULL;
}
Exemplo n.º 6
0
//-----------------------------------------------------------------------------
// DateVar_GetValue()
//   Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static PyObject *DateVar_GetValue(
    udt_DateVar *var,                   // variable to determine value for
    unsigned pos)                       // array position
{
    DATE_STRUCT *sqlValue;

    sqlValue = &var->data[pos];
    return PyDate_FromDate(sqlValue->year, sqlValue->month, sqlValue->day);
}
Exemplo n.º 7
0
PyObject * fudgepyc_convertDateToPython ( FudgeDate * source )
{
    PythonDateTime pdt;

    if ( fudgepyc_convertDateToPythonDateTime ( &pdt, source ) )
        return 0;

    return PyDate_FromDate ( pdt.years, pdt.months, pdt.days );
}
Exemplo n.º 8
0
static 
PyObject* from_date_kobject(K x) {
     /* on the python side we need to add date(2000, 1, 1) to these values */
     PyDateTime_IMPORT;
     PyObject* result;
     Py_ssize_t i, length ;
     length = (Py_ssize_t)(x->n);
     int value, y, m, d;

     if(scalar(x)) {
	  value = (x->i);
	  date_helper(value, &y, &m, &d);
	  result = PyDate_FromDate(y, m, d);
     }
     else {
	  result = PyList_New(length);
	  for(i = 0; i != length; ++i) {
	       value = (int) xI[i] ;
	       date_helper(value, &y, &m, &d);
	       PyList_SetItem(result, i, PyDate_FromDate(y, m, d) );
	  }
     }
     return result;
}
Exemplo n.º 9
0
static void *PyDateToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
  PyObject *obj = (PyObject *) _obj;
  PyObject *date, *ord;
  int y, m, d, days;

  y = PyDateTime_GET_YEAR(obj);
  m = PyDateTime_GET_MONTH(obj);
  d = PyDateTime_GET_DAY(obj);

  date = PyDate_FromDate(y, m, 1);
  ord = PyObject_CallMethod(date, "toordinal", NULL);
  days = PyInt_AS_LONG(ord) - EPOCH_ORD + d - 1;
  Py_DECREF(date);
  Py_DECREF(ord);
  *( (JSINT64 *) outValue) = ((JSINT64) days * 86400);

  return NULL;
}
Exemplo n.º 10
0
static PyObject *
str_to_date(PyObject *self, PyObject *arg)
{
    const char *str;
    unsigned int year, month, day;

    if (arg == Py_None)
        Py_RETURN_NONE;

    str = PyString_AsString(arg);
    if (str == NULL)
        return NULL;

    if (sscanf(str, "%4u-%2u-%2u", &year, &month, &day) != 3) {
        PyErr_SetString(PyExc_ValueError, "Couldn't parse date string.");
        return NULL;
    }
    return PyDate_FromDate(year, month, day);
}
Exemplo n.º 11
0
int API_resultRowValue(void *result, int column, UMTypeInfo *ti, char *value, size_t cbValue)
{
  PyObject *valobj = NULL;

  PRINTMARK();
  //fprintf (stderr, "%s: Got %p (%08x) %08x\n", __FUNCTION__, value, ti->type, cbValue);

  if (value == NULL)
  {
    valobj = Py_None;
    Py_IncRef(valobj);
  }
  else
  {

    switch (ti->type)
    {
      //PyNone:
    case MFTYPE_NULL:
      valobj = Py_None;
      Py_IncRef(valobj);
      break;

      // Use PyLong for "INT UNSIGNED".
    case MFTYPE_LONG:
      if(isUnsigned(ti->flags)){
        // XXX: No overflow detected.
        valobj = PyLong_FromLongLong(parseINT64 (value, ((char *) value) + cbValue));
        break;
      }
      // "INT" only, let it fall through to PyInt.

      //PyInt
    case MFTYPE_TINY:
    case MFTYPE_SHORT:
    case MFTYPE_INT24:
      {
        valobj = PyInt_FromLong(parseINT32 (value, ((char *) value) + cbValue));
        break;
      }

      //PyLong
    case MFTYPE_LONGLONG:
      {
        if(isUnsigned(ti->flags)){
          valobj = PyLong_FromUnsignedLongLong(parseUINT64 (value, ((char *) value) + cbValue));
        }else{
          valobj = PyLong_FromLongLong(parseINT64 (value, ((char *) value) + cbValue));
        }
        break;
      }

      //PyFloat
    case MFTYPE_FLOAT:
    case MFTYPE_DOUBLE:
      {
        //FIXME: Too f*****g slow
        PyObject *sobj = PyString_FromStringAndSize((char *) value, cbValue);
        valobj = PyFloat_FromString (sobj, NULL);
        Py_DECREF(sobj);
        break;
      }

    case MFTYPE_DATE:
      {
        int year;
        int month;
        int day;

        year = parseINT32 (value, value + 4);

        if (year < 1)
        {
          valobj = Py_None;
          Py_IncRef(valobj);
          break;
        }

        value += 5;
        month = parseINT32 (value, value + 2);
        value += 3;
        day = parseINT32 (value, value + 2);
        value += 3;

        valobj = PyDate_FromDate (year, month, day);
        break;
      }

    case MFTYPE_TIMESTAMP:
    case MFTYPE_DATETIME:
      {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;

        //9999-12-31 23:59:59
        char temp[20];
        memcpy (temp, value, cbValue);
        temp[cbValue] = '\0';

        year = parseINT32 (value, value + 4);
        value += 5;
        month = parseINT32 (value, value + 2);
        value += 3;
        day = parseINT32 (value, value + 2);
        value += 3;
        hour = parseINT32 (value, value + 2);
        value += 3;
        minute = parseINT32 (value, value + 2);
        value += 3;
        second = parseINT32 (value, value + 2);
        value += 3;

        if (year < 1)
        {
          valobj = Py_None;
          Py_IncRef(valobj);
          break;
        }


        valobj = PyDateTime_FromDateAndTime (year, month, day, hour, minute, second, 0);
        break;
      }

      // We ignore these

    case MFTYPE_TIME:
    case MFTYPE_YEAR:
    case MFTYPE_NEWDATE:
      // Fall through for string encoding

      //Blob goes as String
    case MFTYPE_TINY_BLOB:
    case MFTYPE_MEDIUM_BLOB:
    case MFTYPE_LONG_BLOB:
    case MFTYPE_BLOB:
      if (ti->flags & MFFLAG_BINARY_FLAG) {
        valobj = PyString_FromStringAndSize( (const char *) value, cbValue);
      } else {
        valobj = DecodeString (ti, value, cbValue);
      }
      break;

      //PyString family
    case MFTYPE_VAR_STRING:
    case MFTYPE_VARCHAR:
    case MFTYPE_STRING:
      valobj = DecodeString (ti, value, cbValue);
      break;

    case MFTYPE_ENUM:
    case MFTYPE_GEOMETRY:
    case MFTYPE_BIT:
    case MFTYPE_NEWDECIMAL:
    case MFTYPE_SET:
    case MFTYPE_DECIMAL:
      // Fall through for string encoding
      valobj = PyString_FromStringAndSize( (const char *) value, cbValue);
      break;

    }
  }

  if (valobj == NULL)
  {
    if (PyErr_Occurred())
    {
      return FALSE;
    }

    PyErr_Format (umysql_Error, "Unable to convert field of type %d", ti->type);
    return FALSE;
  }

  PyTuple_SET_ITEM(((ResultSet *)result)->currRow, column, valobj);
  PRINTMARK();

  return TRUE;
}
Exemplo n.º 12
0
static PyObject*
decode_datetime(JSONData *jsondata)
{

    PyObject *object;
    char c = 0;
    int n = 0;
    
    char *ptr = NULL,
         *tinfo_str = NULL;

    int year = 0,
        month = 0,
        day = 0,

        hour = 0,
        minute = 0,
        second = 0,
        usecond = 0;

    int is_tdelta = False,
        tinfo_len;

    // look for the closing quote
    ptr = jsondata->ptr + 2; // Skip the type hint and the opening quote

    while (True) {
        c = *ptr;
        if (c == 0) {
            PyErr_Format(JSON_DecodeError,
                "unterminated datetime string starting at position " SSIZE_T_F,
                (Py_ssize_t)(jsondata->ptr - jsondata->str));
            goto failure;
        }
        if (c == '"') {
            break;
        }
        if (ptr - jsondata->ptr - 2 == 0 ) { // first character
            switch (c) {
                case '-':
                case '+':
                    is_tdelta = True;
            }
        }

        ptr++;
    }

    // get only the actual datetime information
    skipSpaces(jsondata);
    tinfo_len = ptr - (jsondata->ptr + 2);
    if (tinfo_len) {
        tinfo_str = (char*) malloc(tinfo_len);
        strncpy(tinfo_str, jsondata->ptr + 2, tinfo_len);
    }

    if (is_tdelta) {
        n = sscanf(tinfo_str, "%d:%u:%u:%u.%u", &day, &hour, &minute, &second, &usecond);
        if (n != 3) {
            PyErr_Format(JSON_DecodeError, "bad timedelta format at position " SSIZE_T_F ": %s",
                (Py_ssize_t)(jsondata->ptr - jsondata->str),
                tinfo_str);
                goto failure;
        } else {
            second += minute * 60;
            second += hour * 60 * 60;
            object = PyDelta_FromDSU(day, second, usecond);
        }
    } else {
        char* is_datetime = strchr(tinfo_str, ' ');

        if (is_datetime == NULL) {
            switch (tinfo_len) {
                case 10:
                    n = sscanf(tinfo_str, "%u-%u-%u", &year, &month, &day);
                    object = PyDate_FromDate(year, month, day);
                    break;
                case 8:
                    n = sscanf(tinfo_str, "%u:%u:%u", &hour, &minute, &second);
                    object = PyTime_FromTime(hour, minute, second, 0);
                    break;
                case 15:
                    n = sscanf(tinfo_str, "%u:%u:%u.%u", &hour, &minute, &second, &usecond);
                    object = PyTime_FromTime(hour, minute, second, usecond);
                    break;
            }
        } else {
            switch (tinfo_len) {
                case 19:
                    n = sscanf(tinfo_str, "%u-%u-%u %u:%u:%u", &year, &month, &day, &hour, &minute, &second);
                    object = PyDateTime_FromDateAndTime(year, month, day, hour, minute, second, 0);
                    break;
                case 26:
                    n = sscanf(tinfo_str, "%u-%u-%u %u:%u:%u.%u", &year, &month, &day, &hour, &minute, &second, &usecond);
                    object = PyDateTime_FromDateAndTime(year, month, day, hour, minute, second, usecond);
                    break;
            }
        }
    }

    if (object == NULL) {
        PyErr_Format(JSON_DecodeError, "bad format for time, date, or datetime at position " SSIZE_T_F ": %s",
            (Py_ssize_t)(jsondata->ptr - jsondata->str),
            tinfo_str);
        goto failure;
    }

    jsondata->ptr = jsondata->ptr + tinfo_len + 3;

    return object;

failure:
    free(tinfo_str);
    Py_XDECREF(object);
    return NULL;

}
Exemplo n.º 13
0
static PyObject *
str_to_date(PyObject *self, PyObject *arg)
{
#if PY_MAJOR_VERSION >= 3
    PyObject *bytes;
    PyObject *err_bytes;
#endif
    const char *str;
    int numparsed;
    unsigned int year, month, day;
    PyObject *err_repr;

    if (arg == Py_None)
        Py_RETURN_NONE;

#if PY_MAJOR_VERSION >= 3
    bytes = PyUnicode_AsASCIIString(arg);
    if (bytes == NULL)
        str = NULL;
    else
        str = PyBytes_AS_STRING(bytes);
#else
    str = PyString_AsString(arg);
#endif
    if (str == NULL) {
        err_repr = PyObject_Repr(arg);
        if (err_repr == NULL)
            return NULL;
#if PY_MAJOR_VERSION >= 3
        err_bytes = PyUnicode_AsASCIIString(err_repr);
        if (err_bytes == NULL)
            return NULL;
        PyErr_Format(
                PyExc_ValueError,
                "Couldn't parse date string '%.200s' - value is not a string.",
                PyBytes_AS_STRING(err_bytes));
        Py_DECREF(err_bytes);
#else
        PyErr_Format(
                PyExc_ValueError,
                "Couldn't parse date string '%.200s' - value is not a string.",
                PyString_AsString(err_repr));
#endif
        Py_DECREF(err_repr);
        return NULL;
    }

    numparsed = sscanf(str, "%4u-%2u-%2u", &year, &month, &day);
#if PY_MAJOR_VERSION >= 3
    Py_DECREF(bytes);
#endif
    if (numparsed != 3) {
        err_repr = PyObject_Repr(arg);
        if (err_repr == NULL)
            return NULL;
#if PY_MAJOR_VERSION >= 3
        err_bytes = PyUnicode_AsASCIIString(err_repr);
        if (err_bytes == NULL)
            return NULL;
        PyErr_Format(
                PyExc_ValueError,
                "Couldn't parse date string: %.200s",
                PyBytes_AS_STRING(err_bytes));
        Py_DECREF(err_bytes);
#else
        PyErr_Format(
                PyExc_ValueError,
                "Couldn't parse date string: %.200s",
                PyString_AsString(err_repr));
#endif
        Py_DECREF(err_repr);
        return NULL;
    }
    return PyDate_FromDate(year, month, day);
}