I work with the POSIXct data type in R. In my work, I include a function that returns two POSIXct dates in a vector. However, I find some unexpected actions. I wrote some code examples to illustrate my problem:
returnTime <- function(date) {
oneDay <- 60 * 60 * 24
nextDay <- date + oneDay
print(date)
print(nextDay)
return(c(date, nextDay))
}
myTime <- as.POSIXct("2015-01-01", tz = "UTC")
bothDays <- returnTime(myTime)
print(bothDays)
Print operators in a function give:
[1] "2015-01-01 UTC"
[1] "2015-01-02 UTC"
While the print statement at the end of the code gives:
[1] "2014-12-31 19:00:00 EST" "2015-01-01 19:00:00 EST"
I understand what is happening, but I do not understand why. It may be a simple mistake that eludes me, but I'm really very confused. I do not understand why the time zone changes upon return. The class is still POSIXct as well, only the time zone has changed.
, , , , . , , - . !
. :
return(list(date, nextDay))
.