如何在 R 中格式化没有 TZ 信息的 Index()?

How to format Index() without TZ information in R?

我想测试 xts 对象最后一行的日期是否等于当前日期。我的问题是 last(index(obj)) returns Sys.Date() 函数没有时区信息。

> last(index(obj))
[1] "2016-09-16 UTC"

> Sys.Date()
[1] "2016-09-16"

我的解决方法是 paste0 Sys.Date()Sys.timezone() 但它并不优雅。有更好的解决方案吗?

# Test if we already have data
  if (last(index(obj)) == paste0(Sys.Date(), " ", Sys.timezone())) {

    next

  }

* 编辑 *

更多信息:

> str(obj)
An ‘xts’ object on 2013-06-01/2016-09-16 containing:
  Data: num [1:1204, 1:6] NA NA NA NA NA NA NA NA NA NA ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "Open" "High" "Low" "Close" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
  xts Attributes:  
List of 1
 $ dateFormat: chr "Date"

谢谢,

打印时区是因为 index(obj) 是一个 POSIXct class 对象。如果要将 index(obj) 的输出与 Sys.Date() 的输出进行比较,您需要确保 index 的输出是一个 Date class 对象。有几种方法可以做到这一点:

1)直接将index的输出转换为Date

as.Date(last(index(obj))) == Sys.Date()

2) 将obj的整个索引转换为Date(注意索引class当前为POSIXct):

indexClass(obj) <- "Date"
last(index(obj)) == Sys.Date()

其实问题出在我对象的POSIXct时间格式上。使用 as.Date(last(index(obj))) 转换为 Date 解决了我的问题。