Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

c# - Force a particular Timezone across an application

A tricky situation here. Our application runs in a system that is set with a particular TimeZone (let's say Asia time). However a client request that his application to be run using Europe timezone.

Since our data are not stored in UTC, is there anyway we can set the locale in the application so that all dates displayed will be using Europe TimeZone? I understand that we can set the Culture info at the Web.Config but i'm not sure whether that can help set the TimeZone as well.

BTW our application is running using C# WebForm and MSSQL 2008 R2.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How your data is stored is entirely separate to how you choose to display it. Assuming the dates and times are meant to represent fixed points in time (rather than floating "local" times) I would try to write as much of the application as possible with DateTime values in UTC. When you retrieve data, convert it from your "storage time zone" if you need to (you haven't said which zone your data is stored in) and convert it back when you store it. When you display it, display it in whichever time zone you need to.

You don't need to set this at a global level as far as .NET is concerned - you can have an application-wide configuration setting (if you really want to) and "helper" code to convert it whenever you need to display. I would do so very explicitly though: you really don't want conversions happening implicitly when it comes to time stuff.

(I'd also mention that my Noda Time library may come in useful as a clearer API when it comes to dates and times. I'm biased, of course. I'd also suggest migrating to storing everything in UTC if you can... and only where appropriate, of course.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...