__DATE__ & __TIME expansion v2
PATCH 1/1 __DATE__ & __TIME expansion v2
Makes __DATE__expand to a character string literal of the form "Mmm dd yyyy" where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10.
Makes __TIME__ expand to a a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function.
--- ident-list.h | 2 ++ lib.c | 2 -- pre-process.c | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-)
ident-list.h +2 -0
@@ -75,6 +75,8 @@ __IDENT(pragma_ident, "__pragma__", 0);__IDENT (__VA_ARGS___ident,"__VA_ARGS__" ,0 );__IDENT (__LINE___ident,"__LINE__" ,0 );__IDENT (__FILE___ident,"__FILE__" ,0 ); +__IDENT (__DATE___ident,"__DATE__" ,0 ); +__IDENT (__TIME___ident,"__TIME__" ,0 );__IDENT (__func___ident,"__func__" ,0 );__IDENT (__FUNCTION___ident,"__FUNCTION__" ,0 );__IDENT (__PRETTY_FUNCTION___ident,"__PRETTY_FUNCTION__" ,0 );
lib.c +0 -2
@@ -602,8 +602,6 @@ void create_builtin_stream(void)/* FIXME! We need to do these as special magic macros at expansion time! */ add_pre_buffer ("#define __BASE_FILE__ \"base_file.c\"\n" ); -add_pre_buffer ("#define __DATE__ \"??? ?? ????\"\n" ); -add_pre_buffer ("#define __TIME__ \"??:??:??\"\n" );if (optimize)add_pre_buffer ("#define __OPTIMIZE__ 1\n" );
pre-process.c +13 -0
@@ -18,6 +18,7 @@#include <unistd.h> #include <fcntl.h> #include <limits.h> +#include <time.h> #include "pre-process.h" #include "lib.h"
@@ -141,6 +142,8 @@ static int expand_one_symbol(struct token **list) {struct token *token = *list;struct symbol *sym; +static char buffer[12 ];/* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */ +static time_t t =0 ;if (token->pos.noexpand)return 1 ;
@@ -154,6 +157,16 @@ static int expand_one_symbol(struct token **list)replace_with_integer (token, token->pos.line); }else if (token->ident == &__FILE___ident) {replace_with_string (token,stream_name (token->pos.stream)); + }else if (token->ident == &__DATE___ident) { +if (!t) +time (&t); +strftime (buffer,12 ,"%b %e %Y" ,localtime (&t)); +replace_with_string (token, buffer); + }else if (token->ident == &__TIME___ident) { +if (!t) +time (&t); +strftime (buffer,9 ,"%T" ,localtime (&t)); +replace_with_string (token, buffer); }return 1 ; }
Signature · 1 line
1.5.2.rc3.87.g404fd
Josh Triplett
AppliedQuoted 8 lines · Damien Lespiau
Makes
__DATE__expandto a character string literal of the form "Mmm dd yyyy" where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10.
Makes__TIME__expand to a a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function.
Applied, thanks!
$ ./sparse -E /dev/stdin <<<'__DATE__ __TIME__' ; date
"May 20 2007" "17:39:38"
Sun May 20 17:39:38 PDT 2007
- Josh Triplett