datetime.datetime and mysql different after python2.3

Discussion in 'Python' started by Tobiah, Jun 1, 2011.

  1. Tobiah

    Tobiah Guest

    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
    Tobiah, Jun 1, 2011
    #1
    1. Advertising

  2. Tobiah

    Tobiah Guest


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


    News reader stripped newlines
    Tobiah, Jun 1, 2011
    #2
    1. Advertising

  3. 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
    Thomas Rachel, Jun 3, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xavier
    Replies:
    0
    Views:
    405
    Xavier
    Aug 6, 2003
  2. Ksenia Marasanova
    Replies:
    2
    Views:
    345
    Mike Meyer
    Nov 17, 2005
  3. Xiao Jianfeng
    Replies:
    1
    Views:
    365
    Mike Meyer
    Nov 18, 2005
  4. Joshua Kordani
    Replies:
    0
    Views:
    291
    Joshua Kordani
    Jan 7, 2010
  5. Steve Holden
    Replies:
    0
    Views:
    674
    Steve Holden
    Jan 7, 2010
Loading...

Share This Page