From 2eae8ddb04c9544d8df8c9db75c8aff6bf950ce9 Mon Sep 17 00:00:00 2001 From: Aron Atkins Date: Wed, 17 Jan 2024 16:43:33 -0500 Subject: [PATCH] Sys.setlocale returns the updated locale, not the previous one (#1035) --- NEWS.md | 2 ++ R/http.R | 5 +++-- tests/testthat/test-http.R | 9 +++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index cb737ade..c61e96a5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # rsconnect (development version) +* Restore the `LC_TIME` locale after computing an RFC-2616 date. (#1035) + # rsconnect 1.2.0 * Addressed a number of republishing and collaboration issues where the diff --git a/R/http.R b/R/http.R index a90bc4d8..467fa744 100644 --- a/R/http.R +++ b/R/http.R @@ -518,10 +518,11 @@ signatureHeaders <- function(authInfo, method, path, file = NULL) { rfc2616Date <- function(time = Sys.time()) { # set locale to POSIX/C to ensure ASCII date - old <- Sys.setlocale("LC_TIME", "C") + old <- Sys.getlocale("LC_TIME") + Sys.setlocale("LC_TIME", "C") defer(Sys.setlocale("LC_TIME", old)) - strftime(Sys.time(), "%a, %d %b %Y %H:%M:%S GMT", tz = "GMT") + strftime(time, "%a, %d %b %Y %H:%M:%S GMT", tz = "GMT") } # Helpers ----------------------------------------------------------------- diff --git a/tests/testthat/test-http.R b/tests/testthat/test-http.R index 61ebe287..39b42485 100644 --- a/tests/testthat/test-http.R +++ b/tests/testthat/test-http.R @@ -177,3 +177,12 @@ test_that("parse and build are symmetric", { round_trip("https://google.com:80/a/b") round_trip("https://google.com:80/a/b/") }) + +test_that("rcf2616 returns an ASCII date and undoes changes to the locale", { + old <- Sys.getlocale("LC_TIME") + defer(Sys.setlocale("LC_TIME", old)) + + date <- rfc2616Date(time = as.POSIXct("2024-01-01 01:02:03", tz = "EST")) + expect_equal(date, "Mon, 01 Jan 2024 06:02:03 GMT") + expect_equal(Sys.getlocale("LC_TIME"), old) +})