Odd behavior of the POSIXct function in R

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:

# POSIXct returning issue:

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))

.

+4
2

, c :

attributes(myTime)
#$class
#[1] "POSIXct" "POSIXt" 
#
#$tzone
#[1] "UTC"

attributes(c(myTime))
#$class
#[1] "POSIXct" "POSIXt"

, , , setattr data.table, :

(setattr(c(myTime), 'tzone', attributes(myTime)$tzone))
#[1] "2015-01-01 UTC"
+3

?c.POSIXct:

c "POSIXlt" , "POSIXct" "tzone" ( ).

. .

+4

Source: https://habr.com/ru/post/1606658/


All Articles