0

Have spent a few hours trying to figure this out unsuccessfully.

I've imported some 'date' (not datetime) fields from MySQL into a python list. If I print the list, the values will show up as:

my_list = [[100, datetime.date(2013, 3, 11)], [101, datetime.date(2013, 4, 13,)],[102,datetime.date(2013, 4, 13)]

(...where 101, 102, 103 are my corresponding event_id's, you can disregard them)

I've found that the following will get the conversion correct:

unix_time = time.mktime(datetime.date(2009,2,17).timetuple())

However, if I try to do something like:

for x in my_list:
    x[1] = time.mktime(datetime.date(x[1]).timetuple())

It will not work.

2
  • Just an FYI, you can convert the date in mysql: select unix_timestamp(your_date). Commented Apr 24, 2013 at 1:58
  • you want to have some validations before sending to mysql.. there's a mysql drive in Python. but simple solution in python: see this for conversion: stackoverflow.com/questions/2460491/… Commented Apr 24, 2013 at 2:04

1 Answer 1

3

First solution was ugly, new edit:

for x in my_list:
  x[1] = time.mktime(x[1].timetuple())

should get you your list. No need to wrap the datetime.date in another datetime.date

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.