Why does strptime
give different timezones here:
strptime(c("10/24/05", "3/1/04"),"%m/%d/%y")
Here is the output I get:
> strptime(c("10/24/05", "3/1/04"),"%m/%d/%y")
[1] "2005-10-24 PDT" "2004-03-01 PST"
I can format
it but, I want to understand why different timezones are automatically picked up!!
strptime
converts to POSIXlt, a datetime class, which is why you're getting timezones in the first place. If you don't specify a tz
parameter, it will use your current location's time zone to parse whatever bits you give it. Since you're giving it dates with no times, it's actually inserting zeros for the times, as well, i.e. the results you get are actually being set to midnight in your time zone on those days, though its printing method doesn't show that bit. They're there, though:
format(strptime(c("10/24/05", "3/1/04"),"%m/%d/%y"), format = '%F %T %Z')
# [1] "2005-10-24 00:00:00 EDT" "2004-03-01 00:00:00 EST"
Since your time zone actually changes from Pacific Standard Time to Pacific Daylight Time, strptime
is picking the appropriate time zone for the appropriate date.
The simplest workaround is not to use format
(which would convert back to character strings), but to use R's Date class, which is purpose-built:
as.Date(c("10/24/05", "3/1/04"),"%m/%d/%y")
# [1] "2005-10-24" "2004-03-01"