Setting the time on netmf automatically

I’m being a bit lazy however is there a way that I can get the current datetime at compile time and populate a const in the program with it, so that the device starts up knowing this data.

e.g.


const DateTime = new DateTime.Parse( {some magic compile time thingy that gets the current datetime and populates this value in the code with it});

Something like this solution but more elegant [url]c# - Can I make a constant from a compile-time env variable in csharp? - Stack Overflow

Since this time will not be accurate, even at the time you compile and deploy, why not just using a simple constant value in your code?

@ Reinhard Ostermeier - Because my scatter brains would forget to set it each time before I compile and deploy :slight_smile:

@ andre.m - But the compiler would be able to get the current datetime and populate that class correctly?

Right then, I just reread the post. “…and the project always has a constant available that is the latest build number and time.”

Gonna try this on .netmf

Use a SD2405 Real-Time clock Module SD2405 Real-Time clock Module(Arduino Gadgeteer Compatible)

I have a driver for it here http://gadgeteerdfrobot.codeplex.com/ (might need some updating)

@ Duke Nukem - :slight_smile: and where do you get the time to set the clock in the first place?

I know I’ll have to implement NTP so that the robot can get it’s time from internet servers; and I’ll prob use the RTC on the cerb to keep time when it’s off.

@ Mr. John Smith - Once its set it has a battery backup so it keeps the time internally, set once use many times :slight_smile:

@ Mr. John Smith -

In your AssemblyInfo.cs set the version info like this:


When you use the .* at the end, VS puts in a build number and revision automatically, based on the current time.
You can then decode the time in your application, like this:  

```cs

System.Version ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime BuildDate = new DateTime(2000, 1, 1) + new TimeSpan(ver.Build * TimeSpan.TicksPerDay + ver.Revision * TimeSpan.TicksPerSecond * 2);

We use this both to display the software build date to the user, and as a check on the battery backed RTC.
If it is < the build date, or too far in the future, it is probably wrong.

4 Likes

@ C-Born - I did not know that is where VS gets that info from. Thanks much! this is a much simpler implementation.

Works prefectly, to within a few seconds of my local system time


        System.Version ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        DateTime BuildDate = new DateTime(2000, 1, 1) + new TimeSpan(ver.Build * TimeSpan.TicksPerDay + ver.Revision * TimeSpan.TicksPerSecond * 2);
        Utility.SetLocalTime(BuildDate);

easy peasy.