SystemyDialogowe/dateHelper.py

158 lines
4.3 KiB
Python
Raw Permalink Normal View History

2021-06-06 21:18:33 +02:00
from datetime import date, datetime, timedelta, time
2021-06-06 21:56:01 +02:00
import re
2021-06-07 00:13:44 +02:00
from typing import Generator
2021-06-06 21:56:01 +02:00
2021-06-06 21:18:33 +02:00
# Monday - 0
# Tuesday - 1
# Wednesday - 2
# Thursday - 3
# Friday - 4
# Saturday - 5
# Sunday -6
2021-06-07 00:13:44 +02:00
def format_date(d):
return d.strftime('%d.%m.%Y')
2021-06-06 21:18:33 +02:00
def get_current_date():
return date.today()
def get_current_datetime():
return datetime.today()
def get_current_day_of_week():
return datetime.today().weekday()
def convert_timestamp_to_UTC(timestamp):
return datetime.fromtimestamp(timestamp)
def convert_UTC_to_timestamp(date):
return datetime.timestamp(date)
def get_day_of_week_number(day):
2021-06-06 21:56:01 +02:00
if day == 'dziś' or day == 'dzisiaj':
return get_current_day_of_week()
2021-06-06 21:18:33 +02:00
if day == 'poniedziałek':
return 0
elif day == 'wtorek':
return 1
elif day == 'środa':
return 2
elif day == 'czwartek':
return 3
elif day == 'piątek':
return 4
elif day == 'sobota':
return 5
elif day == 'niedziela':
return 6
def get_time_from_hours_minutes(hours, minutes):
return time(hours, minutes, 0)
2021-06-07 00:13:44 +02:00
def format_time(time):
if re.match(r'^[0-2][0-9]$', time):
hours = int(time)
minutes = 00
elif re.match(r'^([0-2][0-9]).([0-9][0-9])(-)?.*$', time):
m = re.search(r'^([0-2][0-9]).([0-9][0-9])(-)?.*$', time)
hours = int(m.group(1))
minutes = int(m.group(2))
return get_time_from_hours_minutes(hours, minutes).strftime('%H:%M')
def get_end_time(time):
if re.match(r'^([0-2][0-9]).?([0-9][0-9])?-([0-2][0-9]).?([0-9][0-9])?$', time):
m = re.search(
r'^^([0-2][0-9]).?([0-9][0-9])?-([0-2][0-9]).?([0-9][0-9])?$', time)
hours = int(m.group(3))
if m.group(4) is None:
minutes = 0
else:
minutes = int(m.group(4))
return get_time_from_hours_minutes(hours, minutes).strftime('%H:%M')
else:
# jak nie ma to zakładamy że trwa godzinę
if re.match(r'^[0-2][0-9]$', time):
hours = int(time)
minutes = 00
elif re.match(r'^([0-2][0-9]).([0-9][0-9])(-)?.*$', time):
m = re.search(r'^([0-2][0-9]).([0-9][0-9])(-)?.*$', time)
hours = int(m.group(1))
minutes = int(m.group(2))
hours += 1
if hours == 23:
hours = 24
elif hours == 24:
hours = 1
return get_time_from_hours_minutes(hours, minutes).strftime('%H:%M')
2021-06-06 21:18:33 +02:00
def get_datetime_from_day(day, hours, minutes):
current_day_of_week = get_current_day_of_week()
input_date_of_week = get_day_of_week_number(day)
input_time = get_time_from_hours_minutes(hours, minutes)
# data wypada na dzisiaj i trzeba sprawdzić godziny
if current_day_of_week == input_date_of_week:
# Podanej czas już był
if input_time < datetime.now().time():
date = get_current_date() + timedelta(days=7)
return datetime.combine(date, input_time)
# Podana czas jeszcze nie był
else:
date = get_current_date()
return datetime.combine(date, input_time)
# data będzie z przyszłośći
else:
date = get_current_date() + timedelta(days=(7 - abs(current_day_of_week-input_date_of_week)))
return datetime.combine(date, input_time)
2021-06-06 21:56:01 +02:00
def get_date(date, time):
# parsowanie czasu
if re.match(r'^[0-2][0-9]$', time):
hours = int(time)
minutes = 00
2021-06-07 00:13:44 +02:00
elif re.match(r'^([0-2][0-9]).([0-9][0-9])(-)?.*$', time):
m = re.search(r'^([0-2][0-9]).([0-9][0-9])(-)?.*$', time)
2021-06-06 21:56:01 +02:00
hours = int(m.group(1))
minutes = int(m.group(2))
# TODO jeżeli jeszcze jakieś dziwne formaty to obsłużyć przypadki
if re.match(r'^[0-3][0-9]\.[0-1][0-9]\.20[0-9]{2}$', date):
date = datetime.strptime(date, '%d.%m.%Y').date()
return datetime.combine(date, get_time_from_hours_minutes(hours, minutes))
else:
return get_datetime_from_day(date, hours, minutes)
# Tests
2021-06-07 00:13:44 +02:00
2021-06-06 21:56:01 +02:00
# print(get_datetime_from_day('niedziela', 22, 21))
# print(get_datetime_from_day('niedziela', 18, 21))
# print(get_datetime_from_day('poniedziałek', 18, 21))
# print(get_datetime_from_day('wtorek', 18, 21))
# print(get_date("07.06.2021", "12:00"))
# print(get_date("sobota", "00:00"))
# print(get_date("wtorek", "12:00"))
# print(get_date("12.06.2021", "23"))
2021-06-07 00:13:44 +02:00
# print(get_current_date())