From f62b1f5500a183495e1e80d1485d2a783a37c2b2 Mon Sep 17 00:00:00 2001 From: tradke Date: Fri, 28 Jul 2006 09:57:00 +0000 Subject: Add new flesh API functions CCTK_CompileDateTime() and Util_CurrentDateTime() to return a date/time stamp following the ISO 8601 standard format if possible (see http://www.cactuscode.org/old/pipermail/developers/2006-July/004971.html). Add the timezone information in the time stamp returned by Util_CurrentTime(). You also need to update lib/make/make.configuration now in order to compile src/datestamp.c. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4358 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/datestamp.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/include/cctk_Misc.h | 1 + src/include/cctk_Version.h | 2 ++ src/util/Time.c | 49 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/datestamp.c b/src/datestamp.c index bc73cfd3..7f0dd7b8 100644 --- a/src/datestamp.c +++ b/src/datestamp.c @@ -8,6 +8,7 @@ #version $Id$ @@*/ +#include #include #include @@ -92,6 +93,52 @@ const char *CCTK_CompileDate (void) #define REALSTRINGIFY(a) #a + /*@@ + @routine CCTK_CompileDateTime + @date 21 July 2006 + @author Thomas Radke + @desc + Returns a pointer to a formatted string with the datetime stamp + when Cactus was compiled. + The format of the string returned tries to be compatible + to the machine-processable format defined in ISO 8601 chapt. 5.4 + however this depends on the actual date command which returns + the raw datetime string CCTK_COMPILE_DATETIME + in lib/make/make.configuration. + @enddesc + + @returntype const char * + @returndesc + pointer to the static datetime stamp string buffer + @endreturndesc +@@*/ +const char *CCTK_CompileDateTime (void) +{ +#define DATETIME_BUFLEN sizeof("YYYY-MM-DDThh:mm:ss+hh:mm") + const int len = DATETIME_BUFLEN; + static char buffer[DATETIME_BUFLEN] = ""; + + if (buffer[0] == 0 && sizeof (STRINGIFY (CCTK_COMPILE_DATETIME)) == len-1) + { + strcpy (buffer, STRINGIFY (CCTK_COMPILE_DATETIME)); + + /* if the timezone part is returned as "(+|-)hhmm" + then turn it into "(+|-)hh:mm" */ + if ((buffer[len-7] == '+' || buffer[len-7] == '-') && + isdigit (buffer[len-6]) && isdigit (buffer[len-5]) && + isdigit (buffer[len-4]) && isdigit (buffer[len-3]) && + buffer[len-2] == 0) + { + buffer[len-2] = buffer[len-3]; + buffer[len-3] = buffer[len-4]; + buffer[len-4] = ':'; + } + } + + return (buffer[0] ? buffer : STRINGIFY (CCTK_COMPILE_DATETIME)); +} + + /*@@ @routine CCTK_FullVersion CCTK_MajorVersion CCTK_MinorVersion @date Mon May 11 10:20:58 1998 diff --git a/src/include/cctk_Misc.h b/src/include/cctk_Misc.h index a51f7a87..6ac16e56 100644 --- a/src/include/cctk_Misc.h +++ b/src/include/cctk_Misc.h @@ -45,6 +45,7 @@ int CCTK_SetBoolean(CCTK_INT *data, const char *value); int CCTK_RunTime(void); int Util_CurrentTime(int len, char *now); int Util_CurrentDate(int len, char *now); +char *Util_CurrentDateTime(void); int CCTK_RunTitle(int len, char *title); diff --git a/src/include/cctk_Version.h b/src/include/cctk_Version.h index d010c9a3..efe1ee59 100644 --- a/src/include/cctk_Version.h +++ b/src/include/cctk_Version.h @@ -33,6 +33,8 @@ const char *CCTK_CompileTime(void); const char *CCTK_CompileDate(void); +const char *CCTK_CompileDateTime(void); + #ifdef __cplusplus } #endif diff --git a/src/util/Time.c b/src/util/Time.c index b902622f..f71b3fdb 100644 --- a/src/util/Time.c +++ b/src/util/Time.c @@ -10,7 +10,9 @@ /* #define DEBUG_TIME */ +#include #include +#include #include #include "cctk_Flesh.h" @@ -45,7 +47,7 @@ int Util_CurrentTime(int len, char *now) { int retval; time_t timep; - const char *fmt = "%X"; + const char *fmt = "%X%z"; timep = time(NULL); strftime(now, len, fmt, localtime(&timep)); @@ -96,3 +98,48 @@ int Util_CurrentDate(int len, char *now) return retval; } + + + /*@@ + @routine Util_CurrentDateTime + @date Wed 19 July 2006 + @author Thomas Radke + @desc + Returns the current datetime in a machine-processable format + as defined in ISO 8601 chapter 5.4. + @enddesc + + @returntype char * + @returndesc + pointer to an allocated string buffer containing the datetime, + must be freed by the user + @endreturndesc +@@*/ +char *Util_CurrentDateTime(void) +{ + char *buffer; + time_t timep; + const int len = sizeof ("YYYY-MM-DDThh:mm:ss+hh:mm"); + const char *fmt = "%Y-%m-%dT%H:%M:%S%z"; + + buffer = calloc (1, len); + if (buffer) + { + timep = time (NULL); + strftime (buffer, len, fmt, localtime (&timep)); + + /* if the timezone part is returned as "(+|-)hhmm" + then turn it into "(+|-)hh:mm" */ + if ((buffer[len-7] == '+' || buffer[len-7] == '-') && + isdigit (buffer[len-6]) && isdigit (buffer[len-5]) && + isdigit (buffer[len-4]) && isdigit (buffer[len-3]) && + buffer[len-2] == 0) + { + buffer[len-2] = buffer[len-3]; + buffer[len-3] = buffer[len-4]; + buffer[len-4] = ':'; + } + } + + return (buffer); +} -- cgit v1.2.3