强制跨应用程序使用特定时区

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.

由于我们的数据不是以 UTC 格式存储的,我们是否可以在应用程序中设置区域设置,以便显示的所有日期都使用欧洲时区?我知道我们可以在 Web.Config 中设置文化信息,但我不确定这是否也有助于设置时区.

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.

顺便说一句,我们的应用程序使用 C# WebForm 和 MSSQL 2008 R2 运行.

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

推荐答案

您的数据如何存储与您选择的显示方式完全不同.假设日期和时间旨在表示固定的时间点(而不是浮动的本地"时间),我将尝试使用 UTC 中的 DateTime 值编写尽可能多的应用程序.当您检索数据时,如果需要,请从您的存储时区"转换它(您没有说明您的数据存储在哪个区域)并在存储时将其转换回来.当您显示时,在您需要的任何时区显示它.

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.

就 .NET 而言,您不需要在全局级别进行设置 - 您可以拥有应用程序范围的配置设置(如果您真的想要)和帮助程序"代码以在需要时对其进行转换显示.不过,我会非常明确地这样做:当涉及到时间问题时,您真的不希望转换隐式发生.

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.

(我还要提一下,我的 Noda Time 库可能会在以下情况下用作更清晰的 API涉及日期和时间.当然,我有偏见.如果可以,我还建议迁移到 UTC 中的所有内容......当然,仅在适当的情况下.)

(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.)

这篇关于强制跨应用程序使用特定时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)