Module: Utilities::Dates

Defined in:
lib/utilities/dates.rb

Constant Summary

LONG_MONTHS =
%w{january february march april may june july august september october november december}
SHORT_MONTHS =
%w{jan feb mar apr may jun jul aug sep oct nov dec}
ROMAN_MONTHS =
i ii iii iv v vi vii viii ix x xi xii
MONTHS_FOR_SELECT =
LONG_MONTHS.collect { |m| [m.capitalize, LONG_MONTHS.index(m) + 1] }
(1..12).to_a +
(1..12).to_a.collect { |d| d.to_s } +
ROMAN_MONTHS.map(&:to_s) +
ROMAN_MONTHS.map(&:to_s).map(&:upcase) +
SHORT_MONTHS +
SHORT_MONTHS.map(&:capitalize) +
SHORT_MONTHS.map(&:upcase) +
SHORT_MONTHS.map(&:to_sym) +
LONG_MONTHS +
LONG_MONTHS.map(&:capitalize) +
LONG_MONTHS.map(&:upcase)
SHORT_MONTH_FILTER =

TODO: Write unit tests Concept from from www.rdoc.info/github/inukshuk/bibtex-ruby/master/BibTeX/Entry Converts integers, month names, or roman numerals, regardless of class to three letter symbols.

SHORT_MONTH_FILTER[1]         # => :jan
SHORT_MONTH_FILTER['JANUARY'] # => :jan
SHORT_MONTH_FILTER['i']       # => :jan
SHORT_MONTH_FILTER['I']       # => :jan
SHORT_MONTH_FILTER['foo']     # => 'foo':
Hash.new do |h, k|
  v = k.to_s.strip
  if v =~ /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i
    h[k] = v[0, 3].downcase.to_sym
  else
    i = nil
    if v =~ /^\d\d?$/
      i = v.to_i
    elsif ROMAN_MONTHS.include?(v.downcase.to_sym)
      i = ROMAN_MONTHS.index(v.downcase.to_sym) + 1
    end

    if !i.nil? && i > 0 && i < 13
      h[k] = DateTime.new(1, i, 1).strftime('%b').downcase.to_sym
    else # return the value passed if it doesn't match
      k
    end
  end
end

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (String) add_st_day(sql, st_day)

Returns of sql

Parameters:

  • sql (String)
  • st_day (Integer)

Returns:

  • (String)

    of sql



212
213
214
215
216
217
218
# File 'lib/utilities/dates.rb', line 212

def self.add_st_day(sql, st_day)
  unless st_day.blank?
    prefix = sql.blank? ? '' : ' and '
    sql    += "#{prefix}(start_date_day = #{st_day})"
  end
  sql
end

+ (String) add_st_month(sql, st_month)

Returns of sql

Parameters:

  • sql (String)
  • st_month (Integer)

Returns:

  • (String)

    of sql



201
202
203
204
205
206
207
# File 'lib/utilities/dates.rb', line 201

def self.add_st_month(sql, st_month)
  unless st_month.blank?
    prefix = sql.blank? ? '' : ' and '
    sql    += "#{prefix}(start_date_month = #{st_month})"
  end
  sql
end

+ (String) add_st_year(sql, st_year)

Returns of sql

Parameters:

  • sql (String)
  • st_year (Integer)

Returns:

  • (String)

    of sql



190
191
192
193
194
195
196
# File 'lib/utilities/dates.rb', line 190

def self.add_st_year(sql, st_year)
  unless st_year.blank?
    prefix = sql.blank? ? '' : ' and '
    sql    += "#{prefix}(start_date_year = #{st_year})"
  end
  sql
end

+ (String?) date_range_sentence_tag(date_range)

Returns a sentence spelling out the date range

Parameters:

  • of (Array)

    0-2 dates

Returns:

  • (String, nil)

    a sentence spelling out the date range



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/utilities/dates.rb', line 272

