在.NETMF 4.0解析RFC822,日期时间 [英] Parsing an RFC822-Datetime in .NETMF 4.0
问题描述
我已经写在.NETMF一个应用程序,需要我能够解析RFC822,日期时间。
I have an application written in .NETMF that requires that I be able to parse an RFC822-Datetime.
通常情况下,这将是容易的,但NETMF没有一个DateTime.parse()方法,也不会有某种匹配的实施模式,所以我:M几乎卡住
Normally, this would be easy, but NETMF does not have a DateTime.parse() method, nor does it have some sort of a pattern matching implementation, so I'm pretty much stuck.
任何想法
编辑:很可能需要智能的解决方案。部分原因,这是困难的是,有问题的日期时间必须有多余的空格中(但只是有时)的倾向。一个简单的解决方案子可能工作一天,但失败时,旁边的日期时间有一个额外的空间某处部分之间。我没有在日期时间的控制,是从NOAA。
"Intelligent" solutions are probably needed. Part of the reason this is difficult is that the datetime in question has a tendency to have extra spaces in it (but only sometimes). A simple substring solution might work one day, but fail the next when the datetime has an extra space somewhere between the parts. I do not have control over the datetime, it is from the NOAA.
推荐答案
好醇'字符串操作:
Sun, 06 Jun 2010 20:07:44 +0000
1 2 3
0123456789012345678901234567890
string x = Sanitize(" Sun, 06 \t Jun 2010 \r\n 20:07:44 +0000 ");
int day = int.Parse(x.Substring(5, 2));
int month = Array.IndexOf(months, x.Substring(8, 3)) + 1;
int year = int.Parse(x.Substring(12, 4));
int hour = int.Parse(x.Substring(17, 2));
int minute = int.Parse(x.Substring(20, 2));
int second = int.Parse(x.Substring(23, 2));
int offsetSgn = (x[26] == "-") ? -1 : 1;
int offsetHour = int.Parse(x.Substring(27, 2));
int offsetMinute = int.Parse(x.Substring(29, 2));
DateTime result = new DateTime(year, month, day, hour, minute, second, 0);
TimeSpan offset = new TimeSpan(offsetHour, offsetMinute, 0);
// TODO: add offset...
与
with
string[] months = new string[12];
months[0] = "Jan";
months[1] = "Feb";
months[2] = "Mar";
months[3] = "Apr";
months[4] = "May";
months[5] = "Jun";
months[6] = "Jul";
months[7] = "Aug";
months[8] = "Sep";
months[9] = "Oct";
months[10] = "Nov";
months[11] = "Dec";
和
string Sanitize(string s)
{
if (s == null)
{
return null;
}
char[] buffer = new char[s.Length];
int pos = 0;
bool inSpace = true;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n')
{
if (!inSpace)
{
buffer[pos] = ' ';
pos++;
inSpace = true;
}
}
else
{
buffer[pos] = s[i];
pos++;
inSpace = false;
}
}
return new string(buffer, 0, pos);
}
这篇关于在.NETMF 4.0解析RFC822,日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!