Skip to content

Commit eda8d95

Browse files
fwrite respects dec=',' for time columns when needed
1 parent 02ced80 commit eda8d95

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
1. Using `print.data.table()` with character truncation using `datatable.prettyprint.char` no longer errors with `NA` entries, [#6441](https://github.com/Rdatatable/data.table/issues/6441). Thanks to @r2evans for the bug report, and @joshhwuu for the fix.
1212

13+
2. `fwrite()` respects `dec=','` for timestamp columns (`POSIXct` or `nanotime`) with sub-second accuracy, [#6446](https://github.com/Rdatatable/data.table/issues/6446). Thanks @kav2k for pointing out the inconsistency and @MichaelChirico for the PR.
14+
1315
## NOTES
1416

1517
1. Tests run again when some Suggests packages are missing, [#6411](https://github.com/Rdatatable/data.table/issues/6411). Thanks @aadler for the note and @MichaelChirico for the fix.

inst/tests/other.Rraw

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,8 @@ if (loaded[["dplyr"]]) {
761761
DT = data.table(a = 1, b = 2, c = '1,2,3,4', d = 4)
762762
test(30, DT[, c := strsplit(c, ',', fixed = TRUE) %>% lapply(as.integer) %>% as.list]$c, list(1:4)) # nolint: pipe_call_linter. Mimicking MRE as filed.
763763
}
764+
765+
if (loaded[["nanotime"]]) {
766+
# respect dec=',' for nanotime, related to #6446, corresponding to tests 2281.*
767+
test(31, fwrite(data.table(as.nanotime(.POSIXct(0))), dec=',', sep=';'), output="1970-01-01T00:00:00,000000000Z")
768+
}

inst/tests/tests.Rraw

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19059,3 +19059,7 @@ test(2280.1, internal_error("broken"), error="Internal error.*broken")
1905919059
test(2280.2, internal_error("broken %d%s", 1, "2"), error="Internal error.*broken 12")
1906019060
foo = function(...) internal_error("broken")
1906119061
test(2280.3, foo(), error="Internal error in foo: broken")
19062+
19063+
# fwrite respects dec=',' for sub-second timestamps, #6446
19064+
test(2281.1, fwrite(data.table(a=.POSIXct(0.001)), dec=',', sep=';'), output="1970-01-01T00:00:00,001Z")
19065+
test(2281.2, fwrite(data.table(a=.POSIXct(0.0001)), dec=',', sep=';'), output="1970-01-01T00:00:00,000100Z")

src/fwrite.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,21 @@ void writePOSIXct(const void *col, int64_t row, char **pch)
438438
ch -= squashDateTime;
439439
write_time(t, &ch);
440440
if (squashDateTime || (m && m%1000==0)) {
441+
Rprintf("here i\n");
441442
// when squashDateTime always write 3 digits of milliseconds even if 000, for consistent scale of squash integer64
442443
// don't use writeInteger() because it doesn't 0 pad which we need here
443444
// integer64 is big enough for squash with milli but not micro; trunc (not round) micro when squash
444445
m /= 1000;
445-
*ch++ = '.';
446+
*ch++ = dec;
446447
ch -= squashDateTime;
447448
*(ch+2) = '0'+m%10; m/=10;
448449
*(ch+1) = '0'+m%10; m/=10;
449450
*ch = '0'+m;
450451
ch += 3;
451452
} else if (m) {
453+
Rprintf("here ii\n");
452454
// microseconds are present and !squashDateTime
453-
*ch++ = '.';
455+
*ch++ = dec;
454456
*(ch+5) = '0'+m%10; m/=10;
455457
*(ch+4) = '0'+m%10; m/=10;
456458
*(ch+3) = '0'+m%10; m/=10;
@@ -472,6 +474,7 @@ void writeNanotime(const void *col, int64_t row, char **pch)
472474
if (x == INT64_MIN) {
473475
write_chars(na, &ch);
474476
} else {
477+
Rprintf("here iii\n");
475478
int d/*days*/, s/*secs*/, n/*nanos*/;
476479
n = x % 1000000000;
477480
x /= 1000000000;
@@ -488,7 +491,7 @@ void writeNanotime(const void *col, int64_t row, char **pch)
488491
*ch++ = 'T';
489492
ch -= squashDateTime;
490493
write_time(s, &ch);
491-
*ch++ = '.';
494+
*ch++ = dec;
492495
ch -= squashDateTime;
493496
for (int i=8; i>=0; i--) { *(ch+i) = '0'+n%10; n/=10; } // always 9 digits for nanoseconds
494497
ch += 9;

0 commit comments

Comments
 (0)