Skip to content
Prev 345896 / 398502 Next

Help with Time

On Nov 21, 2014, at 2:55 PM, Raghuraman Ramachandran wrote:

            
That's just truncation. Should be very easy to hack trunc.POSIXt to deliver that result. Add a "half_hr" unit to the list and then a simple extra clause that check for minutes >30.
function (x, units = c("secs", "mins", "hours", "days"), ...) 
{
    units <- match.arg(units)
    x <- as.POSIXlt(x)
    if (length(x$sec)) 
        switch(units, secs = {
            x$sec <- trunc(x$sec)
        }, mins = {
            x$sec[] <- 0
        }, hours = {
            x$sec[] <- 0
            x$min[] <- 0L
        }, days = {
            x$sec[] <- 0
            x$min[] <- 0L
            x$hour[] <- 0L
            x$isdst[] <- -1L
        })
    x
}
<bytecode: 0x10a54b038>
<environment: namespace:base>