1. Exercise 2.18 (Current time) Listing 2.7, ShowCurrentTime.py, gives a program
ID: 3756150 • Letter: 1
Question
1. Exercise 2.18 (Current time) Listing 2.7, ShowCurrentTime.py, gives a program that dispiays the current timein GMT. Revise the program so that it prompts the user to enter the time zone in hours away from (offset to) GMT and displays the time in the specified time zone.
Enter the time zone offset to GMT : -5
The current time is :
I need this exact code using python. If you can please write neatly or type and please put exact coding that i need to use because I am confused with this exercise.
This is the coding I have currently typed. What do I need to add to this to make python run the entire answer?
This is what I get as my output:
This Program Displays The Time In The Specified Time Zone.
Enter the time zone offset to GMT: -5
My current input:
import time
print("This Program Displays The Time In The Specified Time Zone.")
#Obtain the total milliseconds
totalMilliseconds = int(round(time.time()*1000))
#Obtain the total seconds
totalSeconds = totalMilliseconds / 1000
#Compute the current second in the minute in the hour
currentSecond = totalSeconds % 60
#Obtain the total minutes
totalMinutes = totalSeconds / 60
#Compute the current minute in the hour
currentMinute = totalMinutes % 60
#Obtain the total hours
totalHours = totalMinutes / 60
#Compute the current hour
currentHour = totalHours % 24
timeZoneOffset=int(input("Enter the time zone offset to GMT: -5 " ))
currentHour = currentHour + timeZoneOffset;
if currentHour >= 24:
currentHour = currentHour - 24
elif currentHour == 0:
currentHour = 24 + timeZoneOffset
print("Current time is " + str(round(currentHour)) + ":"
+ str(round(currentMinute)) + ":" + str(round(currentSecond)) +
" GMT " + str(round(timeZoneOffset)) + ".");
Explanation / Answer
#Here the code is trying to calculate the new value. The first condition will work but the second condition is wrong. If on adding the time zone the currentHour results into zero there is no need to change the value. But a new condition is needed to check if the value of currentHour is negative or not and the negative currentHour value should be added from 24 to get the appropriate result.
if currentHour >= 24:
currentHour = currentHour - 24
elif currentHour == 0:
currentHour = 24 + timeZoneOffset
So here is the corrected code:
import time
print("This Program Displays The Time In The Specified Time Zone.")
#Obtain the total milliseconds
totalMilliseconds = int(round(time.time()*1000))
#Obtain the total seconds
totalSeconds = totalMilliseconds / 1000
#Compute the current second in the minute in the hour
currentSecond = totalSeconds % 60
#Obtain the total minutes
totalMinutes = totalSeconds / 60
#Compute the current minute in the hour
currentMinute = totalMinutes % 60
#Obtain the total hours
totalHours = totalMinutes / 60
#Compute the current hour
currentHour = totalHours % 24
timeZoneOffset=int(input("Enter the time zone offset to GMT: " ))
currentHour = currentHour + timeZoneOffset;
if currentHour >= 24:
currentHour = currentHour - 24
elif currentHour < 0:
currentHour = 24 + currentHour
print("Current time is " + str(round(currentHour)) + ":"+ str(round(currentMinute)) + ":" + str(round(currentSecond)) +
" GMT " + str(round(timeZoneOffset)) + ".");
#Code Ends Here
Also the given code does not account for minutes in the change of time zone, so I have assumed that the user will always enter integral number of hours, thus keeping initial logic unchanged.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.