dater 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/README.md +20 -7
- data/lib/dater/version.rb +1 -1
- data/lib/dater.rb +46 -37
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
|
1
|
+
Dater
|
2
2
|
=====
|
3
3
|
|
4
|
-
Convert
|
5
|
-
# Dater
|
6
|
-
|
7
|
-
Convert a period of time expressed in a literal way like 'in 2 days' to a real date with a given format from today.
|
4
|
+
Convert a period of time expressed in a literal way like 'in 2 days' to a formatted future date with a given format. It aims to be helpful in regression tests when you have to deal with dinamic dates.
|
8
5
|
|
9
6
|
You can also, convert dates from 'dd/mm/yyyy' to 'yyyy-mm-dd' or viceversa.
|
10
7
|
|
11
|
-
|
8
|
+
If you want to pass a formatted date like dd/mm/yyyy to convert to yyyy-mm-dd, you have to take care about only two things with the argument:
|
9
|
+
|
12
10
|
- Year number must have four digits.
|
11
|
+
|
13
12
|
- Month number must be always in the middle.
|
14
13
|
|
15
14
|
## Installation
|
@@ -38,13 +37,27 @@ To get the converted date:
|
|
38
37
|
date.for("in 10 months") # => yyyy-mm-dd (date for 10 months from the date of today)
|
39
38
|
date.for("in 1 year") # => yyyy-mm-dd (date for 1 year from the date of today)
|
40
39
|
|
40
|
+
date.for("today") # => yyyy-mm-dd (date of today)
|
41
|
+
|
42
|
+
date.for("tomorrow") # => yyyy-mm-dd (date of tomorrow)
|
43
|
+
|
44
|
+
date.for("yesterday") # => yyyy-mm-dd (date of yesterday)
|
45
|
+
|
46
|
+
If the argument is nil, it will return the date of today.
|
47
|
+
|
48
|
+
You'll find format directives at following link:
|
49
|
+
|
50
|
+
http://apidock.com/ruby/Time/strftime
|
51
|
+
|
52
|
+
It does not work correctly with leap-years and calculates months of 30 days only.
|
53
|
+
|
41
54
|
==================================================
|
42
55
|
|
43
56
|
Si deseas usar esta gema en español puedes inicializar la clase de la siguiente manera:
|
44
57
|
|
45
58
|
date=Dater::Resolver.new('%Y-%m-%d', "en")
|
46
59
|
|
47
|
-
Con eso puedes pasar argumentos en idioma español (en 2 días, en 10 meses, en 1 año)
|
60
|
+
Con eso puedes pasar argumentos en idioma español (por ejemplo 'en 2 días', 'en 10 meses', 'en 1 año')
|
48
61
|
|
49
62
|
==================================================
|
50
63
|
|
data/lib/dater/version.rb
CHANGED
data/lib/dater.rb
CHANGED
@@ -11,50 +11,59 @@ module Dater
|
|
11
11
|
# Param [String] lang = languaje for matching (en=english, es=spanish, pt=portuguese)
|
12
12
|
def initialize(format='%Y-%m-%d', lang="en")
|
13
13
|
@format=format
|
14
|
-
@lang=lang
|
14
|
+
@lang=lang if ["en","es","pt"].include? lang
|
15
15
|
end
|
16
16
|
|
17
17
|
# Convert the period of time passed as argument to the configured format
|
18
18
|
#
|
19
19
|
# Param [String] period = a period of time like "in 3 days" or "in 10 months" or "in 2 years". It could be a formatted date to convert to the wanted format
|
20
|
-
# Return [String] converted date to the configured format
|
21
|
-
def for(period)
|
22
|
-
return (Time.now
|
23
|
-
|
24
|
-
|
25
|
-
return
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
return
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
20
|
+
# Return [String] converted date to the configured format. If period is nil, returns date for tomorrow
|
21
|
+
def for(period=nil)
|
22
|
+
return (Time.now).strftime(@format) if period.nil?
|
23
|
+
case period.downcase
|
24
|
+
when 'today','hoy','hoje'
|
25
|
+
return (Time.now).strftime(@format)
|
26
|
+
when 'tomorrow','mañana','manhã'
|
27
|
+
return (Time.now+(3600*24)).strftime(@format)
|
28
|
+
when 'yesterday','ayer','ontem'
|
29
|
+
return (Time.now-(3600*24)).strftime(@format)
|
30
|
+
else
|
31
|
+
if period.include?('/')
|
32
|
+
date=period.split('/')
|
33
|
+
return date.join('-') if date.first.size==4
|
34
|
+
return "#{date[2]}-#{date[1]}-#{date[0]}"
|
35
|
+
elsif period.include?('-')
|
36
|
+
date=period.split('-')
|
37
|
+
return date.join('-') if date.first.size==4
|
38
|
+
return "#{date[2]}-#{date[1]}-#{date[0]}"
|
39
|
+
elsif (amount=period.scan(/\d+/)).size>0
|
40
|
+
case @lang
|
41
|
+
when 'es'
|
42
|
+
days=true if period.scan(/dia(s)?/).size>0
|
43
|
+
months=true if period.scan(/mes(es)?/).size>0
|
44
|
+
year=true if period.scan(/año(s)?/).size>0
|
45
|
+
when 'en'
|
46
|
+
days=true if period.scan(/day(s)?/).size>0
|
47
|
+
months=true if period.scan(/month(s)?/).size>0
|
48
|
+
year=true if period.scan(/year(s)?/).size>0
|
49
|
+
when 'pt'
|
50
|
+
days=true if period.scan(/dia(s)?/).size>0
|
51
|
+
months=true if period.scan(/mes(es)?/).size>0
|
52
|
+
year=true if period.scan(/ano(s)?/).size>0
|
53
|
+
end
|
54
|
+
if year
|
55
|
+
multiply_by=3600*24*30*12
|
56
|
+
elsif months
|
57
|
+
multiply_by=3600*24*30
|
58
|
+
else
|
59
|
+
multiply_by=3600*24
|
60
|
+
end
|
61
|
+
additional=amount[0].to_i*multiply_by
|
62
|
+
@date=Time.now+additional
|
63
|
+
@date=@date.strftime(@format)
|
50
64
|
else
|
51
|
-
|
65
|
+
return period
|
52
66
|
end
|
53
|
-
additional=amount[0].to_i*multiply_by
|
54
|
-
@date=Time.now+additional
|
55
|
-
@date=@date.strftime(@format)
|
56
|
-
else
|
57
|
-
return period
|
58
67
|
end
|
59
68
|
return @date
|
60
69
|
end
|