timelord 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.rdoc +26 -0
- data/Rakefile +6 -19
- data/lib/timelord.rb +42 -21
- data/spec/spec_helper.rb +5 -0
- data/spec/timelord_spec.rb +121 -0
- metadata +9 -5
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 halogenandtoast
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
=Timelord
|
2
|
+
|
3
|
+
Timelord parses dates out of strings. The string itself can contain non-date text - for instance: "Call Matt on Tuesday"
|
4
|
+
will return a date object for the upcoming Tuesday.
|
5
|
+
|
6
|
+
==Usage
|
7
|
+
|
8
|
+
require 'timelord'
|
9
|
+
Timelord.parse("Tuesday").to_s # "2011-01-04"
|
10
|
+
Timelord.parse("On Tuesday go for a walk").to_s # "2011-01-04"
|
11
|
+
|
12
|
+
For more examples, check out the spec[https://github.com/halogenandtoast/timelord/blob/master/spec/timelord_spec.rb]
|
13
|
+
|
14
|
+
==Date format
|
15
|
+
|
16
|
+
The default date format is the international format. 11/01 is January 11th.
|
17
|
+
|
18
|
+
To use the american date format pass in :american as the second parameter
|
19
|
+
|
20
|
+
Timelord.parse("11/01").to_s # "2011-01-11"
|
21
|
+
Timelord.parse("11/01", :american) # "2011-11-01"
|
22
|
+
|
23
|
+
==In the future
|
24
|
+
|
25
|
+
As of version 0.0.1 all dates without a year automatically choose the next occurrence of that date. If there is demand
|
26
|
+
for the ability to retrieve previous dates, then the functionality will be added.
|
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'spec/rake/spectask'
|
3
3
|
require 'rake/rdoctask'
|
4
|
-
require '
|
4
|
+
require 'rake/gempackagetask'
|
5
5
|
|
6
6
|
Spec::Rake::SpecTask.new do |t|
|
7
7
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
@@ -10,24 +10,11 @@ end
|
|
10
10
|
task :default => [:test]
|
11
11
|
task :test => [:spec]
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
spec.summary = 'Pull dates out of strings'
|
19
|
-
spec.description = 'Pull dates out of strings.'
|
20
|
-
spec.email = 'halogenandtoast@gmail.com'
|
21
|
-
spec.homepage = 'http://github.com/halogenandtoast/timelord'
|
22
|
-
spec.authors = ['Matthew Mongeau']
|
23
|
-
spec.files = PKG_FILES
|
24
|
-
end
|
25
|
-
|
26
|
-
desc "Generate a gemspec file"
|
27
|
-
task :gemspec do
|
28
|
-
File.open("#{gem_spec.name}.gemspec", "w") do |f|
|
29
|
-
f.write gem_spec.to_yaml
|
30
|
-
end
|
13
|
+
specification = Gem::Specification.load("timelord.gemspec")
|
14
|
+
desc 'Package gem'
|
15
|
+
Rake::GemPackageTask.new(specification) do |package|
|
16
|
+
package.need_zip = true
|
17
|
+
package.need_tar = true
|
31
18
|
end
|
32
19
|
|
33
20
|
desc 'Generate documentation'
|
data/lib/timelord.rb
CHANGED
@@ -6,7 +6,8 @@ class Timelord
|
|
6
6
|
SHORT_MATCHER = SHORT_MONTHS.join('|').freeze
|
7
7
|
ORDINAL_MATCHER = "st|nd|rd|th".freeze
|
8
8
|
DAY_NAMES = %w(monday tuesday wednesday thursday friday saturday sunday).freeze
|
9
|
-
|
9
|
+
SHORT_DAY_NAMES = %w(mon tue wed thu fri sat sun).freeze
|
10
|
+
DAY_MATCHER = (DAY_NAMES + SHORT_DAY_NAMES).join('|').freeze
|
10
11
|
|
11
12
|
# Parses a date str. Second parameter switches between international and american date formats.
|
12
13
|
#
|
@@ -44,34 +45,54 @@ class Timelord
|
|
44
45
|
date = Date.civil(today.year, today.month, $1.to_i)
|
45
46
|
set_to_future(date)
|
46
47
|
elsif str =~/next (#{DAY_MATCHER})/i
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
diff = expected_index - current_index
|
54
|
-
today + diff + 7
|
55
|
-
end
|
48
|
+
expected_index = DAY_NAMES.index($1.downcase) || SHORT_DAY_NAMES.index($1.downcase)
|
49
|
+
next_weekday(expected_index)
|
50
|
+
elsif str =~/next tues/
|
51
|
+
next_weekday(DAY_NAMES.index("tue"))
|
52
|
+
elsif str =~/next (thur|thurs)/
|
53
|
+
next_weekday(DAY_NAMES.index("tue"))
|
56
54
|
elsif str =~/(#{DAY_MATCHER})/i
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
today + diff
|
65
|
-
end
|
66
|
-
elsif str =~ /today/i
|
55
|
+
expected_index = DAY_NAMES.index($1.downcase) || SHORT_DAY_NAMES.index($1.downcase)
|
56
|
+
current_weekday(expected_index)
|
57
|
+
elsif str =~/tues/
|
58
|
+
current_weekday(DAY_NAMES.index("tue"))
|
59
|
+
elsif str =~/(thur|thurs)/
|
60
|
+
current_weekday(DAY_NAMES.index("tue"))
|
61
|
+
elsif str =~ /(today|tod)/i
|
67
62
|
today
|
68
|
-
elsif str =~ /tomorrow/i
|
63
|
+
elsif str =~ /(tomorrow|tom)/i
|
69
64
|
today + 1
|
70
65
|
end
|
71
66
|
end
|
72
67
|
|
73
68
|
private
|
74
69
|
|
70
|
+
def self.today
|
71
|
+
Date.today
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.next_weekday(date_index)
|
75
|
+
current_date = today.strftime("%A").downcase
|
76
|
+
current_index = DAY_NAMES.index(current_date)
|
77
|
+
if date_index <= current_index
|
78
|
+
today + (7 - current_index + date_index) + 7
|
79
|
+
else
|
80
|
+
diff = date_index - current_index
|
81
|
+
today + diff + 7
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.current_weekday(date_index)
|
86
|
+
current_date = today.strftime("%A").downcase
|
87
|
+
current_index = DAY_NAMES.index(current_date)
|
88
|
+
if date_index <= current_index
|
89
|
+
today + (7 - current_index + date_index)
|
90
|
+
else
|
91
|
+
diff = date_index - current_index
|
92
|
+
today + diff
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
75
96
|
def self.set_to_future(date)
|
76
97
|
today = Date.today
|
77
98
|
if date && date < today
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Timelord, 'parse' do
|
4
|
+
before do
|
5
|
+
local_time = Time.local(2010,12,1,10,5,0)
|
6
|
+
Timecop.freeze(local_time)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns nil when no time is present" do
|
10
|
+
Timelord.parse("No time here").should == nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "parses 'today'" do
|
14
|
+
Timelord.parse("I need to do something today.").should == Date.today
|
15
|
+
end
|
16
|
+
|
17
|
+
it "parses 'tod'" do
|
18
|
+
Timelord.parse("I need to do something tod.").should == Date.today
|
19
|
+
end
|
20
|
+
|
21
|
+
it "parses 'tomorrow'" do
|
22
|
+
tomorrow = Date.today + 1
|
23
|
+
Timelord.parse("I need to do something tomorrow.").should == tomorrow
|
24
|
+
end
|
25
|
+
|
26
|
+
it "parses 'tom'" do
|
27
|
+
tomorrow = Date.today + 1
|
28
|
+
Timelord.parse("I need to do something tom.").should == tomorrow
|
29
|
+
end
|
30
|
+
|
31
|
+
it "parses '12 Dec'" do
|
32
|
+
twelth_of_december = Date.civil(2010,12,12)
|
33
|
+
Timelord.parse("On 12 Dec I need to do something.").should == twelth_of_december
|
34
|
+
end
|
35
|
+
|
36
|
+
it "parses 'Dec 12'" do
|
37
|
+
twelth_of_december = Date.civil(2010,12,12)
|
38
|
+
Timelord.parse("On Dec 12 I need to do something.").should == twelth_of_december
|
39
|
+
end
|
40
|
+
|
41
|
+
it "parses American style dates" do
|
42
|
+
first_of_december = Date.today
|
43
|
+
Timelord.parse("On 12/1/2010 I need to do something.", :american).should == first_of_december
|
44
|
+
end
|
45
|
+
|
46
|
+
it "parses Internation style dates by default" do
|
47
|
+
first_of_december = Date.today
|
48
|
+
Timelord.parse("On 1/12/2010 I need to do something.", :international).should == first_of_december
|
49
|
+
end
|
50
|
+
|
51
|
+
it "parses yyyy/mm/dd" do
|
52
|
+
first_of_december = Date.today
|
53
|
+
Timelord.parse("On 2010/12/01 I need to do something.").should == first_of_december
|
54
|
+
end
|
55
|
+
|
56
|
+
it "parses yyyy/mm/d" do
|
57
|
+
first_of_december = Date.today
|
58
|
+
Timelord.parse("On 2010/12/1 I need to do something.").should == first_of_december
|
59
|
+
end
|
60
|
+
|
61
|
+
it "parses yyyy/m/d" do
|
62
|
+
first_of_january = Date.civil(2010, 1, 1)
|
63
|
+
Timelord.parse("On 2010/1/1 I need to do something.").should == first_of_january
|
64
|
+
end
|
65
|
+
|
66
|
+
it "parses yyyy-mm-dd" do
|
67
|
+
first_of_december = Date.today
|
68
|
+
Timelord.parse("On 2010-12-01 I need to do something.").should == first_of_december
|
69
|
+
end
|
70
|
+
|
71
|
+
it "parses mm/dd" do
|
72
|
+
first_of_december = Date.today
|
73
|
+
Timelord.parse("On 12/1 I need to do something.", :american).should == first_of_december
|
74
|
+
Timelord.parse("On 12/01 I need to do something.", :american).should == first_of_december
|
75
|
+
Timelord.parse("On 1/12 I need to do something.", :international).should == first_of_december
|
76
|
+
Timelord.parse("On 01/12 I need to do something.", :international).should == first_of_december
|
77
|
+
end
|
78
|
+
|
79
|
+
it "parses formats like 1st,2nd,3rd,4th,25th" do
|
80
|
+
first_of_december = Date.today
|
81
|
+
second_of_december = Date.today + 1
|
82
|
+
third_of_december = Date.today + 2
|
83
|
+
forth_of_december = Date.today + 3
|
84
|
+
twenty_fifth_of_december = Date.civil(2010,12,25)
|
85
|
+
Timelord.parse("On the 1st").should == first_of_december
|
86
|
+
Timelord.parse("On the 2nd").should == second_of_december
|
87
|
+
Timelord.parse("On the 3rd").should == third_of_december
|
88
|
+
Timelord.parse("On the 4th").should == forth_of_december
|
89
|
+
Timelord.parse("On the 25th").should == twenty_fifth_of_december
|
90
|
+
end
|
91
|
+
|
92
|
+
it "parses the day of the week" do
|
93
|
+
friday = Date.today + 2
|
94
|
+
monday = Date.today + 5
|
95
|
+
tuesday = Date.today + 6
|
96
|
+
thursday = Date.today + 1
|
97
|
+
Timelord.parse("On friday do something").should == friday
|
98
|
+
Timelord.parse("On Monday do something").should == monday
|
99
|
+
Timelord.parse("On fri do something").should == friday
|
100
|
+
Timelord.parse("On mon do something").should == monday
|
101
|
+
Timelord.parse("On tues do something").should == tuesday
|
102
|
+
Timelord.parse("On thurs do something").should == thursday
|
103
|
+
end
|
104
|
+
|
105
|
+
it "parses the next weekday" do
|
106
|
+
friday = Date.today + 9
|
107
|
+
monday = Date.today + 12
|
108
|
+
Timelord.parse("On next friday do something").should == friday
|
109
|
+
Timelord.parse("On next Monday do something").should == monday
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'returns a date in the future' do
|
113
|
+
first_of_january = Date.civil(2011,1,1)
|
114
|
+
Timelord.parse("On 1 Jan I need to do something.").should == first_of_january
|
115
|
+
end
|
116
|
+
|
117
|
+
after do
|
118
|
+
Timecop.return
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timelord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matthew Mongeau
|
@@ -15,11 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-02-16 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description:
|
22
|
+
description:
|
23
23
|
email: halogenandtoast@gmail.com
|
24
24
|
executables: []
|
25
25
|
|
@@ -28,8 +28,12 @@ extensions: []
|
|
28
28
|
extra_rdoc_files: []
|
29
29
|
|
30
30
|
files:
|
31
|
+
- README.rdoc
|
32
|
+
- LICENSE
|
31
33
|
- Rakefile
|
32
34
|
- lib/timelord.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
- spec/timelord_spec.rb
|
33
37
|
has_rdoc: true
|
34
38
|
homepage: http://github.com/halogenandtoast/timelord
|
35
39
|
licenses: []
|