Lazarus

Programming => General => Topic started by: cappe on October 11, 2021, 05:25:49 pm

Title: [Solve] Date GMT conversion
Post by: cappe on October 11, 2021, 05:25:49 pm
How do I convert this "Thu Oct 14 2021 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)//" date to tdatetime?
Title: Re: Date GMT conversion
Post by: sstvmaster on October 11, 2021, 10:00:06 pm
I think so:
Code: Pascal  [Select][+][-]
  1. ...
  2. uses DateUtils;  
  3. ...
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. const
  6.   SDN : TWeekNameArray = ('Sun','Mon','Tue','Wen','Thu','Fri','Sat');
  7.   SMN : TMonthNameArray = ('Jan','Feb','Mar','Apr','May','Jun',
  8.                            'Jul','Aug','Sep','Oct','Nov','Dec');
  9. var
  10.   dt: TDateTime;
  11.   fs: TFormatSettings;
  12. begin
  13.   fs := DefaultFormatSettings;
  14.   fs.ShortMonthNames := SMN;
  15.   fs.ShortDayNames   := SDN;
  16.   dt := ScanDateTime('DDD mmm d yyyy hh:nn:ss','Mon Oct 11 2021 21:54:00 GMT+0200', fs);
  17.  
  18.   // test only
  19.   ShowMessage( FormatDateTime('dd.mm.yyyy hh:nn:ss', dt) );
  20. end;
  21.  

If you are in german speaking country SDN and SMN must set to english words.
Title: Re: Date GMT conversion
Post by: marcov on October 11, 2021, 10:06:31 pm

On Windows you can get fs for a given language directly, e.g.


Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. uses sysutils;
  4.  
  5. const LCID_English = 2057; // british
  6.  
  7. begin
  8.    GetLocaleFormatSettings(LCID_English,fs);
  9.   dt:= .... scandatetime...
  10. end;
  11.  
TinyPortal © 2005-2018