tags:

views:

921

answers:

2

XML date and time are in the format

'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?

were

•'-'? yyyy is a four-or-more digit optionally negative-signed numeral that represents the year; if more than four digits, leading zeros are prohibited, and '0000' is prohibited

•the remaining '-'s are separators between parts of the date portion;

•the first mm is a two-digit numeral that represents the month;

•dd is a two-digit numeral that represents the day;

•'T' is a separator indicating that time-of-day follows;

•hh is a two-digit numeral that represents the hour; '24' is permitted if the minutes and seconds represented are zero, and the dateTime value so represented is the first instant of the following day (the hour property of a dateTime object in the ·value space· cannot have a value greater than 23);

•':' is a separator between parts of the time-of-day portion;

•the second mm is a two-digit numeral that represents the minute;

•ss is a two-integer-digit numeral that represents the whole seconds;

•'.' s+ (if present) represents the fractional seconds;

•zzzzzz (if present) represents the timezone (as described below).

here are more examples

Simple Example 2009-08-31T19:30:00

More complex examples

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.

see www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html for more info

+2  A: 

OmniXML's unit OmniXMLUtils contains bunch of funcions to do XML to date and date to XML conversions.

function XMLStrToDateTime(nodeValue: XmlString; var value: TDateTime): boolean; overload;
function XMLStrToDateTime(nodeValue: XmlString): TDateTime; overload;
function XMLStrToDateTimeDef(nodeValue: XmlString; defaultValue: TDateTime): TDateTime;
function XMLStrToDate(nodeValue: XmlString; var value: TDateTime): boolean; overload;
function XMLStrToDate(nodeValue: XmlString): TDateTime; overload;
function XMLStrToDateDef(nodeValue: XmlString; defaultValue: TDateTime): TDateTime;
function XMLStrToTime(nodeValue: XmlString; var value: TDateTime): boolean; overload;
function XMLStrToTime(nodeValue: XmlString): TDateTime; overload;
function XMLStrToTimeDef(nodeValue: XmlString; defaultValue: TDateTime): TDateTime;

function XMLDateTimeToStr(value: TDateTime): XmlString;
function XMLDateTimeToStrEx(value: TDateTime): XmlString;
function XMLDateToStr(value: TDateTime): XmlString;
function XMLTimeToStr(value: TDateTime): XmlString;
gabr
+8  A: 

Delphi has a XSBuiltIns unit (since Delphi 6) that contains data types that can help you convert some XML data types:

(there are more, like TXSDecimal, you get the idea)

All of these contain at least these two methods:

You can use it like this:

with TXSDateTime.Create() do
  try
    AsDateTime := ClientDataSetParam.AsDateTime; // convert from TDateTime
    Attribute.DateTimeValue := NativeToXS; // convert to WideString
  finally
    Free;
  end;

with TXSDateTime.Create() do
  try
    XSToNative(XmlAttribute.DateTimeValue); // convert from WideString
    CurrentField.AsDateTime := AsDateTime; // convert to TDateTime
  finally
    Free;
  end;

That should get you going.

--jeroen

Jeroen Pluimers
+1 I never realised this unit existed! Is it possible to ignore things like milliseconds/utc offset?
James
I think you can do that using the `TXSBaseCustomDateTime` class in the same unit.
Jeroen Pluimers