dater 0.0.1
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/.gitignore +18 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +1 -0
- data/dater.gemspec +23 -0
- data/lib/dater.rb +62 -0
- data/lib/dater/version.rb +3 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Roman Rodriguez
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
dater
|
|
2
|
+
=====
|
|
3
|
+
|
|
4
|
+
Convert literal periods of time in a formatted date
|
|
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.
|
|
8
|
+
|
|
9
|
+
You can also, convert dates from 'dd/mm/yyyy' to 'yyyy-mm-dd' or viceversa.
|
|
10
|
+
|
|
11
|
+
You have to be care about only two things in the argument if you want to pass a formatted date to convert:
|
|
12
|
+
- Year number must have four digits.
|
|
13
|
+
- Month number must be always in the middle.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Add this line to your application's Gemfile:
|
|
18
|
+
|
|
19
|
+
gem 'dater'
|
|
20
|
+
|
|
21
|
+
And then execute:
|
|
22
|
+
|
|
23
|
+
$ bundle
|
|
24
|
+
|
|
25
|
+
Or install it yourself as:
|
|
26
|
+
|
|
27
|
+
$ gem install dater
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
Initialize a dater object with a format to convert to:
|
|
32
|
+
|
|
33
|
+
date=Dater::Resolver.new('%Y-%m-%d')
|
|
34
|
+
|
|
35
|
+
To get the converted date:
|
|
36
|
+
|
|
37
|
+
date.for("in 2 days") # => yyyy-mm-dd (date for 2 days from the date of today)
|
|
38
|
+
date.for("in 10 months") # => yyyy-mm-dd (date for 10 months from the date of today)
|
|
39
|
+
date.for("in 1 year") # => yyyy-mm-dd (date for 1 year from the date of today)
|
|
40
|
+
|
|
41
|
+
==================================================
|
|
42
|
+
|
|
43
|
+
Si deseas usar esta gema en español puedes inicializar la clase de la siguiente manera:
|
|
44
|
+
|
|
45
|
+
date=Dater::Resolver.new('%Y-%m-%d', "en")
|
|
46
|
+
|
|
47
|
+
Con eso puedes pasar argumentos en idioma español (en 2 días, en 10 meses, en 1 año)
|
|
48
|
+
|
|
49
|
+
==================================================
|
|
50
|
+
|
|
51
|
+
Se você quiser usar esta gem em Português, você pode inicializar a classe da seguinte forma:
|
|
52
|
+
|
|
53
|
+
date=Dater::Resolver.new('%Y-%m-%d', "pt")
|
|
54
|
+
|
|
55
|
+
Com isso você pode passar argumentos em Português (em dois dias, em 10 meses, 1 ano)
|
|
56
|
+
## Contributing
|
|
57
|
+
|
|
58
|
+
1. Fork it
|
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
62
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/dater.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'dater/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "dater"
|
|
8
|
+
spec.version = Dater::VERSION
|
|
9
|
+
spec.authors = ["Roman Rodriguez"]
|
|
10
|
+
spec.email = ["roman.g.rodriguez@gmail.com"]
|
|
11
|
+
spec.description = %q{This gem aims to help you with dates treatment, specially in regression test where you have to use future dates}
|
|
12
|
+
spec.summary = %q{Converts periods of time expresed in literal mode to a formatted date from the date of today}
|
|
13
|
+
spec.homepage = "https://github.com/gitroman/dater"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
end
|
data/lib/dater.rb
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
require_relative "dater/version"
|
|
3
|
+
|
|
4
|
+
module Dater
|
|
5
|
+
|
|
6
|
+
class Resolver
|
|
7
|
+
|
|
8
|
+
# Creates a Dater::Resolver object
|
|
9
|
+
#
|
|
10
|
+
# Param [String] format = date format
|
|
11
|
+
# Param [String] lang = languaje for matching (en=english, es=spanish, pt=portuguese)
|
|
12
|
+
def initialize(format='%Y-%m-%d', lang="en")
|
|
13
|
+
@format=format
|
|
14
|
+
@lang=lang
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Convert the period of time passed as argument to the configured format
|
|
18
|
+
#
|
|
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+(3600*24)).strftime(@format) if period.nil?
|
|
23
|
+
if period.include?('/')
|
|
24
|
+
date=period.split('/')
|
|
25
|
+
return date.join('-') if date[0].size==4
|
|
26
|
+
return "#{date[2]}-#{date[1]}-#{date[0]}"
|
|
27
|
+
elsif period.include?('-')
|
|
28
|
+
date=period.split('-')
|
|
29
|
+
return date.join('-') if date[0].size==4
|
|
30
|
+
return "#{date[2]}-#{date[1]}-#{date[0]}"
|
|
31
|
+
elsif (amount=period.scan(/\d+/)).size>0
|
|
32
|
+
case @lang
|
|
33
|
+
when 'es'
|
|
34
|
+
days=true if period.scan(/dia(s)?/).size>0
|
|
35
|
+
months=true if period.scan(/mes(es)?/).size>0
|
|
36
|
+
year=true if period.scan(/año(s)?/).size>0
|
|
37
|
+
when 'en'
|
|
38
|
+
days=true if period.scan(/day(s)?/).size>0
|
|
39
|
+
months=true if period.scan(/month(s)?/).size>0
|
|
40
|
+
year=true if period.scan(/year(s)?/).size>0
|
|
41
|
+
when 'pt'
|
|
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(/ano(s)?/).size>0
|
|
45
|
+
end
|
|
46
|
+
if year
|
|
47
|
+
multiply_by=3600*24*30*12
|
|
48
|
+
elsif months
|
|
49
|
+
multiply_by=3600*24*30
|
|
50
|
+
else
|
|
51
|
+
multiply_by=3600*24
|
|
52
|
+
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
|
+
end
|
|
59
|
+
return @date
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dater
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Roman Rodriguez
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.3'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.3'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rake
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
description: This gem aims to help you with dates treatment, specially in regression
|
|
47
|
+
test where you have to use future dates
|
|
48
|
+
email:
|
|
49
|
+
- roman.g.rodriguez@gmail.com
|
|
50
|
+
executables: []
|
|
51
|
+
extensions: []
|
|
52
|
+
extra_rdoc_files: []
|
|
53
|
+
files:
|
|
54
|
+
- .gitignore
|
|
55
|
+
- Gemfile
|
|
56
|
+
- Gemfile.lock
|
|
57
|
+
- LICENSE.txt
|
|
58
|
+
- README.md
|
|
59
|
+
- Rakefile
|
|
60
|
+
- dater.gemspec
|
|
61
|
+
- lib/dater.rb
|
|
62
|
+
- lib/dater/version.rb
|
|
63
|
+
homepage: https://github.com/gitroman/dater
|
|
64
|
+
licenses:
|
|
65
|
+
- MIT
|
|
66
|
+
post_install_message:
|
|
67
|
+
rdoc_options: []
|
|
68
|
+
require_paths:
|
|
69
|
+
- lib
|
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
|
+
none: false
|
|
72
|
+
requirements:
|
|
73
|
+
- - ! '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
none: false
|
|
78
|
+
requirements:
|
|
79
|
+
- - ! '>='
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 1.8.25
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 3
|
|
87
|
+
summary: Converts periods of time expresed in literal mode to a formatted date from
|
|
88
|
+
the date of today
|
|
89
|
+
test_files: []
|