Re: [fpc-pascal] date formating dificulty
Em 18.11.2014 10:27, Michael Van Canneyt escreveu: > On Tue, 18 Nov 2014, Philippe wrote: > >> Em 18.11.2014 09:49, Philippe escreveu: I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/', now) returns 18-11-2014 and not 18/11/2014 ? strtodate( formatDateTime( 'DD/MM/', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits. I am confused with the following syntax function FormatDateTime( const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [1] I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date)); TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ... > > It should be, I will check. > > Anyway, as Graeme hinted: ShortDateFormat contains a format string, and / is a placeholder for the date > separator character on the computer on which the program is running. > > If you want to force a character, you can enclose it in quotes: > ShortDateFormat:='dd"/"mm"/"' > will always use / as the date separator, no matter what the internatonalization settings on the PC. > > But it is bad practice to do so. > > Secondly, it is a common mistake to think that StrToDate will always correctly perform the opposite of DateToStr. > It does not, but this is documented. > > Michael. > > I still don't know how to use at the same time formatStr and FormatSettings in FormatDateTime. > > 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 > > Thanks > > ___ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [2] Links: -- [1] http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [2] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] date formating dificulty
Em 19.11.2014 12:17, Philippe escreveu: > Em 18.11.2014 10:27, Michael Van Canneyt escreveu: > >> On Tue, 18 Nov 2014, Philippe wrote: >> >>> Em 18.11.2014 09:49, Philippe escreveu: I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/', now) returns 18-11-2014 and not 18/11/2014 ? strtodate( formatDateTime( 'DD/MM/', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits. I am confused with the following syntax function FormatDateTime( const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [1] I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date)); TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ... >> >> It should be, I will check. >> >> Anyway, as Graeme hinted: ShortDateFormat contains a format string, and / is a placeholder for the date >> separator character on the computer on which the program is running. >> >> If you want to force a character, you can enclose it in quotes: >> ShortDateFormat:='dd"/"mm"/"' >> will always use / as the date separator, no matter what the internatonalization settings on the PC. >> >> But it is bad practice to do so. >> >> Secondly, it is a common mistake to think that StrToDate will always correctly perform the opposite of DateToStr. >> It does not, but this is documented. >> >> Michael. >> >> I still don't know how to use at the same time formatStr and FormatSettings in FormatDateTime. >> >> 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 >> >> Thanks >> >> interesting stuff ... but ... out of date using deprecatings ... (from year 2000 !!!) ... does not help much!!! >> >> http://www.math.uni-leipzig.de/pool/tuts/FreePascal/units/node21.html#SECTION002141000 >> >> ___ >> fpc-pascal maillist - fpc-pascal@lists.freepascal.org >> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [2] > > ___ > fpc-pascal maillist - fpc-pascal@lists.freepascal.org > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [2] Links: -- [1] http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [2] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] date formating dificulty
On Wed, 19 Nov 2014, Philippe wrote: Em 19.11.2014 12:17, Philippe escreveu: Em 18.11.2014 10:27, Michael Van Canneyt escreveu: On Tue, 18 Nov 2014, Philippe wrote: Em 18.11.2014 09:49, Philippe escreveu: I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/', now) returns 18-11-2014 and not 18/11/2014 ? strtodate( formatDateTime( 'DD/MM/', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits. I am confused with the following syntax function FormatDateTime( const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date)); TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ... It should be, I will check. Anyway, as Graeme hinted: ShortDateFormat contains a format string, and / is a placeholder for the date separator character on the computer on which the program is running. If you want to force a character, you can enclose it in quotes: ShortDateFormat:='dd"/"mm"/"' will always use / as the date separator, no matter what the internatonalization settings on the PC. But it is bad practice to do so. Secondly, it is a common mistake to think that StrToDate will always correctly perform the opposite of DateToStr. It does not, but this is documented. Michael. I still don't know how to use at the same time formatStr and FormatSettings in FormatDateTime. 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 Good practice is to avoid converting to and from string the whole time. If you need to store a timestamp, use the ISO 8601 format or RFC 3339 (or is it 3999?). Regarding formatsettings: Since some versions, most date/time/money/float formatting routines support passing a TFormatSettings record to it to control the formatting. (this is mostly done for multi-threaded apps where different threads can have different settings, like in web-apps). If no TFormatSettings record is passed, the global FormatSettings record is used. The various format variables which existed prior to this change, have simply been moved to this global FormatSettings record, with some backwards compatibility code. So if you had old code that did something like: ShortDateStr:='dd/mm/'; you should now use FormatSettings.ShortDateStr:='dd/mm/'; That is all there is to it. Hardly worth a tutorial. But if you have suggestions for the documentation, I will be glad to add them. Michael.___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] date formating dificulty
On 2014-11-19 20:04, Michael Van Canneyt wrote: > If you need to store a timestamp, use the ISO 8601 format or RFC 3339 (or is > it 3999?). I can't agree more!! Snippet from tiOPF code { Summary of ISO 8601 http://www.cl.cam.ac.uk/~mgk25/iso-time.html } cIntlDateTimeStor = 'mmdd"T"hhnnss';// for storage cIntlDateTimeDisp = '-mm-dd hh:nn:ss'; // for display CIntlDateDisp = '-mm-dd'; // for display See tiIntlDateDispAsDateTime(), tiIntlDateStorAsDateTime() in tiOPF's tiUtils.pas unit. I even store TDateTime data as cIntlDateTimeStor formatted strings in databases. Never had an issue with date conversions since. Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] date formating dificulty
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 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 = May ShortDaysddd = Thu LongDays = Thursday ShortDate d = 5/6/2049 LongDate dd = Thursday, May 06, 2049 AMPM hhampm = 01AM ShortTime t = 1:02 LongTime tt = 1:02:03 2DigitCentury dd/mm/ = 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 = JUNE ShortDaysddd = SUN LongDays = SUNDAY ShortDate d = 05-JUN-49 LongDate dd = SUNDAY 05 of JUNE of 1949 AMPM hhampm = 01morning ShortTime t = 01_02_03 LongTime tt = 01 _ 02 _ 03 . 004 2DigitCentury dd/mm/ = 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 = '+FormatDateTime('', myDate)); // Use ShortDayNames WriteLn(' ShortDaysddd = '+FormatDateTime('ddd', myDate)); // Use LongDayNames WriteLn(' LongDays = '+FormatDateTime('', myDate)); // Use the ShortDateFormat string WriteLn(' ShortDate d = '+FormatDateTime('d', myDate)); // Use the LongDateFormat string WriteLn(' LongDate dd = '+FormatDateTime('dd', 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/ = '+FormatDateTime('dd/mm/', myDate)); WriteLn(''); WriteLn('Modified formatting:'); // Now chan
Re: [fpc-pascal] date formating dificulty
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 = May > ShortDays ddd = Thu > LongDays = Thursday > ShortDate d = 5/6/2049 > LongDate dd = Thursday, May 06, 2049 > AMPM hhampm = 01AM > ShortTime t = 1:02 > LongTime tt = 1:02:03 > 2DigitCentury dd/mm/ = 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 = JUNE > ShortDays ddd = SUN > LongDays = SUNDAY > ShortDate d = 05-JUN-49 > LongDate dd = SUNDAY 05 of JUNE of 1949 > AMPM hhampm = 01morning > ShortTime t = 01_02_03 > LongTime tt = 01 _ 02 _ 03 . 004 > 2DigitCentury dd/mm/ = 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 = '+FormatDateTime('', myDate)); > > // Use ShortDayNames > WriteLn(' ShortDays ddd = '+FormatDateTime('ddd', myDate)); > > // Use LongDayNames > WriteLn(' LongDays = '+FormatDateTime('', myDate)); > > // Use the ShortDateFormat string > WriteLn(' ShortDate d = '+FormatDateTime('d', myDate)); > > // Use the LongDateFormat string > WriteLn(' LongDate dd = '+FormatDateTime('dd', 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/ = '+FormatDateTime('dd/mm/', > myDate)); > > WriteLn(''); > WriteLn('Modified formatting:'); > > // Now change the defaults > DefaultFormatSettings.DateSeparator := '-'; > DefaultFormatSettings.TimeSeparator := '_'; > DefaultFormatSettings.ShortDateFormat := 'dd/mmm/yy'; > DefaultFormatSettings.LongDateFormat := ' dd "of" "of" '; > DefaultFormatSettings.TimeAMString := 'morning'; > DefaultFormatSettings.TimePMString := 'afternoon'