Eli Sander
Eli Sander

Reputation: 1248

Round an POSIXct date up to the next day

I have a question similar to Round a POSIX date (POSIXct) with base R functionality, but I'm hoping to always round the date up to midnight the next day (00:00:00).

Basically, I want a function equivalent to ceiling for POSIX-formatted dates. As with the related question, I'm writing my own package, and I already have several package dependencies so I don't want to add more. Is there a simple way to do this in base R?

Upvotes: 5

Views: 2955

Answers (2)

James
James

Reputation: 66844

A quick and dirty method is to convert to a Date (which truncates the time), add 1 (which is a day for Date) and then convert back to POSIX to be at midnight UTC on the next day. As @Joshua Ulrich points out, timezone/daylight savings issues may give results you don't expect:

as.POSIXct(as.Date(Sys.time())+1)
[1] "2012-08-10 01:00:00 BST"

Upvotes: 0

shhhhimhuntingrabbits
shhhhimhuntingrabbits

Reputation: 7475

Maybe

trunc(x,"days") + 60*60*24

> x <- as.POSIXct(Sys.time())
> x
[1] "2012-08-09 18:40:08 BST"
> trunc(x,"days")+ 60*60*24
[1] "2012-08-10 BST"

Upvotes: 9

Related Questions