I need to parse UTC date using Delphi 6:
2013-12-24T11:05:01.000+09:00
In Delphi 7 I managed to do this with the following code:
Using TXsDateTime
:
var
utcTime : TXsDateTime;
localTime : TDateTime;
temp : string;
begin
temp := '2013-12-24T00:00:00.000-02:00';
utcTime.XSToNative(temp);
localTime := utcTime.AsUTCDateTime; // get time in +00:00 timezone
localTime := IncHour(localTime, 9); // het time local timezone
//...
end;
Using StrToDateTime
overload with TFormatSettings
:
var
localTime : TDateTime;
temp, datetimePart : string;
formatSettings : TFormatSettings;
begin
temp := '2013-12-24T00:00:00.000+01:00';
//init format settings
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
formatSettings.DateSeparator := '-';
formatSettings.ShortDateFormat := 'yyyy-MM-dd';
//parse datetime
datetimePart := StringReplace(copy(temp,1,Length(temp)-10),'T',' ',[]);
localTime := StrToDateTime(datetimePart, formatSettings);
//get datetime in +00:00 timezone
localTime := IncHour(localTime, -1*strtoint(copy(temp,Length(temp)-5,3)));
localTime := IncMinute(localTime, -1*strtoint(copy(temp,Length(temp)-1,2)));
//get datetime in local timezone
localTime := IncHour(localTime , 9);
//...
end;
But in Delphi 6:
XSToNative
, as it throws EConvertError
misplacing month and day parts of the date; and also TXsDateTime
does not contain definition for AsUTCDateTime
...SysUtils
does not contain definition for TFormatSettings
and consequently the overload of StrToDateTime
I use is unavailable.Is there anything I am missing, or what else can I use to parse this format in Delphi 6?
In the end I used EncodeDateTime
function:
var
time1, time2 : TDateTime;
year,month,day,hour,minute,second,mlsecond, hourOffset, minOffset : integer;
temp :string;
begin
temp := '2013-12-24T00:00:00.000-09:30';
//parse separate parts of the datetime
year := strtoint(copy(temp,1,4));
month := strtoint(copy(temp,6,2));
day := strtoint(copy(temp,9,2));
hour := strtoint(copy(temp,12,2));
minute := strtoint(copy(temp,15,2));
second := strtoint(copy(temp,18,2));
mlsecond := strtoint(copy(temp,21,3));
hourOffset := strtoint(copy(temp,25,2));
minOffset := strtoint(copy(temp,28,2));
//adjust timezone offset sign
if(temp[24]='+')then
begin
hourOffset := -1 * hourOffset;
minOffset := -1 * minOffset;
end;
//get time in the +00:00 timezone
time1 := EncodeDateTime(year,month,day,hour,minute,second,mlsecond);
time1 := IncHour(time1, hourOffset);
time1 := IncMinute(time1, minOffset);
//get time in local timezone
time2 := IncHour(time1, 9);
//...
end;