Beispiel #1
0
//-----------------------------------------------------------------------------
// 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
{
    TIMESTAMP_STRUCT *sqlValue;

    sqlValue = &var->data[pos];
    sqlValue->fraction = 0;
    if (PyDateTime_Check(value)) {
        sqlValue->year = PyDateTime_GET_YEAR(value);
        sqlValue->month = PyDateTime_GET_MONTH(value);
        sqlValue->day = PyDateTime_GET_DAY(value);
        sqlValue->hour = PyDateTime_DATE_GET_HOUR(value);
        sqlValue->minute = PyDateTime_DATE_GET_MINUTE(value);
        sqlValue->second = PyDateTime_DATE_GET_SECOND(value);
    } else if (PyDate_Check(value)) {
        sqlValue->year = PyDateTime_GET_YEAR(value);
        sqlValue->month = PyDateTime_GET_MONTH(value);
        sqlValue->day = PyDateTime_GET_DAY(value);
        sqlValue->hour = 0;
        sqlValue->minute = 0;
        sqlValue->second = 0;
    } else {
        PyErr_SetString(PyExc_TypeError, "expecting date or datetime data");
        return -1;
    }

    return 0;
}
Beispiel #2
0
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;
}
Beispiel #3
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;
}
Beispiel #4
0
static int convertTo_QDate(PyObject *sipPy,void **sipCppPtrV,int *sipIsErr,PyObject *sipTransferObj)
{
    QDate **sipCppPtr = reinterpret_cast<QDate **>(sipCppPtrV);

#line 39 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\sip/QtCore/qdatetime.sip"
if (!PyDateTimeAPI)
    PyDateTime_IMPORT;

// Allow a Python date object whenever a QDate is expected.

if (sipIsErr == NULL)
    return (PyDate_Check(sipPy) ||
            sipCanConvertToType(sipPy, sipType_QDate, SIP_NO_CONVERTORS));

if (PyDate_Check(sipPy))
{
    *sipCppPtr = new QDate(PyDateTime_GET_YEAR(sipPy),
                           PyDateTime_GET_MONTH(sipPy),
                           PyDateTime_GET_DAY(sipPy));

    return sipGetState(sipTransferObj);
}

*sipCppPtr = reinterpret_cast<QDate *>(sipConvertToType(sipPy, sipType_QDate, sipTransferObj, SIP_NO_CONVERTORS, 0, sipIsErr));

return 0;
#line 1308 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\QtCore/sipQtCoreQDate.cpp"
}
	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;
	}
