467,219 Members | 1,461 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,219 developers. It's quick & easy.

Convert time format to mysql datetime format

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
  • viewed: 20370
Share:
5 Replies
bartonc
Expert 4TB
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 2GB
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 4TB
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 4TB
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
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.

Similar topics

7 posts views Thread by Tony Clarke | last post: by
6 posts views Thread by Thomas Bartkus | last post: by
2 posts views Thread by bhv | last post: by
8 posts views Thread by Tony B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.