diff --git a/preprocess.c b/preprocess.c index 715830d..7b40774 100644 --- a/preprocess.c +++ b/preprocess.c @@ -893,6 +893,20 @@ static Token *counter_macro(Token *tmpl) { return new_num_token(i++, tmpl); } +// __TIMESTAMP__ is expanded to a string describing the last +// modification time of the current file. E.g. +// "Fri Jul 24 01:32:50 2020" +static Token *timestamp_macro(Token *tmpl) { + struct stat st; + if (stat(tmpl->file->name, &st) != 0) + return new_str_token("??? ??? ?? ??:??:?? ????", tmpl); + + char buf[30]; + ctime_r(&st.st_mtime, buf); + buf[24] = '\0'; + return new_str_token(buf, tmpl); +} + // __DATE__ is expanded to the current date, e.g. "May 17 2020". static char *format_date(struct tm *tm) { static char mon[][4] = { @@ -957,6 +971,7 @@ void init_macros(void) { add_builtin("__FILE__", file_macro); add_builtin("__LINE__", line_macro); add_builtin("__COUNTER__", counter_macro); + add_builtin("__TIMESTAMP__", timestamp_macro); time_t now = time(NULL); struct tm *tm = localtime(&now); diff --git a/test/macro.c b/test/macro.c index 700865d..1f8f13d 100644 --- a/test/macro.c +++ b/test/macro.c @@ -373,6 +373,8 @@ int main() { ASSERT(1, __COUNTER__); ASSERT(2, __COUNTER__); + ASSERT(24, strlen(__TIMESTAMP__)); + printf("OK\n"); return 0; }