Skip to content

proposed change to [.POSIXct

2 messages · Whit Armstrong, Brian Ripley

#
R developers,

The "tzone" attribute is stripped from a POSIXct object when the subscript
command is called ("[.POISXct").  This results in dates being printed in the
locale specific format after a subscript operation is applied to a POSIXct
object which has cause several problems for me in the past.

Here is an example of this problem under R 1.9.1:
[1] "2000-03-26 GMT" "2000-03-26 GMT"
[1] "2000-03-25 19:00:00 Eastern Standard Time"
[1] 954028800 954028800
attr(,"tzone")
[1] "GMT"
[1] 954028800
Here is the current code for "[.POSIXct"
function (x, ..., drop = TRUE) 
{
    cl <- oldClass(x)
    class(x) <- NULL
    val <- NextMethod("[")
    class(val) <- cl
    val
}
<environment: namespace:base>
I believe a sensible change is the following:

"[.POSIXct" <-
function (x, ..., drop = TRUE) 
{
    at <- attributes(x)
    class(x) <- NULL
    val <- NextMethod("[")
    attributes(val) <- at
    val
}

Using this code, the "tzone" attribute is preserved allowing dates to
continue to be printed in their intended timezone.

Thank you for considering this issue.

Regards,
Whit Armstrong

Here are the details of my system:
$platform
[1] "i386-pc-mingw32"

$arch
[1] "i386"

$os
[1] "mingw32"

$system
[1] "i386, mingw32"

$status
[1] ""

$major
[1] "1"

$minor
[1] "9.1"

$year
[1] "2004"

$month
[1] "06"

$day
[1] "21"

$language
[1] "R"

  
  
#
Sorry, but you really do not want that the change your patch makes.
Copying across the attributes will for example copy the original names,
any dim's etc.  There is a very good reason why [ does not do that itself.

I am not even sure you want to preserve the "tzone" attribute.  A POSIXct
time is an absolute time, and that attribute is just a reminder of where
it came from.  Since you have operated on it, the history is broken.

As timezones are always used when printing, I don't see anything
problematic about your example.  Do you have a more convincing example
from `which has cause several problems for me in the past'?
On Thu, 19 Aug 2004, Whit Armstrong wrote: