开发者

How to control Time zone formatting in System.Xml.Serialization or during application execution?

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?


I think you will be able to achieve this by using System.TimeZoneInfo. For example:

TimeZoneInfo.ConvertTime(myLocalTime, TimeZoneInfo.FindSystemTimeZoneById(“Pacific Standard Time”));


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