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
Go to the original post written by @water huang
Hello @water huang,
The default implementation of the XMLExportToString in %XML.Adaptor basically converts the date format as 2023-11-28T13:57:26Z for the property value by the logic $translate(value," ","T")_"Z"). So, I guess you may need to convert by own.
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 @Enrico Parisi
That's right, However Instead of creating a custom datatype to fix this. create/use the auto generated method for that property "OPDTLogicalToXSD" and implement the code logic.
ClassMethod OPDTLogicalToXSD(%val As %TimeStamp) As %String [ ServerOnly = 1 ] { Set %val=##class(%Library.TimeStamp).LogicalToXSD(%val) Quit $translate(%val,"TZ"," ") }
Enrico,thank you in fact ,i need TimeStamp!
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
They are both possible solutions, however if you create a custom datatype you can then use it in any other property and class, without the need to implement <PropertyName>LogicalToXSD() for each property in each class.
Enrico
Ashok,thank you,this is what i asked,in fact ,i should change %Library.DateTime to and %Library.TimeStamp
and how to parse "<NewClass2><OPDT>2023-11-30 11:07:02</OPDT></NewClass2>" to a object as type of Samples.NewClass2?