Skip to content

Date method of as.POSIXct does not respect tz

5 messages · Roland Fuß, Kurt Hornik, Martin Maechler

#
Hello,

May I follow up on this? Has it fallen through the cracks or were there 
considerations against passing `tz` to `.POSIXct`?

Regards,

Roland

Am 17.05.2018 um 19:55 schrieb Martin Maechler:

  
    
3 days later
#
> Hello, May I follow up on this? Has it fallen through the
    > cracks or were there considerations against passing `tz`
    > to `.POSIXct`?

    > Regards,
    > Roland

Hmm...  I don't remember  even though I had replied positively
to your question in May 2018.

PROS (for allowing 'tz') to be passed to
----   as.POSIXct.Date()  and hence ideally also to as.POSIXlt.Date() 

1) as.POSIXct() and as.POSIXlt()   all show with a 'tz' argument
  on the help page, and hence for consistency users should
  really expect  that 'tz = ".."'  also works in these two cases:

  Usage:

     as.POSIXct(x, tz = "", ...)
     as.POSIXlt(x, tz = "", ...)
     
     ## S3 method for class 'character'
     as.POSIXlt(x, tz = "", format,
                tryFormats = c("%Y-%m-%d %H:%M:%OS",
                               "%Y/%m/%d %H:%M:%OS",
                               "%Y-%m-%d %H:%M",
                               "%Y/%m/%d %H:%M",
                               "%Y-%m-%d",
                               "%Y/%m/%d"),
                optional = FALSE, ...)
     ## Default S3 method:
     as.POSIXlt(x, tz = "",
                optional = FALSE, ...)
     ## S3 method for class 'numeric'
     as.POSIXlt(x, tz = "", origin, ...)


2) It is easy to be implemented for  as.POSIXct()   as you found
   already in 2018.


CONS:
-----

3) The 'Details:' section on the help page,
   end of 1st paragraph says

     Dates without times are treated as being at midnight UTC.

   So the current hardwired behavior is documented.

4a) For  as.POSIXlt.Date()  to allow passing 'tz' needs changes
   in the C code,  i.e., is considerable more work than for the as.POSIXct(),
   (or actually just post-processing in the R code, much less work)

4b) Only changing the behavior for as.POSIXct()  but not for as.POSIXlt()
    is also an inconsistency.


My personal inclination would still be to do the change,
by giving most weight to  '1)' above.

What do other experienced R developers think ?

Martin


    > Am 17.05.2018 um 19:55 schrieb Martin Maechler:
    >>>>>>> Roland Fu? on Wed, 16 May 2018 17:21:07 +0200
    >>>>>>> writes:
    >> > R 3.5.0 Is it intended that the Date method of
    >> as.POSIXct > does not respect the tz parameter? I suggest
    >> changing > as.POSIXct.Date
    >> 
    >> which is
    >> 
    >> function (x, ...) .POSIXct(unclass(x) * 86400)
    >> 
    >> > to this:
    >> 
    >> function (x, tz = "", ...)  .POSIXct(unclass(x) * 86400,
    >> tz = tz)
    >> 
    >> or rather just forward the '...', i.e., use
    >> 
    >> function (x, ...) .POSIXct(unclass(x) * 86400, ...)
    >> 
    >> ??
    >> 
    >> > Currently, the best workaround seems to be using the >
    >> character method if one doesn't want the default timezone
    >> > (which is often an annoying DST timezone).
    >> 
    >> > This came up on Stack Overflow: >
    >> https://stackoverflow.com/q/50373340/1412059
    >> 
    >> > --
    >> > Roland
    >> 
    >> Thank you Roland for your notice (and the help on SO).
    >> 
    >> Best, Martin

    > -- 
    > Dr. Roland Fu?

    > Th?nen-Institut f?r Agrarklimaschutz/ Th?nen Institute of
    > Climate-Smart Agriculture

    > Bundesallee 65 D-38116 Braunschweig, Germany

    > Tel.: ++49 531 596 2627 Fax: ++49 531 596 2699 Email:
    > roland.fuss at thuenen.de

    > Arbeitsgruppe "Emissionsberichterstattung"/ Working group
    > "Emission Inventories" Email:
    > emissionsinventare at thuenen.de

    > Das Johann Heinrich von Th?nen-Institut,
    > Bundesforschungsinstitut f?r L?ndliche R?ume, Wald und
    > Fischerei ? kurz: Th?nen-Institut ? besteht aus 15
    > Fachinstituten, die in den Bereichen ?konomie, ?kologie
    > und Technologie forschen und die Politik beraten.

    > The Johann Heinrich von Th?nen Institute, Federal Research
    > Institute for Rural Areas, Forestry and Fisheries ? Th?nen
    > Institute in brief ? consists of 15 specialized institutes
    > that carry out research and provide policy advice in the
    > fields of economy, ecology and technology.
#
Currently in R-devel,

R> as.POSIXlt.Date
function (x, ...) 
{
    if (any((y <- unclass(x)) > .Machine$integer.max, na.rm = TRUE)) 
        as.POSIXlt(.POSIXct(y * 86400), tz = "UTC")
    else .Internal(Date2POSIXlt(x))
}

R> as.POSIXct.Date
function (x, ...) 
.POSIXct(unclass(x) * 86400)

Adding tz to the latter is easy, and the former could do the if() part
also with a given tz without needing to change the .Internal?

Best
-k

        
#
> Currently in R-devel,

    R> as.POSIXlt.Date
    > function (x, ...) 
    > {
    >    if (any((y <- unclass(x)) > .Machine$integer.max, na.rm = TRUE)) 
    >         as.POSIXlt(.POSIXct(y * 86400), tz = "UTC")
    >    else .Internal(Date2POSIXlt(x))
    > }

    R> as.POSIXct.Date
    > function (x, ...) 
    > .POSIXct(unclass(x) * 86400)

    > Adding tz to the latter is easy, and the former could do the if() part
    > also with a given tz without needing to change the .Internal?

Genau!  Even more elegantly than I first thought when I wrote
"post-processing":

The following even adds internal consistency inside as.POSIXlt.Date() :

as.POSIXlt.Date <- function(x, tz = "UTC", ...) {
    as.POSIXlt(if(any((y <- unclass(x)) > .Machine$integer.max, na.rm = TRUE))
                   .POSIXct(y * 86400)
               else
                   .Internal(Date2POSIXlt(x))
             , tz = tz)
}

because now, tz is even *formally* treated the same in both
cases (whereas previously it only appeared visually in one case).

So probably, another reason to go there.
Note that I also think we'd keep the    tz = "UTZ"
default argument, even when the
other  as.POSIX[cl]t() methods have 'tz = ""'
>>> Hello, May I follow up on this? Has it fallen through the
    >>> cracks or were there considerations against passing `tz`
    >>> to `.POSIXct`?

    >>> Regards,
    >>> Roland

    >> Hmm...  I don't remember  even though I had replied positively
    >> to your question in May 2018.

    >> PROS (for allowing 'tz') to be passed to
    >> ----   as.POSIXct.Date()  and hence ideally also to as.POSIXlt.Date() 

    >> 1) as.POSIXct() and as.POSIXlt()   all show with a 'tz' argument
    >> on the help page, and hence for consistency users should
    >> really expect  that 'tz = ".."'  also works in these two cases:

    >> Usage:

    >> as.POSIXct(x, tz = "", ...)
    >> as.POSIXlt(x, tz = "", ...)
     
    >> ## S3 method for class 'character'
    >> as.POSIXlt(x, tz = "", format,
    >> tryFormats = c("%Y-%m-%d %H:%M:%OS",
    >> "%Y/%m/%d %H:%M:%OS",
    >> "%Y-%m-%d %H:%M",
    >> "%Y/%m/%d %H:%M",
    >> "%Y-%m-%d",
    >> "%Y/%m/%d"),
    >> optional = FALSE, ...)
    >> ## Default S3 method:
    >> as.POSIXlt(x, tz = "",
    >> optional = FALSE, ...)
    >> ## S3 method for class 'numeric'
    >> as.POSIXlt(x, tz = "", origin, ...)


    >> 2) It is easy to be implemented for  as.POSIXct()   as you found
    >> already in 2018.


    >> CONS:
    >> -----

    >> 3) The 'Details:' section on the help page,
    >> end of 1st paragraph says

    >> Dates without times are treated as being at midnight UTC.

    >> So the current hardwired behavior is documented.

    >> 4a) For  as.POSIXlt.Date()  to allow passing 'tz' needs changes
    >> in the C code,  i.e., is considerable more work than for the as.POSIXct(),
    >> (or actually just post-processing in the R code, much less work)

    >> 4b) Only changing the behavior for as.POSIXct()  but not for as.POSIXlt()
    >> is also an inconsistency.


    >> My personal inclination would still be to do the change,
    >> by giving most weight to  '1)' above.

    >> What do other experienced R developers think ?

    >> Martin


    >>> Am 17.05.2018 um 19:55 schrieb Martin Maechler:
    >>>>>>>>> Roland Fu? on Wed, 16 May 2018 17:21:07 +0200
    >>>>>>>>> writes:
    >>>> > R 3.5.0 Is it intended that the Date method of
    >>>> as.POSIXct > does not respect the tz parameter? I suggest
    >>>> changing > as.POSIXct.Date
    >>>> 
    >>>> which is
    >>>> 
    >>>> function (x, ...) .POSIXct(unclass(x) * 86400)
    >>>> 
    >>>> > to this:
    >>>> 
    >>>> function (x, tz = "", ...)  .POSIXct(unclass(x) * 86400,
    >>>> tz = tz)
    >>>> 
    >>>> or rather just forward the '...', i.e., use
    >>>> 
    >>>> function (x, ...) .POSIXct(unclass(x) * 86400, ...)
    >>>> 
    >>>> ??
    >>>> 
    >>>> > Currently, the best workaround seems to be using the >
    >>>> character method if one doesn't want the default timezone
    >>>> > (which is often an annoying DST timezone).
    >>>> 
    >>>> > This came up on Stack Overflow: >
    >>>> https://stackoverflow.com/q/50373340/1412059
    >>>> 
    >>>> > --
    >>>> > Roland
    >>>> 
    >>>> Thank you Roland for your notice (and the help on SO).
    >>>> 
    >>>> Best, Martin

    >>> -- 
    >>> Dr. Roland Fu?

    >>> Th?nen-Institut f?r Agrarklimaschutz/ Th?nen Institute of
    >>> Climate-Smart Agriculture

    >>> Bundesallee 65 D-38116 Braunschweig, Germany

    >>> Tel.: ++49 531 596 2627 Fax: ++49 531 596 2699 Email:
    >>> roland.fuss at thuenen.de

    >>> Arbeitsgruppe "Emissionsberichterstattung"/ Working group
    >>> "Emission Inventories" Email:
    >>> emissionsinventare at thuenen.de

    >>> Das Johann Heinrich von Th?nen-Institut,
    >>> Bundesforschungsinstitut f?r L?ndliche R?ume, Wald und
    >>> Fischerei ? kurz: Th?nen-Institut ? besteht aus 15
    >>> Fachinstituten, die in den Bereichen ?konomie, ?kologie
    >>> und Technologie forschen und die Politik beraten.

    >>> The Johann Heinrich von Th?nen Institute, Federal Research
    >>> Institute for Rural Areas, Forestry and Fisheries ? Th?nen
    >>> Institute in brief ? consists of 15 specialized institutes
    >>> that carry out research and provide policy advice in the
    >>> fields of economy, ecology and technology.

    >> ______________________________________________
    >> R-devel at r-project.org mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel
1 day later
#
Hmm, good plan, but not working for the (more important) second
branch:

   as.POSIXlt(<POSIXlt>, tz = "..")

immediately returns the first argument without ever looking if
there are further arguments, in this case 'tz'..

--> we would also have to slightly change  as.POSIXlt.default(x)
such that it obeyed a 'tz = ".."'  in the case of x being
"POSIXlt".
Almost surely this will break some code which had relied on the
current -- a bit surpristing to me -- behavior.

Well,... I think we *should* want the above {setting a "tz"} for
POSIXlt to work.

Martin