472,139 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

Convert time format to mysql datetime format

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
5 20674
bartonc
6,596 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
2,851 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
6,596 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
6,596 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
rouven
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.

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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.