Sunday, March 3, 2013

Planned obsoles-C-ence

Here's some notes on how to put an epoch time stamp in your C/C++ program using the preprocessor macros __DATE__ and __TIME__


#include <time.h>



/* put this stuff in your function */


struct tm build_tm;
time_t build_t;

if (strptime(__DATE__, "%b  %d %Y", &build_tm) == NULL)

      return(); /* error! */
if (strptime(__TIME__, "%H:%M:%S", &build_tm) == NULL)
      return(); /* error! */

build_t = strptime(&build_tm);
  
if (build_t == -1)
      return(); /* error! */

return(build_t);

There you go.  Current epoch time is even easier:

#include <stdio.h>


time_t sec;

sec = time (NULL);

Assuming the user doesn't reset their system time just to run your program, you could use those two variables - build date and current date - to make decisions based on the age of the program.

- nzvyyx



No comments:

Post a Comment