Skip to content

Commit

Permalink
fix unicsv reader truncation differences on i386/debian:sid
Browse files Browse the repository at this point in the history
with 1.9.0 debian had some test failures on i386.
in the unicsv test with reference unicsv_subsecond.csv
13:20:20.060 was echoed as 13:20:20.059.
13:20:21.600 was echoed as 13:20:21.599.

These variations aren't serious, but this PR uses an integer arithmetic
conversion instead of a floating point arithmetic to avoid any variation
in the intentional truncation to milliseconds.
  • Loading branch information
tsteven4 committed Oct 27, 2023
1 parent c95e1cf commit e5b3093
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
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);
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

0 comments on commit e5b3093

Please sign in to comment.