-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom time format implementations #5123
Merged
bcardiff
merged 8 commits into
crystal-lang:master
from
straight-shoota:jm-time-formats-simplified
Jun 10, 2018
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5568528
Prepare: minor reorganization
straight-shoota e403e3c
Add custom format implementations for ISO 8601, RFC 3339, RFC 2822
straight-shoota 1794a7a
Add formatter and parser class methods to `Time` for easier access
straight-shoota b69bc43
Add custom formatter for HTTP date format
straight-shoota 8d5dc10
fixup! Add custom formatter for HTTP date format
straight-shoota 7cd3a52
fixup! Add custom formatter for HTTP date format
straight-shoota db01f48
fixup! Add custom format implementations for ISO 8601, RFC 3339, RFC …
straight-shoota fee9ec4
fixup! Add custom format implementations for ISO 8601, RFC 3339, RFC …
straight-shoota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add custom formatter for HTTP date format
- Loading branch information
commit b69bc43aec7c2d3630cd4080097af201d91c648a
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
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 |
---|---|---|
|
@@ -4,8 +4,6 @@ | |
{% end %} | ||
|
||
module HTTP | ||
private DATE_PATTERNS = {"%a, %d %b %Y %H:%M:%S %z", "%d %b %Y %H:%M:%S %z", "%A, %d-%b-%y %H:%M:%S %z", "%a %b %e %H:%M:%S %Y"} | ||
|
||
# :nodoc: | ||
MAX_HEADER_SIZE = 16_384 | ||
|
||
|
@@ -227,25 +225,38 @@ module HTTP | |
ComputedContentTypeHeader.new(content_type.strip, nil) | ||
end | ||
|
||
# Parse a time string using the formats specified by [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1) | ||
# | ||
# ``` | ||
# HTTP.parse_time("Sun, 14 Feb 2016 21:00:00 GMT") # => "2016-02-14 21:00:00 UTC" | ||
# HTTP.parse_time("Sunday, 14-Feb-16 21:00:00 GMT") # => "2016-02-14 21:00:00 UTC" | ||
# HTTP.parse_time("Sun Feb 14 21:00:00 2016") # => "2016-02-14 21:00:00 UTC" | ||
# ``` | ||
# | ||
# Uses `Time::Format::HTTP_DATE` as parser. | ||
def self.parse_time(time_str : String) : Time? | ||
DATE_PATTERNS.each do |pattern| | ||
begin | ||
return Time.parse(time_str, pattern, location: Time::Location::UTC) | ||
rescue Time::Format::Error | ||
end | ||
end | ||
|
||
nil | ||
Time::Format::HTTP_DATE.parse(time_str) | ||
rescue Time::Format::Error | ||
end | ||
|
||
# Format a Time object as a String using the format specified by [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55). | ||
# Format a `Time` object as a `String` using the format specified as `sane-cookie-date` | ||
# by [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1) which is | ||
# according to [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1) a | ||
# [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55) format with explicit | ||
# timezone `GMT` (interpreted as `UTC`). | ||
# | ||
# ``` | ||
# HTTP.rfc1123_date(Time.new(2016, 2, 15)) # => "Sun, 14 Feb 2016 21:00:00 GMT" | ||
# HTTP.format_time(Time.new(2016, 2, 15)) # => "Sun, 14 Feb 2016 21:00:00 GMT" | ||
# ``` | ||
def self.rfc1123_date(time : Time) : String | ||
# TODO: GMT should come from the Time classes instead | ||
time.to_utc.to_s("%a, %d %b %Y %H:%M:%S GMT") | ||
# | ||
# Uses `Time::Format::HTTP_DATE` as formatter. | ||
def self.format_time(time : Time) : String | ||
Time::Format::HTTP_DATE.format(time) | ||
end | ||
|
||
# DEPRECATED: Use `HTTP.format_time` instead. | ||
def self.rfc1123_time(time : Time) : String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this is left and marked as deprecated and rfc1123_date dropped completely? I think is either both or none. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it needs to be removed. |
||
format_time(time) | ||
end | ||
|
||
# Dequotes an [RFC 2616](https://tools.ietf.org/html/rfc2616#page-17) | ||
|
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,106 @@ | ||
require "./rfc_2822" | ||
|
||
# Parse a time string using the formats specified by [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1). | ||
# | ||
# Supported formats: | ||
# * [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55) | ||
# * [RFC 850](https://tools.ietf.org/html/rfc850#section-2.1.4) | ||
# * [asctime](http://en.cppreference.com/w/c/chrono/asctime) | ||
# | ||
# ``` | ||
# Time::Format::HTTP_DATE.parse("Sun, 14 Feb 2016 21:00:00 GMT") # => 2016-02-14 21:00:00 UTC | ||
# Time::Format::HTTP_DATE.parse("Sunday, 14-Feb-16 21:00:00 GMT") # => 2016-02-14 21:00:00 UTC | ||
# Time::Format::HTTP_DATE.parse("Sun Feb 14 21:00:00 2016") # => 2016-02-14 21:00:00 UTC | ||
# | ||
# Time::Format::HTTP_DATE.format(Time.new(2016, 2, 15)) # => "Sun, 14 Feb 2016 21:00:00 GMT" | ||
# ``` | ||
struct Time::Format | ||
module HTTP_DATE | ||
# Parses a string into a `Time`. | ||
def self.parse(string, location = Time::Location::UTC) : Time | ||
parser = Parser.new(string) | ||
parser.http_date | ||
parser.time(location) | ||
end | ||
|
||
# Formats a `Time` into the given *io*. | ||
# | ||
# *time* is always converted to UTC. | ||
def self.format(time : Time, io : IO) | ||
formatter = Formatter.new(time.to_utc, io) | ||
formatter.rfc_2822(time_zone_gmt: true) | ||
io | ||
end | ||
|
||
# Formats a `Time` into a `String`. | ||
# | ||
# *time* is always converted to UTC. | ||
def self.format(time : Time) | ||
String.build do |io| | ||
format(time, io) | ||
end | ||
end | ||
end | ||
|
||
struct Parser | ||
def http_date | ||
ansi_c_format = http_date_short_day_name_with_comma? | ||
|
||
if ansi_c_format | ||
return http_date_ansi_c | ||
end | ||
|
||
day_of_month_zero_padded | ||
|
||
if current_char.ascii_whitespace? | ||
whitespace | ||
short_month_name | ||
whitespace | ||
year | ||
else | ||
char '-' | ||
short_month_name | ||
char '-' | ||
year_modulo_100 | ||
end | ||
|
||
whitespace | ||
twenty_four_hour_time_with_seconds | ||
whitespace | ||
time_zone_gmt_or_rfc2822 | ||
end | ||
|
||
def http_date_ansi_c | ||
short_month_name | ||
whitespace | ||
day_of_month_blank_padded | ||
|
||
whitespace | ||
|
||
twenty_four_hour_time_with_seconds | ||
|
||
whitespace | ||
|
||
year | ||
|
||
@location = Time::Location::UTC | ||
end | ||
|
||
def http_date_rfc1123?(ansi_c_format) | ||
!ansi_c_format && current_char.ascii_whitespace? | ||
end | ||
|
||
def http_date_short_day_name_with_comma? | ||
return unless current_char.ascii_letter? | ||
|
||
short_day_name | ||
|
||
ansi_c_format = current_char != ',' | ||
next_char unless ansi_c_format | ||
|
||
whitespace | ||
|
||
ansi_c_format | ||
end | ||
end | ||
end |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The preferred format is with two day digits as of https://tools.ietf.org/html/rfc7231#section-7.1.1.1 and https://tools.ietf.org/html/rfc2616#page-21
Somehow I would prefer rfc/iso in the function names for the format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name was changed because the methods doesn't implement one exact standard format but generally format and parse time instances for use in a HTTP protocol context (see previous comments in #4729 (comment)).
I'll look into the preferred digit format.