diff --git a/notebooks/notebook_current_data.ipynb b/notebooks/notebook_current_data.ipynb index 0352dca..0d1a0fb 100644 --- a/notebooks/notebook_current_data.ipynb +++ b/notebooks/notebook_current_data.ipynb @@ -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", @@ -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", diff --git a/src/my_package/date_to_unix.py b/src/my_package/date_to_unix.py index 9677d40..78c631d 100644 --- a/src/my_package/date_to_unix.py +++ b/src/my_package/date_to_unix.py @@ -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. @@ -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 @@ -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 @@ -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 diff --git a/src/my_package/util.py b/src/my_package/util.py index 93ac923..0f6294e 100644 --- a/src/my_package/util.py +++ b/src/my_package/util.py @@ -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): '''