C# 使用PST/CEST/UTC/etc格式的时区分析DateTime
我试图解析一个国际日期时间字符串,类似于:C# 使用PST/CEST/UTC/etc格式的时区分析DateTime,c#,.net,parsing,datetime,timezone,C#,.net,Parsing,Datetime,Timezone,我试图解析一个国际日期时间字符串,类似于: 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); 问题是格式字符串中的“…”应该使用什么
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格式的时区解析格式字符串。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);
我有两个答案,因为我不确定你在问什么 1) 我看到您正在使用CultureInfo,所以如果您只想格式化 日期和时间是特定于区域性的,我将分离日期/时间和时区,在日期/时间上应用区域性方法并附加时区。如果“CEST”对于不同的文化是不同的,那么您必须通过列出所有选项(可能在案例陈述中)来更改它 2) 如果要将日期/时间转换为其他时区,则不能使用CultureInfo 我建议阅读: 您还可以使用.net framework 3.5类TimeZoneInfo(与时区不同)使您的生活更轻松
以下是我必须做的 我从javascript接收日期时间,然后将其传递到ASP.NET以存储在Oracle数据库中。这是我为《东方时报》和《中央时报》写的C代码
string datetimevalue = hidfileDateTime.Value;
datetimevalue= datetimevalue.Replace("EDT", "EST");
datetimevalue = datetimevalue.Replace("CDT", "CST");
if (datetimevalue.Contains("CST"))
{
filedt = DateTime.ParseExact(datetimevalue, "ddd MMM d HH:mm:ss CST yyyy", provider).ToUniversalTime().AddHours(1).ToLocalTime();
}
else
{
filedt = DateTime.ParseExact(datetimevalue, "ddd MMM d HH:mm:ss EST yyyy", provider);
}
快速的回答是,你做不到
这就是为什么 有一个确定的世界时区数据库,你可以从 问题是,3或4个字母的缩写与IANA时区有多对一的关联。例如,这意味着不同的事情,这取决于您的文化、您所在的地区以及应用程序的上下文
AMT "Armenia Time" Asia UTC + 4 hours
AMT "Amazon Time" South America UTC - 4 hours
如果您真的想解决这个问题,我建议使用来表示您的实例。您必须编写一些代码来将缩写转换为标准IANA时区
我们不能为您这样做,这取决于您的应用程序的上下文。
另一个很好的例子是
如果您决定采用搜索和替换路线,请使用缩写词典(我确实这么做了)
Dictionary\u时区=新字典(){
{“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”},
{“打赌”,“-1100”},
{“BST”,“-0300”},
{“BT”,“+0300”},
{“BZT2”,“-0300”},
{“CADT”,“+1030”},
{“CAST”,“+0930”},
{“猫”,“-1000”},
{“CCT”,“+0800”},
{“CDT”,“-0500”},
{“CED”,“+0200”},
{“CET”,“+0100”},
{“CEST”,“+0200”},
{“CST”,“-0600”},
{“东”、“+1000”},
{“EDT”,“-0400”},
{“EED”,“+0300”},
{“EET”,“+0200”},
{“EEST”,“+0300”},
{“EST”,“-0500”},
{“FST”,“+0200”},
{“FWT”,“+0100”},
{“GMT”,“GMT”},
{“GST”,“+1000”},
{“HDT”,“-0900”},
{“HST”,“-1000”},
{“空闲”,“+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”},
{“新西兰”,“+1100”},
{“NZST”,“+1200”},
{“NZDT”,“+1300”},
{“NZT”,“+1200”},
{“PDT”,“-0700”},
{“PST”,“-0800”},
{“韩国”,“+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”},
{“湿”,“-0000”},
{“WST”,“+0800”},
{“YDT”,“-0800”},
{“YST”,“-0900”},
{“ZP4”,“+0400”},
{“ZP5”,“+0500”},
{“ZP6”,“+0600”}
};
这就是:
获取字符串(前提条件:格式:ddd,dd-MMM-yyy-HH:mm:ss-zzz)
获取最后一个空格
从字符串中删除zzz,但保存zzz的值
zzz的查找偏移量
向字符串添加偏移量
与
private static System.Collections.Generic.Dictionary s_时区偏移量=
新的System.Collections.Generic.Dictionary(){
{“ACDT”,“+10:30”},
{“ACST”,“+09:30”},
{“ADT”,“-03:00”},
{“AEDT”,“+11:00”},
{“AEST”,“+10:00”},
{“AHDT”,“-09:00”},
{“AHST”,“-10:00”},
{“AST”,“-04:00”},
{“在”,“-02:00”},
{“AWDT”,“+09:00”},
{“AWST”,“+08:00”},
{“蝙蝠”,“+03:00”},
{“BDST”,“+02:00”},
{“打赌”,“-11:00”},
{“BST”,“-03:00”},
{“BT”,“+03:00”},
{“BZT2”,“-03:00”},
{“CADT”,“+10:30”},
{“CAST”,“+09:30”},
{“猫”,“-10:00”},
{“CCT”,“+08:00”},
{“CDT”,“-05:00”},
{“CED”,“+02:00”},
{“CET”,“+01:00”},
{“CEST”,“+02:00”},
{“CST”,“-06:00”},
{“东”,“+10:00”},
{“EDT”,“-04:00”},
{“EED”,“+03:00”},
{“EET”,“+02:00”},
{“EEST”,“+03:00”},
{“EST”,“-05:00”},
{“FST”,“+02:00”},
{“FWT”,“+01:00”},
{“GMT”,“+00:00”},
{“GST”,“+1
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
Dictionary<string, string> _timeZones = new Dictionary<string, string>() {
{"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"}
};
string dateString = reader.ReadContentAsString();
int timeZonePos = dateString.LastIndexOf(' ') + 1;
string tz = dateString.Substring(timeZonePos);
dateString = dateString.Substring(0, dateString.Length - tz.Length );
dateString += s_timeZoneOffsets[tz];
// https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
//string es = reader.ReadElementString("pubDate");
this.m_value = System.DateTime.ParseExact(dateString, "ddd, dd MMM yyyy HH:mm zzz", System.Globalization.CultureInfo.InvariantCulture);
private static System.Collections.Generic.Dictionary<string, string> s_timeZoneOffsets =
new System.Collections.Generic.Dictionary<string, string>() {
{"ACDT", "+10:30"},
{"ACST", "+09:30"},
{"ADT", "-03:00"},
{"AEDT", "+11:00"},
{"AEST", "+10:00"},
{"AHDT", "-09:00"},
{"AHST", "-10:00"},
{"AST", "-04:00"},
{"AT", "-02:00"},
{"AWDT", "+09:00"},
{"AWST", "+08:00"},
{"BAT", "+03:00"},
{"BDST", "+02:00"},
{"BET", "-11:00"},
{"BST", "-03:00"},
{"BT", "+03:00"},
{"BZT2", "-03:00"},
{"CADT", "+10:30"},
{"CAST", "+09:30"},
{"CAT", "-10:00"},
{"CCT", "+08:00"},
{"CDT", "-05:00"},
{"CED", "+02:00"},
{"CET", "+01:00"},
{"CEST", "+02:00"},
{"CST", "-06:00"},
{"EAST", "+10:00"},
{"EDT", "-04:00"},
{"EED", "+03:00"},
{"EET", "+02:00"},
{"EEST", "+03:00"},
{"EST", "-05:00"},
{"FST", "+02:00"},
{"FWT", "+01:00"},
{"GMT", "+00:00"},
{"GST", "+10:00"},
{"HDT", "-09:00"},
{"HST", "-10:00"},
{"IDLE", "+12:00"},
{"IDLW", "-12:00"},
{"IST", "+05:30"},
{"IT", "+03:30"},
{"JST", "+09:00"},
{"JT", "+07:00"},
{"MDT", "-06:00"},
{"MED", "+02:00"},
{"MET", "+01:00"},
{"MEST", "+02:00"},
{"MEWT", "+01:00"},
{"MST", "-07:00"},
{"MT", "+08:00"},
{"NDT", "-02:30"},
{"NFT", "-03:30"},
{"NT", "-11:00"},
{"NST", "+06:30"},
{"NZ", "+11:00"},
{"NZST", "+12:00"},
{"NZDT", "+13:00"},
{"NZT", "+12:00"},
{"PDT", "-07:00"},
{"PST", "-08:00"},
{"ROK", "+09:00"},
{"SAD", "+10:00"},
{"SAST", "+09:00"},
{"SAT", "+09:00"},
{"SDT", "+10:00"},
{"SST", "+02:00"},
{"SWT", "+01:00"},
{"USZ3", "+04:00"},
{"USZ4", "+05:00"},
{"USZ5", "+06:00"},
{"USZ6", "+07:00"},
{"UT", "-00:00"},
{"UTC", "-00:00"},
{"UZ10", "+11:00"},
{"WAT", "-01:00"},
{"WET", "-00:00"},
{"WST", "+08:00"},
{"YDT", "-08:00"},
{"YST", "-09:00"},
{"ZP4", "+04:00"},
{"ZP5", "+05:00"},
{"ZP6", "+06:00"}
};