Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGZILLA #17437] as.POSIXlt timezone Handled Inconsistently (and not as documented) #6611

Open
MichaelChirico opened this issue May 19, 2020 · 2 comments

Comments

@MichaelChirico
Copy link
Owner

The following code does not return the same object for both ways of converting the date to a POSIXlt object. Specifically, the timezone is both stored in a different way and set to a different value:

dput(as.POSIXlt(as.Date("2018-01-01")))
#> structure(list(sec = 0, min = 0L, hour = 0L, mday = 1L, mon = 0L, 
#>     year = 118L, wday = 1L, yday = 0L, isdst = 0L), .Names = c("sec", 
#> "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
#> ), class = c("POSIXlt", "POSIXt"), tzone = "UTC")
dput(as.POSIXlt("2018-01-01"))         
#> structure(list(sec = 0, min = 0L, hour = 0L, mday = 1L, mon = 0L, 
#>     year = 118L, wday = 1L, yday = 0L, isdst = 0L, zone = "EST", 
#>     gmtoff = NA_integer_), .Names = c("sec", "min", "hour", "mday", 
#> "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", 
#> "POSIXt"))

When casting as.POSIXlt from a date, the time zone is set to "UTC" as an attribute of the object (not as one of the names of the main list). When casting as.POSIXlt from a character string, the time zone is set to the current time zone (mine is "EST"), two additional list elements are present ("zone" and "gmtoff"), and no attribute is present.

The behavior for as.POSIXlt.Date doesn't align with the "as.POSIXlt" documentation text for the tz argument:

"" is the current time zone

Since the time zone was implicitly "", and it was set to "UTC".


METADATA

  • Bug author - Bill Denney
  • Creation time - 2018-06-24 14:57:05 UTC
  • Bugzilla link
  • Status - UNCONFIRMED
  • Alias - None
  • Component - Add-ons
  • Version - R 3.4.4
  • Hardware - All All
  • Importance - P5 normal
  • Assignee - R-core
  • URL -
  • Modification time - 2020-01-19 07:49 UTC
@MichaelChirico
Copy link
Owner Author

A related issue and more severe is that the tz argument for as.POSIXlt.Date appears to be ignored:

dput(as.POSIXlt(as.Date("2018-01-01"), tz="EST"))

structure(list(sec = 0, min = 0L, hour = 0L, mday = 1L, mon = 0L,
year = 118L, wday = 1L, yday = 0L, isdst = 0L), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"), tzone = "UTC")

I specified "EST" and I got out "UTC".


METADATA

  • Comment author - Bill Denney
  • Timestamp - 2018-06-24 15:15:53 UTC

@MichaelChirico
Copy link
Owner Author

The generic for as.POSIXlt has this profile:
function (x, tz = "", ...)
UseMethod("as.POSIXct")

The documentation for the function shows

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, ...)

However, as.POSIXlt.Date doesn't pass through the timezone argument

as.POSIXlt.Date <- function(x, ...) .Internal(Date2POSIXlt(x))

so therefore, including a timezone argument does not change the result.

as.POSIXlt.factor also ignores the time zone,

function (x, ...)
{
y <- as.POSIXlt(as.character(x), ...)
names(y$year) <- names(x)
y
}

but as.POSIXlt.character() itself does take a timezone argument and so do the default and numeric methods.

So, the report is correct. If as.POSIXlt.Date used the tz parameter it could be added to the object that is returned from .Internal(Date2POSIXlt(x)).

It is definitely not working as documented.


METADATA

  • Comment author - elin.waring
  • Timestamp - 2020-01-19 07:49:29 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant