From ade9654df11a7aab25bf2f87456fda6388a4c33c Mon Sep 17 00:00:00 2001 From: abi Date: Sun, 11 Oct 2020 21:06:49 +0100 Subject: [PATCH 1/2] initial commit timer --- .../Countdowntimer/Readme-countdowntimer.md | 2 + .../Countdowntimer/countdown timer program.py | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 Python/Countdowntimer/Readme-countdowntimer.md create mode 100644 Python/Countdowntimer/countdown timer program.py diff --git a/Python/Countdowntimer/Readme-countdowntimer.md b/Python/Countdowntimer/Readme-countdowntimer.md new file mode 100644 index 0000000..b1b7fce --- /dev/null +++ b/Python/Countdowntimer/Readme-countdowntimer.md @@ -0,0 +1,2 @@ +# Description: + This is the python program for a countdowntimer. The user can enter any future date and the countdown begins. diff --git a/Python/Countdowntimer/countdown timer program.py b/Python/Countdowntimer/countdown timer program.py new file mode 100644 index 0000000..4807338 --- /dev/null +++ b/Python/Countdowntimer/countdown timer program.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[12]: + + +import time +import datetime + + +# Get the input in dd/mm/year format +print('Enter the date in dd mm year as space separated integers') +[dd,mm,year]=list(map(int,input().split())) + +#Get today's date using datetime module +today=datetime.datetime.now().date() + +#convert the date as date/month/year format +today_date = today.strftime("%d/%m/%Y") +[ddtoday,mmtoday,yeartoday]=list(map(int,today_date.split('/'))) + +# Get the time object from datetime module +now = datetime.datetime.now().time() + +#convert the time as hour/minute/second format +now_time = now.strftime("%H:%M:%S") +[hr,mins,sec]=list(map(int,now_time.split(':'))) + +#Using timedelta to get the difference between two dates +given_date = datetime.datetime(year,mm,dd,00,00,00) +today_date = datetime.datetime(yeartoday,mmtoday,ddtoday,hr,mins,sec) +diff = given_date-today_date + + +#From the output of timedelta function, calculate the remaining number of days, hours, minutes and seconds +diff_string=str(diff) +[a,b]=diff_string.split(',') +dayss=int(a.split(' ')[0]) +[h,m,s]=list(map(int,b.split(':'))) + +#If any of the values is negative, that means user entered a past date which is not valid for a countdowntimer +if (dayss<0) or (h<0) or (m<0) or (s<0): + print('The given date is not a valid date') + +#If all the values are positive, the timer can run +else: + + #Calculate the total number of seconds + total=(dayss*24*60*60)+diff.seconds + + #the timer has to run until the total no. of seconds become zero + #Interrupt the kernel to stop the timer in between + while total: + + #calculate the no. of seconds, minutes, hours,days using the total no. of seconds + mz,sz=divmod(total,60) + hz,mz=divmod(mz,60) + dz,hz=divmod(hz,24) + + #Display the timer in the days, hours, minutes,seconds format + timer = '{:02d} days :{:02d} hours :{:02d} minutes : {:02d} seconds'.format(dz,hz,mz,sz) + + print(timer, end="\r") + + #Pause the timer for 1 second + time.sleep(1) + + #Decrement the total number of seconds by 1 + total=total-1 + + + + + + + From ff5185aa16b3cb26cf7fee30765cf778036149ed Mon Sep 17 00:00:00 2001 From: Abirami-S21 <67931551+Abirami-S21@users.noreply.github.com> Date: Sun, 11 Oct 2020 21:26:41 +0100 Subject: [PATCH 2/2] Update countdown timer program.py There was a trailing space error. So, tried to fix it. --- Python/Countdowntimer/countdown timer program.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Python/Countdowntimer/countdown timer program.py b/Python/Countdowntimer/countdown timer program.py index 4807338..6ae0a0d 100644 --- a/Python/Countdowntimer/countdown timer program.py +++ b/Python/Countdowntimer/countdown timer program.py @@ -67,10 +67,3 @@ #Decrement the total number of seconds by 1 total=total-1 - - - - - - -