简体   繁体   English

如何在.Net应用程序中使用地区时区?

[英]How to use Region Time Zone with a .Net application?

I'm working on an asp.net mvc application. 我正在一个asp.net mvc应用程序上。 Each user have his own time zone. 每个用户都有自己的时区。

Right now, I'm using "TimeZoneInfo.GetSystemTimeZones" to generate a drop down list in order for the user to select a timezone and this is what I store in my db 现在,我正在使用“ TimeZoneInfo.GetSystemTimeZones”生成一个下拉列表,以便用户选择时区,这就是我存储在数据库中的内容
They are like that: 他们是这样的:
Morocco Standard Time (00:00:00) 摩洛哥标准时间(00:00:00)
UTC (00:00:00) 世界标准时间(00:00:00)
GMT Standard Time (00:00:00) 格林尼治标准时间(00:00:00)
... ...

I know that php use a different timezone set, they are "region timezone", for example: 我知道php使用不同的时区集,它们是“区域时区”,例如:
Europe/Paris 欧洲/巴黎
Europe/London 欧洲/伦敦
... ...

My question is: is there a way to play with the region timezone (like php) in an .NET application? 我的问题是:.NET应用程序中是否可以使用区域时区(如php)播放? The only way I can think of is to bind each php region timezone to the .net timezone. 我能想到的唯一方法是将每个php区域时区绑定到.net时区。

Also, the "TimeZoneInfo.GetSystemTimeZones" list all of the timezone on the machine. 另外,“ TimeZoneInfo.GetSystemTimeZones”列出了计算机上的所有时区。 Is the list different between windows server, windows vista, windows xp? Windows Server,Windows Vista,Windows XP之间的列表是否不同?

Hope this make sense, thanks guys 希望这有意义,谢谢大家

The zone name schema used by php is often called Olson database. php使用的区域名称架构通常称为Olson数据库。
You can find a conversion table for windows zone (id) -> Olson DB at http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html 您可以在http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html上找到 Windows区域(id)-> Olson DB的转换表

You might also be interested in noda-time (though there's not release yet). 您可能还对noda-time感兴趣(尽管还没有发布)。 Jon Skeet talked a bit about it on the 200th hanselminutes episode and it's going to use Olson DB as well. 乔恩·斯凯特(Jon Skeet)在第200部hanselminutes节目中谈到了这一点,并且还将使用Olson DB。


In case the list/document at http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml is complete and up-to-date you can create a simple conversion function for php via 如果位于http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml的列表/文档是最新的,则可以通过以下方式为php创建简单的转换函数:

$supplementalData = simplexml_load_file('http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml');

$zones = array();
foreach( $supplementalData->windowsZones->mapTimezones->mapZone as $zi ) {
  $zones[ (string)$zi['other'] ] = (string)$zi['type'];
}

file_put_contents('zoneconvert.php', '<?php function windowszone2olson($winzone) {
  static $zi = '.var_export($zones, true).';
  return isset($zi[$winzone]) ? $zi[$winzone] : false;
}');

This will create/overwrite a file zoneconvert.php that exposes the function windowszone2olson($winzone) . 这将创建/覆盖一个文件zoneconvert.php ,该文件公开了函数windowszone2olson($winzone) You can do more or less the same on the .net end, too (which is probably the better place). 您也可以在.net端执行或多或少相同的操作(这可能是更好的地方)。

It sounds like you're after the zoneinfo or Olson database names. 听起来您好像在使用zoneinfo或Olson数据库名称。 Basically those are the names the non-Windows world uses :) 基本上,这些是非Windows世界使用的名称:)

They're also what Noda Time uses typically, although we'll support the Windows names as well eventually. 它们也是Noda Time通常使用的方式,尽管最终我们也将支持Windows名称。 Unfortunately Noda Time isn't production-ready yet - when it is, it should be the right answer to your question :) 不幸的是Noda Time还没有准备好投入生产-那时,它应该是您问题的正确答案:)

In the meantime, if you're definitely only going to be using Windows, I think it would be reasonable to store the Windows time zone IDs - make sure you use the Id property instead of StandardName though. 同时,如果您绝对只打算使用Windows,则我认为存储Windows时区ID是合理的-确保您使用Id属性而不是StandardName There are translations between the Windows IDs and the Olson IDs around the web - but I don't know how comprehensive (or up-to-date) they are. 网络的Windows ID和Olson ID之间存在转换-但是我不知道它们的全面性(或最新性)。

I would use that method to initially populate your Time Zone table with data, not pull from the collection each time. 我将使用该方法最初使用数据填充时区表,而不是每次都从集合中提取数据。 If something does change, you could accidently change the Ids that each person uses at some point later down the road. 如果确实发生了变化,则您可能会在以后的某个时刻意外更改每个人使用的ID。 You also then don't have to worry about any potential differences between OS'. 然后,您也不必担心OS'之间的任何潜在差异。

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

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