Module: Whcal

Defined in:
lib/whcal.rb,
lib/whcal/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#diff_day(from_date, to_date) ⇒ Object



19
20
21
# File 'lib/whcal.rb', line 19

def diff_day(from_date, to_date)
  diff_hour(from_date,to_date) / 24.0 
end

#diff_hour(from_date, to_date) ⇒ Object



23
24
25
# File 'lib/whcal.rb', line 23

def diff_hour(from_date,to_date)
  diff_minute(from_date,to_date) / 60.0 
end

#diff_minute(from_date, to_date) ⇒ Object



27
28
29
# File 'lib/whcal.rb', line 27

def diff_minute(from_date,to_date)
  diff_second(from_date,to_date) / 60.0
end

#diff_month(from_date, to_date) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/whcal.rb', line 10

def diff_month(from_date, to_date)
  return ((((to_date.methods.include?(:year) ? to_date.year  : 0.0) - (to_date.methods.include?(:year) ? from_date.year : 0.0 )).abs.to_f*12.0 - 
    ((to_date.methods.include?(:month) ? to_date.month  : 0.0) - (to_date.methods.include?(:month) ? from_date.month : 0.0 )).abs.to_f) +
  ((to_date.methods.include?(:day) ? to_date.day  : 0.0) - (to_date.methods.include?(:day) ? from_date.day : 0.0 )).abs.to_f/30.0 + 
  ((to_date.methods.include?(:hour) ? to_date.hour  : 0.0) - (to_date.methods.include?(:hour) ? from_date.hour : 0.0 )).abs.to_f/(24*30.0) + 
  ((to_date.methods.include?(:minute) ? to_date.minute : 0.0) - (to_date.methods.include?(:minute) ? from_date.minute : 0.0 )).abs.to_f/(60*24*30.0) + 
  ((to_date.methods.include?(:second) ? to_date.second : 0.0) - (to_date.methods.include?(:second) ? from_date.second : 0.0 )).abs.to_f/(60*60*24*30.0))
end

#diff_second(from_date, to_date) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/whcal.rb', line 31

def diff_second(from_date,to_date)
  return (( to_date.methods.include?(:day) && from_date.methods.include?(:day) ? (to_date.to_date-from_date.to_date).to_i.abs.to_f*24*60*60 : 0.0) + 
  ((to_date.methods.include?(:hour) ? to_date.hour  : 0.0) - (to_date.methods.include?(:hour) ? from_date.hour : 0.0 )).abs.to_f*60*60 +
   ((to_date.methods.include?(:minute) ? to_date.minute : 0.0) - (to_date.methods.include?(:minute) ? from_date.minute : 0.0 )).abs.to_f*60 + 
   ((to_date.methods.include?(:second) ? to_date.second : 0.0) - (to_date.methods.include?(:second) ? from_date.second : 0.0 )).abs.to_f ) 
  
end

#diff_year(from_date, to_date) ⇒ Object



6
7
8
# File 'lib/whcal.rb', line 6

def diff_year(from_date, to_date)
  return diff_month(from_date, to_date)/12.0 
end