UI - Always use UTC timezone for storing data, show local timezone (#2799)

pull/2809/head
dgtlmoon 1 month ago committed by GitHub
parent 1462ad89ac
commit feccb18cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -38,6 +38,8 @@ from flask_restful import abort, Api
from flask_cors import CORS from flask_cors import CORS
from flask_wtf import CSRFProtect from flask_wtf import CSRFProtect
from loguru import logger from loguru import logger
from zoneinfo import ZoneInfo
from changedetectionio import html_tools, __version__ from changedetectionio import html_tools, __version__
from changedetectionio import queuedWatchMetaData from changedetectionio import queuedWatchMetaData
@ -159,21 +161,6 @@ def _jinja2_filter_pagination_slice(arr, skip):
return arr return arr
def app_get_system_time():
from zoneinfo import ZoneInfo # Built-in timezone support in Python 3.9+
system_timezone = datastore.data['settings']['application'].get('timezone')
if not system_timezone:
system_timezone = os.environ.get("TZ")
try:
system_zone = ZoneInfo(system_timezone)
except Exception as e:
logger.warning(f'Warning, unable to use timezone "{system_timezone}" defaulting to UTC- {str(e)}')
system_zone = ZoneInfo("UTC") # Fallback to UTC if the timezone is invalid
return system_zone
@app.template_filter('format_seconds_ago') @app.template_filter('format_seconds_ago')
def _jinja2_filter_seconds_precise(timestamp): def _jinja2_filter_seconds_precise(timestamp):
if timestamp == False: if timestamp == False:
@ -258,9 +245,6 @@ def changedetection_app(config=None, datastore_o=None):
# (instead of the global var) # (instead of the global var)
app.config['DATASTORE'] = datastore_o app.config['DATASTORE'] = datastore_o
# Just to check (it will output some debug if not)
app_get_system_time()
login_manager = flask_login.LoginManager(app) login_manager = flask_login.LoginManager(app)
login_manager.login_view = 'login' login_manager.login_view = 'login'
app.secret_key = init_app_secret(config['datastore_path']) app.secret_key = init_app_secret(config['datastore_path'])
@ -968,12 +952,8 @@ def changedetection_app(config=None, datastore_o=None):
else: else:
flash("An error occurred, please see below.", "error") flash("An error occurred, please see below.", "error")
# Convert to ISO 8601 format, all date/time relative events stored as UTC time
system_timezone = app_get_system_time() utc_time = datetime.now(ZoneInfo("UTC")).isoformat()
system_time = datetime.now(system_timezone)
# Fallback for locale formatting
formatted_system_time = system_time.strftime("%Y-%m-%d %H:%M:%S %Z%z") # Locale-aware time
output = render_template("settings.html", output = render_template("settings.html",
api_key=datastore.data['settings']['application'].get('api_access_token'), api_key=datastore.data['settings']['application'].get('api_access_token'),
@ -983,8 +963,7 @@ def changedetection_app(config=None, datastore_o=None):
hide_remove_pass=os.getenv("SALTED_PASS", False), hide_remove_pass=os.getenv("SALTED_PASS", False),
min_system_recheck_seconds=int(os.getenv('MINIMUM_SECONDS_RECHECK_TIME', 3)), min_system_recheck_seconds=int(os.getenv('MINIMUM_SECONDS_RECHECK_TIME', 3)),
settings_application=datastore.data['settings']['application'], settings_application=datastore.data['settings']['application'],
system_time=formatted_system_time, utc_time=utc_time,
timezone_name=system_timezone
) )
return output return output

@ -24,5 +24,8 @@ $(document).ready(function () {
$(target).toggle(); $(target).toggle();
}); });
$(".local-time").each(function (e) {
$(this).text(new Date($(this).data("utc")).toLocaleString());
})
}); });

@ -79,8 +79,8 @@
<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> <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>
</div> </div>
<div class="pure-control-group"> <div class="pure-control-group">
<p><strong>Local Time:</strong> {{ system_time }}</p> <p><strong>UTC Time from Server:</strong> <span id="utc-time" >{{ utc_time }}</span></p>
<p><strong>Timezone:</strong> {{ timezone_name }}</p> <p><strong>Local Time in Browser:</strong> <span class="local-time" data-utc="{{ utc_time }}"></span></p>
</div> </div>
{% if form.requests.proxy %} {% if form.requests.proxy %}
<div class="pure-control-group inline-radio"> <div class="pure-control-group inline-radio">

Loading…
Cancel
Save