timelord 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/Rakefile +30 -0
- data/lib/timelord.rb +75 -0
- metadata +68 -0
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
Spec::Rake::SpecTask.new do |t|
|
6
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
7
|
+
end
|
8
|
+
|
9
|
+
task :default => [:test]
|
10
|
+
task :test => [:spec]
|
11
|
+
|
12
|
+
PKG_FILES = %w(Rakefile) + Dir.glob("{lib}/**/*")
|
13
|
+
|
14
|
+
gem_spec = Gem::Specification.new do |spec|
|
15
|
+
spec.name = 'timelord'
|
16
|
+
spec.version = '0.0.1'
|
17
|
+
spec.summary = 'Pull dates out of strings'
|
18
|
+
spec.description = 'Pull dates out of strings.'
|
19
|
+
spec.email = 'halogenandtoast@gmail.com'
|
20
|
+
spec.homepage = 'http://github.com/halogenandtoast/timelord'
|
21
|
+
spec.authors = ['Matthew Mongeau']
|
22
|
+
spec.files = PKG_FILES
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Generate a gemspec file"
|
26
|
+
task :gemspec do
|
27
|
+
File.open("#{gem_spec.name}.gemspec", "w") do |f|
|
28
|
+
f.write gem_spec.to_yaml
|
29
|
+
end
|
30
|
+
end
|
data/lib/timelord.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class Timelord
|
4
|
+
|
5
|
+
SHORT_MONTHS = %w(jan feb mar apr may jun jul aug sep oct nov dec).freeze
|
6
|
+
SHORT_MATCHER = SHORT_MONTHS.join('|').freeze
|
7
|
+
ORDINAL_MATCHER = "st|nd|rd|th".freeze
|
8
|
+
DAY_NAMES = %w(monday tuesday wednesday thursday friday saturday sunday).freeze
|
9
|
+
DAY_MATCHER = DAY_NAMES.join('|').freeze
|
10
|
+
|
11
|
+
def self.parse(str, format = :international)
|
12
|
+
today = Date.today
|
13
|
+
if str =~ /(\d{4})\/(\d{1,2})\/(\d{1,2})/i
|
14
|
+
Date.civil($1.to_i, $2.to_i, $3.to_i)
|
15
|
+
elsif str =~ /(\d{4})\-(\d{1,2})\-(\d{1,2})/i
|
16
|
+
Date.civil($1.to_i, $2.to_i, $3.to_i)
|
17
|
+
elsif str =~ /(\d{1,2})\/(\d{1,2})\/(\d{2}(\d{2})?)/i
|
18
|
+
if format == :american
|
19
|
+
Date.civil($3.to_i, $1.to_i, $2.to_i)
|
20
|
+
else
|
21
|
+
Date.civil($3.to_i, $2.to_i, $1.to_i)
|
22
|
+
end
|
23
|
+
elsif str =~ /(\d{1,2})\/(\d{1,2})/i
|
24
|
+
date = if format == :american
|
25
|
+
Date.civil(today.year, $1.to_i, $2.to_i)
|
26
|
+
else
|
27
|
+
Date.civil(today.year, $2.to_i, $1.to_i)
|
28
|
+
end
|
29
|
+
set_to_future(date)
|
30
|
+
elsif str =~ /(\d{1,2})\s+(#{SHORT_MATCHER})/i
|
31
|
+
date = Date.civil(today.year, SHORT_MONTHS.index($2.downcase) + 1, $1.to_i)
|
32
|
+
set_to_future(date)
|
33
|
+
elsif str =~ /(#{SHORT_MATCHER})\s+(\d{1,2})/i
|
34
|
+
date = Date.civil(today.year, SHORT_MONTHS.index($1.downcase) + 1, $2.to_i)
|
35
|
+
set_to_future(date)
|
36
|
+
elsif str =~ /(\d{1,2})(#{ORDINAL_MATCHER})/i
|
37
|
+
date = Date.civil(today.year, today.month, $1.to_i)
|
38
|
+
set_to_future(date)
|
39
|
+
elsif str =~/next (#{DAY_MATCHER})/i
|
40
|
+
current_date = today.strftime("%A").downcase
|
41
|
+
current_index = DAY_NAMES.index(current_date)
|
42
|
+
expected_index = DAY_NAMES.index($1.downcase)
|
43
|
+
if expected_index <= current_index
|
44
|
+
today + (7 - current_index + expected_index) + 7
|
45
|
+
else
|
46
|
+
diff = expected_index - current_index
|
47
|
+
today + diff + 7
|
48
|
+
end
|
49
|
+
elsif str =~/(#{DAY_MATCHER})/i
|
50
|
+
current_date = today.strftime("%A").downcase
|
51
|
+
current_index = DAY_NAMES.index(current_date)
|
52
|
+
expected_index = DAY_NAMES.index($1.downcase)
|
53
|
+
if expected_index <= current_index
|
54
|
+
today + (7 - current_index + expected_index)
|
55
|
+
else
|
56
|
+
diff = expected_index - current_index
|
57
|
+
today + diff
|
58
|
+
end
|
59
|
+
elsif str =~ /today/i
|
60
|
+
today
|
61
|
+
elsif str =~ /tomorrow/i
|
62
|
+
today + 1
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def self.set_to_future(date)
|
69
|
+
today = Date.today
|
70
|
+
if date && date < today
|
71
|
+
date = Date.civil(date.year + 1, date.month, date.day)
|
72
|
+
end
|
73
|
+
return date
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timelord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Mongeau
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-02 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Pull dates out of strings.
|
23
|
+
email: halogenandtoast@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- Rakefile
|
32
|
+
- lib/timelord.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/halogenandtoast/timelord
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Pull dates out of strings
|
67
|
+
test_files: []
|
68
|
+
|