static bool GetDateTimeInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInfo& info) { info.Data.timestamp.year = (SQLSMALLINT) PyDateTime_GET_YEAR(param); info.Data.timestamp.month = (SQLUSMALLINT)PyDateTime_GET_MONTH(param); info.Data.timestamp.day = (SQLUSMALLINT)PyDateTime_GET_DAY(param); info.Data.timestamp.hour = (SQLUSMALLINT)PyDateTime_DATE_GET_HOUR(param); info.Data.timestamp.minute = (SQLUSMALLINT)PyDateTime_DATE_GET_MINUTE(param); info.Data.timestamp.second = (SQLUSMALLINT)PyDateTime_DATE_GET_SECOND(param); // SQL Server chokes if the fraction has more data than the database supports. We expect other databases to be the // same, so we reduce the value to what the database supports. http://support.microsoft.com/kb/263872 int precision = ((Connection*)cur->cnxn)->datetime_precision - 20; // (20 includes a separating period) if (precision <= 0) { info.Data.timestamp.fraction = 0; } else { info.Data.timestamp.fraction = (SQLUINTEGER)(PyDateTime_DATE_GET_MICROSECOND(param) * 1000); // 1000 == micro -> nano // (How many leading digits do we want to keep? With SQL Server 2005, this should be 3: 123000000) int keep = (int)pow(10.0, 9-min(9, precision)); info.Data.timestamp.fraction = info.Data.timestamp.fraction / keep * keep; info.DecimalDigits = (SQLSMALLINT)precision; } info.ValueType = SQL_C_TIMESTAMP; info.ParameterType = SQL_TIMESTAMP; info.ColumnSize = (SQLUINTEGER)((Connection*)cur->cnxn)->datetime_precision; info.StrLen_or_Ind = sizeof(TIMESTAMP_STRUCT); info.ParameterValuePtr = &info.Data.timestamp; return true; }
static void construct( PyObject *obj_ptr, converter::rvalue_from_python_stage1_data *data ) { assert( obj_ptr ); assert( PyDateTime_Check( obj_ptr ) ); PyDateTime_DateTime const* datetime = reinterpret_cast<PyDateTime_DateTime*>( obj_ptr ); const posix_time::ptime t( gregorian::date( PyDateTime_GET_YEAR( datetime ), PyDateTime_GET_MONTH( datetime ), PyDateTime_GET_DAY( datetime ) ), posix_time::time_duration( PyDateTime_DATE_GET_HOUR( datetime ), PyDateTime_DATE_GET_MINUTE( datetime ), PyDateTime_DATE_GET_SECOND( datetime ), 0 ) + posix_time::microseconds( PyDateTime_DATE_GET_MICROSECOND( datetime ) ) ); void* storage = (( converter::rvalue_from_python_storage<posix_time::ptime>* ) data )->storage.bytes; new( storage ) posix_time::ptime( t ); data->convertible = storage; }
/** Convert a Python datetime.datetime to MySQL DATETIME. Convert the PyObject obj, which must be a datetime.datetime, to MySQL DATETIME value. Raises TypeError when obj is not a PyDateTime_Type. @param obj the PyObject to be converted @return Converted datetime object. @retval PyBytes Python v3 @retval PyString Python v2 @retval NULL Exception */ PyObject* pytomy_datetime(PyObject *obj) { char result[27]= ""; PyDateTime_IMPORT; if (!obj || !PyDateTime_Check(obj)) { PyErr_SetString(PyExc_ValueError, "Object must be a datetime.datetime"); return NULL; } if (PyDateTime_DATE_GET_MICROSECOND(obj)) { PyOS_snprintf(result, 27, "%04d-%02d-%02d %02d:%02d:%02d.%06d", PyDateTime_GET_YEAR(obj), PyDateTime_GET_MONTH(obj), PyDateTime_GET_DAY(obj), PyDateTime_DATE_GET_HOUR(obj), PyDateTime_DATE_GET_MINUTE(obj), PyDateTime_DATE_GET_SECOND(obj), PyDateTime_DATE_GET_MICROSECOND(obj)); } else { PyOS_snprintf(result, 27, "%04d-%02d-%02d %02d:%02d:%02d", PyDateTime_GET_YEAR(obj), PyDateTime_GET_MONTH(obj), PyDateTime_GET_DAY(obj), PyDateTime_DATE_GET_HOUR(obj), PyDateTime_DATE_GET_MINUTE(obj), PyDateTime_DATE_GET_SECOND(obj)); } return PyBytesFromString(result); }
static long long millis_from_datetime(PyObject* datetime) { struct TM timeinfo; long long millis; timeinfo.tm_year = PyDateTime_GET_YEAR(datetime) - 1900; timeinfo.tm_mon = PyDateTime_GET_MONTH(datetime) - 1; timeinfo.tm_mday = PyDateTime_GET_DAY(datetime); timeinfo.tm_hour = PyDateTime_DATE_GET_HOUR(datetime); timeinfo.tm_min = PyDateTime_DATE_GET_MINUTE(datetime); timeinfo.tm_sec = PyDateTime_DATE_GET_SECOND(datetime); millis = timegm64(&timeinfo) * 1000; millis += PyDateTime_DATE_GET_MICROSECOND(datetime) / 1000; return millis; }
bool Device::getTimevalFromDateTime(PyObject *py_time, struct timeval &tv_time) { if ((!py_time) || (!PyDateTime_Check(py_time))) { return false; } struct tm t; t.tm_year = PyDateTime_GET_YEAR(py_time) - 1900; t.tm_mon = PyDateTime_GET_MONTH(py_time) - 1; t.tm_mday = PyDateTime_GET_DAY(py_time); t.tm_hour = PyDateTime_DATE_GET_HOUR(py_time); t.tm_min = PyDateTime_DATE_GET_MINUTE(py_time); t.tm_sec = PyDateTime_DATE_GET_SECOND(py_time); tv_time.tv_sec = mktime(&t); tv_time.tv_usec = PyDateTime_DATE_GET_MICROSECOND(py_time); return true; }
static double _udate(PyObject *dt) { PyObject *result = PyObject_CallMethodObjArgs(dt, toordinal_NAME, NULL); if (!result) return 0.0; #if PY_MAJOR_VERSION >= 3 unsigned long ordinal = PyLong_AsUnsignedLong(result); #else unsigned long ordinal = PyInt_AS_LONG(result); #endif Py_DECREF(result); return ((ordinal - 719163) * 86400.0 + PyDateTime_DATE_GET_HOUR(dt) * 3600.0 + PyDateTime_DATE_GET_MINUTE(dt) * 60.0 + PyDateTime_DATE_GET_SECOND(dt) + PyDateTime_DATE_GET_MICROSECOND(dt) / 1e6) * 1000.0; }
static PyObject *t_tzinfo_utcoffset(t_tzinfo *self, PyObject *dt) { PyObject *weekday = PyObject_CallMethodObjArgs(dt, weekday_NAME, NULL); if (!weekday) return NULL; // python's MINYEAR is 1 int era = GregorianCalendar::AD; int year = PyDateTime_GET_YEAR(dt); // python's month is 1-based, 1 is January // ICU's month is 0-based, 0 is January int month = PyDateTime_GET_MONTH(dt) - 1; int day = PyDateTime_GET_DAY(dt); // python's weekday is 0-based, 0 is Monday // ICU's dayofweek is 1-based, 1 is Sunday int dayofweek = ((PyInt_AsLong(weekday) + 1) % 7) + 1; Py_DECREF(weekday); int millis = (int) ((PyDateTime_DATE_GET_HOUR(dt) * 3600.0 + PyDateTime_DATE_GET_MINUTE(dt) * 60.0 + PyDateTime_DATE_GET_SECOND(dt) + PyDateTime_DATE_GET_MICROSECOND(dt) / 1e6) * 1000.0); int offset; STATUS_CALL(offset = self->tz->object->getOffset(era, year, month, day, dayofweek, millis, status)); PyObject *args = PyTuple_New(2); PyObject *result; PyTuple_SET_ITEM(args, 0, PyInt_FromLong(0)); PyTuple_SET_ITEM(args, 1, PyInt_FromLong(offset / 1000)); result = PyObject_Call((PyObject *) datetime_deltaType, args, NULL); Py_DECREF(args); return result; }
//----------------------------------------------------------------------------- // TimestampVar_SetValue() // Set the value of the variable. //----------------------------------------------------------------------------- static int TimestampVar_SetValue( udt_TimestampVar *var, // variable to set value for unsigned pos, // array position to set PyObject *value) // value to set { sword status; uword valid; // make sure a timestamp is being bound if (!PyDateTime_Check(value)) { PyErr_SetString(PyExc_TypeError, "expecting timestamp data"); return -1; } // store a copy of the value status = OCIDateTimeConstruct(var->environment->handle, var->environment->errorHandle, var->data[pos], (sb2) PyDateTime_GET_YEAR(value), PyDateTime_GET_MONTH(value), PyDateTime_GET_DAY(value), PyDateTime_DATE_GET_HOUR(value), PyDateTime_DATE_GET_MINUTE(value), PyDateTime_DATE_GET_SECOND(value), PyDateTime_DATE_GET_MICROSECOND(value) * 1000, NULL, 0); if (Environment_CheckForError(var->environment, status, "TimestampVar_SetValue(): create structure") < 0) return -1; status = OCIDateTimeCheck(var->environment->handle, var->environment->errorHandle, var->data[pos], &valid); if (Environment_CheckForError(var->environment, status, "TimestampVar_SetValue()") < 0) return -1; if (valid != 0) { PyErr_SetString(g_DataErrorException, "invalid date"); return -1; } return 0; }
dbtype_t from_python(pgctx_t *ctx, PyObject *ob) { dbtype_t db; char *buf; Py_ssize_t length; PyObject *items; struct tm tm; long usec; //int i; if (PyObject_HasAttrString(ob, "__topongo__")) { ob = PyObject_CallMethod(ob, "__topongo__", NULL); if (PyErr_Occurred()) return DBNULL; } if (ob == Py_None) { db = DBNULL; } else if (ob == pongo_id) { db = dbuuid_new(ctx, NULL); } else if (ob == pongo_utcnow) { db = dbtime_now(ctx); } else if (PyBool_Check(ob)) { db = dbboolean_new(ctx, ob == Py_True); } else if (PyInt_Check(ob)) { db = dbint_new(ctx, PyInt_AsLong(ob)); } else if (PyLong_Check(ob)) { db = dbint_new(ctx, PyLong_AsLongLong(ob)); } else if (PyFloat_Check(ob)) { db = dbfloat_new(ctx, PyFloat_AsDouble(ob)); } else if (PyString_Check(ob)) { PyString_AsStringAndSize(ob, &buf, &length); // FIXME: //db = dbbuffer_new(ctx, buf, length); db = dbstring_new(ctx, buf, length); } else if (PyUnicode_Check(ob)) { ob = PyUnicode_AsUTF8String(ob); if (ob) { PyString_AsStringAndSize(ob, &buf, &length); db = dbstring_new(ctx, buf, length); Py_DECREF(ob); } } else if (PyDateTime_Check(ob)) { memset(&tm, 0, sizeof(tm)); tm.tm_year = PyDateTime_GET_YEAR(ob); tm.tm_mon = PyDateTime_GET_MONTH(ob); tm.tm_mday = PyDateTime_GET_DAY(ob); tm.tm_hour = PyDateTime_DATE_GET_HOUR(ob); tm.tm_min = PyDateTime_DATE_GET_MINUTE(ob); tm.tm_sec = PyDateTime_DATE_GET_SECOND(ob); usec = PyDateTime_DATE_GET_MICROSECOND(ob); tm.tm_year -= 1900; db = dbtime_newtm(ctx, &tm, usec); #ifdef WANT_UUID_TYPE } else if (PyObject_TypeCheck(ob, uuid_class)) { ob = PyObject_CallMethod(ob, "get_bytes", NULL); PyString_AsStringAndSize(ob, &buf, &length); db = dbuuid_new(ctx, (uint8_t*)buf); #endif } else if (Py_TYPE(ob) == &PongoList_Type) { // Resolve proxy types back to their original dbtype PongoList *p = (PongoList*)ob; db = p->dbptr; } else if (Py_TYPE(ob) == &PongoDict_Type) { // Resolve proxy types back to their original dbtype PongoDict *p = (PongoDict*)ob; db = p->dbptr; } else if (Py_TYPE(ob) == &PongoCollection_Type) { // Resolve proxy types back to their original dbtype PongoCollection *p = (PongoCollection*)ob; db = p->dbptr; } else if (PyMapping_Check(ob)) { length = PyMapping_Length(ob); items = PyMapping_Items(ob); if (items) { // mapping object implements "items" db = dbobject_new(ctx); dbobject_update(ctx, db, length, _py_mapping_cb, items, NOSYNC); Py_XDECREF(items); } else { // mapping object implements iterator protocol // don't have to decref the iterator object cuz it self-decrefs // upon StopIteration PyErr_Clear(); items = PyObject_GetIter(ob); db = dbobject_new(ctx); dbobject_update(ctx, db, length, _py_itermapping_cb, items, NOSYNC); } } else if (PySequence_Check(ob)) { length = PySequence_Length(ob); db = dblist_new(ctx); dblist_extend(ctx, db, length, _py_sequence_cb, ob, NOSYNC); } else { // FIXME: Unknown object type PyErr_SetObject(PyExc_TypeError, (PyObject*)Py_TYPE(ob)); db = DBNULL; } return db; }
EXPORT UDate PyObject_AsUDate(PyObject *object) { if (PyFloat_CheckExact(object)) return (UDate) (PyFloat_AsDouble(object) * 1000.0); else { if (PyDateTime_CheckExact(object)) { PyObject *tzinfo = PyObject_GetAttrString(object, "tzinfo"); PyObject *utcoffset, *ordinal; if (tzinfo == Py_None) { PyObject *m = PyImport_ImportModule("icu"); PyObject *cls = PyObject_GetAttrString(m, "ICUtzinfo"); tzinfo = PyObject_CallMethodObjArgs(cls, getDefault_NAME, NULL); Py_DECREF(cls); Py_DECREF(m); utcoffset = PyObject_CallMethodObjArgs(tzinfo, utcoffset_NAME, object, NULL); Py_DECREF(tzinfo); } else { utcoffset = PyObject_CallMethodObjArgs(object, utcoffset_NAME, NULL); Py_DECREF(tzinfo); } ordinal = PyObject_CallMethodObjArgs(object, toordinal_NAME, NULL); if (utcoffset != NULL && PyDelta_CheckExact(utcoffset) && ordinal != NULL && PyInt_CheckExact(ordinal)) { #if PY_MAJOR_VERSION >= 3 double ordinalValue = PyLong_AsDouble(ordinal); #else long ordinalValue = PyInt_AsLong(ordinal); #endif double timestamp = (ordinalValue - 719163) * 86400.0 + PyDateTime_DATE_GET_HOUR(object) * 3600.0 + PyDateTime_DATE_GET_MINUTE(object) * 60.0 + (double) PyDateTime_DATE_GET_SECOND(object) + PyDateTime_DATE_GET_MICROSECOND(object) / 1e6 - #ifndef PYPY_VERSION (((PyDateTime_Delta *) utcoffset)->days * 86400.0 + (double) ((PyDateTime_Delta *) utcoffset)->seconds); #else (PyDateTime_DELTA_GET_DAYS( (PyDateTime_Delta *) utcoffset) * 86400.0 + (double) PyDateTime_DELTA_GET_SECONDS( (PyDateTime_Delta *) utcoffset)); #endif Py_DECREF(utcoffset); Py_DECREF(ordinal); return (UDate) (timestamp * 1000.0); } Py_XDECREF(utcoffset); Py_XDECREF(ordinal); } } PyErr_SetObject(PyExc_TypeError, object); throw ICUException(); }