Skip to content

Commit

Permalink
change from utc to cest(utc+2)
Browse files Browse the repository at this point in the history
  • Loading branch information
toravest committed May 24, 2025
1 parent a8ccaea commit 21c31ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
15 changes: 11 additions & 4 deletions notebooks/notebook_current_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@
"outputs": [],
"source": [
"import pandas as pd\n",
"import sys\n",
"import os\n",
"\n",
"# Gets the absolute path to the src folder\n",
"sys.path.append(os.path.abspath(\"../src\"))\n",
"from my_package.util import CEST_OFFSET\n",
"\n",
"# Normalize the json-structure, to add better readability\n",
"df = pd.json_normalize(data)\n",
Expand All @@ -149,12 +155,13 @@
"df = df.drop(columns=\"sys.type\")\n",
"df = df.drop(columns=\"sys.id\")\n",
"\n",
"# Convert 'dt' column from Unix timestamp to datetime and set it as the index\n",
"df['dt'] = pd.to_datetime(df['dt'] + CEST_OFFSET, unit='s')\n",
"\n",
"# Change from unix to datetime for sunrise and sunset\n",
"df['sys.sunrise'] = pd.to_datetime(df['sys.sunrise'], unit='s')\n",
"df['sys.sunset'] = pd.to_datetime(df['sys.sunset'], unit='s')\n",
"df['sys.sunrise'] = pd.to_datetime(df['sys.sunrise'] + CEST_OFFSET, unit='s')\n",
"df['sys.sunset'] = pd.to_datetime(df['sys.sunset'] + CEST_OFFSET, unit='s')\n",
"\n",
"# Convert 'dt' column from Unix timestamp to datetime and set it as the index\n",
"df['dt'] = pd.to_datetime(df['dt'], unit='s')\n",
"df.set_index('dt', inplace=True)\n",
"\n",
"# Drops the whole column, if all values is 'NaN' value.\n",
Expand Down
14 changes: 8 additions & 6 deletions src/my_package/date_to_unix.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# importing datetime module
import datetime
import time

from my_package.util import CEST_OFFSET


def get_unix_timestamp():
'''
This function will make the user input a start_date and end_date, then return a unix timestamp.
Expand Down Expand Up @@ -48,8 +50,8 @@ def get_unix_timestamp():
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()))
unix_end = int(time.mktime(end_date_timestamp.timetuple()))
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
Expand All @@ -60,8 +62,8 @@ 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)
end_from_unix = datetime.datetime.fromtimestamp(unix_end)
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 @@ -84,7 +86,7 @@ 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()))
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
3 changes: 3 additions & 0 deletions src/my_package/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
import pandas as pd

# UTC+2, Central European Summer Time
CEST_OFFSET = 2 * 3600 # in seconds

# Function to replace the norcid 'æøå'
def replace_nordic(city_name):
'''
Expand Down

0 comments on commit 21c31ad

Please sign in to comment.