Board index » delphi » ISO Date/Time Conversion

ISO Date/Time Conversion


2005-08-18 03:05:32 PM
delphi54
Are there any ready-made functions out there to validate and convert the ISO
date, time, and date/time string formats to a Delphi TDateTime? I haven't
found any in the JCL, RTL, etc. Something like this would be a good start:
function ISOStrToDateTime(const DateTime: string): TDateTime;
The DateTime param would be something like this: YYYY-MM-DDThh:nn:ss.zzz
or one of the allowed variants such as without the ms, space instead of a T,
etc. Similar functions for just dates and just times would also be good.
Thanks!
 
 

Re:ISO Date/Time Conversion

In article <43043330$XXXX@XXXXX.COM>, XXXX@XXXXX.COM says...
Quote
Are there any ready-made functions out there to validate and convert the ISO
date, time, and date/time string formats to a Delphi TDateTime? I haven't
found any in the JCL, RTL, etc. Something like this would be a good start:

function ISOStrToDateTime(const DateTime: string): TDateTime;

The DateTime param would be something like this: YYYY-MM-DDThh:nn:ss.zzz
or one of the allowed variants such as without the ms, space instead of a T,
etc. Similar functions for just dates and just times would also be good.

Thanks!

I may have one in my GExperts Code lib on my home
computer. I will take a look this evening.
 

Re:ISO Date/Time Conversion

"Morgan" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
Are there any ready-made functions out there to validate and convert the
ISO
date, time, and date/time string formats to a Delphi TDateTime? I haven't
found any in the JCL, RTL, etc. Something like this would be a good
start:

function ISOStrToDateTime(const DateTime: string): TDateTime;

The DateTime param would be something like this: YYYY-MM-DDThh:nn:ss.zzz
or one of the allowed variants such as without the ms, space instead of a
T,
etc. Similar functions for just dates and just times would also be good.
I made these functions recently to convert dates/times strings in a XML file
to TDateTime.
Some comments are in danish, feel free to translate them to english :-)
function XmlDateStrToDate(Str: string): TDateTime;
// '2004-06-08T00:00:00.0000000+02:00'
// '2004-06-08T11:47:00.0000000+01:00'
// '2004-06-08T18:47:58.0000000+01:00'
var
y, m, d: Word;
begin
if Copy(Str, 1, 1) = '-' then
Delete(Str, 1, 1);
y := StrToInt(Copy(Str, 1, 4));
m := StrToInt(Copy(Str, 6, 2));
d := StrToInt(Copy(Str, 9, 2));
Result := EncodeDate(y, m, d);
end;
{
For example, 2002-10-10T12:00:00-05:00 (noon on 10 October 2002,
Central Daylight Savings Time as well as Eastern Standard Time in the U.S.)
is 2002-10-10T17:00:00Z, five hours later than 2002-10-10T12:00:00Z.
The mapping so defined is one-to-one,
except that '+00:00', '-00:00', and 'Z' all represent the same zero-length
duration timezone, UTC;
'Z' is its canonical representation.
When a timezone is added to a UTC dateTime,
the result is the date and time "in that timezone".
For example, 2002-10-10T12:00:00+05:00 is 2002-10-10T07:00:00Z
and
2002-10-10T00:00:00+05:00 is 2002-10-09T19:00:00Z
<date>2005-05-24T00:00:00.0000000+02:00</date>
<begin>2005-05-24T10:37:21.0000000+02:00</begin>// 10:37:21=Lokal tid
08:37:21=UTC
<end>2005-05-24T10:40:48.0000000+02:00</end>
}
function XmlDateTimeStrToDateTime(Str: string; ReturnUTC: Boolean):
TDateTime;
// '2004-06-08T00:00:00.0000000+02:00'
// '2004-06-08T11:47:00.0000000+01:00' // 11:47:00=LokalTid, 12:47:00=UTC
// '2004-06-08T18:47:58.0000000+01:00'
// '2004-06-08T18:47:58.0000000Z'
// ReturnUTC: False=returner lokal tid, True=returner UTC
var
h, n, s: Word;
p: Integer;
Sfrac: string;
V: Double;
Code: Integer;
begin
Result := XmlDateStrToDate(Str);
p := Pos('T', Str);
if p = 0 then
Exit;
Delete(Str, 1, p); // '11:47:00.0000000+01:00'
h := StrToInt(Copy(Str, 1, 2));
n := StrToInt(Copy(Str, 4, 2));
s := StrToInt(Copy(Str, 7, 2));
if h = 24 then
begin
Result := Result + 1;
h := 0;
end;
Result := Result + EncodeTime(h, n, s, 0);
Delete(Str, 1, 8); // '.0000000+01:00'
// fractional seconds // '.0000000+01:00'
if Copy(Str, 1, 1) = '.' then
begin
Delete(Str, 1, 1); // '0000000+01:00'
p := 0;
while (p < Length(Str)) and (Str[p+1] in ['0'..'9']) do
Inc(p);
if p>0 then
begin
Sfrac := '0.' + Copy(Str, 1, p); // '0.0000000'
Delete(Str, 1, p); // '+01:00' eller 'Z'
Val(Sfrac, V, Code);
if Code = 0 then
Result := Result + (V / SysUtils.SecsPerDay);
end;
end;
// Nu har vi lokal tid
if ReturnUTC then
begin
//Hvis der skal bruges
// timezone // '+01:00' eller 'Z'
if Copy(Str, 1, 1) = 'Z' then
Exit;
if (Copy(Str, 1, 1) = '-') or (Copy(Str, 1, 1) = '+') then
begin
h := StrToInt(Copy(Str, 2, 2));
n := StrToInt(Copy(Str, 5, 2));
s := 0;
if (Copy(Str, 1, 1) = '-') then
Result := Result + EncodeTime(h, n, s, 0);
if (Copy(Str, 1, 1) = '+') then
Result := Result - EncodeTime(h, n, s, 0);
end;
end;
end;
--
Finn Tolderlund