Skip to content

Commit

Permalink
add UTC+1 (cet), and function to check cet vs cest 2024/2025
Browse files Browse the repository at this point in the history
  • Loading branch information
toravest committed May 25, 2025
1 parent ddf57f4 commit 2af0ec4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/my_package/date_to_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
'''
Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/my_package/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2af0ec4

Please sign in to comment.