Class: Appstats::EntryDate

Inherits:
Object
  • Object
show all
Defined in:
lib/appstats/entry_date.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ EntryDate

Returns a new instance of EntryDate.



7
8
9
10
11
12
13
14
15
16
# File 'lib/appstats/entry_date.rb', line 7

def initialize(data = {})
  @year = data[:year]
  @month = data[:month]
  @day = data[:day]
  @hour = data[:hour]
  @min = data[:min]
  @sec = data[:sec]
  @week = data[:week]
  @quarter = data[:quarter]
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def day
  @day
end

#hourObject

Returns the value of attribute hour.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def hour
  @hour
end

#minObject

Returns the value of attribute min.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def min
  @min
end

#monthObject

Returns the value of attribute month.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def month
  @month
end

#quarterObject

Returns the value of attribute quarter.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def quarter
  @quarter
end

#secObject

Returns the value of attribute sec.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def sec
  @sec
end

#weekObject

Returns the value of attribute week.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def week
  @week
end

#yearObject

Returns the value of attribute year.



5
6
7
# File 'lib/appstats/entry_date.rb', line 5

def year
  @year
end

Class Method Details

.calculate_quarter_of(time) ⇒ Object



75
76
77
78
# File 'lib/appstats/entry_date.rb', line 75

def self.calculate_quarter_of(time)
  return nil if time.nil?
  (time.month - 1) / 3 + 1
end

.calculate_week_of(time) ⇒ Object



80
81
82
83
84
# File 'lib/appstats/entry_date.rb', line 80

def self.calculate_week_of(time)
  return nil if time.nil?
  return -1 if time.beginning_of_week.year != time.year
  time.strftime("%W").to_i
end

.parse(raw_input) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
141
142
143
144
145
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/appstats/entry_date.rb', line 86

def self.parse(raw_input)
  date = EntryDate.new
  return date if raw_input.nil? || raw_input == ''
  input = raw_input.strip

  if input.match(/^\d*$/) # year
    date.year = input.to_i
    return date
  end
  
  t = Time.now
  t_parts = nil
  
  if input.match(/^YTD|ytd$/)
    t_parts = [:year]
  elsif input.match(/^today$/)
    t_parts = [:year,:month,:day]
  elsif input.match(/^yesterday$/)
    t -= 1.day
    t_parts = [:year,:month,:day]
  elsif input.match(/^this year$/)
    t_parts = [:year]
  elsif input.match(/^this quarter$/)
    t = t.beginning_of_quarter
    t_parts = [:year,:month, :quarter]
  elsif input.match(/^this month$/)
    t_parts = [:year,:month]
  elsif input.match(/^this week$/)
    t = t.beginning_of_week
    t_parts = [:year,:month,:day, :week]
  elsif input.match(/^this day$/)
    t_parts = [:year,:month,:day]
  elsif input.match(/^(.*)\s*(\d+),[^\d]*(\d*)$/) # month day, year
    t = Time.parse(input)
    t_parts = [:year,:month,:day]
  elsif input.match(/^(.*),[^\d]*(\d*)$/) # month, year
    t = Time.parse(input)
    t_parts = [:year,:month]
  elsif input.match(/^(\d*)-(\d*)-(\d*)$/) # YYYY-mm-dd
    t = Time.parse(input)
    t_parts = [:year,:month,:day]
  end

  m = input.match(/^(last|previous)\s*(\d*)\s*years?$/)
  if m
    t -= last_date_offset(m).year
    t_parts = [:year]
  end

  m = input.match(/^(last|previous)\s*(\d*)\s*quarters?$/)
  if m
    t = t.beginning_of_quarter
    last_date_offset(m).times { t = (t - 1.day).beginning_of_quarter }
    t_parts = [:year,:month,:quarter]
  end
  
  m = input.match(/^(last|previous)\s*(\d*)\s*months?$/)
  if m
    t -= last_date_offset(m).month
    t_parts = [:year,:month]
  end

  m = input.match(/^(last|previous)\s*(\d*)\s*weeks?$/)
  if m
    t = (t - last_date_offset(m).week).beginning_of_week
    t_parts = [:year,:month,:day,:week]
  end

  m = input.match(/^(last|previous)\s*(\d*)\s*days?$/)
  if m
    t -= last_date_offset(m).day
    t_parts = [:year,:month,:day]
  end

  m = input.match(/^(\d*)\s*years?\s*ago/)
  if m
    t -= ago_date_offset(m).year
    t_parts = [:year]
  end

  m = input.match(/^(\d*)\s*quarters?\s*ago/)
  if m
    t = t.beginning_of_quarter
    ago_date_offset(m).times { t = (t - 1.day).beginning_of_quarter }
    t_parts = [:year,:month,:quarter]
  end

  m = input.match(/^(\d*)\s*months?\s*ago/)
  if m
    t -= ago_date_offset(m).month
    t_parts = [:year,:month]
  end

  m = input.match(/^(\d*)\s*weeks?\s*ago/)
  if m
    t = (t - ago_date_offset(m).week).beginning_of_week
    t_parts = [:year,:month,:day,:week]
  end

  m = input.match(/^(\d*)\s*days?\s*ago/)
  if m
    t -= ago_date_offset(m).day
    t_parts = [:year,:month,:day]
  end

  unless t_parts.nil?
    t_parts.each do |label|
      if [:quarter,:week].include?(label)
        date.send("#{label}=",EntryDate.send("calculate_#{label}_of",t))
      else
        date.send("#{label}=",t.send(label))
      end
    end
    return date
  end
  

  begin
    t = Time.parse(input)
    return EntryDate.new(:year => t.year, :month => t.month, :day => t.day, :hour => t.hour, :min => t.min, :sec => t.sec)
  rescue
    return EntryDate.new
  end
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



