简体   繁体   English

C#TimeZoneInfo将GMT时区名称转换为系统时区

[英]C# TimeZoneInfo to convert GMT timezone name to system timezone

In windows, we get timezone list like this: 在Windows中,我们得到如下时区列表:

ID    Time zone name               Display string
--    --------------               --------------
0     Dateline Standard Time       (UTC-12:00) International Date Line West
110   UTC-11                       (UTC-11:00) Coordinated Universal Time -11
200   Hawaiian Standard Time       (UTC-10:00) Hawaii
300   Alaskan Standard Time        (UTC-09:00) Alaska

more here . 这里更多。

I use this list to convert from one timezone to another using TimeZoneInfo class which accepts time zone name shown in above list. 我使用TimeZoneInfo类使用该列表将一个时区转换为另一个时区,该类接受上面列表中显示的时区名称。

Ex. 例如

// Local time zone to UTC
var utcOffset = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero);
var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezoneName); // here tz name can be any name from above table
var localOffset = new DateTimeOffset(date.Value, localTimeZone.GetUtcOffset(utcOffset));
DateTime utcDate = localOffset.UtcDateTime;

Now I came across SalesForce timezone representation like: 现在,我遇到了SalesForce时区表示形式,例如:

Time Zone Code  Time Zone Name
--------------  --------------
GMT+14:00       Line Is. Time (Pacific/Kiritimati)
GMT+13:00       Phoenix Is.Time (Pacific/Enderbury)
GMT+13:00       Tonga Time (Pacific/Tongatapu)
GMT+12:45       Chatham Standard Time (Pacific/Chatham)

more here . 这里更多。

I couldn't find built in functionality to use either time zone code or time zone name given in above table for the conversion. 我找不到内置功能来使用上表中给出的time zone codetime zone name进行转换。

If you're happy to stick with TimeZoneInfo and DateTime / DateTimeOffset , you can use Matt Johnson's TimeZoneConverter library to convert the IANA ID (the part in brackets, eg Pacific/Kiritimati) to a Windows system time zone ID. 如果您愿意坚持使用TimeZoneInfoDateTime / DateTimeOffset ,则可以使用Matt Johnson的TimeZoneConverter库将IANA ID(括号内的部分,例如Pacific / Kiritimati)转换为Windows系统时区ID。

Examples from the project page docs : 项目页面docs中的示例:

string tz = TZConvert.IanaToWindows("America/New_York");
// Result:  "Eastern Standard Time"

Or: 要么:

TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("America/New_York");

However, things to be aware of: 但是,需要注意的事项:

  • There won't always be a complete mapping from IANA IDs to Windows IDs. 从IANA ID到Windows ID不会总是有完整的映射。 Some aren't mapped, although the situation is better than it used to be. 尽管情况比以前好了一些,但没有映射。
  • The Windows time zone data can be different to the IANA data sometimes. Windows时区数据有时可能与IANA数据不同。 (Again, this is getting better.) (再次,情况越来越好。)
  • Time zone data changes over time. 时区数据随时间变化。 If you're storing future date/time values, you might want to store the time zone and local time rather than the UTC instant. 如果要存储将来的日期/时间值,则可能要存储时区和本地时间,而不是UTC时间。 (That instant may not map to the same local time in the future.) (该瞬间将来可能不会映射到相同的本地时间。)
  • Some local times can be ambiguous - eg if the clocks go back from 2am to 1am on a particular date in a particular time zone, then 1:15am on that day will occur twice in that time zone. 某些当地时间可能会模棱两可-例如,如果时钟在特定时区中的特定日期从凌晨2点回到凌晨1点,那么当天的1:15将在该时区中出现两次。 Think about how you want to handle that, and test it. 考虑一下您要如何处理并进行测试。

I'd personally recommend using my Noda Time library anyway, as a cleaner way of handling date/time, but I acknowledge that if you've got a lot of code dealing with DateTime already, that may not be feasible. 我个人还是建议使用Noda Time库,这是一种更干净的日期/时间处理方式,但是我承认,如果您已经有很多处理DateTime的代码,那可能不可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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