[Rd] trunc.Date and round.Date + documentation of DateTimeClasses

SOEIRO Thomas Thom@@@SOEIRO @end|ng |rom @p-hm@|r
Thu Sep 30 16:09:12 CEST 2021


Sorry for missing the issue on Bugzilla with Dirk's (better) proposal before posting on the list. I agree adding the whole family (ceiling(), floor(), trunc(x), and round ()) would be very useful (while it may be useful to provide the enhanced trunc.Date in the meantime). I unfortunately don't have the skills to contribute for the related functions. In any case thanks for all the work!

In addition, what do you think about the second proposal? (documentation of DateTimeClasses)

-----Message d'origine-----
De : Martin Maechler [mailto:maechler using stat.math.ethz.ch] 
Envoyé : jeudi 30 septembre 2021 15:27
À : SOEIRO Thomas
Cc : r-devel using r-project.org; Dirk Eddelbuettel
Objet : Re: [Rd] trunc.Date and round.Date + documentation of DateTimeClasses

EMAIL EXTERNE - TRAITER AVEC PRÉCAUTION LIENS ET FICHIERS

Excuse the exceptional top-reply:

Note that a very related issue has been raised not so long ago by Dirk (in CC) on R's Bugzilla :

  trunc.Date should support months and years arguments as trunc.POSIXt does
  https://urldefense.com/v3/__https://bugs.r-project.org/show_bug.cgi?id=18099__;!!JQ5agg!IcA4cGE0MAGw1HNXz9F5WN_MhMReK2hNeT997bHYUMwGr3_tISpW0NTUF1Ll1MMV614A$

which had some agreement (also with you: I agree we should change something about this) but I also had proposed to approach it more generally than in the PR .. which you already did by mentioning trunc() and round() methods together.

Still, Dirk's proposal would try harder to remain back compatible in those cases where  trunc.Date() currently does "behave as it should".

Martin Maechler
ETH Zurich  and  R Core

>>>>> SOEIRO Thomas
>>>>>     on Thu, 30 Sep 2021 10:32:32 +0000 writes:

    > About fractional days, trunc.Date2 actually seems to have no regression and to be backward compatible compared to the original trunc.Date:

    > frac <- as.Date("2020-01-01") + 0.5
    > identical(trunc(frac), trunc.Date2(frac))

    > (I may still miss something since I do not understand how
    > trunc.Date manage fractional days with round(x - 0.4999999).)

    > -----Message d'origine-----
    > De : SOEIRO Thomas
    > Envoyé : mercredi 29 septembre 2021 17:00
    > À : 'r-devel using r-project.org'
    > Objet : trunc.Date and round.Date + documentation of DateTimeClasses

    > Dear All,

    > 1) trunc.Date and round.Date:

    > Currently, the help page for trunc.Date and round.Date
    > says "The methods for class "Date" are of little use
    > except to remove fractional days". However, e.g.,
    > trunc.POSIXt(Sys.Date(), "years") and
    > round.POSIXt(Sys.Date(), "years") work because the
    > functions start with x <- as.POSIXlt(x).

    > Would you consider a simple implementation of trunc.Date
    > and round.Date based on trunc.POSIXt and round.POSIXt?
    > This would enable to avoid coercion from Date to POSIXt
    > and back to Date for these simple manipulations.

    > For example:
    > # (I do not have a clear understanding of what "remove fractional days" means, and I did not implement it.)

> trunc.Date2 <-
>   function(x, units = c("days", "months", "years"), ...)
>   {
>     units <- match.arg(units)
>     x <- as.POSIXlt(x)
>
>     switch(units,
>            "days" = {
>              x$sec[] <- 0; x$min[] <- 0L; x$hour[] <- 0L;
>              x$isdst[] <- -1L
>            },
>            "months" = {
>              x$sec[] <- 0; x$min[] <- 0L; x$hour[] <- 0L;
>              x$mday[] <- 1L
>              x$isdst[] <- -1L
>            },
>            "years" = {
>              x$sec[] <- 0; x$min[] <- 0L; x$hour[] <- 0L;
>              x$mday[] <- 1L; x$mon[] <- 0L
>              x$isdst[] <- -1L
>            }
>     )
>     as.Date(x)
>   }



    > 2) documentation of DateTimeClasses:

    > It may be useful to add in the documentation of
    > DateTimeClasses that manipulating elements of POSIXlt
    > objects may results in "invalid" entries (e.g., mon = 12
    > or mday = 0), but that the object is nevertheless
    > correctly printed/coerced.

    > Is this behavior explicitly supported?

    > d <- as.POSIXlt("2000-01-01")
    > unclass(d)
    > d$mon <- d$mon + 12
    > d$mday <- d$ mday - 1
    > unclass(d)
    > d
    > d <- as.POSIXlt(as.POSIXct(d))
    > dput(d)



    > Best,
    > Thomas



More information about the R-devel mailing list