Making a system time to GMT+3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Making a system time to GMT+3

from datetime import datetime from itertools import cycle now = datetime.now() hour = str(now.hour).zfill(2) minute = str(now.minute).zfill(2) second = str(now.second).zfill(2) ptime = cycle(hour + minute + second) print(f'System time: {hour}:{minute}:{second}') How can I make the clock to GMT +3 Hours

27th Dec 2023, 1:39 PM
The Saint
3 Answers
+ 5
from datetime import datetime, timezone, timedelta from itertools import cycle import pytz now = datetime.now(timezone(timedelta(hours=+3))) hour = str(now.hour).zfill(2) minute = str(now.minute).zfill(2) second = str(now.second).zfill(2) ptime = cycle(hour + minute + second) print(f'System time: {hour}:{minute}:{second}')
27th Dec 2023, 2:53 PM
JaScript
JaScript - avatar
+ 3
Rather than zero-filling the individual time components, it is much more simple to use a format mask. print(now.strftime('%H:%M:%S'))
27th Dec 2023, 6:48 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Hey, it is exactly my code. https://sololearn.com/compiler-playground/cJT3A3qAUMhR/?ref=app I opted not to play with the timezone because it requires user input before running it in playground, and the input prompt doesn't display to the user. You can copy the code to a .py file in your conputer. Run the code in your own device, it will get the time from it.
27th Dec 2023, 4:11 PM
Wong Hei Ming
Wong Hei Ming - avatar