Question
· Nov 30, 2023

property of %Library.DateTime export to xml

I create a class, property OPDT as %Library.DateTime,the class extends %XML.Adaptor,after run " d obj.XMLExportToString(.xml)" OPDT`s value is 2023-11-28T13:57:26,but what i need is 2023-11-28 13:57:26,so is there any solution?

Product version: Ensemble 2016.1
$ZV: Cache for Windows (x86-64) 2016.2.3 (Build 907_11_20446U) Thu Nov 12 2020 16:56:45 EST
Discussion (8)2
Log in or sign up to continue

I would create my "custom" datatype extending %Library.DateTime:

Class Community.dt.CustomDateTime Extends %Library.DateTime
{

/// Converts the %TimeStamp value to the canonical SOAP encoded value.
ClassMethod LogicalToXSD(%val As %TimeStamp) As %String [ ServerOnly = 1 ]
{
	Set %val=##class(%Library.TimeStamp).LogicalToXSD(%val)
	Quit $translate(%val,"TZ"," ")
}

}

Then in your class define your property as:

Property OPDT As Community.dt.CustomDateTime;

Are you sure you really need %Library.DateTime and not %Library.TimeStamp?
The difference is the JDBC/ODBC format.

If you prefer using %Library.TimeStamp, then change the superclass in my sample code.

Enrico

Hello @water huang 

As You can create a class method with name of your property " OPDTLogicalToXSD" and add the code conversion for the datetime as mentioned by @Enrico Parisi at XML export time.

It's suitable for both XML and JSON adaptor.

Sample code.

Class Samples.NewClass2 Extends (%Persistent, %Populate, %JSON.Adaptor, %XML.Adaptor)
{
Property OPDT As %Library.DateTime;

ClassMethod OPDTLogicalToXSD(%val As %TimeStamp) As %String [ ServerOnly = 1 ]
{
	Set %val=##class(%Library.TimeStamp).LogicalToXSD(%val)
	Quit $translate(%val,"TZ"," ")
}
}

output

 <NewClass2><OPDT>2023-11-30 11:07:02</OPDT></NewClass2>