[egenix-users] mxDateTime for Date Only fields

M.-A. Lemburg mal at egenix.com
Thu Dec 15 18:40:32 CET 2016


Hi Jan,

mxDateTime doesn't have a notion of a date only data
type, which is why you get a DateTime instance for
date fields in the database, with the time part set to
midnight.

If you just want to get the date part of a DateTime
instance, you can use dt.date. Likewise for the time
part: dt.time ...

>>> from mx.DateTime import *
>>> dt = Date(2016,12,15)
>>> dt
<mx.DateTime.DateTime object for '2016-12-15 00:00:00.00' at 7f98d5991858>
>>> dt.date
'2016-12-15'
>>> dt.time
'00:00:00.00'
>>>

For more flexibility, use the .strftime() method, or convert
the DateTime instance into a datetime module date object:

>>> dt.pydate()
datetime.date(2016, 12, 15)

and then work on this.

You can also tell mxODBC to send you back datetime
module objects or even strings, by setting the
cursor or connection attribute

.datetimeformat

to PYDATETIME_DATETIMEFORMAT or STRING_DATETIMEFORMAT.

See http://www.egenix.com/products/python/mxODBC/doc/#_Toc427693210
for details.

The setting on the connection object is inherited by all
newly created cursors on this connection. The cursor attribute
only refers to that one cursor.

mxODBC is also customizable for decimal and string formats
using similar attributes.

Cheers,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Dec 15 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



On 15.12.2016 14:55, Jan Murre wrote:
> Hi,
> 
> I am getting back data from an MS SQL database with Date columns.
> For serialization I have a generic function to convert date, datetime and
> mx.DateTime.DateTime objects to strings.
> 
> When I do a python str() on an mx.DateTime.DateTime object, I always get a
> string with date and time. Although, in cursor.description I can see that
> the datatype of the column is 91 (which is Manager.TYPE_DATE). So, the
> information that this is a date-only columns is available, but gets lost in
> the conversion to mx.DateTime.DateTime.
> 
> What I do now is a check on the hour, minute, second attributes being 0 to
> decide if this originally was a date-only column.
> 
> Is a more elegant approach possible?
> 
> Regards, Jan
> 
> 
> 
> 
> _______________________________________________________________________
> eGenix.com User Mailing List                     http://www.egenix.com/
> https://www.egenix.com/mailman/listinfo/egenix-users
> 



More information about the egenix-users mailing list