From j|r|@c@mor@vec @end|ng |rom gm@||@com  Fri Feb  9 01:58:00 2024
From: j|r|@c@mor@vec @end|ng |rom gm@||@com (=?UTF-8?B?SmnFmcOtIE1vcmF2ZWM=?=)
Date: Fri, 9 Feb 2024 13:58:00 +1300
Subject: [Rd] round.Date and trunc.Date not working / implemented
In-Reply-To: <41265ce9-5a0c-4e38-af69-461e065ab81f@gmail.com>
References: <41265ce9-5a0c-4e38-af69-461e065ab81f@gmail.com>
Message-ID: <8de0995f-dc5c-4788-8a8b-959bf79ee71c@gmail.com>

 > This is a workaround, and could be the basis for a round.Date 
improvement:
 >?? date <- Sys.Date()
 >?? as.Date(round(as.POSIXct(date), "years"))
 >?? as.Date(round(as.POSIXct(Sys.Date() + 180), "years"))
 > Duncan Murdoch

That would work, perhaps structured similarly as `trunc.Date` is.
The only issue might be that `trunc.Date` is currently using `round.Date`
in its numeric form likely to prevent ?expensive? conversion to POSIXt 
when it is not required.

 > trunc.Date
 > function (x, units = c("secs", "mins", "hours", "days", "months",
 >? ?? "years"), ...)
 > {
 >??? units <- match.arg(units)
 >??? if (units == "months" || units == "years")
 >??????? as.Date(trunc.POSIXt(x, units, ...))
 >??? else round(x - 0.4999999)
 > }

Perhaps the working version of `round.Date` could be:

 ? round.Date = function(x, units = c("secs", "mins", "hours", "days", 
"months", "years")){
 ??? units = match.arg(units)

 ??? if (units == "months" || units == "years")
 ??? ? as.Date(round.POSIXt(x, units, ...))
 ??? else .Date(round(as.numeric(x)))
 ? }

Or perhaps `unclass` instead of `as.numeric`. Since the default `units` 
for round(x) evaluates
to `sec`, this should correctly skip the first condition in `round` and 
get to the correct numeric
rounding.

Perhaps the `trunc.Date` should be modified as well so that the call to 
`round.Date` is skipped in favour of internal `round.numeric`, saving 
few cycles.

-- Jirka



[Rd] round.Date and trunc.Date not working / implemented
=?UTF-8?B?SmnFmcOtIE1vcmF2ZWM=?=