kronos 0.1.1 → 0.1.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/README.md CHANGED
@@ -1,30 +1,74 @@
1
- # README
1
+ # Kronos
2
2
 
3
- Kronos provides flexible date parsing. Currently, it is just a thin wrapper over [ParseDate](http://ruby-doc.org/stdlib/libdoc/parsedate/rdoc/index.html). Here is how it works:
3
+ Kronos is a library for Ruby that provides flexible date-related parsing.
4
+
5
+ ## Introduction
6
+
7
+ The approach of Kronos is simple: parse what you can and don't be more specific than is justified. In other words, don't attempt to force the rest into a Date object. It can handle a date, just a month and day, or just a year. Some examples:
4
8
 
5
9
  c = Kronos.parse("1991")
6
- c.year
7
- # => 1991
8
- # these work too: "91" and "'91"
10
+ c.year # => 1991
11
+ c.month # => nil
12
+ c.day # => nil
9
13
 
14
+ # These give the same results:
15
+ c = Kronos.parse("91")
16
+ c = Kronos.parse("'91")
17
+
10
18
  Kronos.parse("1/17/2007")
11
- c.year
12
- # => 2007
13
-
19
+ c.year # => 2007
20
+ c.month # => 1
21
+ c.day # => 17
22
+
14
23
  Kronos.parse("January 1976")
15
- c.year
16
- # => 1976
24
+ c.year # => 1976
25
+ c.month # => 1
26
+ c.day # => nil
27
+
28
+ ## Comparison with other libraries
29
+
30
+ In contrast, other date parsing libraries, such as [Chronic](http://github.com/mojombo/chronic) or [ParseDate](http://ruby-doc.org/stdlib/libdoc/parsedate/rdoc/index.html), try to parse a string into a full Date object, which requires a year, month, and day. Even if a month or day is not specified, they are going to pick one for you. For example:
31
+
32
+ require 'parsedate'
33
+ require 'chronic'
34
+
35
+ Chronic.parse("Aug-96")
36
+ # => Fri Aug 16 12:00:00 -0400 1996
37
+ # The day (16) is arbitrarily chosen
38
+
39
+ ParseDate.parsedate("Aug-96")
40
+ # => [nil, 8, 96, nil, nil, nil, nil, nil]
41
+ # The year (nil) is wrong
42
+ # The day (96) is arbitrarily chosen and out of range
43
+
44
+ Chronic.parse("1991")
45
+ # => Fri Dec 11 20:31:00 -0500 2009
46
+ # The year is wrong (2009)
47
+ # The month (12) and day (11) are arbitrarily chosen
48
+
49
+ ParseDate.parsedate("1991")
50
+ # => [nil, 19, 91, nil, nil, nil, nil, nil]
51
+ # The year is wrong (nil)
52
+ # The month (19) and day (91) are arbitrarily chosen and out of range
53
+
54
+ ## Implementation
55
+
56
+ Kronos is currently implemented as a thin wrapper over [ParseDate](http://ruby-doc.org/stdlib/libdoc/parsedate/rdoc/index.html).
57
+
58
+ ## Future Plans
59
+
60
+ In the future, I would like Kronos to be able to handle date-related comparisons properly.
17
61
 
18
- At present, neither [Chronic](http://github.com/mojombo/chronic) nor ParseDate parses a plain year (e.g. "1991") correctly.
62
+ The Ruby date libraries that I have seen make the assumption that you are specifying a point (i.e. a particular date), not an interval (such as an entire month or year). Comparing intervals adds a little bit of complexity. For example:
19
63
 
20
- In the future, I would like Kronos to be able to handle comparisons properly. Most Ruby date libraries seem to have the assumption that you are specifying a point, not an interval. Comparing intervals adds a little bit of complexity. For example:
64
+ year = Kronos.parse("1974")
65
+ month = Kronos.parse("March 1974")
66
+ year < month # => false
67
+ year > month # => false
68
+ month == year # => false
69
+ year.in?(month) # => false
70
+ month.in?(year) # => true
21
71
 
22
- c1 = Kronos.parse("1974")
23
- c2 = Kronos.parse("March 1974")
24
- c1 < c2 # => false
25
- c1 > c2 # => false
26
- c1 == c2 # => false
27
- c1.in?(c2) # => false
28
- c2.in?(c1) # => true
72
+ ## Feedback
29
73
 
30
- I have not yet seen Ruby date libraries that behave in this way.
74
+ I would appreciate your feedback. Thanks! (David James, Sunlight Labs, Washington, DC)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kronos}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David James"]
12
- s.date = %q{2009-12-02}
12
+ s.date = %q{2009-12-11}
13
13
  s.description = %q{Kronos provides flexible date parsing. Currently just a thin layer on top of ParseDate.}
14
14
  s.email = %q{djames@sunlightfoundation.com}
15
15
  s.extra_rdoc_files = [
@@ -8,13 +8,20 @@ class Kronos
8
8
  if string =~ /^\d{4}$/
9
9
  self.year = string.to_i
10
10
  elsif string =~ /^[']?(\d{2})$/
11
- n = $1.to_i
12
- self.year = n >= 70 ? 1900 + n : 2000 + n
11
+ self.year = to_full_year($1.to_i)
13
12
  else
14
13
  values = ParseDate.parsedate(string)
15
- self.year = values[0]
16
- self.month = values[1]
17
- self.day = values[2]
14
+ if values[0]
15
+ # ParseDate parsed a year
16
+ self.year = to_full_year(values[0])
17
+ self.month = values[1]
18
+ self.day = values[2]
19
+ elsif values[2]
20
+ # ParseDate parsed a day but not a year
21
+ self.year = to_full_year(values[2])
22
+ self.month = values[1]
23
+ self.day = nil
24
+ end
18
25
  end
19
26
  self
20
27
  end
@@ -31,4 +38,14 @@ class Kronos
31
38
  self.new.parse(string)
32
39
  end
33
40
 
41
+ protected
42
+
43
+ def to_full_year(x)
44
+ case x
45
+ when (0 .. 69) then 2000 + x
46
+ when (70 .. 99) then 1900 + x
47
+ else x
48
+ end
49
+ end
50
+
34
51
  end
@@ -97,6 +97,29 @@ describe "Kronos" do
97
97
  }
98
98
  end
99
99
 
100
+ it "Aug-96" do
101
+ c = Kronos.parse("Aug-96")
102
+ c.year.should == 1996
103
+ c.month.should == 8
104
+ c.day.should == nil
105
+ c.to_hash.should == {
106
+ 'year' => 1996,
107
+ 'month' => 8,
108
+ }
109
+ end
110
+
111
+ it "15-Mar-96" do
112
+ c = Kronos.parse("15-Mar-96")
113
+ c.year.should == 1996
114
+ c.month.should == 3
115
+ c.day.should == 15
116
+ c.to_hash.should == {
117
+ 'year' => 1996,
118
+ 'month' => 3,
119
+ 'day' => 15,
120
+ }
121
+ end
122
+
100
123
  it "empty string" do
101
124
  c = Kronos.parse("")
102
125
  c.year.should == nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kronos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David James
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-02 00:00:00 -05:00
12
+ date: 2009-12-11 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency