【问题标题】:C# TryParseExact Reading Time Zone [duplicate]C# TryParseExact 读取时区 [重复]
【发布时间】:2012-12-02 15:21:25
【问题描述】:

可能重复:
Parse DateTime with timezone of form PST/CEST/UTC/etc

我有一个如下格式的日期字符串

Fri, 14 Dec 2012 6:52 am PST

我想将其转换为日期时间。我正在使用DateTime.TryParseExact 来实现它,但我无法读取时区。如果我将字符串(没有时区)更改为

Fri, 14 Dec 2012 6:52 am

并使用

DateTime.TryParseExact(DateString, "ddd, dd MMM yyyy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None,out dt)

它有效。 如何读取时区。

编辑 - 我正在从 Yahoo 天气 API 获取数据(例如 http://weather.yahooapis.com/forecastrss?w=2420380)。

谢谢。

【问题讨论】:

    标签: c# datetime


    【解决方案1】:

    PST 不是 .NET 框架的可解析时区 - 时区格式说明符需要一个数字偏移量,例如 -0800。

    解决此问题的一种方法是在尝试解析之前将每个时区转换为其数字表示。

    【讨论】:

    • 有趣。我正在查看链接msdn.microsoft.com/en-us/library/8kb3ddd4.aspx,但找不到任何可以与之匹配的内容。感谢您的信息。
    • @arunlalam - 是的,k 和 z、zz、zzz 格式说明符仅适用于数字时区偏移量,而不适用于命名的时区偏移量。命名时区没有格式说明符。
    【解决方案2】:

    我不相信有处理时区的custom date and time format specifier(而不是当前的 UTC 偏移量)。即使有,时区缩写也可能不明确,因此它们作为传达时区信息的方式通常很差。不幸的是,它们被广泛使用,当然......

    我怀疑你最好创建一个从缩写到“最佳猜测时区”的映射,然后从输入字符串中删除最后一个空格之后的任何内容,并使用它来获取时区。

    以防万一有人想知道,Noda Time 也没有处理这个问题 - 但我希望在某个时候取得进展:)

    【讨论】:

    • 这可能是最好的选择,但仍然不是很好。特别是因为一些缩写在多个地方使用(例如,澳大利亚、北美和中国使用 CST,偏移量非常不同)。还请记住,人们经常滥用日光/标准缩写(例如,当他们实际上在标准时间时说 CDT,反之亦然)。您需要查看日期以查看它是否实际上是在标准时间与白天时间之间。 en.wikipedia.org/wiki/List_of_time_zone_abbreviations
    • @BrianReischl:当然——这就是我特别指出歧义的原因。但不幸的是,这是我们目前最好的:(
    • 啊,对。我读到这指的是日光/标准切换期间的歧义,但我知道你可能同时指的是两者。
    【解决方案3】:

    您可以将时区部分替换为偏移量:

    string DateString = "Fri, 14 Dec 2012 6:52 am PST";
    DateTime dt;
    bool parsable = DateTime.TryParseExact(
        DateString.Replace("PST", "+2")
        , "ddd, dd MMM yyyy h:mm tt z"
        , CultureInfo.InvariantCulture
        , DateTimeStyles.None
        , out dt);
    

    Demo

    【讨论】:

    • 我可以为时区创建一个字典/数据库表,但名称有歧义。
    【解决方案4】:

    是的,这是不可能的,但它不是问题的解决方案。所以我所做的是试图为 arunlalam 找到解决方案:)

    在这里(更新:更正时区-/+):

        public static string[][] TimeZones = new string[][] {
            new string[] {"ACDT", "+1030", "Australian Central Daylight"},
            new string[] {"ACST", "+0930", "Australian Central Standard"},
            new string[] {"ADT", "-0300", "(US) Atlantic Daylight"},
            new string[] {"AEDT", "+1100", "Australian East Daylight"},
            new string[] {"AEST", "+1000", "Australian East Standard"},
            new string[] {"AHDT", "-0900", ""},
            new string[] {"AHST", "-1000", ""},
            new string[] {"AST", "-0400", "(US) Atlantic Standard"},
            new string[] {"AT", "-0200", "Azores"},
            new string[] {"AWDT", "+0900", "Australian West Daylight"},
            new string[] {"AWST", "+0800", "Australian West Standard"},
            new string[] {"BAT", "+0300", "Bhagdad"},
            new string[] {"BDST", "+0200", "British Double Summer"},
            new string[] {"BET", "-1100", "Bering Standard"},
            new string[] {"BST", "-0300", "Brazil Standard"},
            new string[] {"BT", "+0300", "Baghdad"},
            new string[] {"BZT2", "-0300", "Brazil Zone 2"},
            new string[] {"CADT", "+1030", "Central Australian Daylight"},
            new string[] {"CAST", "+0930", "Central Australian Standard"},
            new string[] {"CAT", "-1000", "Central Alaska"},
            new string[] {"CCT", "+0800", "China Coast"},
            new string[] {"CDT", "-0500", "(US) Central Daylight"},
            new string[] {"CED", "+0200", "Central European Daylight"},
            new string[] {"CET", "+0100", "Central European"},
            new string[] {"CST", "-0600", "(US) Central Standard"},
            new string[] {"EAST", "+1000", "Eastern Australian Standard"},
            new string[] {"EDT", "-0400", "(US) Eastern Daylight"},
            new string[] {"EED", "+0300", "Eastern European Daylight"},
            new string[] {"EET", "+0200", "Eastern Europe"},
            new string[] {"EEST", "+0300", "Eastern Europe Summer"},
            new string[] {"EST", "-0500", "(US) Eastern Standard"},
            new string[] {"FST", "+0200", "French Summer"},
            new string[] {"FWT", "+0100", "French Winter"},
            new string[] {"GMT", "-0000", "Greenwich Mean"},
            new string[] {"GST", "+1000", "Guam Standard"},
            new string[] {"HDT", "-0900", "Hawaii Daylight"},
            new string[] {"HST", "-1000", "Hawaii Standard"},
            new string[] {"IDLE", "+1200", "Internation Date Line East"},
            new string[] {"IDLW", "-1200", "Internation Date Line West"},
            new string[] {"IST", "+0530", "Indian Standard"},
            new string[] {"IT", "+0330", "Iran"},
            new string[] {"JST", "+0900", "Japan Standard"},
            new string[] {"JT", "+0700", "Java"},
            new string[] {"MDT", "-0600", "(US) Mountain Daylight"},
            new string[] {"MED", "+0200", "Middle European Daylight"},
            new string[] {"MET", "+0100", "Middle European"},
            new string[] {"MEST", "+0200", "Middle European Summer"},
            new string[] {"MEWT", "+0100", "Middle European Winter"},
            new string[] {"MST", "-0700", "(US) Mountain Standard"},
            new string[] {"MT", "+0800", "Moluccas"},
            new string[] {"NDT", "-0230", "Newfoundland Daylight"},
            new string[] {"NFT", "-0330", "Newfoundland"},
            new string[] {"NT", "-1100", "Nome"},
            new string[] {"NST", "+0630", "North Sumatra"},
            new string[] {"NZ", "+1100", "New Zealand "},
            new string[] {"NZST", "+1200", "New Zealand Standard"},
            new string[] {"NZDT", "+1300", "New Zealand Daylight"},
            new string[] {"NZT", "+1200", "New Zealand"},
            new string[] {"PDT", "-0700", "(US) Pacific Daylight"},
            new string[] {"PST", "-0800", "(US) Pacific Standard"},
            new string[] {"ROK", "+0900", "Republic of Korea"},
            new string[] {"SAD", "+1000", "South Australia Daylight"},
            new string[] {"SAST", "+0900", "South Australia Standard"},
            new string[] {"SAT", "+0900", "South Australia Standard"},
            new string[] {"SDT", "+1000", "South Australia Daylight"},
            new string[] {"SST", "+0200", "Swedish Summer"},
            new string[] {"SWT", "+0100", "Swedish Winter"},
            new string[] {"USZ3", "+0400", "USSR Zone 3"},
            new string[] {"USZ4", "+0500", "USSR Zone 4"},
            new string[] {"USZ5", "+0600", "USSR Zone 5"},
            new string[] {"USZ6", "+0700", "USSR Zone 6"},
            new string[] {"UT", "-0000", "Universal Coordinated"},
            new string[] {"UTC", "-0000", "Universal Coordinated"},
            new string[] {"UZ10", "+1100", "USSR Zone 10"},
            new string[] {"WAT", "-0100", "West Africa"},
            new string[] {"WET", "-0000", "West European"},
            new string[] {"WST", "+0800", "West Australian Standard"},
            new string[] {"YDT", "-0800", "Yukon Daylight"},
            new string[] {"YST", "-0900", "Yukon Standard"},
            new string[] {"ZP4", "+0400", "USSR Zone 3"},
            new string[] {"ZP5", "+0500", "USSR Zone 4"},
            new string[] {"ZP6", "+0600", "USSR Zone 5"}
            };
    

    以及返回可解析偏移量的函数:

    public static string TimeZoneToOffset(string tz)
        {
            tz = tz.ToUpper().Trim();
            for (int i = 0 ; i < TimeZones.Length ; i++)
            {
                if (((string)((string[])TimeZones.GetValue(i)).GetValue(0)) == tz)
                {
                    return ((string)((string[])TimeZones.GetValue(i)).GetValue(1));
                }
            }
            return System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).ToString()
            .Replace(":", "").Substring(0, 5);
        }
    

    在这里找到: http://bytes.com/topic/c-sharp/answers/214648-how-do-i-parse-date-w-time-zone

    希望它有效:) 祝你有美好的一天

    【讨论】:

    • 您可能想要检查这些值。 AEST 是 +10/11 而不是 -10/11(毕竟我住在这里)并且通常使用 AEST 天气,我们目前是否处于 DST,因为某些州没有观察到它并且处于同一时区(QLD 没有t 和 NSW 例如,它们位于同一时区)。因此,在这种情况下,上述内容也会有一个小时的休息时间。
    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 2013-09-19
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多