Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Set the Netduino Date & Time


  • Please log in to reply
2 replies to this topic

#1 Robert L.

Robert L.

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts

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.

    // 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
    }


#2 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

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 Spork

Spork

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts

Posted 14 September 2011 - 04:09 PM

Looks like I just had a syntax error. The syntax checking / error reporting in Visual C# often seems to send me in the wrong direction.

Oops... Posted this on the wrong discussion. Disregard.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.