Skip to content

format.ITime broken for negative values #811

@stefanfritsch

Description

@stefanfritsch

Hi,

format.ITime returns incorrect output for negative values. E.g. in the following example you'd expect -1 second but you get -01:59:59 because the %/% used in format.ITime does not work as intended for values <0.

diff(as.ITime(c(3,2),origin="1970-01-01",tz="UTC"))
# [1] "-1:59:59"

A solution could look like this:

function (x, ...) 
{
  neg<-x<0
  x <- abs(unclass(x))
  hh <- x%/%3600
  mm <- (x - hh * 3600)%/%60
  ss <- trunc(x - hh * 3600 - 60 * mm)
  res<-paste(substring(paste("0", hh, sep = ""), nchar(paste(hh))), 
        substring(paste("0", mm, sep = ""), nchar(paste(mm))), 
        substring(paste("0", ss, sep = ""), nchar(paste(ss))), 
        sep = ":")

  res[neg]<-paste0("-",res[neg])

  res
}

EDIT: tested with a current cvs version of data.table.

Thx
Stefan

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions