Convert UTC string to TDatetime in Delphi
Asked Answered
W

3

6
var
  tm : string;
  dt : tdatetime;

tm := '2009-08-21T09:11:21Z';
dt := ?

I know I can parse it manually but I wonder if there is any built-in function or Win32 API function to do this ?

Wandawander answered 20/10, 2009 at 22:54 Comment(4)
Ended up writing my own class to deal with this, and to fully comply with the standard it was quite a complex one. Would be interested in any quicker solutions for this question.Ariminum
The 'standard' being ISO 8601Ariminum
Related question: #1439370Euphonic
Use the built-in ISO 8601 function in DateUtils: TryISO8601ToDateBiondo
N
3

I don't know why there are so many people shooting their mouth off when they don't know what they are talking about? I have to do this menial work; Is it a RAD tool? I sometimes find Delphi has a real superb architecture, though.

procedure setISOtoDateTime(strDT: string);
var
  // Delphi settings save vars
  ShortDF, ShortTF : string;
  TS, DS : char;
  // conversion vars
  dd, tt, ddtt: TDateTime;
begin
  // example datetime test string in ISO format
  strDT := '2009-07-06T01:53:23Z';

  // save Delphi settings
  DS := DateSeparator;
  TS := TimeSeparator;
  ShortDF := ShortDateFormat;
  ShortTF := ShortTimeFormat;

  // set Delphi settings for string to date/time
  DateSeparator := '-';
  ShortDateFormat := 'yyyy-mm-dd';
  TimeSeparator := ':';
  ShortTimeFormat := 'hh:mm:ss';

  // convert test string to datetime
  try

    dd := StrToDate( Copy(strDT, 1, Pos('T',strDT)-1) );
    tt := StrToTime( Copy(strDT, Pos('T',strDT)+1, 8) );
    ddtt := trunc(dd) + frac(tt);

  except
    on EConvertError do
      ShowMessage('Error in converting : ' + strDT);
  end;

  // restore Delphi settings
  DateSeparator := DS;
  ShortDateFormat := ShortDF;
  TimeSeparator := TS;
  ShortTimeFormat := ShortTF;

  // display test string
  ShowMessage ( FormatDateTime('mm/dd/yyyy hh:mm:ss', ddtt) );
end;

http://coding.derkeiler.com/Archive/Delphi/comp.lang.pascal.delphi.misc/2006-08/msg00190.html

Nylons answered 25/11, 2009 at 21:16 Comment(1)
1) You overwrite global format variables which is BAD thing. Consider using thread-safe routines. 2) You ignore timezone. 3) While trunc() and frac() are valid currently things could change in future. Using DateOf() and TimeOf() instead makes code more portable and clear.Wakashan
N
2

If you are using Indy 10, its StrInternetToDateTime() and GMTToLocalDateTime() functions (in the IdGlobalProtocols unit) can parse ISO-8601 formatted strings.

Nitrogenize answered 14/10, 2016 at 15:4 Comment(0)
C
0

This looks like an internet protocol related activity, so you should have no problems in using the Win32 API for this. However note, that Windows does not correctly support conversion to/from UTC for historical dates that are more than approximately 20 years old - Windows simply doesn't have enough details in its time zone settings for that.

Chronic answered 21/10, 2009 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.