]> git.sesse.net Git - pgn-extract/commitdiff
Output timestamps in the binary format.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 23:01:18 +0000 (00:01 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 12 Dec 2014 21:56:56 +0000 (22:56 +0100)
output.c

index 8a95f2a2540fb4a53dc2a1583a0819c20d9aa5e7..bc19a4a84894e7e0801c363d5ada0f6c734ca7f2 100644 (file)
--- a/output.c
+++ b/output.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <time.h>
 #include "bool.h"
 #include "defs.h"
 #include "typedef.h"
@@ -1205,6 +1206,28 @@ output_sesse_bin_game(Game current_game,FILE *outputfile,
     int white_elo = atoi(white_elo_tag);
     int black_elo = atoi(black_elo_tag);
 
+    // Parse date and time, if it exists. Set invalid dates to year 3000.
+    const char *date_tag = current_game.tags[DATE_TAG];
+    const char *time_tag = current_game.tags[TIME_TAG];
+    struct tm tm = {0};
+    time_t timestamp;
+    int year, month, day;
+    if (date_tag && sscanf(date_tag, "%u.%u.%u", &year, &month, &day) == 3) {
+        int hour, minute, second;
+        tm.tm_year = year - 1900;
+        tm.tm_mon = month - 1;
+        tm.tm_mday = day;
+
+        if (time_tag && sscanf(time_tag, "%u:%u:%u", &hour, &minute, &second) == 3) {
+            tm.tm_hour = hour;
+            tm.tm_min = minute;
+            tm.tm_sec = second;
+        }
+        timestamp = mktime(&tm);
+    } else {
+        timestamp = 32503680000;
+    }
+
     for (move = current_game.moves; move != NULL; move = move->next) {
         unsigned int opening = move->eco ? move->eco->cumulative_hash_value : 0;  // Truncate to 32 bits.
 
@@ -1218,6 +1241,7 @@ output_sesse_bin_game(Game current_game,FILE *outputfile,
         fwrite(&white_elo, sizeof(white_elo), 1, outputfile);
         fwrite(&black_elo, sizeof(black_elo), 1, outputfile);
         fwrite(&opening, sizeof(opening), 1, outputfile);
+        fwrite(&timestamp, sizeof(timestamp), 1, outputfile);
     }
 }