def self.date_range_sentence_tag(date_range)
  format = "%Y-%b-%d"
  date_range.compact!
  if date_range.empty?
    nil
  else
    if date_range.size == 2
      'Records exist in the date range ' + date_range.collect{|d| d.strftime(format)}.join(' to ') + '.'
    elsif date_range.size == 1
      "A record exists from #{date_range.first.strftime(format)}."
    else
      'Hmm, that is a curious date range!'
    end
  end
end

+ (String) date_sql_from_params(params)

TODO: still needs more work for some date combinations

Parameters:

  • params (Hash)

Returns:

  • (String)

    of sql to test dates



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
# File 'lib/utilities/dates.rb', line 102

def self.date_sql_from_params(params)
  st_date, end_date         = params['st_datepicker'], params['en_datepicker']
# processing start date data
  st_year, st_month, st_day = params['start_date_year'], params['start_date_month'], params['start_date_day']
  unless st_date.blank?
    parts                     = st_date.split('/')
    st_year, st_month, st_day = parts[2], parts[0], parts[1]
  end
  st_my                        = (!st_month.blank? and !st_year.blank?)
  st_m                         = (!st_month.blank? and st_year.blank?)
  st_y                         = (st_month.blank? and !st_year.blank?)
  st_blank                     = (st_year.blank? and st_month.blank? and st_day.blank?)
  st_full                      = (!st_year.blank? and !st_month.blank? and !st_day.blank?)
  st_partial                   = (!st_blank and (st_year.blank? or st_month.blank? or st_day.blank?))
  start_time                   = fix_time(st_year, st_month, st_day) if st_full

# processing end date data
  end_year, end_month, end_day = params['end_date_year'], params['end_date_month'], params['end_date_day']
  unless end_date.blank?
    parts                        = end_date.split('/')
    end_year, end_month, end_day = parts[2], parts[0], parts[1]
  end
  end_my      = (!end_month.blank? and !end_year.blank?)
  end_m       = (!end_month.blank? and end_year.blank?)
  end_y       = (end_month.blank? and !end_year.blank?)
  end_blank   = (end_year.blank? and end_month.blank? and end_day.blank?)
  end_full    = (!end_year.blank? and !end_month.blank? and !end_day.blank?)
  end_partial = (!end_blank and (end_year.blank? or end_month.blank? or end_day.blank?))
  end_time    = fix_time(end_year, end_month, end_day) if end_full

  sql_string = ''
# if all the date information is blank, skip the date testing
  unless st_blank and end_blank
    # only start and end year
    if st_y and end_y
      # start and end year may be different, or the same
      # we ignore all records which have a null start year,
      # but include all records for the end year test
      sql_string += "(start_date_year >= #{st_year} and (end_date_year is null or end_date_year <= #{end_year}))"
    end

    # only start month and end month
    if st_m and end_m
      # todo: This case really needs additional consideration
      # maybe build a string of included month and use an 'in ()' construct
      sql_string += "(start_date_month between #{st_month} and #{end_month})"
    end

    if end_blank # !st_blank = st_partial
      # if we have only a start date there are three cases: d/m/y, m/y, y
      if st_year.blank?
        sql_string = add_st_month(sql_string, st_month)
      else
        sql_string = add_st_day(sql_string, st_day)
        sql_string = add_st_month(sql_string, st_month)
        sql_string = add_st_year(sql_string, st_year)
      end
    else
      # end date only, don't do anything
    end

    if ((st_y or st_my) and (end_y or end_my)) and not (st_y and end_y)
      # we have two dates of some kind, complete with years
      # three specific cases:
      #   case 1: start year, (start month, (start day)) forward
      #   case 2: end year, (end month, (end day)) backward
      #   case 3: any intervening year(s) complete
      if st_year
      end
    end
  end
  sql_string
end

+ (Time) fix_time(year, month, day)

Parameters:

  • year (Integer)
  • month (Integer)
  • day (Integer)

Returns:

  • (Time)


