# A python script that takes a date in the form of three integers (year, month, day) # and returns the number of days from that date to the end of its year. # For example, with an input of (2012, 11, 30) the function would return 32. # For example, with an input of (2012, 12, 30) the function would return 1. # If you know how many days are in the year (365 or 366) and if you know what day within the year # your date of interest represents (Feb 1, 2012 is day 32 within the year), then it's simple to # calculate 366-32 and know that from Feb 1, 2012 to Dec 31, 2012 is 334 days. def days_from_date_to_eoy(year, month, day): length_of_year = 366 if is_leap_year(year) else 365 return length_of_year - day_within_year(year, month, day) def is_leap_year(year): # years divisible by 400 (such as 1600 or 2000) are exceptions to the usual "divisible by 4" rule if year%400 == 0: return False else: # years divisible by 4 are leap years, unless they are also divisible by 400 if year%4 == 0: return True else: return False def day_within_year(year, month, day): # Concept: # For Jan 15, the return value should simply be 15. # For Feb 15, the return value should be however many days were in Jan (the prior month), plus 15. # For March 15, the return value should be however many total cumulateive days were in Jan and Feb, (the prior months) plus 15. # Examples in the code: # For inputs of of a February 15 date (month=2), we must look up a value of 31 (the "january prior month cumulative total") and then add 15. # To get a total value of 31 for the "first" month of Jan, we need a list with 31 in the "first" position, but lists are indexed starting with zero. # Since month=2 for Feb input, we have to subtract 2 from the input to get zero, and retrieve 31 as a "total days in the prior months" # For an input of March 15 (month=3) 3-2=1, so prior_months_days[1] will produce 59, the total days in Jan and Feb that must be added to 15 # However, if it's a leap year, we need to add 1 to that total, so that's why we have a leap_year_correction. # For inputs of January (month=1), when we subract 2 from month, we get -1, and a list lookup of listname[-1] produces the last item in the list. # There's no 365 in the list because the highest month that should come in the input is 12, and 12-2=10, so prior_months_days[10] is 334, and that's # sufficient leap_year_correction = 1 if month > 2 and is_leap_year(year) else 0 prior_months_days = [31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0] return prior_months_days[month - 2] + day + leap_year_correction def test(): test_cases = [((2012, 1, 1), 365), ((2012, 1, 31), 335), ((2012, 2, 1), 334), ((2012, 2, 29), 306), ((2012, 3, 1), 305), ((2012, 3, 31), 275), ((2012, 4, 1), 274), ((2012, 4, 30), 245), ((2012, 5, 1), 244), ((2012, 5, 31), 214), ((2012, 6, 1), 213), ((2012, 6, 30), 184), ((2012, 7, 1), 183), ((2012, 7, 31), 153), ((2012, 8, 1), 152), ((2012, 8, 31), 122), ((2012, 9, 1), 121), ((2012, 9, 30), 92), ((2012, 10, 1), 91), ((2012, 10, 31), 61), ((2012, 11, 1), 60), ((2012, 11, 30), 31), ((2012, 12, 1), 30), ((2012, 12, 31), 0), ((2013, 1, 1), 364), ((2013, 1, 31), 334), ((2013, 2, 1), 333), ((2013, 2, 28), 306), ((2013, 3, 1), 305), ((2013, 3, 31), 275), ((2013, 4, 1), 274), ((2013, 4, 30), 245), ((2013, 5, 1), 244), ((2013, 5, 31), 214), ((2013, 6, 1), 213), ((2013, 6, 30), 184), ((2013, 7, 1), 183), ((2013, 7, 31), 153), ((2013, 8, 1), 152), ((2013, 8, 31), 122), ((2013, 9, 1), 121), ((2013, 9, 30), 92), ((2013, 10, 1), 91), ((2013, 10, 31), 61), ((2013, 11, 1), 60), ((2013, 11, 30), 31), ((2013, 12, 1), 30), ((2013, 12, 31), 0)] passes = 0 for (args, answer) in test_cases: result = days_from_date_to_eoy(*args) if result != answer: print "Failed when argument was: ", args else: passes = passes + 1 print str(passes) + " out of " + str(len(test_cases)) + " test cases passed." test() #print days_from_date_to_eoy(2013, 2, 29)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question