Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unicsv reader truncation differences on i386/debian #1201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions reference/unicsv_subsec.csv
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ No,Latitude,Longitude,Name,Date,Time
9,33.784805,-117.474327,"WPT009",2014/09/17,13:20:22
10,33.784805,-117.474327,"WPT010",2014/09/17,13:20:22.200
11,33.784805,-117.474327,"WPT011",2014/09/17,13:20:22.400
12,33.784805,-117.474327,"WPT012",2014/09/17,13:20:23.699
13,33.784805,-117.474327,"WPT013",2014/09/17,13:20:23.69
14,33.784805,-117.474327,"WPT014",2014/09/17,13:20:23.6
15,33.784805,-117.474327,"WPT015",2014/09/17,13:20:23
16,33.784805,-117.474327,"WPT016",2014/09/17,13:20:23.
17,33.784805,-117.474327,"WPT017",2014/09/17,13:20:23.69
18,33.784805,-117.474327,"WPT018",2014/09/17,13:20:23.699
19,33.784805,-117.474327,"WPT019",2014/09/17,13:20:23.6999
20,33.784805,-117.474327,"WPT020",2014/09/17,13:20:23.69999
21,33.784805,-117.474327,"WPT021",2014/09/17,13:20:23.699999
22,33.784805,-117.474327,"WPT022",2014/09/17,13:20:23.6999999
23 changes: 23 additions & 0 deletions reference/unicsv_subsec~csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
No,Latitude,Longitude,Name,Date,Time
1,33.784801,-117.474350,"WPT001",2014/09/17,13:20:20.004
2,33.784801,-117.474342,"WPT002",2014/09/17,13:20:20.060
3,33.784801,-117.474342,"WPT003",2014/09/17,13:20:20.800
4,33.784801,-117.474342,"WPT004",2014/09/17,13:20:21
5,33.784801,-117.474342,"WPT005",2014/09/17,13:20:21.200
6,33.784801,-117.474335,"WPT006",2014/09/17,13:20:21.400
7,33.784801,-117.474335,"WPT007",2014/09/17,13:20:21.600
8,33.784801,-117.474335,"WPT008",2014/09/17,13:20:21.800
9,33.784805,-117.474327,"WPT009",2014/09/17,13:20:22
10,33.784805,-117.474327,"WPT010",2014/09/17,13:20:22.200
11,33.784805,-117.474327,"WPT011",2014/09/17,13:20:22.400
12,33.784805,-117.474327,"WPT012",2014/09/17,13:20:23.699
13,33.784805,-117.474327,"WPT013",2014/09/17,13:20:23.690
14,33.784805,-117.474327,"WPT014",2014/09/17,13:20:23.600
15,33.784805,-117.474327,"WPT015",2014/09/17,13:20:23
16,33.784805,-117.474327,"WPT016",2014/09/17,13:20:23
17,33.784805,-117.474327,"WPT017",2014/09/17,13:20:23.690
18,33.784805,-117.474327,"WPT018",2014/09/17,13:20:23.699
19,33.784805,-117.474327,"WPT019",2014/09/17,13:20:23.699
20,33.784805,-117.474327,"WPT020",2014/09/17,13:20:23.699
21,33.784805,-117.474327,"WPT021",2014/09/17,13:20:23.699
22,33.784805,-117.474327,"WPT022",2014/09/17,13:20:23.699
2 changes: 1 addition & 1 deletion testo.d/unicsv.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ compare ${TMPDIR}/unicsv_encoding.csv ${REFERENCE}/unicsv_encoding.csv

# verify fractional second read and writes.
gpsbabel -i unicsv -f ${REFERENCE}/unicsv_subsec.csv -o unicsv -F ${TMPDIR}/unicsv_subsec.csv
compare ${REFERENCE}/unicsv_subsec.csv ${TMPDIR}/unicsv_subsec.csv
compare ${REFERENCE}/unicsv_subsec~csv.csv ${TMPDIR}/unicsv_subsec.csv

