FAQ
I'm grabbing two fields from a MySQLdb connection.
One is a date type, and one is a time type.

So I put the values in two variables and print them:

import datetime
date, time = get_fields() # for example
print str(type(date)), str((type(time)))
print str(date + time)

In python 2.3.4, I get:

<type 'DateTime'> <type 'DateTimeDelta'>
2010-07-06 09:20:45.00

Put in python2.4 and greater, I get this:

<type 'datetime.date'> <type 'datetime.timedelta'>
2010-07-06

So I'm having trouble adding the two to get one
datetime.

Thanks for any insight.

Tobiah

Search Discussions

  • Tobiah at Jun 1, 2011 at 6:47 pm

    import datetime
    date, time = get_fields() # for example
    print str(type(date)), str((type(time)))
    print str(date + time)
    News reader stripped newlines
  • Tim Roberts at Jun 2, 2011 at 3:41 am

    Tobiah wrote:
    I'm grabbing two fields from a MySQLdb connection.
    One is a date type, and one is a time type.

    So I put the values in two variables and print them:
    ...
    In python 2.3.4, I get:

    <type 'DateTime'> <type 'DateTimeDelta'>
    2010-07-06 09:20:45.00

    Put in python2.4 and greater, I get this:

    <type 'datetime.date'> <type 'datetime.timedelta'>
    2010-07-06

    So I'm having trouble adding the two to get one
    datetime.
    Many of the database layers switched to the built-in "datetime" module when
    it was introduced in 2.4. Prior to that, they had to use custom classes.

    date, time = get_fields()
    date = datetime.datetime.fromordinal( date.toordinal() )
    print str(date+time)
    --
    Tim Roberts, timr at probo.com
    Providenza & Boekelheide, Inc.
  • Thomas Rachel at Jun 3, 2011 at 9:05 am

    Am 01.06.2011 20:42 schrieb Tobiah:
    I'm grabbing two fields from a MySQLdb connection.
    One is a date type, and one is a time type.

    So I put the values in two variables and print them:

    import datetime
    date, time = get_fields() # for example
    print str(type(date)), str((type(time)))
    print str(date + time)

    In python 2.3.4, I get:

    <type 'DateTime'> <type 'DateTimeDelta'>
    2010-07-06 09:20:45.00

    Put in python2.4 and greater, I get this:

    <type 'datetime.date'> <type 'datetime.timedelta'>
    2010-07-06

    So I'm having trouble adding the two to get one
    datetime.
    Here you can do the following:

    import datetime
    date, time = get_fields() # for example
    print str(type(date)), str((type(time)))
    dt = datetime.datetime(*date.timetuple()) + time
    print dt

    (BTW: print calls str() in an case, so it is not needed to put it
    explicitly here...)


    Thomas

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 1, '11 at 6:42p
activeJun 3, '11 at 9:05a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase