Given the number of seconds, the task is to write a Python program to calculate the date, month, and year in the format MM-DD-YYYY that have been passed from 1 January 1947.
Examples:
Input: 0
Output: 01-01-1947
Input: 123456789
Output: 11-29-1950
Input: 9876543210
Output: 12-22-2259
Step-by-step Approach:
- Create a function to get the number of days in a year.
Python
def dayInYear(year):
if (year % 4 ) = = 0 :
if (year % 100 ) = = 0 :
if (year % 400 ) = = 0 :
return 366
else :
return 365
else :
return 366
else :
return 365
|
- Create a function to count the years after 1947.
Python3
def getYear(days):
year = 1946
while True :
year + = 1
dcnt = dayInYear(year)
if days > = dcnt:
days - = dcnt
else :
break
return year, days
|
- Create a function to count the number of months.
Python3
def monthCnt(days, year):
if days = = 0 :
return 1 , 0
else :
month_num = 1
months = [ 31 , 28 , 31 , 30 , 31 ,
30 , 31 , 31 , 30 , 31 ,
30 , 31 ]
if dayInYear(year) = = 366 :
months[ 1 ] = 29
for day in months:
if day < days:
month_num + = 1
days - = day
else :
break
return month_num, days
|
- Create a function to get a date using the number of seconds.
Python3
def getDate(num_sec):
days_sec = 24 * 60 * 60
days = num_sec / / days_sec
day_started = False
if days % days_sec ! = 0 :
day_started = True
year, days = getYear(days)
month, days = monthCnt(days, year)
if day_started or num_sec = = 0 :
days + = 1
date = ""
if month < 10 :
date = date + "0" + str (month)
else :
date = date + str (month)
date = date + "-"
if days < 10 :
date = date + "0" + str (days)
else :
date = date + str (days)
date = date + "-"
date = date + str (year)
return date
|
- Create the driver code and call the required function.
Python3
date_format = getDate( 0 )
print (date_format)
date_format = getDate( 123456789 )
print (date_format)
date_format = getDate( 9876543210 )
print (date_format)
|
Below is the complete program based on the above stepwise approach:
Python3
def dayInYear(year):
if (year % 4 ) = = 0 :
if (year % 100 ) = = 0 :
if (year % 400 ) = = 0 :
return 366
else :
return 365
else :
return 366
else :
return 365
def getYear(days):
year = 1946
while True :
year + = 1
dcnt = dayInYear(year)
if days > = dcnt:
days - = dcnt
else :
break
return year, days
def monthCnt(days, year):
if days = = 0 :
return 1 , 0
else :
month_num = 1
months = [ 31 , 28 , 31 , 30 , 31 ,
30 , 31 , 31 , 30 , 31 ,
30 , 31 ]
if dayInYear(year) = = 366 :
months[ 1 ] = 29
for day in months:
if day < days:
month_num + = 1
days - = day
else :
break
return month_num, days
def getDate(num_sec):
days_sec = 24 * 60 * 60
days = num_sec / / days_sec
day_started = False
if days % days_sec ! = 0 :
day_started = True
year, days = getYear(days)
month, days = monthCnt(days, year)
if day_started or num_sec = = 0 :
days + = 1
date = ""
if month < 10 :
date = date + "0" + str (month)
else :
date = date + str (month)
date = date + "-"
if days < 10 :
date = date + "0" + str (days)
else :
date = date + str (days)
date = date + "-"
date = date + str (year)
return date
date_format = getDate( 0 )
print (date_format)
date_format = getDate( 123456789 )
print (date_format)
date_format = getDate( 9876543210 )
print (date_format)
|
Output:
01-01-1947
11-29-1950
12-22-2259