4

Pretty basic, just not understanding what the tz argument is for if not for this...

dateIWantToConvert <- as.Date("2013-06-01") #the format of my starting object

as.POSIXlt(dateIWantToConvert, tz="America/Chicago")
[1] "2013-06-01 UTC"

There are a whole lot of these in a data.frame, so efficiency is important.

hedgedandlevered
  • 2,314
  • 2
  • 25
  • 54
  • 6
    The `tz` argument is used when you're passing `as.POSIXlt` a character string, not a `date` object. The `date` object already has a timezone, so the `tz` value in `as.POSIXlt` is ignored. – Mekki MacAulay Jan 15 '16 at 19:47
  • 1
    @Mekki: `attributes(as.Date("2013-06-01"))` doesn't show tzone attribute. How does one determine the timezone of a `date`, POSIXct or POSIXlt – Dave X Jun 07 '16 at 19:33
  • 1
    @DaveX I'm not sure. The whole thing is a mess. The DateTimeClasses help file is dense and hard to understand. Users are getting frustrated enough that I hope this will be a priority to revise soon. – Mekki MacAulay Jun 07 '16 at 20:15

3 Answers3

3

You could try without as.Date():

> as.POSIXlt("2013-06-01", tz="America/Chicago")
#[1] "2013-06-01 CDT"
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • assume it's already as.Date()ed, I included that for the example because my original object was in that format. I'll edit so that's more clear. – hedgedandlevered Jan 15 '16 at 19:39
  • 2
    `?POSIXlt` states "Dates without times are treated as being at midnight UTC." So if you supply a parameter that is already in "Date" format, it includes the tz as UTC (and this seems to override other specifications). – RHertel Jan 15 '16 at 19:45
  • If they're already `as.Date()`ed, wherever that object is created, ensure it has a time & timezone component. See update to my answer. – Mekki MacAulay Jan 15 '16 at 19:45
  • @MekkiMacAulay I don't think that a `Date` object has a time zone. At least, there is no hint that it has in the documentation. – Stibu Jan 15 '16 at 20:11
  • The `base:as.Date` documentation suggests otherwise. See https://stat.ethz.ch/R-manual/R-devel/library/base/html/as.Date.html – Mekki MacAulay Jan 16 '16 at 01:07
3

A more reliable way to do this is to use strptime. This takes any tz arguement from OlsonNames(). And by reliable, I mean that this should be OS independent.

(vec.Date <- strptime("2013-06-01", format = "%Y-%m-%d", tz = "America/Chicago"))
# "2013-06-01 CDT"
class(vec.Date)
# [1] "POSIXlt" "POSIXt" 

And for your question regarding efficiency, you really should be using strptime. See Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt

RJ-
  • 2,919
  • 3
  • 28
  • 35
0

As per ?timezones, invalid timezones are treated as UTC often without a warning. It's likely that your specific environment doesn't know how to interpret "America/Chicago". The documentation points out that it's inconsistent.

Try using "CDT" instead of "America/Chicago". Alternatively, there are details in ?timezones about how to feed R a list of timezone formats to interpret.

UPDATE: as.Date returns a "date" object, not a character string. as.POSIXlt will, quote: "Dates without times are treated as being at midnight UTC", from as.POSIXlt. So it seems if x is a date object in as.POSIXlt and it has no time value, the tz option is ignored and replaced with UTC.

As a result, another option is to ensure that the date object passed to as.POSIXlt has time & timezone already specified. Or, convert the object to character string before passing it to as.POSIXlt so that it will use its own tz option.

Mekki MacAulay
  • 1,727
  • 2
  • 12
  • 23
  • 2
    However, "America/Chicago" is one of the time zones listed by `OlsonNames()`. In my case, the OP's code gives the same result as he gets, but if I omit `as.Date`, I get the expected result. `as.POSIXlt("2013-06-01", tz="America/Chicago")` – Stibu Jan 15 '16 at 19:36
  • Hmm, interesting. Me too. Investigating. – Mekki MacAulay Jan 15 '16 at 19:38