Skip to content

Commit feccb18

Browse files
authored
UI - Always use UTC timezone for storing data, show local timezone (#2799)
1 parent 1462ad8 commit feccb18

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

changedetectionio/flask_app.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
from flask_cors import CORS
3939
from flask_wtf import CSRFProtect
4040
from loguru import logger
41+
from zoneinfo import ZoneInfo
42+
4143

4244
from changedetectionio import html_tools, __version__
4345
from changedetectionio import queuedWatchMetaData
@@ -159,21 +161,6 @@ def _jinja2_filter_pagination_slice(arr, skip):
159161

160162
return arr
161163

162-
def app_get_system_time():
163-
from zoneinfo import ZoneInfo # Built-in timezone support in Python 3.9+
164-
165-
system_timezone = datastore.data['settings']['application'].get('timezone')
166-
if not system_timezone:
167-
system_timezone = os.environ.get("TZ")
168-
169-
try:
170-
system_zone = ZoneInfo(system_timezone)
171-
except Exception as e:
172-
logger.warning(f'Warning, unable to use timezone "{system_timezone}" defaulting to UTC- {str(e)}')
173-
system_zone = ZoneInfo("UTC") # Fallback to UTC if the timezone is invalid
174-
175-
return system_zone
176-
177164
@app.template_filter('format_seconds_ago')
178165
def _jinja2_filter_seconds_precise(timestamp):
179166
if timestamp == False:
@@ -258,9 +245,6 @@ def changedetection_app(config=None, datastore_o=None):
258245
# (instead of the global var)
259246
app.config['DATASTORE'] = datastore_o
260247

261-
# Just to check (it will output some debug if not)
262-
app_get_system_time()
263-
264248
login_manager = flask_login.LoginManager(app)
265249
login_manager.login_view = 'login'
266250
app.secret_key = init_app_secret(config['datastore_path'])
@@ -968,12 +952,8 @@ def settings_page():
968952
else:
969953
flash("An error occurred, please see below.", "error")
970954

971-
972-
system_timezone = app_get_system_time()
973-
system_time = datetime.now(system_timezone)
974-
975-
# Fallback for locale formatting
976-
formatted_system_time = system_time.strftime("%Y-%m-%d %H:%M:%S %Z%z") # Locale-aware time
955+
# Convert to ISO 8601 format, all date/time relative events stored as UTC time
956+
utc_time = datetime.now(ZoneInfo("UTC")).isoformat()
977957

978958
output = render_template("settings.html",
979959
api_key=datastore.data['settings']['application'].get('api_access_token'),
@@ -983,8 +963,7 @@ def settings_page():
983963
hide_remove_pass=os.getenv("SALTED_PASS", False),
984964
min_system_recheck_seconds=int(os.getenv('MINIMUM_SECONDS_RECHECK_TIME', 3)),
985965
settings_application=datastore.data['settings']['application'],
986-
system_time=formatted_system_time,
987-
timezone_name=system_timezone
966+
utc_time=utc_time,
988967
)
989968

990969
return output

changedetectionio/static/js/global-settings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ $(document).ready(function () {
2424
$(target).toggle();
2525
});
2626

27+
$(".local-time").each(function (e) {
28+
$(this).text(new Date($(this).data("utc")).toLocaleString());
29+
})
2730
});
2831

changedetectionio/templates/settings.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
<span class="pure-form-message-inline">When a request returns no content, or the HTML does not contain any text, is this considered a change?</span>
8080
</div>
8181
<div class="pure-control-group">
82-
<p><strong>Local Time:</strong> {{ system_time }}</p>
83-
<p><strong>Timezone:</strong> {{ timezone_name }}</p>
82+
<p><strong>UTC Time from Server:</strong> <span id="utc-time" >{{ utc_time }}</span></p>
83+
<p><strong>Local Time in Browser:</strong> <span class="local-time" data-utc="{{ utc_time }}"></span></p>
8484
</div>
8585
{% if form.requests.proxy %}
8686
<div class="pure-control-group inline-radio">

0 commit comments

Comments
 (0)