[fpc-pascal] date formating dificulty

Philippe philippe at quarta.com.br
Thu Nov 20 01:14:29 CET 2014


 

Em 19.11.2014 19:31, waldo kitty escreveu: 

> On 11/19/2014 9:17
AM, Philippe wrote:
> 
>>> I still don't know how to use at the same
time formatStr and FormatSettings in FormatDateTime.
> 
> i'm not sure i
understand your delima... perhaps you are trying to determine how 
> the
FormatSettings work and how you can force the same output format on 
>
different OSes? i found the following (slightly modified by me for CLI)
delphi 
> code that might help... the original code resides at 
>
http://www.delphibasics.co.uk/RTL.asp?Name=FormatDateTime [1]in the
code, we output the results of the various FormatDateTime usages... then

> we change the values of the date and time formatting variables and
run the same 
> outputs again...
> 
>>> I´ll like very much to find a
tutorial or some example about the good practice to manage dates and be
sure it will do the same thing on different machines ....
> 
> in some
cases, you can fall back to delphi tutorials... it can be troublesome in

> some cases where things are slightly different between FPC/Lazarus
and Delphi... 
> the biggest problem i personally run into is that so
much example code is GUI 
> oriented and i still code for CLI...
> 
>
anyway, here is the output of the following code...
> 
> *NOTE:* i think
we've found a bug in (fpc?) trunk or perhaps it is just an 
> unexpected
side effect due to the system's settings... the day and month are 
>
reversed in the first separators line... as the comments in the code
note, we're 
> trying to work with 5 Jun 49... but this output was done
on a US winwhatever 
> (vista) system... it might be getting in the way
by forcing mm/dd/yy format from 
> the dd/mm/yy work string... someone
else will have to speak to that as i would 
> expect it to do the same
in the second output set but it doesn't...
> 
> System formatting:
>
Working with date string = 05/06/49 01:02:03.004
> Separators dd/mm/yy
hh:nn:ss = 06/05/49 01:02:03
> ShortMonths mmm = May
> LongMonths mmmm =
May
> ShortDays ddd = Thu
> LongDays dddd = Thursday
> ShortDate ddddd =
5/6/2049
> LongDate dddddd = Thursday, May 06, 2049
> AMPM hhampm =
01AM
> ShortTime t = 1:02
> LongTime tt = 1:02:03
> 2DigitCentury
dd/mm/yyyy = 06/05/2049
> 
> Modified formatting:
> Working with date
string = 05-06-49 01_02_03.004
> Separators dd/mm/yy hh:nn:ss = 05-06-49
01_02_03
> ShortMonths mmm = JUN
> LongMonths mmmm = JUNE
> ShortDays
ddd = SUN
> LongDays dddd = SUNDAY
> ShortDate ddddd = 05-JUN-49
>
LongDate dddddd = SUNDAY 05 of JUNE of 1949
> AMPM hhampm = 01morning
>
ShortTime t = 01_02_03
> LongTime tt = 01 _ 02 _ 03 . 004
>
2DigitCentury dd/mm/yyyy = 05-06-1949
> 
> ===== snip =====
> program
DTFormatting;
> 
> uses SysUtils;
> 
> var
> DateStr : String;
> myDate
: TDateTime;
> 
> begin
> // Set up our TDateTime variable with a full
date and time :
> // 5th of June 2049 at 01:02:03.004 (.004
milli-seconds)
> //
> // Note that 49 is treated as 2049 as follows :
>
// TwoDigitYearCenturyWindow => 50
> // Current year => 2008 (at time of
writing)
> // Subtract TwoDigitYearCenturyWindow => 1958
> // 2 digit
year to be converted => 49
> // Compare with the last 2 digits of 1958
=> Less
> // So the year is in the next century => 2049
> // (58 would
be converted to 1958)
> 
> WriteLn('System formatting:');
> 
> DateStr
:= '05/06/49 01:02:03.004';
> WriteLn(' Working with date string =
'+DateStr);
> 
> myDate := StrToDateTime(DateStr);
> 
> // Demonstrate
default locale settings
> 
> // Use the DateSeparator and TimeSeparator
values
> WriteLn(' Separators dd/mm/yy hh:nn:ss =
'+FormatDateTime('dd/mm/yy 
> hh:nn:ss', myDate));
> 
> // Use
ShortMonthNames
> WriteLn(' ShortMonths mmm = '+FormatDateTime('mmm',
myDate));
> 
> // Use LongMonthNames
> WriteLn(' LongMonths mmmm =
'+FormatDateTime('mmmm', myDate));
> 
> // Use ShortDayNames
> WriteLn('
ShortDays ddd = '+FormatDateTime('ddd', myDate));
> 
> // Use
LongDayNames
> WriteLn(' LongDays dddd = '+FormatDateTime('dddd',
myDate));
> 
> // Use the ShortDateFormat string
> WriteLn(' ShortDate
ddddd = '+FormatDateTime('ddddd', myDate));
> 
> // Use the
LongDateFormat string
> WriteLn(' LongDate dddddd =
'+FormatDateTime('dddddd', myDate));
> 
> // Use the TimeAmString
>
WriteLn(' AMPM hhampm = '+FormatDateTime('hhampm', myDate));
> 
> // Use
the ShortTimeFormat string
> WriteLn(' ShortTime t =
'+FormatDateTime('t', myDate));
> 
> // Use the LongTimeFormat string
>
WriteLn(' LongTime tt = '+FormatDateTime('tt', myDate));
> 
> // Use the
TwoDigitCenturyWindow
> WriteLn(' 2DigitCentury dd/mm/yyyy =
'+FormatDateTime('dd/mm/yyyy', 
> myDate));
> 
> WriteLn('');
>
WriteLn('Modified formatting:');
> 
> // Now change the defaults
>
DefaultFormatSettings.DateSeparator := '-';
>
DefaultFormatSettings.TimeSeparator := '_';
>
DefaultFormatSettings.ShortDateFormat := 'dd/mmm/yy';
>
DefaultFormatSettings.LongDateFormat := 'dddd dd "of" mmmm "of" yyyy';
>
DefaultFormatSettings.TimeAMString := 'morning';
>
DefaultFormatSettings.TimePMString := 'afternoon';
>
DefaultFormatSettings.ShortTimeFormat := 'hh:nn:ss';
>
DefaultFormatSettings.LongTimeFormat := 'hh : nn : ss . zzz';
>
DefaultFormatSettings.ShortMonthNames[6] := 'JUN';
>
DefaultFormatSettings.LongMonthNames[6] := 'JUNE';
>
DefaultFormatSettings.ShortDayNames[1] := 'SUN';
>
DefaultFormatSettings.LongDayNames[1] := 'SUNDAY';
>
DefaultFormatSettings.TwoDigitYearCenturyWindow := 75; // This means 49
is 
> treated as 1949
> 
> // Set up our TDateTime variable with the
same value as before
> // **except that we must use the new date and
time separators**
> // The TwoDigitYearCenturyWindow variable only takes
effect here
> DateStr := '05-06-49 01_02_03.004';
> WriteLn(' Working
with date string = '+DateStr);
> 
> myDate := StrToDateTime(DateStr);
>

> // Use the DateSeparator and TimeSeparator values
> WriteLn('
Separators dd/mm/yy hh:nn:ss = '+FormatDateTime('dd/mm/yy 
> hh:nn:ss',
myDate));
> 
> // Use ShortMonthNames
> WriteLn(' ShortMonths mmm =
'+FormatDateTime('mmm', myDate));
> 
> // Use LongMonthNames
> WriteLn('
LongMonths mmmm = '+FormatDateTime('mmmm', myDate));
> 
> // Use
ShortDayNames
> WriteLn(' ShortDays ddd = '+FormatDateTime('ddd',
myDate));
> 
> // Use LongDayNames
> WriteLn(' LongDays dddd =
'+FormatDateTime('dddd', myDate));
> 
> // Use the ShortDateFormat
string
> WriteLn(' ShortDate ddddd = '+FormatDateTime('ddddd',
myDate));
> 
> // Use the LongDateFormat string
> WriteLn(' LongDate
dddddd = '+FormatDateTime('dddddd', myDate));
> 
> // Use the
TimeAmString
> WriteLn(' AMPM hhampm = '+FormatDateTime('hhampm',
myDate));
> 
> // Use the ShortTimeFormat string
> WriteLn(' ShortTime t
= '+FormatDateTime('t', myDate));
> 
> // Use the LongTimeFormat
string
> WriteLn(' LongTime tt = '+FormatDateTime('tt', myDate));
> 
>
// Use the TwoDigitCenturyWindow
> WriteLn(' 2DigitCentury dd/mm/yyyy =
'+FormatDateTime('dd/mm/yyyy', 
> myDate));
> end.
> ===== snip
=====

Thanks to you, to Graeme and Michael! 

That almost what I was
looking for. And did not find. Quite simple indeed! 

More as a
curiosity: how should one use formatDateTime( formatStr, dt,
formatSettings); 

Philippe 

Links:
------
[1]
http://www.delphibasics.co.uk/RTL.asp?Name=FormatDateTime
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141119/2724b0e0/attachment.html>


More information about the fpc-pascal mailing list