211
212
213
# File 'lib/appstats/entry_date.rb', line 211

def ==(o)
   o.class == self.class && o.send(:state) == state
end

#end_of_quarterObject



28
29
30
31
# File 'lib/appstats/entry_date.rb', line 28

def end_of_quarter
  t = to_time.end_of_quarter
  EntryDate.new(:year => t.year, :month => t.month, :quarter => EntryDate.calculate_quarter_of(t))
end

#end_of_weekObject



18
19
20
21
22
23
24
25
26
# File 'lib/appstats/entry_date.rb', line 18

def end_of_week
  week = self.dup
  t = to_time.end_of_week
  week.year = t.year
  week.month = t.month
  week.day = t.day
  week.week = EntryDate.calculate_week_of(t)
  week  
end

#to_sObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/appstats/entry_date.rb', line 52

def to_s
  s = ""
  return s if @year.nil?
  s += "#{@year}"

  return s if @month.nil?
  s += "-#{@month.to_s.rjust(2,'0')}"

  return s if @day.nil?
  s += "-#{@day.to_s.rjust(2,'0')}"

  return s if @hour.nil?
  s += " #{@hour.to_s.rjust(2,'0')}"

  return s if @min.nil?
  s += ":#{@min.to_s.rjust(2,'0')}"

  return s if @sec.nil?
  s += ":#{@sec.to_s.rjust(2,'0')}"
  
  s
end

#to_time(mode = :beginning) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appstats/entry_date.rb', line 33

def to_time(mode = :beginning)
  return Time.now if @year.nil?
  t = Time.parse("#{@year}-#{@month||'01'}-#{@day||'01'} #{@hour||'00'}:#{@min||'00'}:#{@sec||'00'}")
  
  if @month.nil?
    method = "_of_year"
  elsif !@quarter.nil?
    method = "_of_quarter"
  elsif !@week.nil?
    method = "_of_week"
  elsif @day.nil?
    method = "_of_month"
  elsif @hour.nil?
    method = "_of_day"
  end
  return t if method.nil?
  t.send("#{mode}#{method}")
end