From 27365d809837e38d13e23c577b5b109533cec86e Mon Sep 17 00:00:00 2001 From: Dimitris Papagiannis Date: Mon, 4 Sep 2023 22:40:36 +0200 Subject: [PATCH] Time is relative --- db.py | 19 ++++++++++++++----- dqmsquare_cfg.py | 3 +++ requirements.txt | 3 ++- tests/test_1.py | 6 +++--- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/db.py b/db.py index 4d4cb41..5bc5386 100644 --- a/db.py +++ b/db.py @@ -1,4 +1,6 @@ ### DQM^2 Mirror DB === > +import os +import sys import psycopg2 import sqlalchemy from sqlalchemy_utils import database_exists @@ -7,6 +9,10 @@ from datetime import datetime from exceptions import DatabaseNotFoundError +sys.path.append(os.path.join(os.path.dirname(__file__), ".")) + +from dqmsquare_cfg import TZ + class DQM2MirrorDB: """ @@ -165,10 +171,12 @@ def fill_graph(self, header: dict, document: dict) -> int: rev = header.get("_rev", -1) timestamp = extra.get( - "timestamp", datetime(2012, 3, 3, 10, 10, 10, 0).timestamp() + "timestamp", + TZ.localize(datetime(2012, 3, 3, 10, 10, 10, 0), is_dst=None).timestamp(), ) global_start = extra.get( - "global_start", datetime(2012, 3, 3, 10, 10, 10, 0).timestamp() + "global_start", + TZ.localize(datetime(2012, 3, 3, 10, 10, 10, 0), is_dst=None).timestamp(), ) stream_data = str(extra.get("streams", "")) @@ -224,8 +232,8 @@ def get_graphs_data(self, run) -> list: answer = list(answer[0]) if answer[-2]: answer[-2] = eval(answer[-2]) # TODO: Not secure!!!!!! - answer[3] = answer[3].timestamp() - answer[4] = answer[4].timestamp() + answer[3] = TZ.localize(answer[3], is_dst=None).timestamp() + answer[4] = TZ.localize(answer[4], is_dst=None).timestamp() return answer @@ -253,7 +261,8 @@ def fill(self, header: dict, document: dict) -> int: pass fi_state = document.get("fi_state", "") timestamp = header.get( - "timestamp", datetime(2012, 3, 3, 10, 10, 10, 0).timestamp() + "timestamp", + TZ.localize(datetime(2012, 3, 3, 10, 10, 10, 0), is_dst=None).timestamp(), ) try: timestamp = datetime.fromtimestamp(timestamp) diff --git a/dqmsquare_cfg.py b/dqmsquare_cfg.py index be3c5b5..400a70a 100644 --- a/dqmsquare_cfg.py +++ b/dqmsquare_cfg.py @@ -6,9 +6,12 @@ """ import os +import pytz import tempfile from dotenv import load_dotenv +TZ = pytz.timezone("Europe/Zurich") + def format_db_uri( username: str = "postgres", diff --git a/requirements.txt b/requirements.txt index 5069bcd..e1b951b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ sqlalchemy-utils gunicorn<21.0.0 psycopg2-binary werkzeug -python-dotenv<2.0.0 \ No newline at end of file +python-dotenv<2.0.0 +pytz \ No newline at end of file diff --git a/tests/test_1.py b/tests/test_1.py index 8436331..c7d3311 100644 --- a/tests/test_1.py +++ b/tests/test_1.py @@ -12,7 +12,7 @@ from sqlalchemy import create_engine, insert from sqlalchemy_utils import create_database, database_exists, drop_database from custom_logger import dummy_log -from dqmsquare_cfg import format_db_uri, load_cfg +from dqmsquare_cfg import format_db_uri, TZ def format_entry_to_db_entry(graph_entry: list, datetime_cols: list): @@ -236,8 +236,8 @@ def test_db_10(testing_database): 123456, -1, "id", - datetime(2012, 3, 3, 10, 10, 10).timestamp(), - datetime(2012, 3, 3, 10, 10, 10).timestamp(), + TZ.localize(datetime(2012, 3, 3, 10, 10, 10), is_dst=None).timestamp(), + TZ.localize(datetime(2012, 3, 3, 10, 10, 10), is_dst=None).timestamp(), "", "", ]