I'm developing a C# .Net Application that is executing on a system located in the Central Time Zone. The application gets information from a third party using an API they provide. I have used the WSDL to produce the code that my application access the API with...their reporting API allows you to define a start date and end date for the report. These are C# DateTime fields and XSD:dateTime. Now when I set the start date and end dates and allow the API to create the SOAP messages the dates don't always include a Time Zone unless I set the date fields using the ToLocalTime method; however, the method will create the DateTime fields in the Central Time Zone (CST) but I need to have it create these fields in the Pacific Time Zone (PST). If I set my machine time to PST all is good...but of course that causes other time issues. What methods can I use to control the formatting of the DateTime? Alternatively, is there a application setting that can be set in C# that allows timezone control?
views:
112answers:
2
+1
Q:
How to control Time zone formatting in System.Xml.Serialization or during application execution?
A:
I think you will be able to achieve this by using System.TimeZoneInfo. For example:
TimeZoneInfo.ConvertTime(myLocalTime, TimeZoneInfo.FindSystemTimeZoneById(“Pacific Standard Time”));
s1mm0t
2010-05-30 15:42:34
Tried variations of this the problem occurs when having to make the assignment to the date time field for the class generated by the WSDL --- I've had to log SOAP messages to see what is sent...the only case where the timezone is there is when using the ToLocalTime method --- which uses the Timezone of the machine the application is executing.
Beal
2010-05-30 22:30:42
A:
I think you have two options. Obviously what you can do will depend on how the target system handle the date times it receives.
1- Convert the datetime to the target timezone and send the request without timezone info. This would assume that the target system will accept a datetime that does not have timezone info as being in the PST timezone.
2- Change to using DateTimeOffset. This will allow you to explicitly specify the timezone offset and will be serialized with the timezone info you specified.
If possible I would go for option 2.
Chris Taylor
2010-05-30 15:57:21
I also tried the DateTimeOffset, which I really preferred...but the WSDL generated class has the data element typed as a DateTime....I never could get variations of using the DateTimeOffset to have the Timezone....are you suggesting I modify the class generated by the WSDL?
Beal
2010-05-30 22:34:18
@Beal, I think you will need to change the generated classes, I realise this is terrible practice and personally I would try avoid it. But at least give it a try to see if it works, then if we are on the right track we can see if there is a better way to handle this.
Chris Taylor
2010-05-31 17:44:19
Tried modifying the generated class...this causes an error with the Provider (Yahoo EWS)...back trying to figure out another alternative....
Beal
2010-06-07 19:46:13