You can get the epoch from the __DATE__
preprocessor macro.
Below the getCompileTimeEpoch()
function, which in turn depends on two other simple auxiliary functions that return the month number and another one to switch the month acronym to lower case.
It puzzles me how GCC developers have introduced a macro with a human readable date and not with an epoch, from which anything else about a date can be derived with maximum reliability.
You may very easily add the time from the __TIME__
macro, should you need it added to the epoch.
I opted for this solution, as it keeps everything inside the code and does not depend on compiler options.
char *tolowercase(char *letstr){
int l;
for(l=0;l<=strlen(letstr);l++){
if(letstr[l]>=65 && letstr[l]<=92){
letstr[l]=letstr[l]+32;
}
}
return letstr;
}
int getMonFromAbbr(char *abbr){
if (strlen(abbr) > 0)
tolowercase(abbr);
if ( strcmp(abbr, "jan") == 0 )
return 0;
if ( strcmp(abbr, "feb") == 0 )
return 1;
if ( strcmp(abbr, "mar") == 0 )
return 2;
if ( strcmp(abbr, "apr") == 0 )
return 3;
if ( strcmp(abbr, "may") == 0 )
return 4;
if ( strcmp(abbr, "jun") == 0 )
return 5;
if ( strcmp(abbr, "jul") == 0 )
return 6;
if ( strcmp(abbr, "aug") == 0 )
return 7;
if ( strcmp(abbr, "sep") == 0 )
return 8;
if ( strcmp(abbr, "oct") == 0 )
return 9;
if ( strcmp(abbr, "nov") == 0 )
return 10;
if ( strcmp(abbr, "dec") == 0 )
return 11;
return(-1);
}
// Convert from __DATE__ macro
uint64_t getCompileTimeEpoch(){
char date_macro[20]="";
strcpy(date_macro, __DATE__);
char *token;
int yea=0;
int mon=0;
int day=0;
token=strtok(date_macro, " ");
if (token != NULL){
mon=getMonFromAbbr(token);
token=strtok(NULL, " ");
if (token != NULL){
day=atoi(token);
token=strtok(NULL, " ");
if (token != NULL){
yea=atoi(token);
struct tm t;
time_t epoch_t;
t.tm_year = yea-1900; // Year - 1900
t.tm_mon = mon; // Month, where 0 = jan
t.tm_mday = day; // Day of the month
t.tm_hour = 0;
t.tm_min = 0;
t.tm_sec = 0;
t.tm_isdst = 0; // Is DST on? 1 = yes, 0 = no, -1 = unknown
epoch_t = mktime(&t);
return epoch_t;
}
}
}
return(-1);
}