aboutsummaryrefslogtreecommitdiff
path: root/src/gensvm_io.c
diff options
context:
space:
mode:
authorGertjan van den Burg <gertjanvandenburg@gmail.com>2016-05-16 18:47:09 +0200
committerGertjan van den Burg <gertjanvandenburg@gmail.com>2016-05-16 18:47:09 +0200
commit044dc5a93c33d7aa4c9c98a626890c16446a56fc (patch)
tree23cc17a595d36a35ad9cb50e3ab18c2956b5f65c /src/gensvm_io.c
parentMove includes to header (diff)
downloadgensvm-044dc5a93c33d7aa4c9c98a626890c16446a56fc.tar.gz
gensvm-044dc5a93c33d7aa4c9c98a626890c16446a56fc.zip
major refactor of the code
Diffstat (limited to 'src/gensvm_io.c')
-rw-r--r--src/gensvm_io.c58
1 files changed, 50 insertions, 8 deletions
diff --git a/src/gensvm_io.c b/src/gensvm_io.c
index c4798d8..696f46f 100644
--- a/src/gensvm_io.c
+++ b/src/gensvm_io.c
@@ -6,16 +6,12 @@
*
* @details
* This file contains functions for reading and writing model files, and data
- * files.
- *
+ * files. It also contains a function for generating a string of the current
+ * time, used in writing output files.
+*
*/
-#include "globals.h"
-#include "gensvm.h"
#include "gensvm_io.h"
-#include "gensvm_matrix.h"
-#include "gensvm_strutil.h"
-#include "gensvm_timer.h"
/**
* @brief Read data from file
@@ -226,7 +222,7 @@ void gensvm_write_model(struct GenModel *model, char *output_filename)
output_filename);
exit(1);
}
- get_time_string(timestr);
+ gensvm_time_string(timestr);
// Write output to file
fprintf(fid, "Output file for GenSVM (version %1.1f)\n", VERSION);
@@ -298,3 +294,49 @@ void gensvm_write_predictions(struct GenData *data, long *predy,
fclose(fid);
}
+
+/**
+ * @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 gensvm_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);
+}