By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

Convert time format to mysql datetime format

P: 2
Hi, i am trying to convert that time format '05:26:40 Jun 19, 2007 PDT' into mysql compatible format like YYYY-MM-DD HH:MM:SS.

the code i tried was:
Expand|Select|Wrap|Line Numbers
  1. from datetime import datetime
  2. from time import strptime
  3. f1.write("strptime(%r); locale: %r" % (olddate, locale.getlocale()))
  4. newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")
  5. mysqldate = time.strftime("%Y-%m-%d %H:%M:%S", newdate)
  6.  
but i get the following error message:

newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")
File "_strptime.py", line 293, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data=01:28:13 Jun 19, 2007 PDT fmt=%H:%M:%S %b %d, %Y %Z


anyone with ideas?

Regards,

Rouven
Jun 19 '07 #1
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
Hi, i am trying to convert that time format '05:26:40 Jun 19, 2007 PDT' into mysql compatible format like YYYY-MM-DD HH:MM:SS.

the code i tried was:
Expand|Select|Wrap|Line Numbers
  1. from datetime import datetime
  2. from time import strptime
  3. f1.write("strptime(%r); locale: %r" % (olddate, locale.getlocale()))
  4. newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")
  5. mysqldate = time.strftime("%Y-%m-%d %H:%M:%S", newdate)
  6.  
but i get the following error message:

newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")
File "_strptime.py", line 293, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data=01:28:13 Jun 19, 2007 PDT fmt=%H:%M:%S %b %d, %Y %Z


anyone with ideas?

Regards,

Rouven
That will depend on how you get olddate. I'll try
Expand|Select|Wrap|Line Numbers
  1. olddate = '05:26:40 Jun 19, 2007 PDT'
Jun 19 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Hi, i am trying to convert that time format '05:26:40 Jun 19, 2007 PDT' into mysql compatible format like YYYY-MM-DD HH:MM:SS.

the code i tried was:
Expand|Select|Wrap|Line Numbers
  1. from datetime import datetime
  2. from time import strptime
  3. f1.write("strptime(%r); locale: %r" % (olddate, locale.getlocale()))
  4. newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")
  5. mysqldate = time.strftime("%Y-%m-%d %H:%M:%S", newdate)
  6.  
but i get the following error message:

newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")
File "_strptime.py", line 293, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data=01:28:13 Jun 19, 2007 PDT fmt=%H:%M:%S %b %d, %Y %Z


anyone with ideas?

Regards,

Rouven
The time zone information is dependent on the value stored in time.tzname. On my machine it is 'Central Daylight Time'. Try 'Pacific Daylight Time' on your machine. This is from Python 2.3 docs:

The use of %Z is now deprecated, but the %z escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries.
Jun 19 '07 #3

bartonc
Expert 5K+
P: 6,596
The time zone information is dependent on the value stored in time.tzname. On my machine it is 'Central Daylight Time'. Try 'Pacific Daylight Time' on your machine. This is from Python 2.3 docs:

The use of %Z is now deprecated, but the %z escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries.
Great info, BV. I had discovered that it was the zone which was not being parsed.
So I stripped it off:
Expand|Select|Wrap|Line Numbers
  1. from time import strptime, strftime
  2.  
  3. olddate = '05:26:40 Jun 19, 2007 PST'
  4. newdate = strptime(" ".join(olddate.split()[:-1]), "%H:%M:%S %b %d, %Y")
  5. print newdate
  6. mysqldate = strftime("%Y-%m-%d %H:%M:%S", newdate)
  7. print mysqldate
  8.  
Jun 19 '07 #4

bartonc
Expert 5K+
P: 6,596
The time zone information is dependent on the value stored in time.tzname. On my machine it is 'Central Daylight Time'. Try 'Pacific Daylight Time' on your machine. This is from Python 2.3 docs:

The use of %Z is now deprecated, but the %z escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries.
I actually get a tuple:
Expand|Select|Wrap|Line Numbers
  1. from time import tzname
  2.  
  3. print tzname
('Pacific Standard Time', 'Pacific Daylight Time')

Oh, joy: more stuff to learn!
Jun 19 '07 #5

P: 2
thank you for your help, now it works but i have to convert the timezone before writing it to the database.
Jun 20 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.