/**
  Convert a Python datetime.time to MySQL TIME.

  Convert the PyObject obj, which must be a datetime.time,
  to MySQL TIME value.

  Raises TypeError when obj is not a PyTime_Type.

  @param    obj     the PyObject to be converted

  @return   Converted time object.
    @retval PyBytes     Python v3
    @retval PyString    Python v2
    @retval NULL        Exception
*/
PyObject*
pytomy_time(PyObject *obj)
{
    char result[17]= "";

    PyDateTime_IMPORT;

    if (!obj || !PyTime_Check(obj))
    {
        PyErr_SetString(PyExc_ValueError,
                        "Object must be a datetime.time");
        return NULL;
    }

    if (PyDateTime_TIME_GET_MICROSECOND(obj))
    {
        PyOS_snprintf(result, 17, "%02d:%02d:%02d.%06d",
                 PyDateTime_TIME_GET_HOUR(obj),
                 PyDateTime_TIME_GET_MINUTE(obj),
                 PyDateTime_TIME_GET_SECOND(obj),
                 PyDateTime_TIME_GET_MICROSECOND(obj));
    }
    else
    {
        PyOS_snprintf(result, 17, "%02d:%02d:%02d",
                 PyDateTime_TIME_GET_HOUR(obj),
                 PyDateTime_TIME_GET_MINUTE(obj),
                 PyDateTime_TIME_GET_SECOND(obj));
    }

    return PyBytesFromString(result);
}
Пример #2
0
static bool GetTimeInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInfo& info)
{
    info.Data.time.hour   = (SQLUSMALLINT)PyDateTime_TIME_GET_HOUR(param);
    info.Data.time.minute = (SQLUSMALLINT)PyDateTime_TIME_GET_MINUTE(param);
    info.Data.time.second = (SQLUSMALLINT)PyDateTime_TIME_GET_SECOND(param);

    info.ValueType         = SQL_C_TYPE_TIME;
    info.ParameterType     = SQL_TYPE_TIME;
    info.ColumnSize        = 8;
    info.ParameterValuePtr = &info.Data.time;
    info.StrLen_or_Ind     = sizeof(TIME_STRUCT);
    return true;
}
Пример #3
0
//-----------------------------------------------------------------------------
// TimeVar_SetValue()
//   Set the value of the variable.
//-----------------------------------------------------------------------------
static int TimeVar_SetValue(
    udt_TimeVar *var,              // variable to set value for
    unsigned pos,                       // array position to set
    PyObject *value)                    // value to set
{
    TIME_STRUCT *sqlValue;

    sqlValue = &var->data[pos];
    if (PyDateTime_Check(value)) {
        sqlValue->hour = PyDateTime_DATE_GET_HOUR(value);
        sqlValue->minute = PyDateTime_DATE_GET_MINUTE(value);
        sqlValue->second = PyDateTime_DATE_GET_SECOND(value);
    } else if (PyTime_Check(value)) {
        sqlValue->hour = PyDateTime_TIME_GET_HOUR(value);
        sqlValue->minute = PyDateTime_TIME_GET_MINUTE(value);
        sqlValue->second = PyDateTime_TIME_GET_SECOND(value);
    } else {
        PyErr_SetString(PyExc_TypeError, "expecting datetime or time data");
        return -1;
    }

    return 0;
}