Как определить, поздно ли сейчас ночь, с помощью Python: примеры кода

Вот несколько способов узнать текущее время и определить, поздно ли сейчас ночь, используя примеры кода на Python:

Метод 1: использование модуля datetime

from datetime import datetime, time
now = datetime.now().time()
late_night_start = time(22, 0)  # 10:00 PM
if now >= late_night_start:
    print("It's late in the night!")
else:
    print("It's not late in the night.")

Метод 2. Использование модуля time

import time
now = time.localtime()
hour = now.tm_hour
if hour >= 22:
    print("It's late in the night!")
else:
    print("It's not late in the night.")

Метод 3: использование библиотеки arrow(требуется установка с помощью pip installarrow)

import arrow
now = arrow.now()
late_night_start = arrow.now().replace(hour=22, minute=0, second=0)
if now >= late_night_start:
    print("It's late in the night!")
else:
    print("It's not late in the night.")

Метод 4. Использование библиотеки pytz(требуется установка с помощью pip install pytz)

import pytz
from datetime import datetime
now = datetime.now(pytz.timezone('Your/Timezone'))
late_night_start = now.replace(hour=22, minute=0, second=0, microsecond=0)
if now >= late_night_start:
    print("It's late in the night!")
else:
    print("It's not late in the night.")