Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 2 - Elementary Programming - Programming Exercises - Page 59: 2.18

Answer

Code: import time # Prompt the user to enter the time zone offset to GMT timeZoneOffset = eval(input("Enter the time zone offset to GMT: ")) current Time $=$ time.time ()$\#$ Get current time # Get total, current seconds since midnight, Jan 1, 1970 totalSeconds $=$ int(currentTime) currentSecond= totalSeconds\% 60 # Get the total and current minutes totalMinutes $=$ totalSeconds $/ / 60$ currentMinute $=$ totalMinutes $\% 60$ # Get the total hours totalHours $=$ totalMinutes $/ / 60$ # Compute the current hour, include GMT offset currentHour= (totalHours + timeZoneOffset) \% 24 # Display time with GMT offset print("Current time is", currentHour, ":", currentMinute ,":", currentSecond)

Work Step by Step

Step 1 of 3: Program Plan: - Prompt the user to enter the time zone offset to GMT. - Get the current, and total seconds, current, total minutes and total hours as per earlier. - Get the current minute in this hour, include the GMT offset. - Display the current time as earlier. Step 2 of 3: Code: import time # Prompt the user to enter the time zone offset to GMT timeZoneOffset = eval(input("Enter the time zone offset to GMT: ")) current Time $=$ time.time ()$\#$ Get current time # Get total, current seconds since midnight, Jan 1, 1970 totalSeconds $=$ int(currentTime) currentSecond= totalSeconds\% 60 # Get the total and current minutes totalMinutes $=$ totalSeconds $/ / 60$ currentMinute $=$ totalMinutes $\% 60$ # Get the total hours totalHours $=$ totalMinutes $/ / 60$ # Compute the current hour, include GMT offset currentHour= (totalHours + timeZoneOffset) \% 24 # Display time with GMT offset print("Current time is", currentHour, ":", currentMinute ,":", currentSecond) Step 3 of 3: Sample Output: Enter the time zone offset to GMT: $-5$ Current time is $1: 51: 50$
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.