Date formatting strange? lazarus / 62-General ---------------------------------------------------------------------- HatForCat · 2017-02-01 20:30 Hi, I get an "invalid date format if I use Code: [Select]var   thDate :TDate; begin thDate:=StrToDate('01/01/1970') If I change the Date-Separator  to ('01-01-1970') it is all happy. I searched the Help here and it suggested using ShortDateFormat - but- that displays 'd/m/y'; Begs the questions. How do I first set the default Date format to the US-format of M/D/Y? The code Message says ShortDateFormat is deprecated so what is the new replacement? Why is the new replacement (obviously being used above) different to the ShortDateFormat? Even deprecated, surely they should at least match. No? All very confusing. ---------------------------------------------------------------------- bytebites · 2017-02-01 20:39 rtfm   http://www.freepascal.org/docs- html/rtl/sysutils/defaultformatsettings.html ---------------------------------------------------------------------- Phil · 2017-02-01 20:45 Quote from: HatForCat on February 01, 2017, 09:30:52 pmHow do I first set the default Date format to the US-format of M/D/Y? Something like this should work:   FormatSettings.ShortDateFormat := 'm/d/yyyy';   FormatSettings.DateSeparator := '/'; If your computer's locale is set to U.S., these should already be set on Windows. On Linux, include clocale unit. On OS X, it's a bit more complicated. ---------------------------------------------------------------------- molly · 2017-02-01 20:55 Nonsense for both  Never ever change the 'global' date time separators etc, they are based on your system local. Provide temp format settings in case you must. Before you know it people come over complaining that their date edits or calender popups are displayed in such strange settings  This works as intended for TS' problem but, does also require to 'override' system locale settings. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; {$MODE OBJFPC}{$H+} uses  SysUtils; // http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html var  dt: TDate;begin  dt := StrToDate('01*1970*01', 'dd*yyyy*mm', '*');  WriteLn(DateToStr(dt));end. ---------------------------------------------------------------------- HatForCat · 2017-02-01 21:22 Quote from: molly on February 01, 2017, 09:55:21 pmNever ever change the 'global' date time separators etc, Exactly. I am using Ubuntu 14-04LTS and the settings are correct within it for my Locale. How do I drag Lazarus kicking and screaming to join me here? That still leaves the issue of ShortDateFormat and whatever Date Format it is using, having different separators. That needs to be brought into line so all Date separators are the same character. Nuts! ---------------------------------------------------------------------- HatForCat · 2017-02-01 21:24 Quote from: bytebites on February 01, 2017, 09:39:39 pmrtfm   http://www.freepascal.org/docs- html/rtl/sysutils/defaultformatsettings.html Pride goeth before a fall. ---------------------------------------------------------------------- molly · 2017-02-01 22:09 Quote from: HatForCat on February 01, 2017, 10:22:40 pmHow do I drag Lazarus kicking and screaming to join me here? Well, using such function as showed (there are many overloads for datetime related routines), or by using your 'custom' FormatSettings (and accompanied routines, such as DateTimeToStr, formatDateTime and StrToDateTime which also provide overload versions allowing for providing these custom settings). In that regards, it is kind of a rtfm ;-) ---------------------------------------------------------------------- Zoran · 2017-02-01 22:14 Quote from: HatForCat on February 01, 2017, 10:22:40 pmI am using Ubuntu 14-04LTS and the settings are correct within it for my Locale. How do I drag Lazarus kicking and screaming to join me here? See the docs about clocale: http://www.freepascal.org/docs- html-3.0.0/rtl/clocale/index.html Add clocale to uses list of the main program, but only on unixes (enclose it in ifdef unix), for instance: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  {$IFDEF UNIX}  clocale,  {$ENDIF}  Interfaces, // this includes the LCL widgetset  Forms, unit1  { you can add units after this }; ---------------------------------------------------------------------- molly · 2017-02-01 22:18 Zoran, Sorry, in case stating the (perhaps) obvious but, are you saying that in order to initialize default formatsettings used by sysutils (and so lazarus) one need to initialize those by using clocale unit ? ---------------------------------------------------------------------- Phil · 2017-02-01 23:55 Quote from: molly on February 01, 2017, 11:18:23 pmZoran, Sorry, in case stating the (perhaps) obvious but, are you saying that in order to initialize default formatsettings used by sysutils (and so lazarus) one need to initialize those by using clocale unit ? {$IFDEF DARWIN}   clocale,   NSFormat,  {Links to and uses Foundation.framework} //  iosxlocale,  {Links to and uses CoreFoundation.framework} {$ENDIF} {$IFDEF LINUX}   clocale, {$ENDIF} On Mac, you need to use either my NSFormat unit or FPC's iosxlocale, which doesn't work as well as mine. With Windows or NSFormat, you can also specify the locale to use for the TFormatSettings record, eg, Windows:   GetLocaleFormatSettings(1033, MyFormatSettings);  {English-U.S. locale settings} Mac:   GetNSFormatSettings(MyFormatSettings, 'en_US');  {English-U.S. locale settings} That's useful where you want to use a different locale than what your system is using. NSFormat.pas is here: https://macpgmr.github.io/ ---------------------------------------------------------------------- molly · 2017-02-02 00:31 Thank you very much for the elaboration on that topic Phil. I always assumed either FPC or Lazarus initialized this by default (with system/user default OS settings) for all supported platforms. My bad for assuming wrongly.  I also didn't realize there was already a platform dependent function (at least for windows) that would let you change locale settings this way (until now, i did this manually using OS calls). So, the hint to your units is also very much appreciated. ---------------------------------------------------------------------- Phil · 2017-02-02 00:38 Quote from: molly on February 02, 2017, 01:31:20 amI always assumed either FPC or Lazarus initialized this by default (with system/user default OS settings) for all supported platforms. My bad for assuming wrongly.  With clocale unit on Unix, it does initialize, it just doesn't initialize correctly on Mac or iOS. Hence the NSFormat.pas unit I developed. Years later, FPC tried to solve this with iosxlocale, but it doesn't include anything like GetNSFormatSettings, so not so useful really. ---------------------------------------------------------------------- Zoran · 2017-02-02 08:04 Quote from: molly on February 01, 2017, 11:18:23 pmZoran, Sorry, in case stating the (perhaps) obvious but, are you saying that in order to initialize default formatsettings used by sysutils (and so lazarus) one need to initialize those by using clocale unit ? Yes, that is what I am saying. And what you are stating is unfortunately far from obvious.  As you can see in Molly's post: Quote from: molly on February 02, 2017, 01:31:20 amI always assumed either FPC or Lazarus initialized this by default (with system/user default OS settings) for all supported platforms. My bad for assuming wrongly.  And Molly surely is not a rookie, so it is not obvious. Even that might not be enough. If you want your application to work well on Mac, you need more work to get format settings properly initialized, as Phil explained. ---------------------------------------------------------------------- Zoran · 2017-02-02 08:38 Quote from: Zoran on February 02, 2017, 09:04:42 amAs you can see in Molly's post: I have just realized that you are Molly. ---------------------------------------------------------------------- HatForCat · 2017-02-02 14:03 Thanks, but now quite confused by Molly and Phil. I am now fully Ubuntu/Lazarus so no more windows anything. Are you guys saying that I need to add   clocale,   NSFormat, to every Unit where I use a call the includes a predefined and Formated string, like Date etc? I've read a great deal of the f-manual and it more a reference than a manual with very few examples. So, please lighten up with th the "rtfm." As a writer of several widely published Technical manuals, I do know the difference. ---------------------------------------------------------------------- molly · 2017-02-02 21:42 Quote from: Zoran on February 02, 2017, 09:38:20 amQuote from: Zoran on February 02, 2017, 09:04:42 amAs you can see in Molly's post: I have just realized that you are Molly.  No problem Zoran. You have no idea how many times we confuse one another between ourselves. It becomes more difficult when mirrors are involved (And how many of them).  The real question is: who of us is the one with the wrong assumption and who the one with the question with the obvious answer  But, you are right. You're the one who also started this questions of my, so kudo's for that to you as well. ---------------------------------------------------------------------- molly · 2017-02-02 21:50 Quote from: HatForCat on February 02, 2017, 03:03:22 pmThanks, but now quite confused by Molly and Phil. That was not the intention, sorry. As you might have realized i was confused about something as well (or at least made the wrong assumption). Quote...to every Unit where I use a call the includes a predefined and Formated string, like Date etc? No. If you are on Linux then you can use unit clocale and, that unit needs to be 'loaded' once. Preferably at the first opportunity possible. Would you do it later then your program might (unknowingly) already have been using a format function that uses 'uninitialized' format settings that don't correspond with your systems locale settings. QuoteSo, please lighten up with th the "rtfm." I am more than happy to add more to the rtfm part but it is all logic there. I made the wrong assumption in believing that the default format settings that are used by the date and time conversion routines are initialized by user locale settings automatically. I thought this was done automatically for each platform. It turns out this in only the case for windows. You need to use unit clocale to make that happen for *nix platforms. Not doing so will result in some 'random' default values that Free Pascal uses. Note the inconsistency there so that you can never rely on something because it depends on the (target) platform. As of the implications of things: Suppose you change one of the global locale settings of FPC to your likings. That is in theory perfectly ok to do. But, what happens when another component also rely on that same locale setting that you've just changed ? It will act as you instructed. And that is also perfectly ok but, only for those cases where the component actually took the correct approach and not use other hard-coded settings of it's own. You might think/believe that is not often the case but, if you take a look around you see many (bad) examples of projects assuming that date/time settings is a hard-coded setting. When using the default conversion routines from string to datetime and vise-verse, then things can go horribly wrong and user is presented with a "invalid date time format" message while the project insist on f.e. the dd/mm/yyy format. That is why i made such a example with a horrible format string for you in the first place. Never will anyone use dd*yyyy*mm format, until the day someone does and those relying on date/time format always to use slashes for separation or YYYY to be at the end of the date format string goes horribly wrong  So, either you 'stick' to what the default user setting are by copying those settings from the underlying OS (as suggested by Zoran and Phil), or you allow user to change setting based on OS locale setting (not the default ones, but allow for the user to change into something more comfortable. Also provided by underlying OS settings) or... and this is important... you use temp settings for your own custom conversion. Either you do that by providing the format parameters using your own format settings or provide a string which contain this setting (as i've shown in my example) The reason i never am confronted by this myself is that in my projects i use my own custom settings (and corresponding date/time/currency conversion routines)  that user can change, and not rely on underlying OS settings (although i allow for user to copy from them from OS on request). Is that perhaps more clear ?