4

I am having issues extracting the correct character values from a POSIXct datetime object using strftime(). The dput() of my sample data is below:

dput(df1)
structure(list(FlowDate3 = structure(c(1388534400, 1388534400, 
1388534400, 1388534400, 1388534400, 1388534400, 1388534400, 1388534400, 
1388534400, 1388534400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    FlowDate4 = c("2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31", 
    "2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31", "2013-12-31", 
    "2013-12-31")), .Names = c("FlowDate3", "FlowDate4"), row.names = c(NA, 
10L), class = "data.frame")

Looks like this:

> df1
    FlowDate3  FlowDate4
1  2014-01-01 2013-12-31
2  2014-01-01 2013-12-31
3  2014-01-01 2013-12-31
4  2014-01-01 2013-12-31
5  2014-01-01 2013-12-31
6  2014-01-01 2013-12-31
7  2014-01-01 2013-12-31
8  2014-01-01 2013-12-31
9  2014-01-01 2013-12-31
10 2014-01-01 2013-12-31

> str(df1)
'data.frame':   10 obs. of  2 variables:
 $ FlowDate3: POSIXct, format: "2014-01-01" "2014-01-01" "2014-01-01" ...
 $ FlowDate4: chr  "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" ...

To create FlowDate4 I did the following:

> strftime(df1$FlowDate3, "%Y-%m-%d")
 [1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
 [7] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"

Which, as you can see is yielding the wrong character strings for the date in FlowDate3... the year, month, and date are off. I've ran around in numerous circles trying to figure out why and am at a complete loss. strftime() is not behaving as I've experienced in the past. Any help is appreciated.

stokeinfo
  • 135
  • 1
  • 2
  • 8

1 Answers1

2

You have to explicitly set the timezone in your call to strptime. The tzone attribute of your POSIXct object is not used.

strftime(df1$FlowDate3, format="%Y-%m-%d", tz=attr(df1$FlowDate3, "tzone"))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Has that always been the case or is it a new requirement for the recent R distribution? I don't remember ever having to do that... – stokeinfo Dec 09 '14 at 17:21
  • Timezones have ALWAYS been confusing. – IRTFM Dec 09 '14 at 17:22
  • @stokeinfo: you might not have had to do it before because your data didn't have a `tzone` attribute or it existed but was set to your local timezone. I don't know if it has recently changed in R. You would need to look at the NEWS file and/or the svn history. – Joshua Ulrich Dec 09 '14 at 17:24
  • Got it. Timezones are not critical to my data, so how can I make sure that my data does not have a timezone attribute? B/c, when I format with as.POSIXct() without designating a timezone, it throws 4 warnings that it's unhappy without a designated timezone. – stokeinfo Dec 09 '14 at 17:30
  • 1
    @stokeinfo: Use `attr(x,'tzone') <- NULL` to remove the attribute. I've never seen warnings like that, so I can't help more until you provide a reproducible example. – Joshua Ulrich Dec 09 '14 at 17:44
  • Ok. Thanks for your help - much appreciated. However now it's working how I've observed in the past with the same function calls... weird, totally boggles me. I'll keep this in mind for future reference if I ever run into a similar issue. – stokeinfo Dec 09 '14 at 17:50