# The following functions are used by our time and date programs to 
# manipulate time and date values.

# here is a list of days in each month
daylist = [31,28,31,30,31,30,31,31,30,31,30,31]

# here is a list of month names (in month-number order)
monthnames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] 

# this function accept the number of seconds, and prints the hours, minutes, and
# seconds that it represents.
def secsToHMS(sec):
    if sec >= 3600:
        print sec/3600, "hours and",
        sec = sec % 3600
    if sec>= 60:    
            print sec/60, "minutes and",
    print sec%60, "seconds"
 
 # this funciton accepts the hours minutes and seconds  and converts it to
 # the number of seconds in a day that it represents.
def hmsToSecs(h,m,s):
    return (s + (h * 3600) + (m * 60))

# check if year is a leap year.
# return 1 if year is a leap year,
# otherwise return 0
# A year is a leap year if it is divisible by four, but not by 100 - unless it
# is divisible by 400.
def isLeap(year):
    leap = 0
    if year%400 == 0:
        leap = 1
    elif year%100 ==0:
        leap = 0
    elif year%4 == 0:
        leap = 1
    else:
        leap = 0
    return leap

# This function accepts the day number and the year number
# and prints the Month day and year of the date specified.
# You must take into consideration leap years.
def getYearDay(day, year):
    month = -1
    i =0
    while i < 12 and month < 0:
        if i == 1 and isLeap(year) == 1:
            if day > daylist[i] + 1:
                day = day - (daylist[i] + 1)
            else: 
                month = i
        else:
            if day > daylist[i]:
                day = day - daylist[i]
            else:
                month = i
        i = i + 1
    print monthnames[month], day, year
    
    
 # this function accept a year, month and day as a number
 # and returns the julian day of the year that they represent.
def ymdToJulian(y, m, d):
    days = 0
    for i in range(0, m-1):
        days = days + daylist[i]
    days = days + d
    if m>2 and isLeap(y) == 1:
        days = days +1
    return days



