How to reliably convert git_time to date? · Issue #407 · ropensci/git2r (original) (raw)
Given a git_commit
object, I would like to be able to return the date of the commit as a character string. Here is my current approach:
> x <- commits(n = 1)[[1]]
> x$author$when
2019-11-08 15:39:58
> as.character(as.Date(as.POSIXct(x$author$when)))
[1] "2019-11-08"
However this occasionally sporadically fails, and the date is off by a whole day. I haven't been able to track down exactly when it happens, but I think it is related to the current time in the given timezone.
As an example, the recent release 1.5.0 of my workflowr package failed the CRAN check on r-devel-windows after it was submitted:
> test_check("workflowr")
-- 1. Failure: Conversion of git_time to character string of date is correct (@t
as.character(Sys.Date()) not identical to as.character(as.Date(as.POSIXct(c1$author$when))).
1/1 mismatches
x[1]: "2019-11-06"
y[1]: "2019-11-05"
I also record the time and timezone during each check, so I was expecting it to return "2019-11-06"
.
< > Sys.time()
< [1] "2019-11-06 00:56:50 CET"
< > Sys.timezone()
< [1] "Europe/Berlin"
This is hard to reproduce. I used devtools::check_win_devel()
to test the package with r-devel on a CRAN Windows machine. This time the test passed (link to log - note: this will expire in a few days). The timezone is the same, but the time of day changed.
< > Sys.time()
< [1] "2019-11-07 23:33:16 CET"
< > Sys.timezone()
< [1] "Europe/Berlin"
How can I obtain the current date for a Git commit that isn't affected by the time of day?