Question

Ok - a bit of a mouthful. So the problem I have is this - I need to store a Date for expiry where only the date part is required and I don't want any timezone conversion. So for example if I have an expiry set to "08 March 2008" I want that value to be returned to any client - no matter what their timezone is. The problem with remoting it as a DateTime is that it gets stored/sent as "08 March 2008 00:00", which means for clients connecting from any timezone West of me it gets converted and therefore flipped to "07 March 2008" Any suggestions for cleanly handling this scenario ? Obviously sending it as a string would work. anything else ? thanks, Ian

Was it helpful?

Solution

You could create a struct Date that provides access to the details you want/need, like:

public struct Date
{
    public int Month; //or string instead of int
    public int Day;
    public int Year;
}

This is lightweight, flexible and gives you full control.

OTHER TIPS

I'm not sure what remoting technology you're referring to, but this is a real problem with WCF, which only currently supports serializing DateTime as xs:DateTime, inappropriate for a date-only value where you are not interested in timezones.

.NET 3.5 introduces the new DateTimeOffset type, which is good for transferring a DateTime between timezones, but doesn't help with the date-only scenario.

Ideally WCF needs to optionally support xs:Date for serializing dates as requested here:

http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=349215

I do it like this: Whenever I have a date in memory or stored in a file it is always in a DateTime in UTC. When I show the date to the user it is always a string. When I convert between the string and the DateTime I also do the time zone conversion.

This way I never have to deal with time zones in my logic, only in the presentation.

You can send it as UTC Time

dateTime1.ToUniversalTime()

I think sending as a timestamp string would be the quickest / easiest way although you could look at forcing a locale to stop the time conversion from occuring.

The easiest way I've handled this on apps in the past is to just store the date as a string in yyyy-mm-dd format. It's unambigious and doesn't get automatically translated by anything.

Yes, it's a pain...

Why don't you send it as a string then convert it back to a date type as needed? This way it will not be converted over different timezones. Keep it simple.

Edit: I like the Struct idea, allows for good functionality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top