当前位置:  开发笔记 > 编程语言 > 正文

使用PST/CEST/UTC /等格式的时区解析DateTime

如何解决《使用PST/CEST/UTC/等格式的时区解析DateTime》经验,为你挑选了3个好方法。

我正在尝试解析类似于的国际日期时间字符串:

24-okt-08 21:09:06 CEST

到目前为止,我有类似的东西:

CultureInfo culture = CultureInfo.CreateSpecificCulture("nl-BE");
DateTime dt = DateTime.ParseExact("24-okt-08 21:09:06 CEST",
    "dd-MMM-yy HH:mm:ss ...", culture);

问题是我应该在格式字符串中使用'...'?查看自定义日期和时间格式字符串 MSDN页面似乎没有列出用于以PST/CEST/GMT/UTC格式解析时区的格式字符串.



1> Panos..:

AFAIK时区缩写无法识别.但是,如果将缩写替换为时区偏移量,则可以.例如:

DateTime dt1 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+2"), "dd-MMM-yy HH:mm:ss z", culture);
DateTime dt2 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02"), "dd-MMM-yy HH:mm:ss zz", culture);
DateTime dt3 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02:00"), "dd-MMM-yy HH:mm:ss zzz", culture);


这会处理日光时间吗?
不,它不会,因为这里手动指定了偏移量.

2> Jodrell..:

答案很简单,你不能这样做.


这就是原因,

有一个世界时区的权威数据库,您可以在这里从IANA获取.

问题是,3或4个字母的缩写与IANA时区有多对一的关联.例如"AMT",根据您的文化,您所处的世界的哪个部分以及您的应用环境,意味着不同的东西.

AMT "Armenia Time" Asia          UTC + 4 hours 
AMT "Amazon Time"  South America UTC - 4 hours 

如果你真的想解决这个问题,我建议用Noda Time代表你Instance的.您必须编写一些代码才能将缩写转换为标准的IANA时区.

我们无法为您执行此操作,具体取决于您的应用程序的上下文.


另一个好例子是"CST".

CST "China Standard Time"   Asia            UTC + 8 hours 
CST "Central Standard Time" Central America UTC - 6 hours 
CST "Cuba Standard Time"    Caribbean       UTC - 5 hours 
CST "Central Standard Time" North America   UTC - 6 hours 


不,你不能.文化信息将告诉您有关用户语言和文化偏好的信息.它不会告诉您有关用户时区的任何信息.即使我访问日本,我的文化也可能是"en-US".

3> Jussi Palo..:

缩写词典,如果你决定去搜索和替换路线(我做了).

Dictionary _timeZones = new Dictionary() {
            {"ACDT", "+1030"},
            {"ACST", "+0930"},
            {"ADT", "-0300"},
            {"AEDT", "+1100"},
            {"AEST", "+1000"},
            {"AHDT", "-0900"},
            {"AHST", "-1000"},
            {"AST", "-0400"},
            {"AT", "-0200"},
            {"AWDT", "+0900"},
            {"AWST", "+0800"},
            {"BAT", "+0300"},
            {"BDST", "+0200"},
            {"BET", "-1100"},
            {"BST", "-0300"},
            {"BT", "+0300"},
            {"BZT2", "-0300"},
            {"CADT", "+1030"},
            {"CAST", "+0930"},
            {"CAT", "-1000"},
            {"CCT", "+0800"},
            {"CDT", "-0500"},
            {"CED", "+0200"},
            {"CET", "+0100"},
            {"CEST", "+0200"},
            {"CST", "-0600"},
            {"EAST", "+1000"},
            {"EDT", "-0400"},
            {"EED", "+0300"},
            {"EET", "+0200"},
            {"EEST", "+0300"},
            {"EST", "-0500"},
            {"FST", "+0200"},
            {"FWT", "+0100"},
            {"GMT", "GMT"},
            {"GST", "+1000"},
            {"HDT", "-0900"},
            {"HST", "-1000"},
            {"IDLE", "+1200"},
            {"IDLW", "-1200"},
            {"IST", "+0530"},
            {"IT", "+0330"},
            {"JST", "+0900"},
            {"JT", "+0700"},
            {"MDT", "-0600"},
            {"MED", "+0200"},
            {"MET", "+0100"},
            {"MEST", "+0200"},
            {"MEWT", "+0100"},
            {"MST", "-0700"},
            {"MT", "+0800"},
            {"NDT", "-0230"},
            {"NFT", "-0330"},
            {"NT", "-1100"},
            {"NST", "+0630"},
            {"NZ", "+1100"},
            {"NZST", "+1200"},
            {"NZDT", "+1300"},
            {"NZT", "+1200"},
            {"PDT", "-0700"},
            {"PST", "-0800"},
            {"ROK", "+0900"},
            {"SAD", "+1000"},
            {"SAST", "+0900"},
            {"SAT", "+0900"},
            {"SDT", "+1000"},
            {"SST", "+0200"},
            {"SWT", "+0100"},
            {"USZ3", "+0400"},
            {"USZ4", "+0500"},
            {"USZ5", "+0600"},
            {"USZ6", "+0700"},
            {"UT", "-0000"},
            {"UTC", "-0000"},
            {"UZ10", "+1100"},
            {"WAT", "-0100"},
            {"WET", "-0000"},
            {"WST", "+0800"},
            {"YDT", "-0800"},
            {"YST", "-0900"},
            {"ZP4", "+0400"},
            {"ZP5", "+0500"},
            {"ZP6", "+0600"}
        };


对不起,但这种方法不可靠.任何硬编码的缩写列表都是关于模糊性的,并且只是时间上的快照.
当然如此.与首先使用这些非标准化时区同样(不)可靠.我的回答只是支持接受的答案.记得也要对此进行投票.
即使对于单一文化,这也不可靠。俄罗斯在过去4年中多次更改DST规则。将缩写映射到IANA或Windows tz名称并查找当前规则要可靠得多
推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有