224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/utilities/dates.rb', line 224

def self.fix_time(year, month, day)
  start = Time.new(1970, 1, 1)
  if year.blank?
    year = start.year
  end
  if month.blank?
    month = start.month
  end
  if day.blank?
    day = start.day
  end
  Time.new(year, month, day)
end

+ (Object) format_to_hours_minutes_seconds(hour, minute, second)

Pass integers



177
178
179
180
181
182
183
# File 'lib/utilities/dates.rb', line 177

def self.format_to_hours_minutes_seconds(hour, minute, second)
  h, m, s = nil, nil, nil
  h       = ('%02d' % hour) if hour
  m       = ('%02d' % minute) if minute
  s       = ('%02d' % second) if second
  [h, m, s].compact.join(':')
end

+ (Integer) month_index(month_value)

Return the month index

Parameters:

  • (String)
  • (Integer)
  • (Symbol)

Returns:

  • (Integer)

    return the month index



60
61
62
63
# File 'lib/utilities/dates.rb', line 60

def self.month_index(month_value)
  return nil if month_value.nil?
  SHORT_MONTHS.index(SHORT_MONTH_FILTER[month_value].to_s) + 1
end

+ (Object) nomenclature_date(day = nil, month = nil, year = nil)

@return a UTC time (Uses Time instead of Date so that it can be saved as a UTC object -

See http://www.ruby-doc.org/core-2.0.0/Time.html)
Returns nomenclature_date based on computation of the values of :year, :month, :day.
 if :year is empty, return nil
 if :month is empty, returns 12/31/:year
 if :day is empty, returns the last day of the month

Use self.month_index to convert months prior to handling them here



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/utilities/dates.rb', line 78

def self.nomenclature_date(day = nil, month = nil, year = nil)
  if year.nil?
    nil
  elsif month.nil?
    Time.utc(year.to_i, 12, 31)
  elsif day.nil?
    tmp = Time.utc(year.to_i, month.to_i)
    if tmp.month == 12 # want the last day of december
      Time.utc(year.to_i, 12, 31)
    else # time + 1 month - 1 day (60 sec * 60 min *24 hours)
      Time.utc(year.to_i, tmp.month + 1) - 86400
    end
  else
    Time.utc(year.to_i, month.to_i, day.to_i)
  end
end

+ (String) normalize_and_order_dates(start_date, end_date)

Returns start_date, end_date in proper order

Parameters:

  • start_date (String)

    in the form of 'yyyy/mm/dd'

  • end_date (String)

    in the form of 'yyyy/mm/dd'

Returns:

  • (String, String)

    start_date, end_date in proper order



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/utilities/dates.rb', line 241

def self.normalize_and_order_dates(start_date, end_date)
  if start_date.blank? and end_date.blank? # set entire range
    start_date = '1700/1/1'
    end_date   = Date.today.strftime('%Y/%m/%d')
  else
    if end_date.blank? # set a one-day range
      end_date = start_date
    end
    if start_date.blank? # set a one-day range
      start_date = end_date
    end
  end

  start_date, end_date = order_dates(start_date, end_date)

  return start_date, end_date
end

+ (String) order_dates(start_date, end_date)

Returns start_date, end_date in proper order

Parameters:

  • start_date (String)

    in the form of 'yyyy/mm/dd'

  • end_date (String)

    in the form of 'yyyy/mm/dd'

Returns:

  • (String, String)

    start_date, end_date in proper order



262
263
264
265
266
267
# File 'lib/utilities/dates.rb', line 262

def self.order_dates(start_date, end_date)
  if Date.parse(start_date) > Date.parse(end_date) # need to swap s and e?
    start_date, end_date = end_date, start_date
  end
  return start_date, end_date
end

Instance Method Details

- (Object) mdy_parse_date(date_string)



95
96
97
# File 'lib/utilities/dates.rb', line 95

def mdy_parse_date(date_string)
  date_string
end