Неправильное преобразование времени с нецелой разницей по Гринвичу в PythonPython

Программы на Python
Ответить
Anonymous
 Неправильное преобразование времени с нецелой разницей по Гринвичу в Python

Сообщение Anonymous »

Вот мой код:

Код: Выделить всё

"""GMT converter"""

from datetime import datetime, timedelta

# Input from user
current_time_str = input("")  # Time now
current_gmt = float(input("")) # GMT current
target_gmt = float(input(""))  # Target GMT

# Replace "A.M." and "P.M." with "AM" and "PM" to match the format of strptime.
current_time_str = current_time_str.replace("A.M.", "AM").replace("P.M.", "PM")

# Convert current time from string format to datetime object.
current_time = datetime.strptime(current_time_str, "%I:%M %p")

# Calculate the GMT difference and use timedelta to shift the time.
gmt_difference = target_gmt - current_gmt
time_difference = timedelta(hours=gmt_difference)

# Increase or decrease the GMT time you want to find.
target_time = current_time + time_difference

# Convert target time back to HH:MM A.M./P.M. format.
output_time_str = target_time.strftime("%I:%M %p")

# Replace "AM" and "PM" with "A.M." and "P.M."
output_time_str = output_time_str.replace("AM", "A.M.").replace("PM", "P.M.")

# Output
print(output_time_str)
Пример 1, ввод:

Код: Выделить всё

03:00 P.M. (Time now)
-1 (GMT Current as real number)
1 (Target GMT as real number)
Выход:

Код: Выделить всё

05:00 P.M.
Пример 2, ввод:

Код: Выделить всё

01:00 P.M.
5.30
7
Ожидаемый результат: 14:30.
Что я получил вместо этого: 14:42
Когда я запускаю код, пример 1 верен, а пример 2 — нет.

Подробнее здесь: https://stackoverflow.com/questions/790 ... -in-python
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»