-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' Parse an http date | ||
#' | ||
#' As defined in RFC2616, | ||
#' \url{http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3}, there are | ||
#' three valid formats: | ||
#' \itemize{ | ||
#' \item Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 | ||
#' \item Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 | ||
#' \item Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format | ||
#' } | ||
#' @param x A character vector of strings to parse. All elements must be of | ||
#' the same type. | ||
#' @export | ||
#' @return A POSIXct object if succesful, otherwise NA. | ||
#' @examples | ||
#' parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT") | ||
#' parse_http_date("Sunday, 06-Nov-94 08:49:37 GMT") | ||
#' parse_http_date("Sun Nov 6 08:49:37 1994") | ||
parse_http_date <- function(x) { | ||
fmts <- c( | ||
"%a, %d %b %Y %H:%M:%S", | ||
"%A, %d-%b-%y %H:%M:%S", | ||
"%a %b %d %H:%M:%S %Y" | ||
) | ||
|
||
# Need to set C locale to ensure that months/days are parsed in English | ||
old <- Sys.getlocale(category = "LC_TIME") | ||
Sys.setlocale(category = "LC_TIME", locale = "C") | ||
on.exit(Sys.setlocale(category = "LC_TIME", locale = old)) | ||
|
||
for (fmt in fmts) { | ||
parsed <- as.POSIXct(strptime(x, fmt, tz = "GMT")) | ||
if (all(!is.na(parsed))) return(parsed) | ||
} | ||
|
||
rep(NA, length(x)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
% Generated by roxygen2 (4.0.1): do not edit by hand | ||
\name{parse_http_date} | ||
\alias{parse_http_date} | ||
\title{Parse an http date} | ||
\usage{ | ||
parse_http_date(x) | ||
} | ||
\arguments{ | ||
\item{x}{A character vector of strings to parse. All elements must be of | ||
the same type.} | ||
} | ||
\value{ | ||
A POSIXct object if succesful, otherwise NA. | ||
} | ||
\description{ | ||
As defined in RFC2616, | ||
\url{http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3}, there are | ||
three valid formats: | ||
\itemize{ | ||
\item Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 | ||
\item Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 | ||
\item Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format | ||
} | ||
} | ||
\examples{ | ||
parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT") | ||
parse_http_date("Sunday, 06-Nov-94 08:49:37 GMT") | ||
parse_http_date("Sun Nov 6 08:49:37 1994") | ||
} | ||