Python gives you freedom to choose between low- and high-level interfaces when working with times and dates.
To leverage the full potential of Python's standard library, we will focus on the datetime module, which is also the foundation for date/time arithmetic.
Fifteen minutes from now would be queried as SELECT SYSDATE 15/1440 FROM dual.
Oracle natively displays dates as strings, as shown by the examples above.
The fastest way in Python to learn more about the attributes and methods an object has is to use the built-in dir() function.
Python's standard library is also heavily documented so at any point you can use the help() function to see a brief description of the object—in most cases sufficient for getting started right away.
Built-in time zone support and several dedicated modules make Python a real time machine.
The transition between Python and Oracle date/time datatypes is completely transparent to developers thanks to cx_Oracle's mapping mechanisms.
Milliseconds, minutes, hours and weeks are the rest of the attributes timedelta exposes.
dir(datetime.datetime) ['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__str__', '__sub__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year'] When only one component of datetime is needed (date or time), you can use date() or time() methods of the datetime.datetime object to return a or object, respectively.
Differences between dates don't need to be calculated by hand because the datetime module already supports such arithmetic with timedelta objects.
See series TOCStarting with the Python 2.4 release, cx_Oracle handles DATE and TIMESTAMP datatypes natively, mapping values of such columns to Python datetime objects from the datetime module.
This offers certain advantages as datetime objects support arithmetic operations in-place.
A set of Oracle functions (TO_DATE, TO_TIMESTAMP, TO_TIMESTAMP_TZ, TO_YMINTERVAL, TO_DSINTERVAL) is available to the developer for converting character values into datetimes.