I have a friend whose having difficulties figuring out how to display a DateTime to users from multiple time zones, but without having the date adjusted for their time zone. For example, let’s say he has a server in Arizona, California and Nevada each with the appropriate time zone of the states. If a user saves a record as say 9:00 AM Arizona time, he wants it to show up as 9:00 AM on the California and Nevada servers when it’s displayed to their users. The database is SQL Server, 2008 I believe.
How can he go about doing that? I’m going to assume that it has something to do with UTC DateTime values? Anyway, I’d appreciate a “complete” example on how he can accomplish this because we seem to be unable to come to an agreement and I can tell he’s getting frustrated…
Thanks in advance!
It sounds like what they’re really entering is a local time, without really a reference to any particular time zone. This is closest to the behaviour of
DateTimeKind.Unspecified(not local, which implies “in the local time zone of the server”, which is a different matter).I don’t believe SQL Server will actually perform any time zone conversions when storing or retrieving data, but it’s probably worth your friend explicitly making the kind “unspecified” using
DateTime.SpecifyKindafter retrieving it. That could save him problems later on.He could use UTC everywhere – but it sounds like it’s not really a UTC date/time… it’s a “local time wherever you are” which is entirely different.
Of course, it would be remiss of me not to mention my own Noda Time project at this point – as what your friend wants is a
LocalDateTime– having a sensible set of date/time types makes it much easier to keep all this straight in your code. I would suggest your friend uses Noda Time everywhere other than the database code itself – you can convert aLocalDateTimeto aDateTimeusingToDateTimeUnspecifiedand convert the other way withFromDateTime.You and your friend may wish to read the core concepts and type choices pages for more food for thought – whether you use Noda Time or not, it will hopefully make you think about the kinds of date/time available more carefully. Then you can read my rant against
DateTimeif you’re still not convinced 🙂