Skip to content
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
Prev Previous commit
Next Next commit
fixup! Add custom formatter for HTTP date format
  • Loading branch information
straight-shoota authored and bcardiff committed Jun 9, 2018
commit 7cd3a52dd6681e50feb5c988866e5ac8c9290bb9
4 changes: 2 additions & 2 deletions spec/std/http/http_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ describe HTTP do
describe "generates HTTP date" do
it "without time zone" do
time = Time.utc(1994, 11, 6, 8, 49, 37, nanosecond: 0)
HTTP.format_time(time).should eq("Sun, 6 Nov 1994 08:49:37 GMT")
HTTP.format_time(time).should eq("Sun, 06 Nov 1994 08:49:37 GMT")
end

it "with local time zone" do
time = Time.new(1994, 11, 6, 8, 49, 37, nanosecond: 0, location: Time::Location.load("Europe/Berlin"))
HTTP.format_time(time).should eq(time.to_utc.to_s("%a, %-d %b %Y %H:%M:%S GMT"))
HTTP.format_time(time).should eq(time.to_utc.to_s("%a, %d %b %Y %H:%M:%S GMT"))
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/time/format/custom/http_date.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Time::Format
# *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)
formatter.rfc_2822(time_zone_gmt: true, two_digit_day: true)
io
end

Expand Down
8 changes: 6 additions & 2 deletions src/time/format/custom/rfc_2822.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ struct Time::Format
end

module Pattern
def rfc_2822(time_zone_gmt = false)
def rfc_2822(time_zone_gmt = false, two_digit_day = false)
cfws?
short_day_name_with_comma?
day_of_month
if two_digit_day
day_of_month_zero_padded
else
day_of_month
end
cfws
short_month_name
cfws
Expand Down