[GNU] Add __TIMESTAMP__ macro

This commit is contained in:
Rui Ueyama 2020-07-22 21:48:49 +09:00
parent aaf20fb96e
commit 922604ae1e
2 changed files with 17 additions and 0 deletions

View File

@ -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);

View File

@ -373,6 +373,8 @@ int main() {
ASSERT(1, __COUNTER__);
ASSERT(2, __COUNTER__);
ASSERT(24, strlen(__TIMESTAMP__));
printf("OK\n");
return 0;
}