summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVedran Karacic <karacicvedran@gmail.com>2025-12-12 10:04:19 +0100
committerJacob Walls <jacobtylerwalls@gmail.com>2026-04-29 11:28:13 -0400
commit61941b6fc172933f425e8ba76bab444ab9b313e4 (patch)
treea59cfb9faa571f6cc329ee8d0868578646169a2b
parent604695cddb41981b84a8d976d1f4c74c39e112b0 (diff)
Fixed #35951 -- Mentioned server timezone in admin DateTime widgets.
The existing note that is shown to the users when entering a time value from a different timezone than the server's timezone was not descriptive enough and led to confusion. This commit updates the note to explicitly state that the user should enter times in the server's timezone.
-rw-r--r--django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js20
-rw-r--r--django/contrib/admin/templates/admin/base.html5
-rw-r--r--js_tests/admin/DateTimeShortcuts.test.js18
3 files changed, 34 insertions, 9 deletions
diff --git a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
index 6f71b25152..27223e2815 100644
--- a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
+++ b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
@@ -82,22 +82,32 @@
return;
}
+ const serverTimezone =
+ document.body.dataset.adminServerTimezone || gettext("server");
let message;
if (timezoneOffset > 0) {
message = ngettext(
- "Note: You are %s hour ahead of server time.",
- "Note: You are %s hours ahead of server time.",
+ "Note: Enter times in the %(timezone)s timezone. " +
+ "(You are %(offset)s hour ahead.)",
+ "Note: Enter times in the %(timezone)s timezone. " +
+ "(You are %(offset)s hours ahead.)",
timezoneOffset,
);
} else {
timezoneOffset *= -1;
message = ngettext(
- "Note: You are %s hour behind server time.",
- "Note: You are %s hours behind server time.",
+ "Note: Enter times in the %(timezone)s timezone. " +
+ "(You are %(offset)s hour behind.)",
+ "Note: Enter times in the %(timezone)s timezone. " +
+ "(You are %(offset)s hours behind.)",
timezoneOffset,
);
}
- message = interpolate(message, [timezoneOffset]);
+ message = interpolate(
+ message,
+ { timezone: serverTimezone, offset: timezoneOffset },
+ true,
+ );
const warning = document.createElement("div");
const id = inp.id;
diff --git a/django/contrib/admin/templates/admin/base.html b/django/contrib/admin/templates/admin/base.html
index 066f5fa65c..8ef92802a9 100644
--- a/django/contrib/admin/templates/admin/base.html
+++ b/django/contrib/admin/templates/admin/base.html
@@ -1,4 +1,4 @@
-{% load i18n static %}<!DOCTYPE html>
+{% load i18n static tz %}<!DOCTYPE html>
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" dir="{{ LANGUAGE_BIDI|yesno:'rtl,ltr,auto' }}">
<head>
@@ -25,7 +25,8 @@
</head>
<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}"
- data-admin-utc-offset="{% now "Z" %}">
+ data-admin-utc-offset="{% now "Z" %}"
+ data-admin-server-timezone="{% get_current_timezone as time_zone %}{{ time_zone }}">
<a href="#content-start" class="skip-to-content-link">{% translate 'Skip to main content' %}</a>
<!-- Container -->
<div id="container">
diff --git a/js_tests/admin/DateTimeShortcuts.test.js b/js_tests/admin/DateTimeShortcuts.test.js
index 5c850ce7d2..28d07fb918 100644
--- a/js_tests/admin/DateTimeShortcuts.test.js
+++ b/js_tests/admin/DateTimeShortcuts.test.js
@@ -1,7 +1,15 @@
/* global QUnit, DateTimeShortcuts */
"use strict";
-QUnit.module("admin.DateTimeShortcuts");
+QUnit.module("admin.DateTimeShortcuts", {
+ afterEach: function () {
+ const $ = django.jQuery;
+ $("body")
+ .removeAttr("data-admin-server-timezone")
+ .removeAttr("data-admin-utc-offset");
+ $(".timezonewarning").remove();
+ },
+});
QUnit.test("init", function (assert) {
const $ = django.jQuery;
@@ -46,11 +54,13 @@ QUnit.test("time zone offset warning - single field", function (assert) {
"data-admin-utc-offset",
new Date().getTimezoneOffset() * -60 + 3600,
);
+ $("body").attr("data-admin-server-timezone", "America/Chicago");
DateTimeShortcuts.init();
$("body").attr("data-admin-utc-offset", savedOffset);
assert.equal(
$(".timezonewarning").text(),
- "Note: You are 1 hour behind server time.",
+ "Note: Enter times in the America/Chicago timezone. " +
+ "(You are 1 hour behind.)",
);
assert.equal(
$(".timezonewarning").attr("id"),
@@ -75,6 +85,10 @@ QUnit.test("time zone offset warning - date and time field", function (assert) {
DateTimeShortcuts.init();
$("body").attr("data-admin-utc-offset", savedOffset);
assert.equal(
+ $(".timezonewarning").text(),
+ "Note: Enter times in the server timezone. (You are 1 hour behind.)",
+ );
+ assert.equal(
$(".timezonewarning").attr("id"),
"id_updated_at_timezone_warning_helptext",
);