secret
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
def leap_day(date)
date.month == 2 && date.mday == 29 && Date.leap?(date.year)
end
 
def hire_date
DateTime.new(2013,4,16) # self.hire_date - Should be a db field, just for temporarily messing around
end
 
def current_date
DateTime.now # maybe should change to DateTime.new(DateTime.now.year, DateTime.now.month, DateTime.now.mday ) to get rid of the timestamp since most places don't care about what hour of the day it is
end
 
def total_days_current_year
DateTime.new(current_date.year, 12, -1).yday()
end
def years_employed
# If leap day, change calculated startding day to the 28th since DateTime doesn't support invalid dates.
# I prefer not to outright subtract since it's not as deliberate and can be an issue if everything is not properly typecast
# Include the day started as a full day, consider current_date a full day
# The leap day adding logic is not quite complete though. What if the current_date is a leap day, etc.?
year_point = current_date.year - hire_date.year
start_date = leap_day(hire_date) ? DateTime.new(hire_date.year, 2, 28) : hire_date # I like recasting the hire day as start_date because then we don't worry about seconds, etc., but should really be addressed in hire_date and start_date
adder_date = DateTime.new(start_date.year + year_point, start_date.month, start_date.mday)
 
if (adder_date != current_date)
(year_point + (current_date.yday() - adder_date.yday() + 1).to_f/total_days_current_year.to_f).to_f
else
year_point
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.