Skip to content

Commit

Permalink
Always use UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtlmoon committed Nov 20, 2024
1 parent 1462ad8 commit a926b98
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
31 changes: 5 additions & 26 deletions changedetectionio/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
from flask_cors import CORS
from flask_wtf import CSRFProtect
from loguru import logger
from zoneinfo import ZoneInfo


from changedetectionio import html_tools, __version__
from changedetectionio import queuedWatchMetaData
Expand Down Expand Up @@ -159,21 +161,6 @@ def _jinja2_filter_pagination_slice(arr, skip):

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')
def _jinja2_filter_seconds_precise(timestamp):
if timestamp == False:
Expand Down Expand Up @@ -258,9 +245,6 @@ def changedetection_app(config=None, datastore_o=None):
# (instead of the global var)
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.login_view = 'login'
app.secret_key = init_app_secret(config['datastore_path'])
Expand Down Expand Up @@ -968,12 +952,8 @@ def settings_page():
else:
flash("An error occurred, please see below.", "error")


system_timezone = app_get_system_time()
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
# Convert to ISO 8601 format, all date/time relative events stored as UTC time
utc_time = datetime.now(ZoneInfo("UTC")).isoformat()

output = render_template("settings.html",
api_key=datastore.data['settings']['application'].get('api_access_token'),
Expand All @@ -983,8 +963,7 @@ def settings_page():
hide_remove_pass=os.getenv("SALTED_PASS", False),
min_system_recheck_seconds=int(os.getenv('MINIMUM_SECONDS_RECHECK_TIME', 3)),
settings_application=datastore.data['settings']['application'],
system_time=formatted_system_time,
timezone_name=system_timezone
utc_time=utc_time,
)

return output
Expand Down
3 changes: 3 additions & 0 deletions changedetectionio/static/js/global-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ $(document).ready(function () {
$(target).toggle();
});

$(".local-time").each(function (e) {
$(this).text(new Date($(this).data("utc")).toLocaleString());
})
});

4 changes: 2 additions & 2 deletions changedetectionio/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
</div>
<div class="pure-control-group">
<p><strong>Local Time:</strong> {{ system_time }}</p>
<p><strong>Timezone:</strong> {{ timezone_name }}</p>
<p><strong>UTC Time from Server:</strong> <span id="utc-time" >{{ utc_time }}</span></p>
<p><strong>Local Time in Browser:</strong> <span class="local-time" data-utc="{{ utc_time }}"></span></p>
</div>
{% if form.requests.proxy %}
<div class="pure-control-group inline-radio">
Expand Down

0 comments on commit a926b98

Please sign in to comment.