Main Site Documentation

Getting compile-time into c# code?


#1

Hi there

I am doing some data-logging stuff, and are sick and tired of setting the time in my system every time I deploy.

Is there a clever way to create a constant value that contains the time of compilation, so the first lines of code sets the RTC based on that value.

That would be really useful in the development process.


#2

Can you power RTC so it keeps the clock?


#3

There is no network, and no RTC-battery (yet!).


#4

@ njbuch - As suggested by Architect a battery for RTC would be the easiest solution. If you realy can’t use a battery there might be a compile time option. I haven’t tried it but this would be my approach.

  • Create a small windows console program that creates a text file and write the current system time to the file.
  • In visual studio project setting.build events you edit pre-build where you call your console application.
  • Include the text file as a resource
  • In your managed app you read the resource content to set the clock

#5

You can also make your class partial with one part that you will modify from the command line tool that @ RobvanSchelven mentioned.

That command line tool can be a complete console app or a script ( see “time /t” command).

There are options.


#6

Oh my i was thinking to difficult… Your approach is even smarter :slight_smile:


#7

Thank you :wink:

@ njbuch - I have just tested this:

Class1.cs:


using System;
using Microsoft.SPOT;

namespace N1
{
    public partial class Class1
    {
        public Class1()
        {
            Debug.Print(CUR_TIME);
        }
    }
}

Class1.Time.cs:


namespace N1 { public partial class Class1 { string CUR_TIME="17:22:45.41"; } } 

Set pre-build and post-build events to the following string:


ECHO namespace N1 { public partial class Class1 { string CUR_TIME="%TIME%"; } } > $(ProjectDir)Class1.Time.cs

Instantiate the class somewhere in code and it will always print new time every time you run the app (emulator is fine to test)


#8

Amazing, there was a reason you got that Fezzie Award. Thanks to much, saving hours…


#9

You are welcome! :slight_smile:


#10

@ Architect - Excellent :slight_smile: