[PATCH 1/2] Allow user to select date format on manual CSV import

Miika Turkia miika.turkia at gmail.com
Tue Nov 25 09:47:04 PST 2014


Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
 dive.h                        |  2 +-
 file.c                        |  8 +++--
 qt-ui/divelogimportdialog.cpp |  2 ++
 qt-ui/divelogimportdialog.ui  | 72 ++++++++++++++++++++++++++++---------------
 xslt/manualcsv2xml.xslt       | 41 +++++++++++++++++++++---
 5 files changed, 93 insertions(+), 32 deletions(-)

diff --git a/dive.h b/dive.h
index 0ddc43f..95f4d6f 100644
--- a/dive.h
+++ b/dive.h
@@ -631,7 +631,7 @@ extern int parse_file(const char *filename);
 extern int parse_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int sepidx, const char *csvtemplate, int units);
 extern int parse_seabear_csv_file(const char *filename, int time, int depth, int temp, int po2f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int sepidx, const char *csvtemplate, int units);
 extern int parse_txt_file(const char *filename, const char *csv);
-extern int parse_manual_file(const char *filename, int separator_index, int units, int number, int date, int time, int duration, int location, int gps, int maxdepth, int meandepth, int buddy, int notes, int weight, int tags);
+extern int parse_manual_file(const char *filename, int separator_index, int units, int dateformat, int number, int date, int time, int duration, int location, int gps, int maxdepth, int meandepth, int buddy, int notes, int weight, int tags);
 
 extern int save_dives(const char *filename);
 extern int save_dives_logic(const char *filename, bool select_only);
diff --git a/file.c b/file.c
index 07774b1..90f9545 100644
--- a/file.c
+++ b/file.c
@@ -943,11 +943,11 @@ int parse_seabear_csv_file(const char *filename, int timef, int depthf, int temp
 	return 0;
 }
 
-int parse_manual_file(const char *filename, int sepidx, int units, int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf, int buddyf, int notesf, int weightf, int tagsf)
+int parse_manual_file(const char *filename, int sepidx, int units, int dateformat, int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf, int buddyf, int notesf, int weightf, int tagsf)
 {
 	struct memblock mem;
 	int pnr = 0;
-	char *params[33];
+	char *params[35];
 	char numberbuf[MAXCOLDIGITS];
 	char datebuf[MAXCOLDIGITS];
 	char timebuf[MAXCOLDIGITS];
@@ -962,6 +962,7 @@ int parse_manual_file(const char *filename, int sepidx, int units, int numberf,
 	char tagsbuf[MAXCOLDIGITS];
 	char separator_index[MAXCOLDIGITS];
 	char unit[MAXCOLDIGITS];
+	char datefmt[MAXCOLDIGITS];
 	time_t now;
 	struct tm *timep;
 	char curdate[9];
@@ -984,6 +985,7 @@ int parse_manual_file(const char *filename, int sepidx, int units, int numberf,
 	snprintf(tagsbuf, MAXCOLDIGITS, "%d", tagsf);
 	snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx);
 	snprintf(unit, MAXCOLDIGITS, "%d", units);
+	snprintf(datefmt, MAXCOLDIGITS, "%d", dateformat);
 	time(&now);
 	timep = localtime(&now);
 	strftime(curdate, DATESTR, "%Y%m%d", timep);
@@ -1024,6 +1026,8 @@ int parse_manual_file(const char *filename, int sepidx, int units, int numberf,
 	params[pnr++] = separator_index;
 	params[pnr++] = "units";
 	params[pnr++] = unit;
+	params[pnr++] = "datefmt";
+	params[pnr++] = datefmt;
 	params[pnr++] = NULL;
 
 	if (filename == NULL)
diff --git a/qt-ui/divelogimportdialog.cpp b/qt-ui/divelogimportdialog.cpp
index 91c1d73..308539e 100644
--- a/qt-ui/divelogimportdialog.cpp
+++ b/qt-ui/divelogimportdialog.cpp
@@ -107,6 +107,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
 			parse_manual_file(fileNames[i].toUtf8().data(),
 					  ui->ManualSeparator->currentIndex(),
 					  ui->Units->currentIndex(),
+                                          ui->DateFormat->currentIndex(),
 					  VALUE_IF_CHECKED(DiveNumber),
 					  VALUE_IF_CHECKED(Date), VALUE_IF_CHECKED(Time),
 					  VALUE_IF_CHECKED(Duration), VALUE_IF_CHECKED(Location),
@@ -163,3 +164,4 @@ void DiveLogImportDialog::unknownImports()
 	if (!specialCSV.contains(ui->knownImports->currentIndex()))
 		ui->knownImports->setCurrentIndex(0);
 }
+
diff --git a/qt-ui/divelogimportdialog.ui b/qt-ui/divelogimportdialog.ui
index 9fd5e44..c68e510 100644
--- a/qt-ui/divelogimportdialog.ui
+++ b/qt-ui/divelogimportdialog.ui
@@ -296,6 +296,53 @@
        <string>Manual dives</string>
       </attribute>
       <layout class="QGridLayout" name="gridLayout_4">
+       <item row="0" column="1">
+        <widget class="QGroupBox" name="groupBox_5">
+         <property name="title">
+          <string>Field separator</string>
+         </property>
+         <layout class="QHBoxLayout" name="horizontalLayout">
+          <item>
+           <widget class="QComboBox" name="ManualSeparator"/>
+          </item>
+         </layout>
+        </widget>
+       </item>
+       <item row="2" column="1">
+        <widget class="QGroupBox" name="groupBox_8">
+         <property name="toolTip">
+          <string>Choose day, month and year order for date format. The field separator can be any of the characters .-/.</string>
+         </property>
+         <property name="title">
+          <string>Date format</string>
+         </property>
+         <widget class="QComboBox" name="DateFormat">
+          <property name="geometry">
+           <rect>
+            <x>20</x>
+            <y>40</y>
+            <width>131</width>
+            <height>27</height>
+           </rect>
+          </property>
+          <item>
+           <property name="text">
+            <string>dd.mm.yyyy</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>mm/dd/yyyy</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>yyyy-mm-dd</string>
+           </property>
+          </item>
+         </widget>
+        </widget>
+       </item>
        <item row="1" column="1">
         <widget class="QGroupBox" name="groupBox_6">
          <property name="title">
@@ -544,31 +591,6 @@
          </layout>
         </widget>
        </item>
-       <item row="0" column="1">
-        <widget class="QGroupBox" name="groupBox_5">
-         <property name="title">
-          <string>Field separator</string>
-         </property>
-         <layout class="QHBoxLayout" name="horizontalLayout">
-          <item>
-           <widget class="QComboBox" name="ManualSeparator"/>
-          </item>
-         </layout>
-        </widget>
-       </item>
-       <item row="2" column="1">
-        <spacer name="verticalSpacer_2">
-         <property name="orientation">
-          <enum>Qt::Vertical</enum>
-         </property>
-         <property name="sizeHint" stdset="0">
-          <size>
-           <width>20</width>
-           <height>40</height>
-          </size>
-         </property>
-        </spacer>
-       </item>
       </layout>
      </widget>
     </widget>
diff --git a/xslt/manualcsv2xml.xslt b/xslt/manualcsv2xml.xslt
index 7e138d1..cbfb9ea 100644
--- a/xslt/manualcsv2xml.xslt
+++ b/xslt/manualcsv2xml.xslt
@@ -18,6 +18,7 @@
   <xsl:param name="buddyField" select="buddyField"/>
   <xsl:param name="notesField" select="notesField"/>
   <xsl:param name="weightField" select="weightField"/>
+  <xsl:param name="dateformat" select="dateformat"/>
   <xsl:output method="xml" indent="yes"/>
 
   <xsl:variable name="lf"><xsl:text>
@@ -79,10 +80,42 @@
       <xsl:attribute name="date">
         <xsl:choose>
           <xsl:when test="$dateField >= 0">
-            <xsl:call-template name="getFieldByIndex">
-              <xsl:with-param name="index" select="$dateField"/>
-              <xsl:with-param name="line" select="$line"/>
-            </xsl:call-template>
+            <xsl:variable name="indate">
+              <xsl:call-template name="getFieldByIndex">
+                <xsl:with-param name="index" select="$dateField"/>
+                <xsl:with-param name="line" select="$line"/>
+              </xsl:call-template>
+            </xsl:variable>
+            <xsl:variable name="separator">
+              <xsl:choose>
+                <xsl:when test="substring-before($indate, '.') != ''">
+                  <xsl:value-of select="'.'"/>
+                </xsl:when>
+                <xsl:when test="substring-before($indate, '-') != ''">
+                  <xsl:value-of select="'-'"/>
+                </xsl:when>
+                <xsl:when test="substring-before($indate, '/') != ''">
+                  <xsl:value-of select="'/'"/>
+                </xsl:when>
+              </xsl:choose>
+            </xsl:variable>
+            <xsl:choose>
+              <!-- dd.mm.yyyy -->
+              <xsl:when test="$datefmt = 0">
+                <xsl:value-of select="concat(substring-after(substring-after($indate, $separator), $separator), '-', substring-before(substring-after($indate, $separator), $separator), '-', substring-before($indate, $separator))"/>
+              </xsl:when>
+              <!-- mm.yy.yyyy -->
+              <xsl:when test="$datefmt = 1">
+                <xsl:value-of select="concat(substring-after(substring-after($indate, $separator), $separator), '-', substring-before($indate, $separator), '-', substring-before(substring-after($indate, $separator), $separator))"/>
+              </xsl:when>
+              <!-- yyyy.mm.dd -->
+              <xsl:when test="$datefmt = 2">
+                <xsl:value-of select="concat(substring-before($indate, $separator), '-', substring-before(substring-after($indate, $separator), $separator), '-', substring-after(substring-after($indate, $separator), $separator))"/>
+              </xsl:when>
+              <xsl:otherwise>
+                <xsl:value-of select="'1900-1-1'"/>
+              </xsl:otherwise>
+            </xsl:choose>
           </xsl:when>
           <xsl:otherwise>
             <xsl:value-of select="concat(substring($date, 1, 4), '-', substring($date, 5, 2), '-', substring($date, 7, 2))"/>
-- 
1.9.1



More information about the subsurface mailing list