Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format.ITime broken for negative values #811

Closed
stefanfritsch opened this issue Sep 15, 2014 · 1 comment
Closed

format.ITime broken for negative values #811

stefanfritsch opened this issue Sep 15, 2014 · 1 comment
Assignees
Milestone

Comments

@stefanfritsch
Copy link

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

@arunsrinivasan arunsrinivasan added this to the v1.9.6 milestone Sep 23, 2014
@arunsrinivasan arunsrinivasan self-assigned this Oct 10, 2014
@arunsrinivasan
Copy link
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
Projects
None yet
Development

No branches or pull requests

3 participants