Experience: is what you get soon after you need it.

Experience: is what you get soon after you need it.

****************I am authoring a Book on Oracle Database Cloud Services.......More details to follow.***************

Title : Oracle Database Cloud Revealed
Publisher : Apress
Release Date : Jan-2019

**********************************************************************************


Rasul Allah (sal Allahu alaihi wa sallam) said: "Restore the trusts of those who trust you, and deal not falsely with him who deals falsely with you." [Abu Dawud, Tirmidhi]


My Cloud Certifications:

AWS Certified Solutions Architect Associate

Oracle Cloud Infrastructure 2018 Certified Architect Associate.

Oracle Cloud Infrastructure Classic 2018 Certified Architect Associate.

Oracle Database Cloud Administrator Certified Professional.

Oracle Database Cloud Service Operations Certified Associate.

Search This Blog

Monday, October 26, 2015

Calculate age in Python


Calculate age:
Takes input a) Future Date(optional) b)Birth Date
Validates for leap year and some basic input validations

 vi age.py
#########START OF SCRIPT#########
from sys import argv
from datetime import datetime,timedelta

oddmonths = [ '01','05','07','08','10','12' ]

def getdata():
     global clockyear
     global clockmonth
     global clockday
     global iyear
     global imonth
     global iday
     global ageyear
     global agemonth
     global ageday
     ask = raw_input("\nDo you want to calc age at a certain date?...Press Y|y|yes:\t")
     if ask == "":
        print "Invalid Response...Exiting"
        exit(1)
     elif ask in ['Y', 'y', 'yes'] :
       get = raw_input("\nPlease enter the from date as yyyy/mm/dd:\t")
       if len(get)< 10:
         print "Invalid Date...Exiting"
       else:
         clock = get.split("/")
         clockyear = int(clock[0])
         clockmonth = int(clock[1])
         clockday = int(clock[2])
     else:
         clock = datetime.now()
         clockyear = clock.year
         clockmonth = clock.month
         clockday = clock.day
    #Assign input variables
     getdetail = raw_input("\nEnter you date of birth as yyyy/mm/dd:\t")
     input = getdetail.split("/")
     iyear = int(input[0])
     imonth = int(input[1])
     iday = int(input[2])
     if iyear > clockyear:
        print "\nCal year cannot be > the birth year..Please Enter valid year\n"
        exit(1)
     #Assign age variables
     ageyear = clockyear - iyear
     agemonth = clockmonth - imonth
     ageday = clockday - iday

getdata()


#Assign age variables
#ageyear = clockyear - iyear
#agemonth = clockmonth - imonth
#ageday = clockday - iday

#print "Your ageyear\t:",ageyear
#print "Your age month\t:",agemonth
#print "Your ageday\t:",ageday

##### Function checks for Leap Year
def leapyear(a):
    if a % 400 == 0:
        return True
    elif a % 100 == 0:
        return False
    elif a % 4 == 0:
        return True
    else:
        return False

#Calc month & year
def month(a,b):
  if imonth < 0 or imonth > 12:
     print "You Entered invalid month...Exiting."
     exit(1)
  elif a < imonth:
      return(12+(a-imonth),b-1)
  elif a > imonth:
        return((a-imonth),b)
  else:
        return(0,b)


def prints(ageyear,agemonth,ageday):
        print "\nYour age is:\t%s Years:\t%s Months:\t%s Days\n" % (ageyear,agemonth,ageday)

def age():
        global imonth
        global iyear
        global iday
        global ageyear
        global agemonth
        global ageday
        global clockmonth
        global clockday
        if iday < 0 or iday > 31:
           print "You Entered invalid day...Exiting."
           exit(1)
        elif clockmonth !=03 and ageday >= 0 and agemonth >  0:
             prints(ageyear,agemonth,ageday)
        elif clockmonth !=03 and ageday >= 0 and agemonth < 0:
             agemonth,ageyear = month(clockmonth,ageyear)
             prints(ageyear,agemonth,ageday)
        elif clockmonth !=03 and ageday < 0 and (clockmonth-1) in oddmonths:
             ageday = 31 + ageday
             agemonth,ageyear = month((clockmonth-1),ageyear)
             prints(ageyear,agemonth,ageday)
        elif clockmonth !=03 and ageday < 0 and (clockmonth-1) not in oddmonths:
             ageday = 30 + ageday
             agemonth,ageyear = month((clockmonth-1),ageyear)
             prints(ageyear,agemonth,ageday)
####Condition for Leap Year check
        elif clockmonth == 03 and leapyear(clockyear):
           if ageday >=0:
             agemonth,ageyear = month((clockmonth),ageyear)
             prints(ageyear,agemonth,ageday)
           elif ageday <0:
             ageday = 29 + ageday
             agemonth,ageyear = month((clockmonth-1),ageyear)
             prints(ageyear,agemonth,ageday)
        elif clockmonth == 03 and not leapyear(clockyear):
           print " I am in 03"
           if ageday >=0:
                    agemonth,ageyear = month((clockmonth),ageyear)
                    prints(ageyear,agemonth,ageday)
           elif ageday <0:
                     ageday = 28 + ageday
                     agemonth,ageyear = month((clockmonth-1),ageyear)
                     prints(ageyear,agemonth,ageday)


def main():
        global imonth
        global iyear
        global iday
        if imonth == 02 and leapyear(iyear) and iday >29:
                print "You cannot have >29 days in Feb of a leapyear:\n"
                print "Enter valid day:\n"
        elif imonth == 02 and not leapyear(iyear) and iday >28:
                print "You cannot have >28 days in Feb,if the year is not a leapyear:\n"
                print "Enter valid day:\n"
        else:
                age()

main()

#########END OF SCRIPT#########


Output-1:
############
shaiks@MAC$py age.py

Do you want to calc age at a certain date?...Press Y|y|yes:    y

Please enter the from date as yyyy/mm/dd:    2017/08/31

Enter you date of birth as yyyy/mm/dd:    2000/02/29

Your age is:    17 Years:    6 Months:    2 Days


Output-2:
############
shaiks@MAC$py age.py

Do you want to calc age at a certain date?...Press Y|y|yes:    n

Enter you date of birth as yyyy/mm/dd:    2000/02/29

Your age is:    15 Years:    7 Months:    27 Days


Error Handling:
###############
shaiks@MAC$py age.py


Do you want to calc age at a certain date?...Press Y|y|yes:    y

Please enter the from date as yyyy/mm/dd:    2017/08/31

Enter you date of birth as yyyy/mm/dd:    2000/02/31
You cannot have >29 days in Feb of a leapyear:

Enter valid day:

No comments: