1
offset = eval(input("Enter the time zone offset to GMT: "))

currentTime = time.time()
totalSeconds = int(currentTime)
currentSecond = totalSeconds % 60
totalMinutes = totalSeconds // 60
currentMinute = totalMinutes % 60
totalHours = totalMinutes // 60
currentHour = (totalHours % 24)
print("Current time is", currentHour,":", currentMinute,":"currentSecond,"GMT")

The task: 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.

As you see, I have already prompted the user to enter the offset, but I cannot figure out what to do next. I have tried subtracting and adding offset, but will end up impossible answers like 28:00:... or -6:00:...

I am new to this stuff, so if you use any advanced (not beginner) terminology and operations, please give a brief brief explanation.

Thanks.

Matt Rumbel
  • 77
  • 1
  • 9

1 Answers1

0

You can try using the datetime module, and apply one of the answers here:

https://stackoverflow.com/a/26293497/296549

https://stackoverflow.com/a/2175170/296549

To prompt the user for the name of the timezone and convert from local time according to that input.

Otherwise, a revisited version of your code, would be:

import time

offset = input("Enter the time zone offset to GMT: ")

currentTime = time.time()
totalSeconds = int(currentTime)
currentSecond = totalSeconds % 60
totalMinutes = totalSeconds // 60
currentMinute = totalMinutes % 60
totalHours = totalMinutes // 60
currentHour = (totalHours % 24) 
print("Current time is: " +  str((currentHour + offset) % 24) + ":" +  str(currentMinute) + ":" + str(currentSecond) + "GMT") 

Where after adding the offset, you use the modulo operator to stay within the 24 units frame.

NOTE: this doesn't check the new timezone based on the offset!

To do that statically, you'd use some if conditions. I'd personally go for the datetime module.

Community
  • 1
  • 1
Bacon
  • 1,814
  • 3
  • 21
  • 36
  • I'm not supposed to use anything like that. Basically, I'm supposed to use basic numerical operations such as -, =, %, //, +=, **, etc... – Matt Rumbel Aug 06 '15 at 03:08
  • 1
    Well that's exactly what's happening here: you add the offset to the currentHour, and then use % to stay in the 24h format. Also, if that's a homework, you should specify it in the question :] – Bacon Aug 06 '15 at 03:09
  • I'm currently trying this myself. Would you be able to physically type of that line of code that uses the % to stay within the 24h format? – Matt Rumbel Aug 06 '15 at 19:03
  • This is was I did based on your suggestion - I'm not really sure exactly why this works, but it does: currentHour = (totalHours + offset) % 24 – Matt Rumbel Aug 06 '15 at 19:06
  • Oh never mind, I understand. Thank you very much for your help. – Matt Rumbel Aug 06 '15 at 19:07
  • Please consider accepting the answer by clicking on the accept button bellow the downvote arrow, if you think it fulfilled your needs :) – Bacon Aug 06 '15 at 19:13