Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upHow to reliably convert git_time to date? #407
Comments
|
Another data point: My workflowr package was checked again on the CRAN server
https://www.r-project.org/nosvn/R.check/r-devel-windows-ix86+x86_64/workflowr-00check.html To summarize so far:
|
|
I'm not sure why it fails. Below is some code I've used to explore the timestamp in the commit and conversion to a character to try to understand why it fails. This is the description of the timestamp format from https://git-scm.com/docs/git-log
library(git2r)
## Read a commit without using git2r
read_git_commit <- function(filename) {
# Read compressed data
n <- file.info(filename)$size
data <- readBin(filename, what = "raw", n = n)
data <- memDecompress(data, "gzip")
# Find "\0" in data; separates header from content
null_byte <- which(data == 0)
# Determine type of git object
header <- rawToChar(data[1:(null_byte-1)])
# Read content
i <- seq(from = null_byte + 1, to = length(data))
content <- readLines(textConnection(rawToChar(data[i])))
c(header, content)
}
## Create a directory in tempdir
path <- tempfile(pattern = "git2r-")
dir.create(path)
## Initialize a repository
repo <- init(path)
config(repo, user.name = "Alice", user.email = "alice@example.org")
## Current time and timezone.
Sys.time()
#> [1] "2019-11-13 23:04:04 CET"
Sys.timezone()
#> [1] "Europe/Stockholm"
## Create a file and commit it.
writeLines("Hello world!", file.path(path, "test.txt"))
add(repo, "test.txt")
commit(repo, "Commit message")
#> [4c7d388] 2019-11-13: Commit message
## Read the content of the commit.
sha1 <- sha(last_commit(path))
filename <- file.path(path, ".git/objects", substr(sha1, 1, 2), substr(sha1, 3, 40))
read_git_commit(filename)
#> [1] "commit 164"
#> [2] "tree a0b0b9e615e9e433eb5f11859e9feac4564c58c5"
#> [3] "author Alice <alice@example.org> 1573682644 +0100"
#> [4] "committer Alice <alice@example.org> 1573682644 +0100"
#> [5] ""
#> [6] "Commit message"
## Inspect time using git2r
(time <- last_commit(path)$author$when$time)
#> [1] 1573682644
(offset <- last_commit(path)$author$when$offset)
#> [1] 60
as.POSIXct(time, origin = "1970-01-01", tz = "UTC")
#> [1] "2019-11-13 22:04:04 UTC"Created on 2019-11-13 by the reprex package (v0.3.0) Internally in git2r, |
|
@stewid Thanks for investigating.
My understanding is that
|
|
I've set up a CI build to run some tests every hour to see if I can reproducibly trigger the error. |
|
My 24 hour test completed. Using the timezone "America/New_York", the results were:
|
|
Does it work if you change line 19 in your test script from |
|
OK. I think I figured it out. The date begins failing the last 5 hours of the day in the timezone America/New_York because GMT is 5 hours ahead.
And the default for
Thus if I want the day in the local timezone as is returned by Git, I need to set
|
|
Great, looking forward to see the results from your new test run |
Note that this wouldn't work. It still returns the time 5 hours ahead:
|
|
My solution of setting the local timezone in the call to |
|
Thanks, your script that was run every hour was a very elegant way to address this issue. |
Given a
git_commitobject, I would like to be able to return the date of the commit as a character string. Here is my current approach: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:
I also record the time and timezone during each check, so I was expecting it to return
"2019-11-06".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.How can I obtain the current date for a Git commit that isn't affected by the time of day?
Potentially related Issues/PRs: #276, #384, #392, #393