// set the system's date and time from a string in this format: "mm/dd/yyyy hh:mm:ss" public static void SetDateTime(string dts) { var year = Str2Int(dts, 6); // convert each of the numbers var month = Str2Int(dts, 0); var day = Str2Int(dts, 3); var hour = Str2Int(dts, 11); var minute = Str2Int(dts, 14); var second = Str2Int(dts, 17); System.DateTime dt = new System.DateTime(year, month, day, hour, minute, second); Microsoft.SPOT.Hardware.Utility.SetLocalTime(dt); } // convert a string to an "int" stops at the end-of-string, or at the first non-digit found public static int Str2Int(string input, int offset) { int ret = 0; // built the result here for( int i = offset; i < input.Length; i++) // stop when all chars have been processed { char c = input[i]; // get the next char if (c < '0') break; // stop if a non-number is found if (c > '9') break; int n = (int)c - 48; // convert the ascii value to a number, IE '1' = 49 ret = n + 10 * ret; // accumulate the result } return ret; // return the result to caller }
Set the Netduino Date & Time
Started by
Robert L.
, Aug 26 2011 05:03 PM
2 replies to this topic
#1
Posted 26 August 2011 - 05:03 PM
I wanted to set the Netduino Plus System Date and Time from a string I receive from a server. The code is pretty straight forward, once you know the right methods to use. But it took more time than I care to admit to get this to work (Hey I am new to C# & .NET micro framework), so perhaps the code below will help someone else. This was tested on 4.2 RC1. It should be pretty easy to modify if your data is in a different format.
#2
Posted 26 August 2011 - 05:54 PM
I wanted to set the Netduino Plus System Date and Time from a string I receive from a server...
Nicely written! Looks like a great start! You also might want to look at this thread:
http://forums.netdui...ch__1#entry3515
-Valkyrie-MT
#3
Posted 14 September 2011 - 04:09 PM
Oops... Posted this on the wrong discussion. Disregard.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users