75fce382ae1f51533ea890ef92dcb47b

Seems like this method could use some re-factoring. Just a simple method which turns some kind of date format (such as a created_at column) into a "human date". E.g.: "July 5, 2008". If time is set to true, then it would return e.g.: "July 5, 2008 at 5:18 PM". Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def date(date, time = false)
    date = Date.parse(date) if date.is_a? String
    day = date.strftime('%d')
    hour = date.strftime('%I') if time
    
    case day
      when '01'..'09' then date_result = date.strftime('%B ') + day.gsub('0', '') + date.strftime(', %Y')
      else date_result = date.strftime('%B %d, %Y')
    end
    
    if time
      case hour
        when '01'..'09' then time_result = ' at ' + hour.gsub('0', '') + date.strftime(':%M %p')
        else time_result = ' at ' + date.strftime('%I:%M %p')
      end
    end
    
    unless time
      return date_result
    else
      return date_result + time_result
    end
  end

Refactorings

No refactoring yet !

1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

July 5, 2008, July 05, 2008 13:10, permalink

No rating. Login to rate!

May be something like that:

1
2
3
4
5
def date(date, time = false)
  date = Date.parse(date) if date.class.is_a? String
  date_str = date.strftime(time ? '%B %d, %Y %I:%M %p' : '%B %d, %Y')
  date_str.gsub(/ 0+/, ' ')
end
F288a8afe5302a16a366d5e9d34f2fec

Joe Grossberg

July 5, 2008, July 05, 2008 15:03, permalink

No rating. Login to rate!

Without getting into better error handling (in case someone passes you an invalid date), this is the initial refactoring I'd do:

1
2
3
4
5
6
7
def date(date, time = false)
    return "" unless date.class.is_a? String
    date = Date.parse(date)
    date_result = date.strftime('%B %d, %Y')
    date_result += date.strftime('at %I:%M %p') if time
    date_result.gsub(' 0', ' ')
end
F9ac05fdd57915983ccea58699e1f942

mvanholstyn

July 5, 2008, July 05, 2008 15:57, permalink

5 ratings. Login to rate!
1
2
3
4
5
def date(date, time = false)
    date = Date.parse(date) if date.class.is_a? String
    format = time ? "%B #{date.day}, %Y at #{date.strftime('%I').to_i}:%M %p" : "%B #{date.day}, %Y"
    date.strftime(format)
end
7bdb696ac0f589522c9ed31a1b24057b

scudco

July 5, 2008, July 05, 2008 21:48, permalink

1 rating. Login to rate!

Edit: Noticed that you had wanted no leading zeros for single digit dates and hours.

Your situation may call for this to all be in one method, but it seems like these should just be two separate methods each expressing what they do. This will help you avoid unreadable looking method calls like date(Time.now, true), where true is never revealing what it is there for. That's my thought, anyways. Good luck!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'date'

now = Time.now

# Note that by calling date.to_s and always parsing you may lose some 
# performance(not really sure) by parsing it every time, but you won't 
# have to check the class of whatever was passed in. That is if date is a string 
# then calling to_s on it will produce itself. If date is a Date/DateTime 
# object then to_s will produce a parseable string. Either way you win.
def date_with_time(date)
  DateTime.parse(date.to_s).strftime("%B %d, %Y at %I:%M %p").gsub(/ 0/,' ')
end

def date(date)
  Date.parse(date.to_s).strftime("%B %d, %Y").sub(/ 0/,' ')
end

puts date(now)
puts date_with_time(now)
Acad2552784135c09b17c00853f5a6f8

dcadenas.blogspot.com

July 6, 2008, July 06, 2008 10:12, permalink

1 rating. Login to rate!

I share the scudco point of view.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def human_date date
  date = Date.parse(date) if date.is_a?(String)
  date.strftime date_format(date)
end

def human_date_with_time date
  date = Date.parse(date) if date.is_a?(String)
  date.strftime "#{date_format(date)} at #{time_format(date)}"
end

def date_format date
  "%B #{date.day}, %Y"
end

def time_format date
  "#{date.strftime('%I').to_i}:%M %p"
end
8484df61a1434e91cb088f53cc089f18

Adam

August 15, 2008, August 15, 2008 15:55, permalink

No rating. Login to rate!

ActiveSupport already does this, sort of. It doesn't include the exact time format you seek. However, you can extend DATE_FORMATS to include your own custom format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require "activesupport"

Date::DATE_FORMATS[:human] = Date::DATE_FORMATS[:long]
Time::DATE_FORMATS[:human] = lambda { |time|
  [ time.to_date.to_s(:human), time.strftime("%I:%M %p").sub(/^0/, "") ].join(" at ")
}

# Date.today.to_s(:human)
# => "August 15, 2008"
#
# Time.now.to_s(:human)
# => "August 15, 2008 at 11:53 AM"
#
# record.created_at.to_s(:human)
# => "August 15, 2008 at 11:53 AM"
#
# record.created_at.to_date.to_s(:human)
# => "August 15, 2008"

Your refactoring





Format Copy from initial code

or Cancel