language-extensions/python/src/PythonDataSet.cpp
//-------------------------------------------------------------------------------------------------
// Name: PythonDataSet::ExtractTimestampFromPyObject
//
// Description:
//  Extract all the time stamp data from a PyObject and return a TIMESTAMP_STRUCT.
//  Because TIMESTAMP is the most general, we return TIMESTAMP even for Date objects,
//  with default values.
//

:ExtractTimestampFromPyObject
Description:
Extract all the time stamp data from a PyObject and return a TIMESTAMP_STRUCT.
Because TIMESTAMP is the most general, we return TIMESTAMP even for Date objects,
with default values.
: ExtractTimestampFromPyObject Description : Extract all the time stamp data from a PyObject and return a TIMESTAMP_STRUCT . Because TIMESTAMP is the most general we return TIMESTAMP even for Date objects with default values .
SQL_TIMESTAMP_STRUCT PythonDataSet::ExtractTimestampFromPyObject(const PyObject *dateObject)
{
	PyDateTime_IMPORT;
	if (!PyDate_Check(dateObject) &&
		!PyDateTime_Check(dateObject))
	{
		throw runtime_error("Failed to extract timestamp from python object");
	}
	SQLSMALLINT year = 1970;
	SQLUSMALLINT month = 1;
	SQLUSMALLINT day = 1;
	SQLUSMALLINT hour = 0;
	SQLUSMALLINT minute = 0;
	SQLUSMALLINT second = 0;
	SQLUINTEGER usec = 0;
	if (PyDate_Check(dateObject))
	{
		year = PyDateTime_GET_YEAR(dateObject);
		month = PyDateTime_GET_MONTH(dateObject);
		day = PyDateTime_GET_DAY(dateObject);
	}
	if (PyDateTime_Check(dateObject))
	{
		hour = PyDateTime_DATE_GET_HOUR(dateObject);
		minute = PyDateTime_DATE_GET_MINUTE(dateObject);
		second = PyDateTime_DATE_GET_SECOND(dateObject);
		usec = PyDateTime_DATE_GET_MICROSECOND(dateObject);
	}
	return { year, month, day, hour, minute, second, usec * 1000 };
}
SQL_TIMESTAMP_STRUCT PythonDataSet :: ExtractTimestampFromPyObject ( const PyObject * dateObject ) { PyDateTime_IMPORT ; if ( ! PyDate_Check ( dateObject ) && ! PyDateTime_Check ( dateObject ) ) { throw runtime_error ( " " ) ; } SQLSMALLINT year = 1970 ; SQLUSMALLINT month = 1 ; SQLUSMALLINT day = 1 ; SQLUSMALLINT hour = 0 ; SQLUSMALLINT minute = 0 ; SQLUSMALLINT second = 0 ; SQLUINTEGER usec = 0 ; if ( PyDate_Check ( dateObject ) ) { year = PyDateTime_GET_YEAR ( dateObject ) ; month = PyDateTime_GET_MONTH ( dateObject ) ; day = PyDateTime_GET_DAY ( dateObject ) ; } if ( PyDateTime_Check ( dateObject ) ) { hour = PyDateTime_DATE_GET_HOUR ( dateObject ) ; minute = PyDateTime_DATE_GET_MINUTE ( dateObject ) ; second = PyDateTime_DATE_GET_SECOND ( dateObject ) ; usec = PyDateTime_DATE_GET_MICROSECOND ( dateObject ) ; } return { year , month , day , hour , minute , second , usec * 1000 } ; }
Name: PythonDataSet::ExtractTimestampFromPyObject
Description:
Extract all the time stamp data from a PyObject and return a TIMESTAMP_STRUCT.
Name : PythonDataSet :: ExtractTimestampFromPyObject Description : Extract all the time stamp data from a PyObject and return a TIMESTAMP_STRUCT .
// Import the PyDateTime API
 //
 // Make sure this PyObject is in the PyDateTime family so that we can extract data
 //
 // Set default values (in case of DATE object) to the python epoch time
 //
 // If this is a PyDate, we only need year/month/day values.
 // PyDateTime is also a PyDate so will return true on this check.
 //
 // If dateObject is a Datetime (not Date) then we need the time values as well.
 //
 // TIMESTAMP_STRUCT stores "fraction" as nanoseconds, so multiply microseconds by 1000
 //
