[PATCH] Add current date to CSV import

Miika Turkia miika.turkia at gmail.com
Fri Oct 18 22:17:13 UTC 2013


Since CSV import does not include date/time stamp, we need to generate
one for ourselves. This patch uses current time of the import as dive
time.

Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
 file.c            | 32 ++++++++++++++++++++++++--------
 xslt/csv2xml.xslt |  8 ++++++++
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/file.c b/file.c
index 39d9a25..4d2382b 100644
--- a/file.c
+++ b/file.c
@@ -6,6 +6,7 @@
 #include <errno.h>
 #include "gettext.h"
 #include <zip.h>
+#include <time.h>
 
 #include "dive.h"
 #include "file.h"
@@ -326,32 +327,47 @@ void parse_file(const char *filename, char **error)
 
 #define MAXCOLDIGITS 3
 #define MAXCOLS 100
-void parse_csv_file(const char *filename, int time, int depth, int temp, char **error)
+void parse_csv_file(const char *filename, int timef, int depthf, int tempf, char **error)
 {
 	struct memblock mem;
-	char *params[7];
+	char *params[11];
 	char timebuf[MAXCOLDIGITS];
 	char depthbuf[MAXCOLDIGITS];
 	char tempbuf[MAXCOLDIGITS];
+	time_t now;
+	struct tm *timep;
+	char curdate[9];
+	char curtime[6];
 
-	if (time >= MAXCOLS || depth >= MAXCOLS || temp >= MAXCOLS) {
+	if (timef >= MAXCOLS || depthf >= MAXCOLS || tempf >= MAXCOLS) {
 		int len = strlen(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d")) + MAXCOLDIGITS;
 		*error = malloc(len);
 		snprintf(*error, len, translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
 
 		return;
 	}
-	snprintf(timebuf, MAXCOLDIGITS, "%d", time);
-	snprintf(depthbuf, MAXCOLDIGITS, "%d", depth);
-	snprintf(tempbuf, MAXCOLDIGITS, "%d", temp);
-
+	snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
+	snprintf(depthbuf, MAXCOLDIGITS, "%d", depthf);
+	snprintf(tempbuf, MAXCOLDIGITS, "%d", tempf);
+	time(&now);
+	timep = localtime(&now);
+	strftime(curdate, sizeof(curdate), "%Y%m%d", timep);
+
+	/* As the parameter is numeric, we need to ensure that the leading zero 
+	* is not discarded during the transform, thus prepend time with 1 */
+	strftime(curtime, sizeof(curtime), "1%H%M", timep);
+;
 	params[0] = "timeField";
 	params[1] = timebuf;
 	params[2] = "depthField";
 	params[3] = depthbuf;
 	params[4] = "tempField";
 	params[5] = tempbuf;
-	params[6] = NULL;
+	params[6] = "date";
+	params[7] = curdate;
+	params[8] = "time";
+	params[9] = curtime;
+	params[10] = NULL;
 
 	if (filename == NULL)
 		return;
diff --git a/xslt/csv2xml.xslt b/xslt/csv2xml.xslt
index f504572..5c9abfe 100644
--- a/xslt/csv2xml.xslt
+++ b/xslt/csv2xml.xslt
@@ -5,6 +5,8 @@
   <xsl:param name="timeField" select="timeField"/>
   <xsl:param name="depthField" select="depthField"/>
   <xsl:param name="tempField" select="tempField"/>
+  <xsl:param name="date" select="date"/>
+  <xsl:param name="time" select="time"/>
   <xsl:output method="xml" indent="yes"/>
 
   <xsl:variable name="lf"><xsl:text>
@@ -15,6 +17,12 @@
     <divelog program="subsurface-import" version="2">
       <dives>
         <dive>
+          <xsl:attribute name="date">
+            <xsl:value-of select="concat(substring($date, 1, 4), '-', substring($date, 5, 2), '-', substring($date, 7, 2))"/>
+          </xsl:attribute>
+          <xsl:attribute name="time">
+            <xsl:value-of select="concat(substring($time, 2, 2), ':', substring($time, 4, 2))"/>
+          </xsl:attribute>
           <divecomputerid deviceid="ffffffff" model="stone" />
           <xsl:call-template name="printLine">
             <xsl:with-param name="line" select="substring-before(//csv, $lf)"/>
-- 
1.8.1.2



More information about the subsurface mailing list