473,372 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 20808
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

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

Similar topics

7
by: Tony Clarke | last post by:
Hi, Is there an easy way of formatting a MySQL DATETIME field in PHP. At the moment I'm using the code below, as using the returned DATETIME value from the database with the date() function...
6
by: Thomas Bartkus | last post by:
MySQL Version 4.0.20 on a Linux server. How does one get the elapsed time between (2) DateTime values? I need the answer to the nearest minute. Is upgrading to Ver 5 with its more robust...
1
by: Google Jenny | last post by:
Precisely, here's what I need: When I run getdate(), I get, for example: August 9 2004 5:17 P.M. I want to turn the date portion into: 8/9/2004 format and update one column with it
1
by: khipra | last post by:
Hi! I'm try to convert a String to Datetime, but convert only works for some format: my value is: "20040719 16:00" This is the String that I want to convert to Datetime. I testing with some...
2
by: A Programmer | last post by:
I can't believe I have never run into this before but, I have an object that has a property of type DateTime. A collection of these objects are used to populate my DataGrid. In DateTime format they...
2
by: bhv | last post by:
hello everybody, i have datetime format in mysql database table. now, in mysql date is YYYY-MM-DD format. how can i insert date in this format with VB Script ? i had tried following dim dt, dy,...
3
by: huohaodian | last post by:
Hi, How can I convert a value created from DateTime.Now() to the datetime format that SQL Server recognises? Thanks in advance.
8
by: Tony B | last post by:
I have a string in a existing php script which is in the form "dd/mm/yyyy" and I need to convert it into a suitable format for mysql which is "yyyy-mm-dd" Is there a neat way of doing this in php ?
4
by: aext | last post by:
Hi please If you know how to convert from Decimal format of DateTime to a real Datetime Format reply please because i'm reading a decimal value of time from a file like this one (2448220711) and I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.