aboutsummaryrefslogtreecommitdiff
path: root/src/timer.c
diff options
context:
space:
mode:
authorGertjan van den Burg <burg@ese.eur.nl>2014-01-24 14:25:19 +0100
committerGertjan van den Burg <burg@ese.eur.nl>2014-01-24 14:25:19 +0100
commit34af6bec60dc6ee52aba4900f1d2e29734cb5c17 (patch)
tree00db16549853063479325c8993aa20b6309170b2 /src/timer.c
parentmoved input/output functions to seperate file (diff)
downloadgensvm-34af6bec60dc6ee52aba4900f1d2e29734cb5c17.tar.gz
gensvm-34af6bec60dc6ee52aba4900f1d2e29734cb5c17.zip
move time string function to timer.c
Diffstat (limited to 'src/timer.c')
-rw-r--r--src/timer.c51
1 files changed, 49 insertions, 2 deletions
diff --git a/src/timer.c b/src/timer.c
index 3a763a0..254f3da 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -2,11 +2,12 @@
* @file timer.c
* @author Gertjan van den Burg
* @date January, 2014
- * @brief Function for calculating time difference
+ * @brief Utility functions relating to time
*
* @details
* This file contains a simple function for calculating the time in seconds
- * elapsed between two clock() calls.
+ * elapsed between two clock() calls. It also contains a function for
+ * generating a string of the current time, used in writing output files.
*/
#include <time.h>
@@ -24,3 +25,49 @@ double elapsed_time(clock_t s_time, clock_t e_time)
{
return ((double) (e_time - s_time))/((double) CLOCKS_PER_SEC);
}
+
+/**
+ * @brief Get time string with UTC offset
+ *
+ * @details
+ * Create a string for the current system time. Include an offset of UTC for
+ * consistency. The format of the generated string is "DDD MMM D HH:MM:SS
+ * YYYY (UTC +HH:MM)", e.g. "Fri Aug 9, 12:34:56 2013 (UTC +02:00)".
+ *
+ * @param[in,out] buffer allocated string buffer, on exit contains
+ * formatted string
+ *
+ */
+void get_time_string(char *buffer)
+{
+ int diff, hours, minutes;
+ char timestr[MAX_LINE_LENGTH];
+ time_t current_time, lt, gt;
+ struct tm *lclt;
+
+ // get current time (in epoch)
+ current_time = time(NULL);
+ if (current_time == ((time_t)-1)) {
+ fprintf(stderr, "Failed to compute the current time.\n");
+ return;
+ }
+
+ // convert time to local time and create a string
+ lclt = localtime(&current_time);
+ strftime(timestr, MAX_LINE_LENGTH, "%c", lclt);
+ if (timestr == NULL) {
+ fprintf(stderr, "Failed to convert time to string.\n");
+ return;
+ }
+
+ // calculate the UTC offset including DST
+ lt = mktime(localtime(&current_time));
+ gt = mktime(gmtime(&current_time));
+ diff = -difftime(gt, lt);
+ hours = (diff/3600);
+ minutes = (diff%3600)/60;
+ if (lclt->tm_isdst == 1)
+ hours++;
+
+ sprintf(buffer, "%s (UTC %+03i:%02i)", timestr, hours, minutes);
+}