Skip to content

Commit 26575aa

Browse files
Support rfc2822 format for Time#to_fs & Date#to_fs (rails#52337)
1 parent c01a846 commit 26575aa

File tree

6 files changed

+13
-0
lines changed

6 files changed

+13
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Support rfc2822 format for Time#to_fs & Date#to_fs.
2+
3+
*Akshay Birajdar*
4+
15
* Optimize load time for `Railtie#initialize_i18n`. Filter `I18n.load_path`s passed to the file watcher to only those
26
under `Rails.root`. Previously the watcher would grab all available locales, including those in gems
37
which do not require a watcher because they won't change.

activesupport/lib/active_support/core_ext/date/conversions.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Date
1717
date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
1818
},
1919
rfc822: "%d %b %Y",
20+
rfc2822: "%d %b %Y",
2021
iso8601: lambda { |date| date.iso8601 }
2122
}
2223

@@ -34,6 +35,7 @@ class Date
3435
# date.to_fs(:long) # => "November 10, 2007"
3536
# date.to_fs(:long_ordinal) # => "November 10th, 2007"
3637
# date.to_fs(:rfc822) # => "10 Nov 2007"
38+
# date.to_fs(:rfc2822) # => "10 Nov 2007"
3739
# date.to_fs(:iso8601) # => "2007-11-10"
3840
#
3941
# == Adding your own date formats to to_fs

activesupport/lib/active_support/core_ext/time/conversions.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Time
2222
offset_format = time.formatted_offset(false)
2323
time.strftime("%a, %d %b %Y %H:%M:%S #{offset_format}")
2424
},
25+
rfc2822: lambda { |time| time.rfc2822 },
2526
iso8601: lambda { |time| time.iso8601 }
2627
}
2728

@@ -40,6 +41,7 @@ class Time
4041
# time.to_fs(:long) # => "January 18, 2007 06:10"
4142
# time.to_fs(:long_ordinal) # => "January 18th, 2007 06:10"
4243
# time.to_fs(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
44+
# time.to_fs(:rfc2822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
4345
# time.to_fs(:iso8601) # => "2007-01-18T06:10:17-06:00"
4446
#
4547
# == Adding your own time formats to +to_fs+

activesupport/test/core_ext/date_ext_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_to_fs
2929
assert_equal "2005-02-21", date.to_fs(:db)
3030
assert_equal "2005-02-21", date.to_fs(:inspect)
3131
assert_equal "21 Feb 2005", date.to_fs(:rfc822)
32+
assert_equal "21 Feb 2005", date.to_fs(:rfc2822)
3233
assert_equal "2005-02-21", date.to_fs(:iso8601)
3334
assert_equal date.to_s, date.to_fs(:doesnt_exist)
3435
assert_equal "21 Feb", date.to_formatted_s(:short)

activesupport/test/core_ext/date_time_ext_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def test_to_fs
2121
assert_equal "21 Feb 14:30", datetime.to_fs(:short)
2222
assert_equal "February 21, 2005 14:30", datetime.to_fs(:long)
2323
assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.to_fs(:rfc822)
24+
assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.to_fs(:rfc2822)
2425
assert_equal "February 21st, 2005 14:30", datetime.to_fs(:long_ordinal)
2526
assert_match(/^2005-02-21T14:30:00(Z|\+00:00)$/, datetime.to_fs)
2627
assert_match(/^2005-02-21T14:30:00(Z|\+00:00)$/, datetime.to_fs(:not_existent))

activesupport/test/core_ext/time_ext_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,11 +832,14 @@ def test_to_fs
832832
assert_equal "February 21st, 2005 17:44", time.to_fs(:long_ordinal)
833833
with_env_tz "UTC" do
834834
assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_fs(:rfc822)
835+
assert_equal "Mon, 21 Feb 2005 17:44:30 -0000", time.to_fs(:rfc2822)
835836
assert_equal "2005-02-21 17:44:30.123456789 +0000", time.to_fs(:inspect)
836837
end
837838
with_env_tz "US/Central" do
838839
assert_equal "Thu, 05 Feb 2009 14:30:05 -0600", Time.local(2009, 2, 5, 14, 30, 5).to_fs(:rfc822)
839840
assert_equal "Mon, 09 Jun 2008 04:05:01 -0500", Time.local(2008, 6, 9, 4, 5, 1).to_fs(:rfc822)
841+
assert_equal "Thu, 05 Feb 2009 14:30:05 -0600", Time.local(2009, 2, 5, 14, 30, 5).to_fs(:rfc2822)
842+
assert_equal "Mon, 09 Jun 2008 04:05:01 -0500", Time.local(2008, 6, 9, 4, 5, 1).to_fs(:rfc2822)
840843
assert_equal "2009-02-05T14:30:05-06:00", Time.local(2009, 2, 5, 14, 30, 5).to_fs(:iso8601)
841844
assert_equal "2008-06-09T04:05:01-05:00", Time.local(2008, 6, 9, 4, 5, 1).to_fs(:iso8601)
842845
assert_equal "2009-02-05T14:30:05Z", Time.utc(2009, 2, 5, 14, 30, 5).to_fs(:iso8601)

0 commit comments

Comments
 (0)