Class: Kronos

Inherits:
Object
  • Object
show all
Defined in:
lib/kronos.rb,
lib/version.rb

Defined Under Namespace

Modules: Version Classes: Exception, Invalid

Constant Summary collapse

IGNORE_PATTERN =
Regexp.compile('^prior')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dayObject

Returns the value of attribute day.



9
10
11
# File 'lib/kronos.rb', line 9

def day
  @day
end

#monthObject

Returns the value of attribute month.



9
10
11
# File 'lib/kronos.rb', line 9

def month
  @month
end

#yearObject

Returns the value of attribute year.



9
10
11
# File 'lib/kronos.rb', line 9

def year
  @year
end

Class Method Details

.from_hash(h) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/kronos.rb', line 233

def self.from_hash(h)
  o = self.new
  o.year  = h['year']  if h['year']
  o.month = h['month'] if h['month']
  o.day   = h['day']   if h['day']
  o
end

.parse(string) ⇒ Object



241
242
243
# File 'lib/kronos.rb', line 241

def self.parse(string)
  self.new.parse(string)
end

Instance Method Details

#<(other) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/kronos.rb', line 146

def <(other)
  if !self.year || !other.year
    false
  elsif self.year < other.year
    true
  elsif self.year > other.year
    false
  elsif self.year == other.year
    if !self.month || !other.month
      false
    elsif self.month < other.month
      true
    elsif self.month > other.month
      false
    elsif self.month == other.month
      if !self.day || !other.day
        false
      elsif self.day < other.day
        true
      elsif self.day > other.day
        false
      elsif self.day == other.day
        false
      else
        raise Exception, 'unexpected error'
      end
    end
  else
    raise Exception, 'unexpected error'
  end
end

#<=(other) ⇒ Object



178
179
180
# File 'lib/kronos.rb', line 178

def <=(other)
  (self < other) || (self == other)
end

#==(other) ⇒ Object



182
183
184
185
186
# File 'lib/kronos.rb', line 182

def ==(other)
  self.year == other.year &&
  self.month == other.month &&
  self.day == other.day 
end

#>(other) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kronos.rb', line 192

def >(other)
  if !self.year || !other.year
    false
  elsif self.year < other.year
    false
  elsif self.year > other.year
    true
  elsif self.year == other.year
    if !self.month || !other.month
      false
    elsif self.month < other.month
      false
    elsif self.month > other.month
      true
    elsif self.month == other.month
      if !self.day || !other.day
        false
      elsif self.day < other.day
        false
      elsif self.day > other.day
        true
      elsif self.day == other.day
        false
      else
        raise Exception, 'unexpected error'
      end
    end
  else
    raise Exception, 'unexpected error'
  end
end

#>=(other) ⇒ Object



188
189
190
# File 'lib/kronos.rb', line 188

def >=(other)
  (self > other) || (self == other)
end

#errorsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kronos.rb', line 113

def errors
  errors = []
  if day && (!month || !year)
    errors << 'if day is given, then month and year must also be given'
  elsif month && !year
    errors << 'if month is given, then year must also be given'
  elsif !year
    errors << 'year must be given'
  else
    if day && !((1 .. 31) === day)
      errors << "day (#{day}) must be between 1 and 31"
    end
    if month && !((1 .. 12) === month)
      errors << "month (#{month}) must be between 1 and 12"
    end
    if year && !((1970 .. 2069) === year)
      errors << "year (#{year}) must be between 1970 and 2069"
    end
    if errors.length == 0 && day && month && year
      begin
        Date.new(year, month, day)
      rescue ArgumentError
        errors << "date (#{year}-#{month}-#{day}) must be valid"
      end
    end
  end
  errors
end

#parse(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kronos.rb', line 13

def parse(string)
  if string.nil? || string =~ IGNORE_PATTERN
    nil # do nothing
  elsif\
    p = parse_date(string, '%Y-%m-%d') || # 1988-09-23
    p = parse_date(string, '%Y-%b-%d') || # 1988-Sep-23
    p = parse_date(string, '%Y-%B-%d') || # 1988-September-23
    p = parse_date(string, '%y-%m-%d') || # 88-9-23
    p = parse_date(string, '%y-%b-%d') || # 88-Sep-23
    p = parse_date(string, '%y-%B-%d') || # 88-September-23
    p = parse_date(string, '%m/%d/%Y') || # 9/23/1988
    p = parse_date(string, '%b/%d/%Y') || # Sep/23/1988
    p = parse_date(string, '%B/%d/%Y') || # September/23/1988
    p = parse_date(string, '%m/%d/%y') || # 11/23/88
    p = parse_date(string, '%b/%d/%y') || # Sep/23/88
    p = parse_date(string, '%B/%d/%y') || # September/23/88
    p = parse_date(string, '%m-%d-%Y') || # 11-23-1988
    p = parse_date(string, '%b-%d-%Y') || # Sep-23-1988
    p = parse_date(string, '%B-%d-%Y') || # September-23-1988
    p = parse_date(string, '%m %d %Y') || # 11 23 1988
    p = parse_date(string, '%b %d %Y') || # Sep 23 1988
    p = parse_date(string, '%B %d %Y') || # September 23 1988
    p = parse_date(string, '%m %d %y') || # 11 23 88
    p = parse_date(string, '%b %d %y') || # Sep 23 88
    p = parse_date(string, '%B %d %y') || # September 23 88
    p = parse_date(string, '%d-%b-%Y') || # 23-Sep-1988
    p = parse_date(string, '%d-%B-%Y') || # 23-September-1988
    p = parse_date(string, '%d-%b-%y') || # 23-Sep-88
    p = parse_date(string, '%d-%B-%y') || # 23-September-88
    p = parse_date(string, '%d %b %Y') || # 23 Sep 1988
    p = parse_date(string, '%d %B %Y') || # 23 September 1988
    p = parse_date(string, '%d %b %y') || # 23 Sep 88
    p = parse_date(string, '%d %B %y') || # 23 September 88
    false
    self.year  = to_full_year(p.year)
    self.month = p.month
    self.day   = p.day
  elsif\
    p = parse_date(string, '%Y-%m') || # 1976-4
    p = parse_date(string, '%Y-%b') || # 1976-Apr
    p = parse_date(string, '%Y-%B') || # 1976-April
    p = parse_date(string, '%m/%Y') || # 4/1976
    p = parse_date(string, '%b/%Y') || # Apr/1976
    p = parse_date(string, '%B/%Y') || # April/1976
    p = parse_date(string, '%m-%Y') || # 4-1976
    p = parse_date(string, '%b-%Y') || # Apr-1976
    p = parse_date(string, '%B-%Y') || # April-1976
    p = parse_date(string, '%m %Y') || # 4 1976
    p = parse_date(string, '%b %Y') || # Apr 1976
    p = parse_date(string, '%B %Y') || # April 1976
    p = parse_date(string, '%m/%y') || # 4/76
    p = parse_date(string, '%b/%y') || # Apr/76
    p = parse_date(string, '%B/%y') || # April/76
    p = parse_date(string, '%m-%y') || # 4-76
    p = parse_date(string, '%b-%y') || # Apr-76
    p = parse_date(string, '%B-%y') || # April-76
    p = parse_date(string, '%m %y') || # 4 76
    p = parse_date(string, '%b %y') || # Apr 76
    p = parse_date(string, '%B %y') || # April 76
    false
    self.year  = to_full_year(p.year)
    self.month = p.month
  elsif\
    p = parse_date(string, '%Y') || # 1991
    p = parse_date(string, '%y') || # 91
    false
    self.year = to_full_year(p.year)
  elsif\
    string =~ /^[']?(\d{2})$/ # '91
    self.year = to_full_year($1.to_i)
  end
  self
end

#parse_date(original, format) ⇒ Object

This method:

1. Swallows ArgumentError from Date.strptime
2. Does not trust a match from Date.strptime unless it matches the
   inverse operation, namely Date#strftime.
3. However, the inverse operation is complicated by the way that
   #strftime handles leading zeros. For now, I have a hack that
   removes leading zeros.


94
95
96
97
98
99
100
101
102
# File 'lib/kronos.rb', line 94

def parse_date(original, format)
  parsed = Date.strptime(original, format)
  t1 = parsed.strftime(format)
  t2 = t1.gsub('/0', '/').gsub('-0', '-').gsub(/^0/, '')
  return unless original == t1 || original == t2
  parsed
rescue ArgumentError
  nil
end

#to_hashObject

Raises:



224
225
226
227
228
229
230
231
# File 'lib/kronos.rb', line 224

def to_hash
  raise Invalid, errors unless valid?
  h = {}
  h['year']  = year  if year
  h['month'] = month if month
  h['day']   = day   if day
  h
end

#to_sObject

Raises:



104
105
106
107
108
109
110
111
# File 'lib/kronos.rb', line 104

def to_s
  s = ''
  raise Invalid, errors unless valid?
  s << '%04i' % year if year
  s << '-%02i' % month if month
  s << '-%02i' % day if day
  s
end

#valid?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/kronos.rb', line 142

def valid?
  errors.length == 0
end