设置/更改的.NET应用程序的时区 [英] Setting/Changing an .NET application's TimeZone

查看:174
本文介绍了设置/更改的.NET应用程序的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来设置一个.NET应用程序的时区为小于操作系统的时区其他?

Is there a way to set a .NET application's TimeZone to a value other than the OS's TimeZone?

例如,我使用与中部标准时间设定在OS上我的应用程序,我想该应用程序的行为就好像它是在美国东部标准时间,而不必使用一种方法来手动转换所有DateTime是否在我的应用程序像 TimeZoneInfo.ConvertTimeBySystemTimeZoneId()

For instance, I am using my application on an OS with Central Standard Time set, and I would like the app to behave as if it were in Eastern Standard Time, without having to manually convert all DateTimes in my application using a method like TimeZoneInfo.ConvertTimeBySystemTimeZoneId().

推荐答案

没有,我不相信有什么办法这样做的。它不喜欢的时区设置在每个线程的基础上,如文化。举例来说,我相信DateTime.ToLocalTime会的总是的使用系统时区

No, I don't believe there's any way of doing this. It's not like the time zone is set on a per thread basis like the culture. For instance, I believe that DateTime.ToLocalTime will always use the system time zone

不过,我不会用 TimeZoneInfo.ConvertTimeBySystemTimeZoneId 随处可见。我想揭露类型的属性的TimeZoneInfo 从而重新presented适当的时区 - 或者建立一个辅助类转换,你需要

However, I wouldn't use TimeZoneInfo.ConvertTimeBySystemTimeZoneId everywhere. I'd expose a property of type TimeZoneInfo which represented the appropriate time zone - or set up a helper class to convert where you need to.

根据您的应用程序,你可能会发现,如果你选择在其中的数据转换成正确的点(其中的不是的始终是只是保持它在UTC到处距的情况下, UI层),你就没有那么多的转换来执行,你可能期望。

Depending on your app, you may well find that if you choose the right points at which to convert the data (which isn't always a case of just keeping it in UTC everywhere apart from the UI layer) you don't have as many conversions to perform as you might expect.

这篇关于设置/更改的.NET应用程序的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