From 2af0ec42aa5006921b4c6d11fa0843ec4df6a7ff Mon Sep 17 00:00:00 2001 From: toravest Date: Sun, 25 May 2025 19:18:38 +0200 Subject: [PATCH] add UTC+1 (cet), and function to check cet vs cest 2024/2025 --- src/my_package/date_to_unix.py | 34 +++++++++++++++++++++++++--------- src/my_package/util.py | 2 ++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/my_package/date_to_unix.py b/src/my_package/date_to_unix.py index 78c631d..3f392f0 100644 --- a/src/my_package/date_to_unix.py +++ b/src/my_package/date_to_unix.py @@ -2,7 +2,7 @@ import datetime import time from my_package.util import CEST_OFFSET - +from my_package.util import CET_OFFSET def get_unix_timestamp(): ''' @@ -49,11 +49,16 @@ def get_unix_timestamp(): # Converts dates to timestamp used to convert to unix, and print, with the user input start_date_timestamp = datetime.datetime(start_year, start_month, start_date, start_hour, start_minute) end_date_timestamp = datetime.datetime(end_year, end_month, end_date, end_hour, end_minute) - - unix_start = int(time.mktime(start_date_timestamp.timetuple())) + CEST_OFFSET - unix_end = int(time.mktime(end_date_timestamp.timetuple())) + CEST_OFFSET - - # return the unix_start and end timestamp + + if (datetime.datetime(2024, 10, 27) <= start_date_timestamp <= datetime.datetime(2024, 12, 31)) or (datetime.datetime(2025, 1, 1) <= start_date_timestamp <= datetime.datetime(2025, 3, 30)): + unix_start = int(time.mktime(start_date_timestamp.timetuple())) + CET_OFFSET + unix_end = int(time.mktime(end_date_timestamp.timetuple())) + CET_OFFSET + else: + unix_start = int(time.mktime(start_date_timestamp.timetuple())) + CEST_OFFSET + unix_end = int(time.mktime(end_date_timestamp.timetuple())) + CEST_OFFSET + + + # return the unix_start and end timestamp return unix_start, unix_end def from_unix_timestamp(unix_start, unix_end): @@ -62,8 +67,14 @@ def from_unix_timestamp(unix_start, unix_end): timestamp to the inserted: yyyy, mm, dd, hh, mm ''' - start_from_unix = datetime.datetime.fromtimestamp(unix_start - CEST_OFFSET) - end_from_unix = datetime.datetime.fromtimestamp(unix_end - CEST_OFFSET) + start_date = datetime.datetime.fromtimestamp(unix_start) + + if (datetime.datetime(2024, 10, 27) <= start_date <= datetime.datetime(2024, 12, 31)) or (datetime.datetime(2025, 1, 1) <= start_date <= datetime.datetime(2025, 3, 30)): + start_from_unix = datetime.datetime.fromtimestamp(unix_start - CET_OFFSET) + end_from_unix = datetime.datetime.fromtimestamp(unix_end - CET_OFFSET) + else: + start_from_unix = datetime.datetime.fromtimestamp(unix_start - CEST_OFFSET) + end_from_unix = datetime.datetime.fromtimestamp(unix_end - CEST_OFFSET) return start_from_unix, end_from_unix @@ -86,7 +97,12 @@ def get_unix_timestamps_for_day(): timestamps = [] for hour in range(24): dt = datetime.datetime(year, month, day, hour, 0) - unix_timestamp = int(time.mktime(dt.timetuple())) + CEST_OFFSET + # Check if dt is in CET period or not + if (datetime.datetime(2024, 10, 27) <= dt <= datetime.datetime(2024, 12, 31)) or (datetime.datetime(2025, 1, 1) <= dt <= datetime.datetime(2025, 3, 30)): + unix_timestamp = int(time.mktime(dt.timetuple())) + CET_OFFSET + else: + unix_timestamp = int(time.mktime(dt.timetuple())) + CEST_OFFSET + timestamps.append((unix_timestamp, dt.strftime('%Y-%m-%d %H:%M:%S'))) # Prevents from getting data for the current day, or the future diff --git a/src/my_package/util.py b/src/my_package/util.py index 0f6294e..ea83dc5 100644 --- a/src/my_package/util.py +++ b/src/my_package/util.py @@ -3,6 +3,8 @@ # UTC+2, Central European Summer Time CEST_OFFSET = 2 * 3600 # in seconds +# UTC+1, Central European Time +CET_OFFSET = 1 * 3600 # in seconds # Function to replace the norcid 'æøå' def replace_nordic(city_name):