POSIXct for character via strftime () results in wrong result

I am having problems retrieving the correct character values ​​from a PATIXct datetime object using strftime (). Below is an example of dput () data of my data:

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:

> 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, results in incorrect character strings for date in FlowDate3 ... year, month and date are off. I've been running in numerous circles, trying to understand why I am at a complete loss. strftime () does not behave the way I have experienced in the past. Any help is appreciated.

+3


source to share


1 answer


You must explicitly set the timezone in your call to strptime

. tzone

Your object attribute is POSIXct

not being used.



strftime(df1$FlowDate3, format="%Y-%m-%d", tz=attr(df1$FlowDate3, "tzone"))

      

+1


source







All Articles