COleDateTime's ParseDateTime and locales....

by Michael S. Kaplan, published on 2005/12/26 15:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2005/12/26/507402.aspx


Some time ago, Mike asked:

I need to convert some strings to dates and then use the dates for some calculations. I was looking at COleDateTime::ParseDateTime() and it had the statement - "Note that the locale ID will also affect whether the string format is acceptable for conversion to a date/time value." Can you give me some direction on what I need to do? Is there an arcticle(s) I can read to get a handle on this?

A very interesting question.

The COleDateTime::ParseDateTime() method is an interesting one. The remarks in the documentation give a hint as to what is going on:

The lpszDate parameter can take a variety of formats. For example, the following strings contain acceptable date/time formats:

"25 January 1996"
"8:30:00"
"20:30:00"
"January 25, 1996 8:30:00"
"8:30:00 Jan. 25, 1996"
"1/25/1996 8:30:00"  // always specify the full year,
                     // even in a 'short date' format

Note that the locale ID will also affect whether the string format is acceptable for conversion to a date/time value.

It may remind some of the 'Evil date parsing', Parse, and ParseExact post from last year. And with good reason -- it is indeed the evil date parsing logic in COM that is largely responsible for the uncertainty here.

When you attempt to parse a date/time value, the method assumes that the data is valid and will do its best to convert it (using the LCID parameter as a 'hint'), even if the conversion is inappropriate. The method would probably be more stable and generally useful if the LCID were used for more than just a hint about issues like the order of date/month/year versus month/date/year, but at this point the behavior cannot be changed.

So the LCID is just a hint and the hope is that you are passing a date to parse that falls within what is reasonable for that locale....

 

This post brought to you by "" (U+0da4, a.k.a. SINHALA LETTER TAALUJA NAASIKYAYA)


# PatriotB on 26 Dec 2005 6:41 PM:

Awww. A blog entry with OLE in the title (somewhat). Brings a smile to my face :)

# Michael S. Kaplan on 27 Dec 2005 12:32 AM:

Yeah, it does kind of take you back....

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day