|
| 1 | +#' Download the latest IANA timezone database on Windows |
| 2 | +#' A helper function taken from https://github.com/apache/arrow/blob/a6f736c962adddd0f7078a10338fe8f1445c583b/python/pyarrow/util.py#L233 |
| 3 | +#' @return Invisible |
| 4 | +#' @details For some reason we have failures on windows relating to the timezone |
| 5 | +#' database. This function is a workaround to download the latest IANA timezone |
| 6 | +#' database on Windows. It is used in tests and vignettes. |
| 7 | +#' @noRd |
| 8 | +#' @keywords internal |
| 9 | +download_tzdata_on_windows <- function(tzdir = NULL) { |
| 10 | + if (.Platform$OS.type != "windows") { |
| 11 | + cli::cli_alert_info( |
| 12 | + "Timezone database is already provided by {(.Platform)$OS.type}" |
| 13 | + ) |
| 14 | + return(NULL) |
| 15 | + } |
| 16 | + |
| 17 | + # Define paths |
| 18 | + if (is.null(tzdir)) { |
| 19 | + tzdata_path <- path.expand( |
| 20 | + file.path(Sys.getenv("USERPROFILE"), "Downloads", "tzdata") |
| 21 | + ) |
| 22 | + } else { |
| 23 | + tzdata_path <- tzdir |
| 24 | + } |
| 25 | + tzdata_compressed <- file.path(tzdata_path, "tzdata.tar.gz") |
| 26 | + |
| 27 | + # Create directory if it doesn't exist |
| 28 | + if (!dir.exists(tzdata_path)) { |
| 29 | + dir.create(tzdata_path, recursive = TRUE) |
| 30 | + } |
| 31 | + |
| 32 | + # Download the latest IANA timezone database |
| 33 | + utils::download.file( |
| 34 | + "https://data.iana.org/time-zones/tzdata-latest.tar.gz", tzdata_compressed, |
| 35 | + mode = "wb" |
| 36 | + ) |
| 37 | + |
| 38 | + # Check if the file exists |
| 39 | + if (!file.exists(tzdata_compressed)) { |
| 40 | + cli::cli_abort("Failed to download the timezone database.") |
| 41 | + } |
| 42 | + |
| 43 | + # Extract the tar.gz file |
| 44 | + utils::untar(tzdata_compressed, exdir = tzdata_path) |
| 45 | + |
| 46 | + # Download the windowsZones.xml file |
| 47 | + windows_zones_url <- paste0( |
| 48 | + "https://raw.githubusercontent.com/unicode-org/", |
| 49 | + "cldr/master/common/supplemental/windowsZones.xml" |
| 50 | + ) |
| 51 | + windows_zones_path <- file.path(tzdata_path, "windowsZones.xml") |
| 52 | + utils::download.file(windows_zones_url, windows_zones_path, mode = "wb") |
| 53 | + |
| 54 | + # Find the "northamerica" file and replace "April" with "Apr" |
| 55 | + northamerica_file <- file.path(tzdata_path, "northamerica") |
| 56 | + if (file.exists(northamerica_file)) { |
| 57 | + file_contents <- readLines(northamerica_file) |
| 58 | + file_contents <- gsub("April", "Apr", file_contents) |
| 59 | + writeLines(file_contents, northamerica_file) |
| 60 | + } else { |
| 61 | + cli::cli_warn("northamerica file not found in tzdata_path") |
| 62 | + } |
| 63 | + |
| 64 | + return(tzdata_path) |
| 65 | +} |
| 66 | + |
| 67 | +#' wrapper for download_tzdata_on_windows for GitHub Actions |
| 68 | +#' @noRd |
| 69 | +#' @keywords internal |
| 70 | +download_tzdata_on_windows_gha <- function() { |
| 71 | + if (Sys.getenv("RUNNER_OS") == "Windows") { |
| 72 | + return(download_tzdata_on_windows("C:/Users/runneradmin/Downloads/tzdata")) |
| 73 | + } else { |
| 74 | + cli::cli_alert_info( |
| 75 | + "Not running on Windows GitHub Action" |
| 76 | + ) |
| 77 | + return(NULL) |
| 78 | + } |
| 79 | +} |
0 commit comments