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

Removed space padding before single digit days on date.to_s(:long) and :short #1994

Closed
wants to merge 0 commits into from
Closed

Removed space padding before single digit days on date.to_s(:long) and :short #1994

wants to merge 0 commits into from

Conversation

benjaminleesmith
Copy link

Changed to_s method on date to not add a space before single digit days for :long and :short date formats.

Having an extra space before single digit days doesn't make sense to me. If date contains 2011-7-6 and date.to_s(:long) is called, "July 7, 2011" is returned (with two spaces between July and 7). It seem more intuitive for just "July 7, 2011" to get returned.

@vijaydev vijaydev closed this Nov 18, 2011
@TylerRick
Copy link
Contributor

I'm having the same problem/opinion as you and would like to get rid of the extra space. What do you have to do to fix this? (I don't see any attached commits)

@TylerRick
Copy link
Contributor

I should add that this (empty) pull request is at the top of the Google search results for: rails date formats :long extra space.

@TylerRick
Copy link
Contributor

I think I found the problem and the solution (though it would be great if this change were merged into Rails core as your pull request suggests!):

The problem (as noted on http://stackoverflow.com/questions/4420449/extra-space-in-cucumber-step-when-using-to-slong) is that Date::DATE_FORMATS[:long] is "%B %e, %Y". The %e, according to ri strftime is blank-padded:

      %d - Day of the month, zero-padded (01..31)
              %-d  no-padded (1..31)
      %e - Day of the month, blank-padded ( 1..31)

Which gives me these results by default in Rails 3.1.3:

(rdb:1) d=Time.now.tomorrow.to_date
Wed, 01 Feb 2012

(rdb:1) d.to_s(:long)
"February  1, 2012"

The solution is to use "%-d" for the day in your format string:

(rdb:1) Date::DATE_FORMATS[:long] = "%B %-d, %Y"
"%B %-d, %Y"

(rdb:1) d.to_s(:long)
"February 1, 2012"

@benjaminleesmith
Copy link
Author

Hmmm... Github seems to have "lost" my pull request, as well as my branch from my fork of Rails. I still have it locally, here's the patch (note, this was from months ago, so ymmv):

diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb
index 281e59f..338104f 100644
--- a/activesupport/lib/active_support/core_ext/date/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -5,8 +5,8 @@ require 'active_support/core_ext/module/remove_method'

class Date
DATE_FORMATS = {

  • :short => lambda { |date| date.strftime("#{date.day} %b") },
  • :long => lambda { |date| date.strftime("%B #{date.day}, %Y") }, # => "April 2, 2007",
  • :short => "%e %b",
  • :long => "%B %e, %Y",
    :db => "%Y-%m-%d",
    :number => "%Y%m%d",
    :long_ordinal => lambda { |date| date.strftime("%B #{ActiveSupport::Inflector.ordinalize(date.day)}, %Y") }, # => "April 25th, 2007"
    diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
    index 24430df..b4f848c 100644
    --- a/activesupport/test/core_ext/date_ext_test.rb
    +++ b/activesupport/test/core_ext/date_ext_test.rb
    @@ -3,13 +3,13 @@ require 'active_support/time'

class DateExtCalculationsTest < ActiveSupport::TestCase
def test_to_s

  • date = Date.new(2005, 2, 1)
  • assert_equal "2005-02-01", date.to_s
  • assert_equal "1 Feb", date.to_s(:short)
  • assert_equal "February 1, 2005", date.to_s(:long)
  • assert_equal "February 1st, 2005", date.to_s(:long_ordinal)
  • assert_equal "2005-02-01", date.to_s(:db)
  • assert_equal " 1 Feb 2005", date.to_s(:rfc822)
  • date = Date.new(2005, 2, 21)
  • assert_equal "2005-02-21", date.to_s
  • assert_equal "21 Feb", date.to_s(:short)
  • assert_equal "February 21, 2005", date.to_s(:long)
  • assert_equal "February 21st, 2005", date.to_s(:long_ordinal)
  • assert_equal "2005-02-21", date.to_s(:db)
  • assert_equal "21 Feb 2005", date.to_s(:rfc822)
    end

def test_readable_inspect

SuShu19 pushed a commit to SuShu19/pawQL that referenced this pull request Nov 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants