http://www.ruby-doc.org/core/classes/Time.html#M000236
1 %a - The abbreviated weekday name (``Sun'')
2 %A - The full weekday name (``Sunday'')
3 %b - The abbreviated month name (``Jan'')
4 %B - The full month name (``January'')
5 %c - The preferred local date and time representation
6 %d - Day of the month (01..31)
7 %H - Hour of the day, 24-hour clock (00..23)
8 %I - Hour of the day, 12-hour clock (01..12)
9 %j - Day of the year (001..366)
10 %m - Month of the year (01..12)
11 %M - Minute of the hour (00..59)
12 %p - Meridian indicator (``AM'' or ``PM'')
13 %S - Second of the minute (00..60)
14 %U - Week number of the current year,
15 starting with the first Sunday as the first
16 day of the first week (00..53)
17 %W - Week number of the current year,
18 starting with the first Monday as the first
19 day of the first week (00..53)
20 %w - Day of the week (Sunday is 0, 0..6)
21 %x - Preferred representation for the date alone, no time
22 %X - Preferred representation for the time alone, no date
23 %y - Year without a century (00..99)
24 %Y - Year with century
25 %Z - Time zone name
26 %% - Literal ``%'' character
%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
1
2 def age
3 ((Time.now - birthday)/(60*60*24)/365.2422).to_i
4 end
5
6
7 def birthday
8 @birthday = Time.local( birth_year, birth_month, birth_day )
9 end
10
11 def age
12 ((Time.now - birthday) / 1.year).to_i
13 end
def age
((Time.now - birthday)/(60*60*24)/365.2422).to_i
end
def birthday
@birthday = Time.local( birth_year, birth_month, birth_day )
end
def age
((Time.now - birthday) / 1.year).to_i
end
1 require "rubygems"
2 require "activesupport"
3 require "unicode"
4
5 class String
6 def as_time
7 str = self.dup
8 str.downcase!
9 str.squish!
10 str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')
11
12 now = Time.now
13
14 if str == 'hoje'
15 Date.today.to_time
16 elsif str == 'agora'
17 Time.now
18 elsif str == 'amanha'
19 Date.tomorrow.to_time
20 elsif str == 'ontem'
21 Date.yesterday.to_time
22 elsif str == 'anteontem'
23 2.days.ago
24 elsif str == 'depois de amanha'
25 2.days.from_now
26 elsif str =~ /^proxim[oa] (semana|dia|mes|ano)$/
27 case $1
28 when 'semana' then now.next_week
29 when 'dia' then Date.tomorrow.to_time
30 when 'mes' then now.next_month
31 when 'ano' then now.next_year
32 end
33 elsif str =~ /^(mes|semana|ano) que vem/
34 case $1
35 when 'mes' then now.next_month
36 when 'semana' then now.next_week
37 when 'ano' then now.next_year
38 end
39 elsif str =~ /^(mes|semana|ano) passad[ao]/
40 case $1
41 when 'mes' then now.last_month
42 when 'semana' then now.beginning_of_week
43 when 'ano' then now.last_year
44 end
45 elsif str =~ /^de (tarde|manha|noite)/
46 case $1
47 when 'noite' then now.beginning_of_day + 20.hours
48 when 'tarde' then now.beginning_of_day + 15.hours
49 when 'manha' then now.beginning_of_day + 9.hours
50 end
51 elsif str =~ /^daqui h?a pouco/
52 1.hour.from_now
53 elsif str =~ /^daqui(?: h?a)? ([0-9]+) (dias?|mes(?:es)?|semanas?|anos?|horas?|minutos?)/
54 num = $1.to_i
55 interval = $2.gsub(/e?s$/, '')
56
57 names = {
58 'dia' => :day,
59 'mes' => :month,
60 'semana' => :week,
61 'ano' => :year,
62 'hora' => :hour,
63 'minuto' => :minute
64 }
65
66 num.send(names[interval]).from_now
67 end
68 end
69 end
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