Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Converting DD MM YYYY into YYYY-MM-DD?

Reply
Thread Tools

Converting DD MM YYYY into YYYY-MM-DD?

 
 
Gilles Ganault
Guest
Posts: n/a
 
      08-17-2009
Hello,

I need to convert DD MM YYYY dates into the MySQL-friendly
YYYY-MM-DD, and translate the month name from literal French to its
numeric equivalent (eg. "Janvier" into "01").

Here's an example:

SELECT dateinscription, dateconnexion FROM membres LIMIT 1;
26 Mai 2007|17 Aot 2009 - 09h20

I'd like to update the row into "2007-05-26" and "2009-08-17 09:20",
respectively.

What is the best way to do this in Python?

Thank you.
 
Reply With Quote
 
 
 
 
Jonathan Gardner
Guest
Posts: n/a
 
      08-17-2009
On Aug 17, 3:26*pm, Gilles Ganault <(E-Mail Removed)> wrote:
> Hello,
>
> * * * * I need to convert DD MM YYYY dates into the MySQL-friendly
> YYYY-MM-DD, and translate the month name from literal French to its
> numeric equivalent (eg. "Janvier" into "01").
>
> Here's an example:
>
> SELECT dateinscription, dateconnexion FROM membres LIMIT 1;
> 26 Mai 2007|17 Aot 2009 - 09h20
>
> I'd like to update the row into "2007-05-26" and "2009-08-17 09:20",
> respectively.
>
> What is the best way to do this in Python?
>
> Thank you.


 
Reply With Quote
 
 
 
 
Che M
Guest
Posts: n/a
 
      08-17-2009
On Aug 17, 6:26*pm, Gilles Ganault <(E-Mail Removed)> wrote:
> Hello,
>
> * * * * I need to convert DD MM YYYY dates into the MySQL-friendly
> YYYY-MM-DD, and translate the month name from literal French to its
> numeric equivalent (eg. "Janvier" into "01").
>
> Here's an example:
>
> SELECT dateinscription, dateconnexion FROM membres LIMIT 1;
> 26 Mai 2007|17 Aot 2009 - 09h20
>
> I'd like to update the row into "2007-05-26" and "2009-08-17 09:20",
> respectively.
>
> What is the best way to do this in Python?
>
> Thank you.


Likely this is not the best way, but I would do, for
the first one (and the same idea for the second):

def convert(date):
frenchdict = {'Mai':'May'} #etc...
day = mystring[:2]
month = frenchdict[ mystring[3:6] ]
year = mystring[7:11]
newdate = year+'-'+month+'-'+day
print 'newdate is ', newdate

 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      08-18-2009
On Aug 17, 3:26*pm, Gilles Ganault <(E-Mail Removed)> wrote:
> * * * * I need to convert DD MM YYYY dates into the MySQL-friendly
> YYYY-MM-DD, and translate the month name from literal French to its
> numeric equivalent (eg. "Janvier" into "01").
>
> Here's an example:
>
> SELECT dateinscription, dateconnexion FROM membres LIMIT 1;
> 26 Mai 2007|17 Aot 2009 - 09h20
>
> I'd like to update the row into "2007-05-26" and "2009-08-17 09:20",
> respectively.
>
> What is the best way to do this in Python?
>


Unfortunately, there isn't any string to date parsers in the built-
ins. Not to worry, though, since writing your own is easy, especially
if you use regular expressions from the re module. I suggest using an
RE such as:

r"(?P<date>\d+)\s+(?P<month>\w+)\s+(?P<year>\d+ )"

If you want to translate month names to month numbers, then you need
some sort of dict to do so. Unfortunately, there isn't a terrific
standard for this, so your best bet is to put it in some file
somewhere, or even hard-code it in your code. (Month names won't
change over the lifetime of your program, so it's reasonable to put
them in your code somewhere.)

month_names_to_numbers = {
'jan':1, ... }

Once you have the year, month, and date, formatting it is trivial with
the built-in formatter.

"%04d-%02d%02d %02d:%02d" % (year, month, date, hour, minute)

The variety of date formats out there have prevented a universal,
clean solution to this problem. Until we all start sticking to the
same conventions, we will always have to write code to translate dates
from one format to another.
 
Reply With Quote
 
Rami Chowdhury
Guest
Posts: n/a
 
      08-18-2009
Correct me if I'm wrong, but doesn't
http://docs.python.org/library/datet...etime.strptime do
this?

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'FR')

'French_France.1252'
>>> date_str = '05 Mai 2009 - 18h25'
>>> fmt = '%d %B %Y - %Hh%M'
>>> date_obj = datetime.strptime(date_str, fmt)
>>> date_obj

datetime.datetime(2009, 5, 5, 18, 25)
>>> date_obj.strftime('%Y-%m-%d %H:%M')

'2009-05-05 18:25'

If you're using a recent enough version of Python (2.5 and up) I'd imagine
that's the best way to do it?

On Mon, 17 Aug 2009 16:58:28 -0700, Che M <(E-Mail Removed)> wrote:

> On Aug 17, 6:26*pm, Gilles Ganault <(E-Mail Removed)> wrote:
>> Hello,
>>
>> * * * * I need to convert DD MM YYYY dates into the MySQL-friendly
>> YYYY-MM-DD, and translate the month name from literal French to its
>> numeric equivalent (eg. "Janvier" into "01").
>>
>> Here's an example:
>>
>> SELECT dateinscription, dateconnexion FROM membres LIMIT 1;
>> 26 Mai 2007|17 Août 2009 - 09h20
>>
>> I'd like to update the row into "2007-05-26" and "2009-08-17 09:20",
>> respectively.
>>
>> What is the best way to do this in Python?
>>
>> Thank you.

>
> Likely this is not the best way, but I would do, for
> the first one (and the same idea for the second):
>
> def convert(date):
> frenchdict = {'Mai':'May'} #etc...
> day = mystring[:2]
> month = frenchdict[ mystring[3:6] ]
> year = mystring[7:11]
> newdate = year+'-'+month+'-'+day
> print 'newdate is ', newdate
>




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      08-18-2009
On Aug 17, 5:20*pm, Ben Finney <(E-Mail Removed)> wrote:
>
> Instead, you should generate the map based on the standard library (in
> this case, the underlying C standard library) locale database
> <URL:http://docs.python.org/library/locale.html?highlight=locale%20date#lo...>:
>


Does Windows support POSIX locales?

 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      08-18-2009
On Aug 17, 7:06*pm, Ben Finney <(E-Mail Removed)> wrote:
> Jonathan Gardner <(E-Mail Removed)> writes:
> > Unfortunately, there isn't any string to date parsers in the built-
> > ins.

>
> Fortunately, Python 2.5 or later has the datetime.strptime function..
>


Hate to weasel out of this one, but the language that strptime
provides is pretty limited. I don't find it useful except in the
trivial cases. Same goes for strftime. Also, both of these are very
Western European centric. Yes, Asian languages are supported but not
naturally.
 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      08-18-2009
On Aug 17, 5:18*pm, "Rami Chowdhury" <(E-Mail Removed)> wrote:
>
> >>> import locale
> >>> locale.setlocale(locale.LC_ALL, 'FR')


locale is nice when you only have a single thread.

Webservers aren't single threaded. You can't serve up one page for one
locale and then another in another locale without seeing very, very
weird behavior.
 
Reply With Quote
 
Rami Chowdhury
Guest
Posts: n/a
 
      08-18-2009


----
My sample interactive session (locale.setlocale and all) was on a 32-bit Vista
install of Python 2.5, so it works on that...

---
Rami Chowdhury
"A man with a watch knows what time it is. A man with two watches is never
sure". -- Segal's Law
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

On Monday 17 August 2009 19:46:24 Ben Finney wrote:
> Jonathan Gardner <(E-Mail Removed)> writes:
> > On Aug 17, 5:20 pm, Ben Finney <(E-Mail Removed)> wrote:
> > > Instead, you should generate the map based on the standard library (in
> > > this case, the underlying C standard library) locale database
> > > <URL:http://docs.python.org/library/local...=locale%20date
> > >#lo...>:

> >
> > Does Windows support POSIX locales?

>
> If it does not, it should since it addresses the problem in one
> standard place. It would be foolish for Python to re-implement that
> functionality when presumably the operating system already knows how to
> map between dates and locale-specific text representations.
>
> You'll need to check the operating system documentation for what
> alternative it might provide.
>
> --
> \ “I got fired from my job the other day. They said my |
> `\ personality was weird. … That's okay, I have four more.” |
> _o__) —Bug-Eyed Earl, _Red Meat_ |
> Ben Finney


 
Reply With Quote
 
Gilles Ganault
Guest
Posts: n/a
 
      08-18-2009
Thanks everyone for the help. This script is just a one-shot thingie
on my work host, not as a web script or anything professional.

On Mon, 17 Aug 2009 17:05:28 -0700 (PDT), Jonathan Gardner
<(E-Mail Removed)> wrote:
>Unfortunately, there isn't any string to date parsers in the built-
>ins. Not to worry, though, since writing your own is easy, especially
>if you use regular expressions from the re module. I suggest using an
>RE such as:
>
> r"(?P<date>\d+)\s+(?P<month>\w+)\s+(?P<year>\d+ )"


I've never seen regexes like this. I'm curious to know what those
mean:

r = Unicode?

(?P<date> = ? means that it shouldn't be greedy, what about P<date>?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert MM/dd/yyyy format into dd/MM/yyyy Ashraf Ansari ASP .Net 4 08-30-2007 10:17 AM
Converting DD/MM/YYYY date format to UTC Matt Gyton Javascript 1 08-17-2006 08:44 PM
How to Display Message Date as dd/mm/yyyy? Elisa Francesca Roselli Firefox 1 04-15-2005 08:10 AM
Dates mm/dd/yyyy & dd/mm/yyyy giving a major headache J P Singh ASP General 4 02-10-2004 10:24 AM
How to show dates in yyyy-MM-dd format Kenneth ASP .Net 2 01-14-2004 09:58 PM



Advertisments