将 POSIXct 日期时间转换为日期时出现意外日期 - 时区问题?

cur*_*cat 4 timezone datetime r date posixct

当我尝试将POSIXct日期时间强制为Dateusing时as.Date,它似乎返回错误的日期。

我怀疑这与时区有关。我尝试了tz中的参数as.Date,但它没有给出预期的日期。

# POSIXct returns day of month 24  
data$Time[3]
# [1] "2020-03-24 00:02:00 IST"

class(data$Time[3])
# [1] "POSIXct" "POSIXt"

# coerce to Date, returns 23 
as.Date(data$Time[3])
# [1] "2020-03-23"

# try the time zone argument, without luck
as.Date(data$Time[3], tz = "IST")
# [1] "2020-03-23"
# Warning message:
# In as.POSIXlt.POSIXct(x, tz = tz) : unknown timezone 'IST' 

Sys.timezone()
# [1] "Asia/Calcutta"
Run Code Online (Sandbox Code Playgroud)

有什么想法这里可能出了什么问题吗?

G. *_*eck 5

使用最后注释中的设置,我们可以使用其中任何一个:

# same date as print(x) shows
as.Date(as.character(x))
## [1] "2020-03-24"

# use the time zone stored in x (or system time zone if that is "")
as.Date(x, tz = attr(x, "tzone"))
## [1] "2020-03-24"

# use system time zone
as.Date(x, tz = "")
## [1] "2020-03-24"

# use system time zone
as.Date(x, tz = Sys.timezone())
## [1] "2020-03-24"

# use indicated time zone
as.Date(x, tz = "Asia/Calcutta")
## [1] "2020-03-24"
Run Code Online (Sandbox Code Playgroud)

笔记

我们已经假设了这个设置。

Sys.setenv(TZ = "Asia/Calcutta")
x <- structure(1584988320, class = c("POSIXct", "POSIXt"), tzone = "")

R.version.string
## [1] "R version 4.0.2 Patched (2020-06-24 r78745)"
Run Code Online (Sandbox Code Playgroud)