Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi web service gets different datetime from .net mvc application

I know the solution most would suggest is to change the properties of my objects from TDateTime to String, but I do not have access to all of the source code for the web service application, so there are cases where I cannot do that. So I'd rather just ensure I take care of the issue.

Briefly, I have a .NET MVC application that calls a web method of a service application written in Delphi 2005 to obtain an appointment object, including start date/time and end date/time. The user can make changes to the appointment, then the MVC app calls another web method, passing in the modified appointment object, for the service application to store.

The StartTime and EndTime properties of the TAppointemnt are defined in Delphi as TDateTime. They appear in the WSDL as xs:dateTime. Visual Studio creates them as System.DateTime in the Reference.cs.

By using the Web App Debugger in Delphi, and setting the endpoints of the MVC app to the web service EXE, I can step through both applications in real-time. What I am seeing is that the StartTime and EndTime properties of sent and received Appointment objects are the same when passed from the web service to .NET MVC, but that the times are adjusted by 13 hours when passing from the .NET MVC to the Delphi web service.

While I understand .NET does some internal timezone adjustments, that should only be apparent when the client and server are in different timezones. In my case, the client browser, .NET MVC app and the Delphi service app are in the same one. In fact, they all run on the same machine, so I am at a loss as to why the difference.

It does look like they encode the datetimes in XML differently...

The RESPONSE from the Delphi service app to the .NET MVC app:

<StartTime xsi:type="xsd:dateTime">2012-10-29T08:00:00.000+13:00</StartTime><EndTime xsi:type="xsd:dateTime">2012-10-29T09:15:00.000+13:00</EndTime>

The POST from .NET MVC app to the Delphi service app:

<StartTime xsi:type="xsd:dateTime">2012-10-29T08:00:00</StartTime><EndTime xsi:type="xsd:dateTime">2012-10-29T09:15:00</EndTime>
like image 408
SiBrit Avatar asked Nov 30 '12 04:11

SiBrit


1 Answers

I found a solution that works for me.

In my model, where I process the update to the appointment, I am passed an appointment object generated by the view component, which does not include all properties. Only those on the view. To complete the appointment, I get the unchanged version from the service application and modify its properties based on the new object.

What I found when debugging was that the StartTime and EndTime properties of the wsAppointment retrieved from the service application had a DateTimeKind of Local, while the StartTime and EndTime of the pAppointment parameter were set to Unspecified. So the Local DateTimeKind that I was passing back to the service app was being overwritten with Unspecified.

To fix this, I used DateTime.SpecifyKind to explicitly set the Kind property, as follows:

wsAppointment.StartTime = DateTime.SpecifyKind(pAppointment.StartTime, DateTimeKind.Local);
wsAppointment.EndTime = DateTime.SpecifyKind(pAppointment.EndTime, DateTimeKind.Local);

Hope this helps out others that are experiencing the same or similar problems.

like image 60
SiBrit Avatar answered Nov 16 '22 02:11

SiBrit