我需要使用Delphi 6解析UTC日期:
2013-12-24T11:05:01.000+09:00
在Delphi 7中,我使用以下代码成功做到了这一点:
使用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;
使用StrToDateTime
重载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;
但是在Delphi 6中:
XSToNative
,因为它把EConvertError
日期的月份和日期部分错开了。并且TXsDateTime
不包含AsUTCDateTime
...的定义SysUtils
不包含的定义TFormatSettings
,因此StrToDateTime
无法使用我使用的重载。我缺少什么,或者在Delphi 6中还可以使用什么解析这种格式?
最后,我使用了EncodeDateTime
函数:
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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句