Skip to content

Instantly share code, notes, and snippets.

@hahahana
Created May 16, 2013 19:24
Show Gist options
  • Save hahahana/43dd9fbce25c4775a20d to your computer and use it in GitHub Desktop.
Save hahahana/43dd9fbce25c4775a20d to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment