module Patriot::Util::DateUtil

namesapce for date functions

Public Instance Methods

date_add(date, interval) click to toggle source

add interval to date @param date [String] date in '%Y-%m-%d' @param interval [Integer] interval in day @return [String] date in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 48
def date_add(date, interval)
  d = to_date_obj(date)
  d = d + interval
  return d.strftime('%Y-%m-%d')
end
date_add_year(date, interval) click to toggle source

@param date [String] date in '%Y-%m-%d' @param interval [Integer] interval in year @return [String] date of some years later in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 67
def date_add_year(date, interval)
  s = date.split("-");
  return Date.new(s[0].to_i + interval,s[1].to_i,s[2].to_i).strftime("%Y-%m-%d")
end
date_format(dt, time, fmt, diff={}) click to toggle source

format date @param dt [String] date in '%Y-%m-%d' @param time [String] time in '%H:%M:%S'

# File lib/patriot/util/date_util.rb, line 10
def date_format(dt, time, fmt, diff={})
  diff = {:year => 0, 
          :month => 0,
          :day => 0,
          :hour => 0,
          :min => 0,
          :sec => 0}.merge(diff)

  dt  = eval "\"#{dt}\""
  time  = eval "\"#{time}\""

  t = time.split(':')
  d = dt.split('-')
  sec = t[2].to_i + diff[:sec]
  min = t[1].to_i + diff[:min] 
  hour = t[0].to_i + diff[:hour] 
  day = d[2].to_i 
  month = d[1].to_i 
  year = d[0].to_i + diff[:year]

  min = min+ sec/60
  hour = hour + min/60
  diff[:day]= diff[:day] + hour/24  

  sec = sec%60
  min = min%60
  hour = hour%24
   
  new_dt = DateTime.new(year, month, day, hour, min, sec)
  new_dt = new_dt >> diff[:month]
  new_dt = new_dt + diff[:day]
  return new_dt.strftime(fmt)
end
date_sub(date, interval) click to toggle source

subtract interval from date @param date [String] date in '%Y-%m-%d' @param interval [Integer] interval in day @return [String] date in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 58
def date_sub(date, interval)
  d = to_date_obj(date)
  d = d - interval
  return d.strftime('%Y-%m-%d')
end
date_sub_year(date, interval) click to toggle source

@param date [String] date in '%Y-%m-%d' @param interval [Integer] interval in year @return [String] date of some years before in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 75
def date_sub_year(date, interval)
  s = date.split("-");
  return Date.new(s[0].to_i - interval,s[1].to_i,s[2].to_i).strftime("%Y-%m-%d")
end
date_to_month(date) click to toggle source

@deprecated

# File lib/patriot/util/date_util.rb, line 168
def date_to_month(date)
  d = date.split('-')
  return Date.new(d[0].to_i, d[1].to_i, d[2].to_i).strftime('%Y-%m')
end
days_of_month(month) click to toggle source

@param month [String] month in '%Y-%m' @return [Array<String>] the list of days in the month

# File lib/patriot/util/date_util.rb, line 142
def days_of_month(month)
  s = month.split('-')
  y = s[0].to_i
  m = s[1].to_i
  return 1.upto(31).map{|d| Date.new(y,m,d).strftime('%Y-%m-%d') if Date.valid_date?(y,m,d)}.compact
end
days_of_month_until(dt) click to toggle source

@param dt [String] date in '%Y-%m-%d' @return [Array<String>] the list of days between the first date and the given date in the month

# File lib/patriot/util/date_util.rb, line 151
def days_of_month_until(dt)
  s = dt.split('-')
  y = s[0].to_i
  m = s[1].to_i
  d = s[2].to_i
  return 1.upto(d).map{|d| Date.new(y,m,d).strftime('%Y-%m-%d') if Date.valid_date?(y,m,d)}.compact
end
days_of_week(date) click to toggle source

@param date [String] date in '%Y-%m-%d' @return [Integer] week of the date in (0-6)

# File lib/patriot/util/date_util.rb, line 135
def days_of_week(date)
  d = to_date_obj(date)
  return d.wday
end
hours() click to toggle source

@return [Array<String>] a list of hours (00..23)

# File lib/patriot/util/date_util.rb, line 174
def hours
  return 0.upto(23).map do |h| h_str = h.to_s.rjust(2, "0") end.flatten
end
month_add(month, interval) click to toggle source

add interval to month @param month [String] month in '%Y-%m' @param interval [Integer] interval in month @return [String] month in '%Y-%m'

# File lib/patriot/util/date_util.rb, line 84
def month_add(month, interval)
  d = to_date_obj("#{month}-01")
  d = d >> interval
  return d.strftime('%Y-%m')
end
month_sub(month, interval) click to toggle source

subtract interval from month @param month [String] month in '%Y-%m' @param interval [Integer] interval in month @return [String] month in '%Y-%m'

# File lib/patriot/util/date_util.rb, line 94
def month_sub(month, interval)
  d = to_date_obj("#{month}-01")
  d = d << interval
  return d.strftime('%Y-%m')
end
to_date_obj(date) click to toggle source

convert to a Date instance @param date [String] date in '%Y-%m-%d' @return [Date]

# File lib/patriot/util/date_util.rb, line 162
def to_date_obj(date)
  d = date.split('-')
  return Date.new(d[0].to_i, d[1].to_i, d[2].to_i)
end
to_end_of_last_month(date) click to toggle source

get the last date of the last month @param date [String] date in '%Y-%m-%d' @return [String] date in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 127
def to_end_of_last_month(date)
  d = to_date_obj(date)
  d = d - d.day
  return d.strftime('%Y-%m-%d')
end
to_end_of_month(date) click to toggle source

get the last date of the month @param date [String] date in '%Y-%m-%d' @return [String] date in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 119
def to_end_of_month(date)
  s = date.split("-");
  return ((Date.new(s[0].to_i,s[1].to_i,1)>>1)-1).strftime("%Y-%m-%d")
end
to_month(date) click to toggle source

convert to month expression @param date [String] date in '%Y-%m-%d' @return [String] month in '%Y-%m'

# File lib/patriot/util/date_util.rb, line 103
def to_month(date)
  d = to_date_obj(date)
  return d.strftime('%Y-%m')
end
to_start_of_month(date) click to toggle source

get first date of the month @param date [String] date in '%Y-%m-%d' @return [String] date in '%Y-%m-%d'

# File lib/patriot/util/date_util.rb, line 111
def to_start_of_month(date)
  s = date.split("-");
  return Date.new(s[0].to_i,s[1].to_i,1).strftime("%Y-%m-%d")
end

Private Instance Methods

validate_and_parse_dates(date) click to toggle source

validate date or date range expression

# File lib/patriot/util/date_util.rb, line 179
def validate_and_parse_dates(date)
  date_objs = date.split(",").map do |d|
    unless d.to_s =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
      raise ArgumentError, "ERROR: invalid date #{d} in #{date}"
    end
    Date.parse(d.to_s)
  end
  if date_objs.size == 1
    return [date]
  elsif date_objs.size == 2
    dates = []
    date_objs[0].upto(date_objs[1]){|d| dates << d.to_s}
    return dates
  end
  raise ArgumentError, "ERROR: invalid date #{date}"
end