Main Site Documentation

Best way to rewrite this in NETMF (RegExp)


#1

I have the following code that I am trying to port over to NETMF:


		$split     = explode(' ', $output['observation_time_rfc822']);
			
		$pnp = array(
			'TZ' => '-\b[0-9]{4}\b$',
			'D'  => '([1-3][0-9]|[1-9])$',
			'M' => '[A-Z][a-z][a-z]$', 
			'Y' => '\b[0-9]{4}\b$',
			'T' => '[0-9][0-9]:[0-5][0-9]:[0-5][0-9]$',
		);
			
		foreach ($pnp as $put=>$pattern)
		{
			foreach ($split as $param)
			{
				if (preg_match('/'.$pattern.'/', $param))
				{
					$pnp[$put] = $param;
					break;
				}
			}
		}

Basically, this code takes a time that conforms to RFC822 and splits it into parts. Although the time is suppose to conform" to RFC822, it sometimes doesn’t, which would create a problem if I just split it and got the values in order.

So, My issue is that .NETMF doesn’t seem to have any implementation of RegExp (or any other pattern matching library for that matter), which this obviously requires.

Any ideas?


#2

I am too lazy to check now :slight_smile: but is tryparse supported ? If yes then I it should be easy.

Let me know on tryparse and I can dig up some code.


#3

Well, I figured this out…

It isn’t pretty at all, but it does seem to work


		private int monthToInt(string monthin)
		{
			int index = 1;

			foreach (string month in months)
			{
				if (monthin == month)
					return index;

				index++;
			}

			// Month wasn't found
			return -1;
		}

		string[] months = new string[12]{
			"Jan",
			"Feb",
			"Mar",
			"Apr",
			"May",
			"Jun",
			"Jul",
			"Aug",
			"Sep",
			"Oct",
			"Nov",
			"Dec",
		};


			int[] dateParts = new int[6];
			int[] timeParts = new int[3];
			int offset = 0;
			string tz = string.Empty;

			// Strip spaces
			output["observation_time_rfc822"] = stripSpaces(output["observation_time_rfc822"]);

			// Sun, 6 Jun 2010 17:54:00 -0500

			// Day
			dateParts[0] = int.Parse(output["observation_time_rfc822"].Substring(5, 2));

			// Because the NOAA doesn't prefix day numbers under 10 with
			// 0s, we have to adjust for an offset
			if (dateParts[0] >= 10)
				offset = 1;

			// Month
			dateParts[1] = monthToInt((output["observation_time_rfc822"].Substring(7 + offset, 3)));

			// Year
			dateParts[2] = int.Parse(output["observation_time_rfc822"].Substring(11 + offset, 4));

			// Time: Hour
			dateParts[3] = int.Parse(output["observation_time_rfc822"].Substring(16 + offset, 2));

			// Time: Minute
			dateParts[4] = int.Parse(output["observation_time_rfc822"].Substring(19 + offset, 2));

			// Time: Second
			dateParts[5] = int.Parse(output["observation_time_rfc822"].Substring(22 + offset, 2));

			// TZ Offset
			tz = output["observation_time_rfc822"].Substring(25 + offset, 5);

Now my issue is turning the collected time into a UNIX timestamp.

Does anyone know what the default timezone of a new DateTime object is? For example, what timezone would this be in?


DateTime metarDate = new DateTime(dateParts[2], dateParts[1], dateParts[0], dateParts[3], dateParts[4], dateParts[5]);


#4

I beleive that the default time zone is : Pacific-US

You can get the current TimeZone using

TimeZone.CurrentTimeZone.StandardName

You may need to add a reference to the Microsoft.SPOT.Native.dll assembly

These are the name spaces

Microsoft.SPOT.Hardware.Utility
Microsoft.SPOT.ExtendedTimeZone
Microsoft.SPOT.TimeZoneId

HTH.

-Rajesh


#5

Thanks Rajesh,
I
finally finished writting the entire NOAA Weather API that this is for late last night. It weights in at
~700 lines of code, and I will be posting it as soon as all the testing is completed.

What I will say is that parsing the date was pure hell. Not because of .NETMF, but because of the way the NOAA gives me date strings. They couldn’t have picked a worse format.

Anyway, more on the Weather API later :wink: