aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/timer.h2
-rw-r--r--src/msvmmaj_io.c36
-rw-r--r--src/timer.c51
3 files changed, 55 insertions, 34 deletions
diff --git a/include/timer.h b/include/timer.h
index d4d4d23..d4af649 100644
--- a/include/timer.h
+++ b/include/timer.h
@@ -16,4 +16,6 @@
double elapsed_time(clock_t s_time, clock_t e_time);
+void get_time_string(char *buffer);
+
#endif
diff --git a/src/msvmmaj_io.c b/src/msvmmaj_io.c
index 7abb182..fc7cc56 100644
--- a/src/msvmmaj_io.c
+++ b/src/msvmmaj_io.c
@@ -10,12 +10,11 @@
*
*/
-#include <time.h>
-
#include "msvmmaj.h"
#include "msvmmaj_io.h"
#include "msvmmaj_matrix.h"
#include "strutil.h"
+#include "timer.h"
/**
* @brief Read data from file
@@ -215,10 +214,7 @@ void msvmmaj_write_model(struct MajModel *model, char *output_filename)
{
FILE *fid;
long i, j;
- int diff, hours, minutes;
- char timestr[1000];
- time_t current_time, lt, gt;
- struct tm *lclt;
+ char timestr[MAX_LINE_LENGTH];
// open output file
fid = fopen(output_filename, "w");
@@ -227,35 +223,11 @@ void msvmmaj_write_model(struct MajModel *model, char *output_filename)
output_filename);
exit(1);
}
-
- // get current time (in epoch)
- current_time = time(NULL);
- if (current_time == ((time_t)-1)) {
- fprintf(stderr, "Failed to compute the current time.\n");
- exit(1);
- }
-
- // convert time to local time and create a string
- lclt = localtime(&current_time);
- strftime(timestr, 1000, "%c", lclt);
- if (timestr == NULL) {
- fprintf(stderr, "Failed to convert time to string.\n");
- exit(1);
- }
-
- // calculate the difference from UTC 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++;
+ get_time_string(timestr);
// Write output to file
fprintf(fid, "Output file for MSVMMaj (version %1.1f)\n", VERSION);
- fprintf(fid, "Generated on: %s (UTC %+03i:%02i)\n\n",
- timestr, hours, minutes);
+ fprintf(fid, "Generated on: %s\n\n", timestr);
fprintf(fid, "Model:\n");
fprintf(fid, "p = %15.16f\n", model->p);
fprintf(fid, "lambda = %15.16f\n", model->lambda);
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);
+}