RFC822-Datetime Parsing in .NETMF 4.0

I have an application written in .NETMF that requires me to be able to parse RFC822-Datetime.

Normally, this would be easy, but NETMF does not have a DateTime.parse () method and does not have any implementation that matches the matching pattern, so I'm pretty stuck.

Any ideas?

EDIT: Perhaps smart solutions are needed. Part of this complicates the fact that the time in question tends to have additional spaces in it (but only sometimes). A simple substring solution may work in one day, but the following does not work when in datetime there is additional space somewhere between the parts. I do not control the date and time, it is from NOAA.

+4
source share
1 answer

Good string handling:

  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... 

from

 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"; 

and

 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); } 
+4
source

Source: https://habr.com/ru/post/1312023/


All Articles