# Verify 'fields' option
gpsbabel -i unicsv,fields=lat+lon+description -f ${REFERENCE}/radius.csv -o csv -F ${TMPDIR}/unicsv_fields.out
Expand Down
8 changes: 5 additions & 3 deletions unicsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ UnicsvFormat::unicsv_parse_time(const char* str, QDate& date)
int sec;
int msec;
int consumed = 0;
double frac_sec;

/* If we have something we're pretty sure is a date, parse that
* first, skip over it, and pass that back to the caller)
Expand All @@ -297,14 +296,17 @@ UnicsvFormat::unicsv_parse_time(const char* str, QDate& date)
str += consumed;
date = ldate;
}
int ct = sscanf(str, "%d%*1[.://]%d%*1[.://]%d%lf", &hour, &min, &sec, &frac_sec);
char msecstr[4];
int ct = sscanf(str, "%d%*1[.://]%d%*1[.://]%d.%3[0123456789]", &hour, &min, &sec, msecstr);
Copy link
Collaborator

@robertlipe robertlipe Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you're tweaking that's there, but this passes testo:

@@ -310,6 +311,11 @@ UnicsvFormat::unicsv_parse_time(const char* str, QDate& date)
   }

   QTime result{hour, min, sec, msec};
+#if 1
+  QTime qt = QTime::fromString(QString(str).section(" ", 0, 0).trimmed(), Qt::ISODateWithMs);
+  qDebug() << result << qt;
+  assert(result == qt);
+#endif
   if (!result.isValid()) {
     fatal(FatalMsg() << MYNAME << ": Invalid time parsed from string (" << str << ").");
   }
 
 
 Which makes this a drop-in replacement for the original:
 diff --git a/unicsv.cc b/unicsv.cc
index 92ca8504..5a24b85f 100644
--- a/unicsv.cc
+++ b/unicsv.cc
@@ -282,12 +282,14 @@ UnicsvFormat::unicsv_parse_date(const char* str, int* consumed)
 QTime
 UnicsvFormat::unicsv_parse_time(const char* str, QDate& date)
 {
+#if 0
   int hour;
   int min;
   int sec;
   int msec;
-  int consumed = 0;
   double frac_sec;
+#endif
+  int consumed = 0;
 
   /* If we have something we're pretty sure is a date, parse that
    * first, skip over it, and pass that back to the caller)
@@ -297,6 +299,8 @@ UnicsvFormat::unicsv_parse_time(const char* str, QDate& date)
     str += consumed;
     date = ldate;
   }
+#if 0
+ qDebug() << str;
   int ct = sscanf(str, "%d%*1[.://]%d%*1[.://]%d%lf", &hour, &min, &sec, &frac_sec);
   if (ct < 3) {
     fatal(FatalMsg() << MYNAME << ": Could not parse time string (" << str << ").");
@@ -310,6 +314,11 @@ UnicsvFormat::unicsv_parse_time(const char* str, QDate& date)
   }
 
   QTime result{hour, min, sec, msec};
+#else
+  QTime result;
+
+  result = QTime::fromString(QString(str).trimmed().section(" ", 0, 0), Qt::ISODateWithMs);
+#endif
   if (!result.isValid()) {
     fatal(FatalMsg() << MYNAME << ": Invalid time parsed from string (" << str << ").");
   }

if (ct < 3) {
fatal(FatalMsg() << MYNAME << ": Could not parse time string (" << str << ").");
}
if (ct >= 4) {
// Don't round up and ripple through seconds, minutes, hours.
// 23:59:59.9999999 -> 24:00:00.000 which is an invalid QTime.
msec = frac_sec * 1000.0;
QByteArray msecba = msecstr;
msecba.append(3 - msecba.size(), '0'); // right pad to 3 digits.
msec = msecba.toInt();
} else {
msec = 0;
}
Expand Down