you are here: codestackercodes [RSS] → tag time [RSS]

humanize secs

source: http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails

def humanize secs
  [[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map{ |count, name|
    if secs > 0
      secs, n = secs.divmod(count)
      "#{n.to_i} #{name}"
    end
  }.compact.reverse.join(' ')
end

p humanize 1234
#=>"20 minutes 34 seconds"

p humanize 12345
#=>"3 hours 25 minutes 45 seconds"

p humanize 123456
#=>"1 days 10 hours 17 minutes 36 seconds"

p humanize(Time.now - Time.local(2010,11,5))
#=>"4 days 18 hours 24 minutes 7 seconds"

time format in ruby

http://www.ruby-doc.org/core/classes/Time.html#M000236

  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character

age calculation

# one method

def age
 ((Time.now - birthday)/(60*60*24)/365.2422).to_i
end

# second method

def birthday
    @birthday = Time.local( birth_year, birth_month, birth_day )
  end
  
  def age
    ((Time.now - birthday) / 1.year).to_i
  end 

Chronic-like in Portuguese

require "rubygems"
require "activesupport"
require "unicode"

class String
  def as_time
    str = self.dup
    str.downcase!
    str.squish!
    str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')
    
    now = Time.now

    if str == 'hoje'
      Date.today.to_time
    elsif str == 'agora'
      Time.now
    elsif str == 'amanha'
      Date.tomorrow.to_time
    elsif str == 'ontem'
      Date.yesterday.to_time
    elsif str == 'anteontem'
      2.days.ago
    elsif str == 'depois de amanha'
      2.days.from_now
    elsif str =~ /^proxim[oa] (semana|dia|mes|ano)$/
      case $1
        when 'semana' then now.next_week
        when 'dia' then Date.tomorrow.to_time
        when 'mes' then now.next_month
        when 'ano' then now.next_year
      end
    elsif str =~ /^(mes|semana|ano) que vem/
      case $1
        when 'mes' then now.next_month
        when 'semana' then now.next_week
        when 'ano' then now.next_year
      end
    elsif str =~ /^(mes|semana|ano) passad[ao]/
      case $1
        when 'mes' then now.last_month
        when 'semana' then now.beginning_of_week
        when 'ano' then now.last_year
      end
    elsif str =~ /^de (tarde|manha|noite)/
      case $1
        when 'noite' then now.beginning_of_day + 20.hours
        when 'tarde' then now.beginning_of_day + 15.hours
        when 'manha' then now.beginning_of_day + 9.hours
      end
    elsif str =~ /^daqui h?a pouco/
      1.hour.from_now
    elsif str =~ /^daqui(?: h?a)? ([0-9]+) (dias?|mes(?:es)?|semanas?|anos?|horas?|minutos?)/
      num = $1.to_i
      interval = $2.gsub(/e?s$/, '')

      names = {
        'dia' => :day,
        'mes' => :month,
        'semana' => :week,
        'ano' => :year,
        'hora' => :hour,
        'minuto' => :minute
      }
      
      num.send(names[interval]).from_now
    end
  end
end
Displaying all 4 Code