Skip to content

Commit

Permalink
Issue Rdatatable#2842: added option for printing POSIX with timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Parages committed Apr 10, 2019
1 parent 91dcb81 commit f09679f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ print.data.table <- function(x, topn=getOption("datatable.print.topn"),
row.names=getOption("datatable.print.rownames"),
col.names=getOption("datatable.print.colnames"),
print.keys=getOption("datatable.print.keys"),
quote=FALSE, ...) { # topn - print the top topn and bottom topn rows with '---' inbetween (5)
quote=FALSE,
timezone=FALSE, ...) {
# topn - print the top topn and bottom topn rows with '---' inbetween (5)
# nrows - under this the whole (small) table is printed, unless topn is provided (100)
# class - should column class be printed underneath column name? (FALSE)
if (!col.names %chin% c("auto", "top", "none"))
Expand Down Expand Up @@ -58,7 +60,8 @@ print.data.table <- function(x, topn=getOption("datatable.print.topn"),
rn = seq_len(nrow(x))
printdots = FALSE
}
toprint=format.data.table(toprint, na.encode=FALSE, ...) # na.encode=FALSE so that NA in character cols print as <NA>

toprint=format.data.table(toprint, na.encode=FALSE, timezone = timezone, ...) # na.encode=FALSE so that NA in character cols print as <NA>

if ((!"bit64" %chin% loadedNamespaces()) && any(sapply(x,inherits,"integer64"))) require_bit64()
# When we depend on R 3.2.0 (Apr 2015) we can use isNamespaceLoaded() added then, instead of %chin% above
Expand Down Expand Up @@ -105,7 +108,7 @@ print.data.table <- function(x, topn=getOption("datatable.print.topn"),
invisible(x)
}

format.data.table <- function (x, ..., justify="none") {
format.data.table <- function (x, ..., justify="none", timezone = FALSE) {
if (is.atomic(x) && !is.null(x)) {
stop("Internal structure doesn't seem to be a list. Possibly corrupt data.table.")
}
Expand All @@ -117,6 +120,16 @@ format.data.table <- function (x, ..., justify="none") {
else
paste0("<", class(x)[1L], ">")
}
# FR #2842 add timezone for posix timestamps
format.timezone <- function(col) { # paste timezone to a time object
tz = attr(col,'tzone', exact = TRUE)
if (!is.null(tz)) { # date object with tz
nas = is.na(col)
col = paste0(as.character(col)," ",tz) # parse to character
col[nas] = NA_character_
}
return(col)
}
# FR #1091 for pretty printing of character
# TODO: maybe instead of doing "this is...", we could do "this ... test"?
char.trunc <- function(x, trunc.char = getOption("datatable.prettyprint.char")) {
Expand All @@ -128,6 +141,7 @@ format.data.table <- function (x, ..., justify="none") {
}
do.call("cbind",lapply(x,function(col,...){
if (!is.null(dim(col))) stop("Invalid column: it has dimensions. Can't format it. If it's the result of data.table(table()), use as.data.table(table()) instead.")
if(timezone) col = format.timezone(col)
if (is.list(col)) col = vapply_1c(col, format.item)
else col = format(char.trunc(col), justify=justify, ...) # added an else here to fix #5435
col
Expand Down

0 comments on commit f09679f

Please sign in to comment.