Module: Patriot::Util::DateUtil

Included in:
Command::Base, Tool::BatchParser, Script, Worker::Servlet::JobAPIServlet
Defined in:
lib/patriot/util/date_util.rb

Overview

namesapce for date functions

Instance Method Summary collapse

Instance Method Details

#date_add(date, interval) ⇒ String

add interval to date



48
49
50
51
52
# 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) ⇒ String



67
68
69
70
# 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 = {}) ⇒ Object

format date



10
11
12
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
# 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) ⇒ String

subtract interval from date



58
59
60
61
62
# 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) ⇒ String



75
76
77
78
# 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) ⇒ Object

Deprecated.


168
169
170
171
# 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) ⇒ Array<String>



142
143
144
145
146
147
# 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) ⇒ Array<String>



151
152
153
154
155
156
157
# 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) ⇒ Integer



135
136
137
138
# File 'lib/patriot/util/date_util.rb', line 135

def days_of_week(date)
  d = to_date_obj(date)
  return d.wday
end

#hoursArray<String>



174
175
176
# 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) ⇒ String

add interval to month



84
85
86
87
88
# 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) ⇒ String

subtract interval from month



94
95
96
97
98
# 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) ⇒ Date

convert to a Date instance



162
163
164
165
# 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) ⇒ String

get the last date of the last month



127
128
129
130
131
# 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) ⇒ String

get the last date of the month



119
120
121
122
# 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) ⇒ String

convert to month expression



103
104
105
106
# 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) ⇒ String

get first date of the month



111
112
113
114
# 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