[fpc-pascal] Why has StrToDate() failing under FPC 2.3.1 (64bit)

Michael Van Canneyt michael at freepascal.org
Tue Oct 20 12:18:57 CEST 2009



On Tue, 20 Oct 2009, Graeme Geldenhuys wrote:

> On 20/10/2009, Michael Van Canneyt <michael at freepascal.org> wrote:
>>
>>  Strip the time part.
>>
>>  You are feeding a date/time pair to something that expects ONLY a date.
>
>
> Surely it must be able to handle that? Delphi does. Extract what it
> needs and leave/ignore the rest?

>From the Delphi help:
-----------------------------------------------------------------
Call StrToDate to parse a string that specifies a date. If S does
not contain a valid date, StrToDate raises an EConvertError exception.

S must consist of two or three numbers, separated by the character defined
by the DateSeparator global variable or its TFormatSettings equivalent. The
order for month, day, and year is determined by the ShortDateFormat global
variable or its TFormatSettings equivalent--possible combinations are m/d/y,
d/m/y, and y/m/d.

If S contains only two numbers, it is interpreted as a date (m/d or d/m) in
the current year.

Year values between 0 and 99 are converted using the
TwoDigitYearCenturyWindow. This value is stored either in a global variable
(first form) or as a field in the FormatSettings parameter (second form) See
"Currency and Date-Time Formatting Variables" for more information.

The first form of StrToDate is not thread-safe, because it uses localization
information contained in global variables. The second form of StrToDate,
which is thread-safe, refers to localization information contained in the
FormatSettings parameter. Before calling the thread-safe form of StrToDate,
you must populate FormatSettings with localization information. To populate
FormatSettings with a set of default locale values, call
GetLocaleFormatSettings.
-------------------------------------------------------------------

First lines say it all. "dd/mm/yyy hh:nn" is not supposed to work.
If it does, it just means that Delphi's implementation is more forgiving.

There are countries (Belgium among them) which use space as a date separator
char, so don't try a 'strip all to the first space' :)

Michael.



More information about the fpc-pascal mailing list