Skip to content

Instantly share code, notes, and snippets.

@AeroStun
Created August 6, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AeroStun/4ad6f51f11e60b031bc9b8b0d239ae9e to your computer and use it in GitHub Desktop.
Save AeroStun/4ad6f51f11e60b031bc9b8b0d239ae9e to your computer and use it in GitHub Desktop.
/* Note: HTTP time is defined by RFC2616 at section 3.3.1
*/
/** The datatype which represents an HTTP date-time
*/
using date_time = boost::local_time::local_date_time;
/** Turns an HTTP date string into a date_time
RFC2616 defines HTTP dates with the following ABNF:
HTTP-date = rfc1123-date | rfc850-date | asctime-date
rfc1123-date = wkday "," SP date1 SP time SP "GMT"
rfc850-date = weekday "," SP date2 SP time SP "GMT"
asctime-date = wkday SP date3 SP time SP 4DIGIT
date1 = 2DIGIT SP month SP 4DIGIT
; day month year (e.g., 02 Jun 1982)
date2 = 2DIGIT "-" month "-" 2DIGIT
; day-month-year (e.g., 02-Jun-82)
date3 = month SP ( 2DIGIT | ( SP 1DIGIT ))
; month day (e.g., Jun 2)
time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
; 00:00:00 - 23:59:59
wkday = "Mon" | "Tue" | "Wed"
| "Thu" | "Fri" | "Sat" | "Sun"
weekday = "Monday" | "Tuesday" | "Wednesday"
| "Thursday" | "Friday" | "Saturday" | "Sunday"
month = "Jan" | "Feb" | "Mar" | "Apr"
| "May" | "Jun" | "Jul" | "Aug"
| "Sep" | "Oct" | "Nov" | "Dec"
@return The date_time with date information,
or set to not_a_date_time on error
*/
date_time
parse_datetime(string_view http_date_str) noexcept;
/** Turns a datetime structure into an RFC1123 date string
Preconditions:
- the datetime has GMT time, since HTTP time must be GMT.
If this precondition is not met, the result will be incorrect
- there are 29 (or more) bytes available at storage
If this precondition is not met, the behavior is undefined
Postconditions:
- on valid input, only the first 29 bytes at storage have been written to
- on error, no bytes at storage have been written to
@param dt A datetime structure with GMT time
@param storage A pointer to at least 29 bytes of valid `char` storage
*/
bool
stringify_datetime_at(date_time const& dt, char* storage) noexcept;
/** Turns a datetime structure into an RFC1123 date string
This function assumes the datetime has GMT time, since HTTP time
must be GMT.
@return a 29 characters long string containing the date, or empty on error
*/
std::string
stringify_datetime(date_time const& dt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment