Skip to content

Commit e73c2c8

Browse files
Merge pull request #6454 from Rdatatable/fwrite-dec-time
fwrite respects dec=',' for time columns when needed
2 parents 02ced80 + 1c3fd8b commit e73c2c8

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,15 @@ void writePOSIXct(const void *col, int64_t row, char **pch)
442442
// don't use writeInteger() because it doesn't 0 pad which we need here
443443
// integer64 is big enough for squash with milli but not micro; trunc (not round) micro when squash
444444
m /= 1000;
445-
*ch++ = '.';
445+
*ch++ = dec;
446446
ch -= squashDateTime;
447447
*(ch+2) = '0'+m%10; m/=10;
448448
*(ch+1) = '0'+m%10; m/=10;
449449
*ch = '0'+m;
450450
ch += 3;
451451
} else if (m) {
452452
// microseconds are present and !squashDateTime
453-
*ch++ = '.';
453+
*ch++ = dec;
454454
*(ch+5) = '0'+m%10; m/=10;
455455
*(ch+4) = '0'+m%10; m/=10;
456456
*(ch+3) = '0'+m%10; m/=10;
@@ -488,7 +488,7 @@ void writeNanotime(const void *col, int64_t row, char **pch)
488488
*ch++ = 'T';
489489
ch -= squashDateTime;
490490
write_time(s, &ch);
491-
*ch++ = '.';
491+
*ch++ = dec;
492492
ch -= squashDateTime;
493493
for (int i=8; i>=0; i--) { *(ch+i) = '0'+n%10; n/=10; } // always 9 digits for nanoseconds
494494
ch += 9;

0 commit comments

Comments
 (0)