R:如何查看时间戳的格式?

use*_*366 4 datetime r

足够简单的问题,但找不到答案。如果我有一些虚拟时间戳:

foo <- strftime(Sys.time(), "%Y-%m-%dT%H:%M:%SZ")
Run Code Online (Sandbox Code Playgroud)

带格式:

"%Y-%m-%dT%H:%M:%SZ". 
Run Code Online (Sandbox Code Playgroud)

我将如何检查 foo 是什么格式?我需要检查用户是否以正确的格式提交了时间戳,因此需要类似的东西

if (func_bar(foo) != "%Y-%m-%dT%H:%M:%SZ") {stop("Complain")}
Run Code Online (Sandbox Code Playgroud)

是否有一个内在的 R 函数?

更新:(为清楚起见)我知道我可以为用户自动更改格式,即只需转换提交的日期。但在这种特殊情况下,我想抓住它。例如我可以使用:

foo1 <- as.POSIXlt(Sys.time(), "UTC", "%Y-%m-%dT%H:%M:%S")

foo <- strftime(foo1, "%Y-%m-%dT%H:%M:%SZ")
Run Code Online (Sandbox Code Playgroud)

把它变成正确的格式。

Lyz*_*deR 5

Sys.time()POSIXct默认输出当前时间。您的用户将提交我假设的字符时间。如果您尝试将其转换为POSIXct,它将变成NA除非它具有您指定的确切格式。所以,这样的事情会起作用:

if (is.na(as.POSIXct('2017-01-01 05:00:00', format = "%Y-%m-%dT%H:%M:%SZ"))) {

  stop('Date not in the right format')

}
#Error: Date not in the right format
Run Code Online (Sandbox Code Playgroud)