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

Incorrect endpoints before the epoch #144

Closed
joshuaulrich opened this issue Jun 18, 2016 · 0 comments
Closed

Incorrect endpoints before the epoch #144

joshuaulrich opened this issue Jun 18, 2016 · 0 comments
Assignees
Labels

Comments

@joshuaulrich
Copy link
Owner

joshuaulrich commented Jun 18, 2016

endpoints returns incorrect values when it is called on an xts object with index values prior to the Unix epoch. For example:

require(xts)
x <- .xts(1:22, 1.0*(-10:11), tzone="UTC")
merge(x, ep=x[endpoints(x, 'seconds', 2)])
                     x ep
1969-12-31 23:59:50  1  1
1969-12-31 23:59:51  2 NA
1969-12-31 23:59:52  3  3
1969-12-31 23:59:53  4 NA
1969-12-31 23:59:54  5  5
1969-12-31 23:59:55  6 NA
1969-12-31 23:59:56  7  7
1969-12-31 23:59:57  8 NA
1969-12-31 23:59:58  9  9
1969-12-31 23:59:59 10 NA
1970-01-01 00:00:00 11 NA
1970-01-01 00:00:01 12 12
1970-01-01 00:00:02 13 NA
1970-01-01 00:00:03 14 14
1970-01-01 00:00:04 15 NA
1970-01-01 00:00:05 16 16
1970-01-01 00:00:06 17 NA
1970-01-01 00:00:07 18 18
1970-01-01 00:00:08 19 NA
1970-01-01 00:00:09 20 20
1970-01-01 00:00:10 21 NA
1970-01-01 00:00:11 22 22

I think the cause of the issue is that casting from double to long truncates toward zero. This is desirable for positive numbers (moving the endpoint back in time). But when the index value is negative, truncating toward zero moves the endpoint forward in time.

This is also the case for integer division in the C99 standard, so the fix should be applied for both integer and double index types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant