Skip to content

Commit

Permalink
Add parse_http_date. #129
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 23, 2014
1 parent 4b00524 commit 99d2762
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export(oauth_exchanger)
export(oauth_header)
export(oauth_listener)
export(oauth_signature)
export(parse_http_date)
export(parse_media)
export(parse_url)
export(parsed_content)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* `url_ok()` works correctly now, instead of always returning `FALSE`,
a bug since version 0.4 (#133).

## Caching

* `parse_http_date()` parses http dates according RFC2616 spec.


# httr 0.4

## New features
Expand Down
37 changes: 37 additions & 0 deletions R/date.R
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))
}
30 changes: 30 additions & 0 deletions man/parse_http_date.Rd
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")
}

0 comments on commit 99d2762

Please sign in to comment.