BrokenDateTime Python::PyToBrokenDateTime(PyObject *py_datetime) {
  return BrokenDateTime(
    PyDateTime_GET_YEAR(py_datetime),
    PyDateTime_GET_MONTH(py_datetime),
    PyDateTime_GET_DAY(py_datetime),
    PyDateTime_DATE_GET_HOUR(py_datetime),
    PyDateTime_DATE_GET_MINUTE(py_datetime),
    PyDateTime_DATE_GET_SECOND(py_datetime));
};
Beispiel #7
0
static bool GetDateInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInfo& info)
{
    info.Data.date.year  = (SQLSMALLINT) PyDateTime_GET_YEAR(param);
    info.Data.date.month = (SQLUSMALLINT)PyDateTime_GET_MONTH(param);
    info.Data.date.day   = (SQLUSMALLINT)PyDateTime_GET_DAY(param);

    info.ValueType         = SQL_C_TYPE_DATE;
    info.ParameterType     = SQL_TYPE_DATE;
    info.ColumnSize        = 10;
    info.ParameterValuePtr = &info.Data.date;
    info.StrLen_or_Ind     = sizeof(DATE_STRUCT);
    return true;
}
/**
  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;
}
Beispiel #10
0
static jsval to_javascript_object(JSContext *context, PyObject *value) {
    if (PyString_Check(value)) {
        JSString *obj = JS_NewStringCopyN(context, PyString_AsString(value), PyString_Size(value));
        return STRING_TO_JSVAL(obj);
    } else if (PyUnicode_Check(value)) {
        PyObject *encoded = PyUnicode_AsUTF8String(value);
        JSString *obj = JS_NewStringCopyN(context, PyString_AsString(encoded), PyString_Size(encoded));
        Py_DECREF(encoded);
        return STRING_TO_JSVAL(obj);
    } else if (PyFloat_Check(value)) {
        return DOUBLE_TO_JSVAL(PyFloat_AsDouble(value));
    } else if (PyInt_Check(value)) {
        return INT_TO_JSVAL(PyInt_AsLong(value));
    } else if (PyLong_Check(value)) {
        return INT_TO_JSVAL(PyLong_AsLong(value));
    } else if (PyList_Check(value)) {
        JSObject *obj = JS_NewArrayObject(context, 0, NULL);
        int i;
        for (i = 0; i < PyList_Size(value); i++) {
            jsval item = to_javascript_object(context, PyList_GetItem(value, i));
            JS_SetElement(context, obj, i, &item);
        }
        return OBJECT_TO_JSVAL(obj);
    } else if (PyTuple_Check(value)) {
        JSObject *obj = JS_NewArrayObject(context, 0, NULL);
        int i;
        for (i = 0; i < PyTuple_Size(value); i++) {
            jsval item = to_javascript_object(context, PyTuple_GetItem(value, i));
            JS_SetElement(context, obj, i, &item);
        }
        return OBJECT_TO_JSVAL(obj);
    } else if (PyDict_Check(value)) {
        JSObject *obj = JS_NewObject(context, NULL, NULL, NULL);
        populate_javascript_object(context, obj, value);
        return OBJECT_TO_JSVAL(obj);
    } else if (PyDateTime_Check(value)) {
        JSObject *obj = JS_NewDateObject(context,
            PyDateTime_GET_YEAR(value),
            PyDateTime_GET_MONTH(value) - 1,
            PyDateTime_GET_DAY(value),
            PyDateTime_DATE_GET_HOUR(value),
            PyDateTime_DATE_GET_MINUTE(value),
            PyDateTime_DATE_GET_SECOND(value));
        return OBJECT_TO_JSVAL(obj);
    } else {
        return JSVAL_NULL;
    }
}
Beispiel #11
0
  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;
  }
/**
  Convert a Python datetime.date to MySQL DATE.

  Convert the PyObject obj, which must be a datetime.date,
  to MySQL DATE value.

  Raises TypeError when obj is not a PyDate_Type.

  @param    date    the PyObject to be converted

  @return   Converted date object.
    @retval PyBytes     Python v3
    @retval PyString    Python v2
    @retval NULL        Exception
*/
PyObject*
pytomy_date(PyObject *obj)
{
    PyDateTime_IMPORT;

    if (!obj || !PyDate_Check(obj))
    {
        PyErr_SetString(PyExc_TypeError, "Object must be a datetime.date");
        return NULL;
    }

    return PyBytesFromFormat("%04d-%02d-%02d",
                             PyDateTime_GET_YEAR(obj),
                             PyDateTime_GET_MONTH(obj),
                             PyDateTime_GET_DAY(obj));
}
Beispiel #13
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;
}
Beispiel #14
0
//-----------------------------------------------------------------------------
// DateVar_SetValue()
//   Set the value of the variable.
//-----------------------------------------------------------------------------
static int DateVar_SetValue(
    udt_DateVar *var,                   // variable to set value for
    unsigned pos,                       // array position to set
    PyObject *value)                    // value to set
{
    DATE_STRUCT *sqlValue;

    sqlValue = &var->data[pos];
    if (PyDateTime_Check(value) || PyDate_Check(value)) {
        sqlValue->year = PyDateTime_GET_YEAR(value);
        sqlValue->month = PyDateTime_GET_MONTH(value);
        sqlValue->day = PyDateTime_GET_DAY(value);
    } else {
        PyErr_SetString(PyExc_TypeError, "expecting date or datetime data");
        return -1;
    }

    return 0;
}
Beispiel #15
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;
}
Beispiel #16
0
//-----------------------------------------------------------------------------
// 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;
}
Beispiel #17
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;
}
static PyObject *
CalcVector(PyRPosObject *self, PyObject *args, PyObject *kwds)
{
  PyObject *location=NULL,*list=NULL, *timetuple=NULL;
  double lat=0,lon=0,mag, azm;
  double clat, clon;
  char *radarcode;
  int stid;
  struct RadarNetwork *network;
  struct Radar *radar;
  struct RadarSite *site;
  static char *kwlist[] = {"mag","azm","location","radarcode","radarid",NULL};
  char *envstr=NULL;
  FILE *fp;
  int yr,mo,dy,hr,mt;
  double sc;
  yr=2008;
  mo=7;
  dy=12;
  hr=12;
  mt=0;
  sc=0; 

  radarcode="kod";
  stid=-1;

  if (! PyArg_ParseTupleAndKeywords(args, kwds, "dd|Osi", kwlist, 
                                      &mag,&azm,&location,&radarcode,&stid))
     return NULL; 

  if ( timetuple != NULL) {
    if (PyDateTime_Check(timetuple)) {	
      yr=PyDateTime_GET_YEAR(timetuple);   
      mo=PyDateTime_GET_MONTH(timetuple);  
      dy=PyDateTime_GET_DAY(timetuple);   
      hr=PyDateTime_DATE_GET_HOUR(timetuple);   
      mt=PyDateTime_DATE_GET_MINUTE(timetuple);   
      sc=PyDateTime_DATE_GET_SECOND(timetuple);   
    }
  } 

  if ( location != NULL) {
    if (PyTuple_Check(location) && PyTuple_Size(location)==2) {	
      lat=PyFloat_AS_DOUBLE(PyTuple_GetItem(location,0));
      lon=PyFloat_AS_DOUBLE(PyTuple_GetItem(location,1));
    }
  } else {
    envstr=getenv("SD_RADAR");
    if (envstr==NULL) {
      fprintf(stderr,"Environment variable 'SD_RADAR' must be defined.\n");
      exit(-1);
    }

    fp=fopen(envstr,"r");

    if (fp==NULL) {
      fprintf(stderr,"Could not locate radar information file.\n");
      exit(-1);
    }

    network=RadarLoad(fp);
    fclose(fp); 

    if (network==NULL) {
      fprintf(stderr,"Failed to read radar information.\n");
      exit(-1);
    }

    envstr=getenv("SD_HDWPATH");
    if (envstr==NULL) {
      fprintf(stderr,"Environment variable 'SD_HDWPATH' must be defined.\n");
      exit(-1);
    }

    RadarLoadHardware(envstr,network);

    if (stid==-1) {
      stid=RadarGetID(network,radarcode);
    } 
    radar=RadarGetRadar(network,stid);
    site=RadarYMDHMSGetSite(radar,yr,mo,dy,hr,mt,(int) sc);
    lat=site->geolat;
    lon=site->geolon;
  }
  RPosCalcVector(lat,lon,mag,azm,&clat,&clon);
  list = PyList_New(0);
  PyList_Append(list,Py_BuildValue("d",clat));
  PyList_Append(list,Py_BuildValue("d",clon));

  return list;  

}
static PyObject *
PosMag(PyRPosObject *self, PyObject *args, PyObject *kwds)
{
  PyObject *list=NULL, *timetuple=NULL;
  char *radarcode;
  struct RadarNetwork *network;
  struct Radar *radar;
  struct RadarSite *site;
  int center, bcrd, rcrd;
  int frang,rsep,rxrise,height;
  int yr,mo,dy,hr,mt;
  double sc;
  int stid;
  double rho,lat,lng;
  char *envstr=NULL;
  FILE *fp;
  int r,c;
  static char *kwlist[] = {"center","bcrd","rcrd","frang","rsep","rxrise","height", "radarcode","radarid","time",NULL};
  PyDateTime_IMPORT;
  center=0;
  bcrd=1;
  rcrd=15;
  frang=1200;
  rxrise=0;
  rsep=100;
  height=300;
  radarcode="kod";
  stid=-1;
  yr=2008;
  mo=7;
  dy=12;
  hr=12;
  mt=0;
  sc=0; 

  if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iiiiiiisiO", kwlist, 
                                      &center,&bcrd,&rcrd,&frang,&rsep,&rxrise,&height,&radarcode,&stid,&timetuple))
     return NULL; 

  if ( timetuple != NULL) {
    if (PyDateTime_Check(timetuple)) {	
      yr=PyDateTime_GET_YEAR(timetuple);   
      mo=PyDateTime_GET_MONTH(timetuple);  
      dy=PyDateTime_GET_DAY(timetuple);   
      hr=PyDateTime_DATE_GET_HOUR(timetuple);   
      mt=PyDateTime_DATE_GET_MINUTE(timetuple);   
      sc=PyDateTime_DATE_GET_SECOND(timetuple);   
    }
  }
  envstr=getenv("SD_RADAR");
  if (envstr==NULL) {
    fprintf(stderr,"Environment variable 'SD_RADAR' must be defined.\n");
    exit(-1);
  }

  fp=fopen(envstr,"r");

  if (fp==NULL) {
    fprintf(stderr,"Could not locate radar information file.\n");
    exit(-1);
  }

  network=RadarLoad(fp);
  fclose(fp); 

  if (network==NULL) {
    fprintf(stderr,"Failed to read radar information.\n");
    exit(-1);
  }

  envstr=getenv("SD_HDWPATH");
  if (envstr==NULL) {
    fprintf(stderr,"Environment variable 'SD_HDWPATH' must be defined.\n");
    exit(-1);
  }

  RadarLoadHardware(envstr,network);


  if (stid==-1) {
    stid=RadarGetID(network,radarcode);
  }
  radar=RadarGetRadar(network,stid);
  site=RadarYMDHMSGetSite(radar,yr,mo,dy,hr,mt,(int) sc);
  RPosMag(center,bcrd,rcrd,site,frang,rsep,rxrise,height,&rho,&lat,&lng);
  radar=NULL;
  site=NULL;
  for (r=0;r<network->rnum;r++) {
    for (c=0;c<network->radar[r].cnum;c++) { 
      if (network->radar[r].code[c] !=NULL) free(network->radar[r].code[c]);
    } 
    if (network->radar[r].code !=NULL) free(network->radar[r].code);
    if (network->radar[r].name !=NULL) free(network->radar[r].name);
    if (network->radar[r].operator !=NULL) free(network->radar[r].operator);
    if (network->radar[r].hdwfname !=NULL) free(network->radar[r].hdwfname);
    if (network->radar[r].site !=NULL) free(network->radar[r].site);
  }
  free(network->radar);
  free(network);
  list = PyList_New(0);
  PyList_Append(list,Py_BuildValue("d",rho));
  PyList_Append(list,Py_BuildValue("d",lat));
  PyList_Append(list,Py_BuildValue("d",lng));
  return list;  
}
Beispiel #20
0
int AppendEscapedArg (Connection *self, char *start, char *end, PyObject *obj)
{
  int ret;
  PyObject *strobj;

  /*
  FIXME: Surround strings with '' could be performed in this function to avoid extra logic in AppendAndEscapeString */
  PRINTMARK();

  if (PyString_Check(obj))
  {
    PRINTMARK();
    return AppendAndEscapeString(start, end, PyString_AS_STRING(obj), PyString_AS_STRING(obj) + PyString_GET_SIZE(obj), TRUE);
  }
  else
    if (PyUnicode_Check(obj))
    {
      PRINTMARK();
      strobj = self->PFN_PyUnicode_Encode(PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), NULL);

      if (strobj == NULL)
      {
        if (PyErr_Occurred())
        {
          return -1;
        }

        PyErr_SetObject (PyExc_ValueError, obj);
        return -1;
      }


      ret = AppendAndEscapeString(start, end, PyString_AS_STRING(strobj), PyString_AS_STRING(strobj) + PyString_GET_SIZE(strobj), TRUE);
      Py_DECREF(strobj);

      return ret;
    }
    else
      if (obj == Py_None)
      {
        (*start++) = 'n';
        (*start++) = 'u';
        (*start++) = 'l';
        (*start++) = 'l';
        return  4;
      }
      else
        if (PyDateTime_Check(obj))
        {
          int len = sprintf (start, "'%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 len;
        }
        else
          if (PyDate_Check(obj))
          {
            int len = sprintf (start, "'%04d:%02d:%02d'", 
              PyDateTime_GET_YEAR(obj),
              PyDateTime_GET_MONTH(obj),
              PyDateTime_GET_DAY(obj));

            return len;
          }

          //FIXME: Might possible to avoid this?
          PRINTMARK();
          strobj = PyObject_Str(obj);
          ret = AppendAndEscapeString(start, end, PyString_AS_STRING(strobj), PyString_AS_STRING(strobj) + PyString_GET_SIZE(strobj), FALSE);
          Py_DECREF(strobj);
          return ret;
}