[PATCH] __DATE__ & __TIME expansion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

So says C99.

Signed-off-by: Damien Lespiau <damien.lespiau@xxxxxxxxx>
---
 ident-list.h                        |    2 ++
 lib.c                               |    2 --
 pre-process.c                       |   11 +++++++++++
 validation/preprocessor-date-time.c |   22 ++++++++++++++++++++++
 4 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 validation/preprocessor-date-time.c

diff --git a/ident-list.h b/ident-list.h
index 7633a2f..dad22ef 100644
--- a/ident-list.h
+++ b/ident-list.h
@@ -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);
diff --git a/lib.c b/lib.c
index 2162007..b678990 100644
--- a/lib.c
+++ b/lib.c
@@ -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");
diff --git a/pre-process.c b/pre-process.c
index afae77a..eb291f2 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -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' */
+	time_t t;
 
 	if (token->pos.noexpand)
 		return 1;
@@ -154,6 +157,14 @@ 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) {
+		time(&t);
+		strftime(buffer, 12, "%b %e %Y", localtime(&t));
+		replace_with_string(token, buffer);
+	} else if (token->ident == &__TIME___ident) {
+		time(&t);
+		strftime(buffer, 9, "%T", localtime(&t));
+		replace_with_string(token, buffer);
 	}
 	return 1;
 }
diff --git a/validation/preprocessor-date-time.c b/validation/preprocessor-date-time.c
new file mode 100644
index 0000000..18d8bdb
--- /dev/null
+++ b/validation/preprocessor-date-time.c
@@ -0,0 +1,22 @@
+/*
+ * __DATE__ & __TIME__ macros handling test.
+ */
+
+/*
+ * Testing it against GCC preprocessor in the same second gives:
+ *
+ * $ gcc -E validation/preprocessor-date-time.c 
+ * # 1 "validation/preprocessor-date-time.c"
+ * # 1 "<built-in>"
+ * # 1 "<command line>"
+ * # 1 "validation/preprocessor-date-time.c"
+ * # 11 "validation/preprocessor-date-time.c"
+ * "May 20 2007" "20:19:00"
+ * $ ./cgcc -E validation/preprocessor-date-time.c 
+ * 
+ * "May 20 2007" "20:19:00"
+ * $
+*/
+
+__DATE__ __TIME__
+
-- 
1.5.2.rc3.87.g404fd


-
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux