dater 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/dater/version.rb +1 -1
- data/lib/dater.rb +81 -32
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/lib/dater/version.rb
CHANGED
data/lib/dater.rb
CHANGED
@@ -9,13 +9,13 @@ module Dater
|
|
9
9
|
attr_accessor :format, :lang
|
10
10
|
|
11
11
|
DICTIONARY = {
|
12
|
-
day: { "en"=>/
|
13
|
-
week: { "en"=>/
|
14
|
-
month: { "en"=>/
|
15
|
-
year: { "en"=>/
|
16
|
-
today: { "en"=>'today', "es" => 'hoy', "pt" => 'hoje'},
|
17
|
-
tomorrow: { "en"=>'tomorrow', "es" => 'mañana', "pt" => 'manhã'},
|
18
|
-
yesterday:{ "en"=>'yesterday',"es" => 'ayer', "pt" => 'ontem'}
|
12
|
+
day: { "en"=>/days/, "es" => /(dias|días)/,"pt" => /dias/, :mult => 86400},
|
13
|
+
week: { "en"=>/weeks/, "es" => /semanas/, "pt" => /semanas/, :mult => 604800},
|
14
|
+
month: { "en"=>/months/, "es" => /meses/, "pt" => /meses/, :mult => 2592000},
|
15
|
+
year: { "en"=>/years/, "es" => /años/, "pt" => /anos/, :mult => 31536000},
|
16
|
+
today: { "en"=>'today', "es" => 'hoy', "pt" => 'hoje' },
|
17
|
+
tomorrow: { "en"=>'tomorrow', "es" => 'mañana', "pt" => 'manhã' },
|
18
|
+
yesterday:{ "en"=>'yesterday',"es" => 'ayer', "pt" => 'ontem' }
|
19
19
|
}
|
20
20
|
|
21
21
|
# Creates a Dater::Resolver object
|
@@ -33,26 +33,21 @@ module Dater
|
|
33
33
|
# Return [String] converted date to the configured format. If period is nil, returns date for tomorrow
|
34
34
|
def for(period=nil)
|
35
35
|
return (Time.now).strftime(@format) if period.nil?
|
36
|
-
case period.downcase
|
36
|
+
@date=case period.downcase
|
37
37
|
when DICTIONARY[:today][@lang]
|
38
|
-
|
38
|
+
self.today(false)
|
39
39
|
when DICTIONARY[:tomorrow][@lang]
|
40
|
-
|
40
|
+
self.tomorrow(false)
|
41
41
|
when DICTIONARY[:yesterday][@lang]
|
42
|
-
|
43
|
-
when /\d
|
44
|
-
|
45
|
-
when /\d
|
46
|
-
|
42
|
+
self.yesterday(false)
|
43
|
+
when /\d+.\d+.\d+/
|
44
|
+
time_from_date(period)
|
45
|
+
when /\d+\s.+/
|
46
|
+
Time.now+period.scan(/\d+/)[0].to_i*self.multiply_by(period,@lang)
|
47
47
|
else
|
48
|
-
|
49
|
-
additional=amount[0].to_i*self.multiply_by(period,@lang)
|
50
|
-
@date=Time.now+additional
|
51
|
-
else
|
52
|
-
return period
|
53
|
-
end
|
48
|
+
return period
|
54
49
|
end
|
55
|
-
|
50
|
+
# unless parts.nil?
|
56
51
|
return @date.strftime(@format)
|
57
52
|
end
|
58
53
|
|
@@ -62,28 +57,82 @@ module Dater
|
|
62
57
|
# Param [String] lang = the languaje to eval
|
63
58
|
# Return [Hash] times
|
64
59
|
def multiply_by(period, lang)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
60
|
+
mult = 1
|
61
|
+
mult = DICTIONARY[:day][:mult] if is_day?(period, lang)
|
62
|
+
mult = DICTIONARY[:week][:mult] if is_week?(period, lang)
|
63
|
+
mult = DICTIONARY[:month][:mult] if is_month?(period, lang)
|
64
|
+
mult = DICTIONARY[:year][:mult] if is_year?(period, lang)
|
65
|
+
return mult
|
66
|
+
end
|
67
|
+
|
68
|
+
def is_day?(period, lang)
|
69
|
+
period.scan(DICTIONARY[:day][lang]).size > 0 ? true : false
|
70
|
+
end
|
71
|
+
|
72
|
+
def is_week?(period, lang)
|
73
|
+
period.scan(DICTIONARY[:week][lang]).size > 0 ? true : false
|
74
|
+
end
|
75
|
+
|
76
|
+
def is_month?(period, lang)
|
77
|
+
period.scan(DICTIONARY[:month][lang]).size > 0 ? true : false
|
78
|
+
end
|
79
|
+
|
80
|
+
def is_year?(period, lang)
|
81
|
+
period.scan(DICTIONARY[:year][lang]).size > 0 ? true : false
|
70
82
|
end
|
71
83
|
|
72
84
|
# Return the Time object according to the splitted date in the given array
|
73
|
-
#
|
85
|
+
# |
|
74
86
|
# Param [Array] date = date splitted
|
75
87
|
# Return [Time]
|
76
88
|
def time_from_date(date)
|
77
|
-
date.map!{|i| i.to_i}
|
78
|
-
if
|
79
|
-
return Date.new(
|
89
|
+
numbers=date.scan(/\d+/).map!{|i| i.to_i}
|
90
|
+
if numbers.first.to_s.size==4
|
91
|
+
return Date.new(numbers[0],numbers[1],numbers[2]).to_time
|
80
92
|
else
|
81
|
-
return Date.new(
|
93
|
+
return Date.new(numbers[2],numbers[1],numbers[0]).to_time
|
82
94
|
end
|
83
95
|
end
|
84
96
|
|
85
97
|
def para(period)
|
86
98
|
self.for(period)
|
87
99
|
end
|
100
|
+
|
101
|
+
def today(formatted=true)
|
102
|
+
formatted ? Time.now.strftime(@format) :Time.now
|
103
|
+
end
|
104
|
+
|
105
|
+
def hoy
|
106
|
+
self.today(true)
|
107
|
+
end
|
108
|
+
|
109
|
+
def hoje
|
110
|
+
self.today(true)
|
111
|
+
end
|
112
|
+
|
113
|
+
def yesterday(formatted=true)
|
114
|
+
formatted ? (Time.now-(3600*24)).strftime(@format) : Time.now-(3600*24)
|
115
|
+
end
|
116
|
+
|
117
|
+
def ayer
|
118
|
+
self.yesterday(true)
|
119
|
+
end
|
120
|
+
|
121
|
+
def ontem
|
122
|
+
self.yesterday(true)
|
123
|
+
end
|
124
|
+
|
125
|
+
def tomorrow(formatted=true)
|
126
|
+
formatted ? (Time.now+(3600*24)).strftime(@format) : (Time.now+(3600*24))
|
127
|
+
end
|
128
|
+
|
129
|
+
def mañana
|
130
|
+
self.tomorrow(true)
|
131
|
+
end
|
132
|
+
|
133
|
+
def manhã
|
134
|
+
self.tomorrow(true)
|
135
|
+
end
|
136
|
+
|
88
137
|
end
|
89
138
|
end
|