Skip to content

format.ITime broken for negative values #811

Closed

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

Activity

  1. added this to the v1.9.6 milestone on Sep 23, 2014
  2. arunsrinivasan commented on Oct 10, 2014

    @arunsrinivasan
    Member

    👍 Much thanks for the fix as well. Incorporated this. Will commit (and close) soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions