简体   繁体   中英

Storing and Displaying DateTime from Multiple Servers in Different Time Zones

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.SpecifyKind after 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 a LocalDateTime to a DateTime using ToDateTimeUnspecified and convert the other way with FromDateTime .

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 DateTime if you're still not convinced :)

It sounds like your friend can just directly store the time inside of SQL. For instance, if two servers in California and Arizona at 9pm local time run the following query, two 9pm values will be inserted.

SqlCommand cmd = new SqlCommand("INSERT INTO table (Date) VALUES (@value)", conn);
cmd.Parameters.AddWithValue("@value", DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss"));
cmd.ExecuteNonQuery();

If you're looking to insert UTC time, you might want to take a look at the equivalent convert_tz functionality for SQL Server 2008. You can store the UTC time and the timezone the server is configured for, you'll be be able perform a conversion to the server's time.

 SELECT convert_tz(table.UtcTime, 'UTC', table.TimeZone) FROM table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM