[Rd] Date method of as.POSIXct does not respect tz
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Fri Sep 23 11:04:12 CEST 2022
>>>>> Kurt Hornik
>>>>> on Fri, 23 Sep 2022 09:57:49 +0200 writes:
>>>>> Martin Maechler writes:
> 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 = ""'
>>>>> Roland Fuß
>>>>> on Tue, 20 Sep 2022 09:25:52 +0200 writes:
>>> 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 using thuenen.de
>>> Arbeitsgruppe "Emissionsberichterstattung"/ Working group
>>> "Emission Inventories" Email:
>>> emissionsinventare using 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 using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list