need to set the LC_TIME to C to ensure the Date is formatted in English#319
need to set the LC_TIME to C to ensure the Date is formatted in English#319wch merged 3 commits intorstudio:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #319 +/- ##
==========================================
+ Coverage 88.87% 88.98% +0.11%
==========================================
Files 27 27
Lines 1177 1189 +12
==========================================
+ Hits 1046 1058 +12
Misses 131 131
Continue to review full report at Codecov.
|
|
I think it would be better to implement this without setting the locale. I think that can potentially cause problems if there are multiple threads. Here's an implementation of the same thing I wrote in C++: https://github.com/rstudio/httpuv/blob/a830a0d/src/utils.h#L197-L247 Now that I think of it, I could export that function from httpuv. |
|
Here's an R function that creates a HTTP date string from a POSIXct object. (I wrote this in the process of creating tests for httpuv). # Given a POSIXct object, return a date string in the format required for a
# HTTP Date header. For example: "Wed, 21 Oct 2015 07:28:00 GMT"
http_date_string <- function(time) {
weekday_names <- c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
weekday_num <- as.integer(strftime(time, format = "%w", tz = "GMT"))
weekday_name <- weekday_names[weekday_num + 1]
month_names <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
month_num <- as.integer(strftime(time, format = "%m", tz = "GMT"))
month_name <- month_names[month_num]
strftime(time,
paste0(weekday_name, ", %d ", month_name, " %Y %H:%M:%S GMT"),
tz = "GMT"
)
}
http_date_string(Sys.time())
#> [1] "Wed, 12 Dec 2018 05:54:57 GMT" |
…changes Thanks to @wch
5cdc558 to
22397cf
Compare
|
@wch I've changed the PR based on your code. Thanks. |
Hi, the
PlumberResponsewill attachDateto the header. However, the output of the below line will be different on a different computer (see the example below), which is problematic because normally the non-English date will not be parsed correctly (for example, httr will parse the date as NA)...With this PR, we can ensure the date is always in English.
https://github.com/trestletech/plumber/blob/8c25b8bfba9774bd422022dc29662ffc17e170bc/R/response.R#L19
The example
Created on 2018-10-12 by the reprex package (v0.2.1)