问题
I have a global application compsed by many sub-applications :
- Web sites
- Windows services
I want to dispatch those "application part" in many servers. My problem is : I want the dates to be still accurate. If an event is stored at 6:00 PM pacific time, I don't want my european users to see "6:00 PM" on their board.
I've read a lot of articles that say "you just have to save your dates that way" => but I can't change the way each single date is saved, that represent too much work (and yes, it's datetime that are saved and not dateoffset).
I've read other articles that propose to change the time zone on the server => But I can't want to do that, as those server can be used for others purposes.
The good news is that dates are always defined in the application and not generated by the database.
In consequence, the only thing I have to do is to define the TimeZone at application level. So wherever they are, my applications will always work with the same timezone. The only step left will be to convert the dates at UI level, in the timezone of the current user (very simple as all dates are passing throught the same localized datetime conversion method).
I really cannot afford to changes the ways the application developpers work, as it will be prone-to-error. They need to keep using DateTime.Now, without ever thinking of conversion.
The exact technologies I use are not relevant here, the question is global .Net.
Thanks for your help
Edit : let's say my problem is just to have DateTime.Now return the UTC date. Is there a way to do that ?
回答1:
Store all times in UTC.
DateTime.UtcNow
then take care of the TZ correction on the client.
Edit: I missed that crucial part about not changing DateTime.Now. Make the programmers do it right; it's not less prone to error.
You either need to have all client code use DateTime.UtcNow OR have all client code use the database for getting timestamps. Either way -- you've got work to do.
Good luck!
回答2:
I'm going to give you a simple answer, with the understanding that if the answer is this simple, I'm probably misunderstanding the question:
Save the timezone of the local application on the server. Then you can transform the local time however you need to. If your local developers are using DateTime.Now, that's fine, as long as somewhere in the database, you're able to say that this value of DateTime.Now is adjusted for this timezone.
Is that it, or am I overlooking something?
回答3:
An option: Save All Datetimes at GMT/UTC then get the timezone via javascript from the user (their browser) and display the dates taking into consideration that timezone.
// Gets the Browsers timezone offset in minutes
function getClientTimezoneOffsetInMinutes()
{
var rightNow = new Date();
var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
var temp = date1.toGMTString();
var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
var temp = date2.toGMTString();
var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
return hoursDiffStdTime;
}
来源:https://stackoverflow.com/questions/4761933/net-defining-timezone-at-application-level