Skip to content

Commit 56a295b

Browse files
authored
Display date and time using user's current timezone (#1624)
* Display date and time using user's current timezone #142 Signed-off-by: tdruez <tdruez@nexb.com> * Do not use relative date in the UI #990 #1503 Signed-off-by: tdruez <tdruez@nexb.com> --------- Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 24ce474 commit 56a295b

File tree

9 files changed

+108
-7
lines changed

9 files changed

+108
-7
lines changed

scancodeio/formats/__init__.py

Whitespace-only changes.

scancodeio/formats/en/__init__.py

Whitespace-only changes.

scancodeio/formats/en/formats.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# http://nexb.com and https://github.com/aboutcode-org/scancode.io
4+
# The ScanCode.io software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode.io is provided as-is without warranties.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
16+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
17+
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
18+
# for any legal advice.
19+
#
20+
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
22+
23+
DATE_FORMAT = "M d, Y"
24+
DATETIME_FORMAT = "Y-m-d H:i"

scancodeio/middleware.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# http://nexb.com and https://github.com/aboutcode-org/scancode.io
4+
# The ScanCode.io software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode.io is provided as-is without warranties.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
16+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
17+
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
18+
# for any legal advice.
19+
#
20+
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
22+
23+
import zoneinfo
24+
25+
from django.utils import timezone
26+
27+
28+
def validate_timezone(tz):
29+
"""Return a valid timezone or None if invalid."""
30+
try:
31+
return zoneinfo.ZoneInfo(tz).key
32+
except (zoneinfo.ZoneInfoNotFoundError, TypeError, ValueError):
33+
return
34+
35+
36+
class TimezoneMiddleware:
37+
def __init__(self, get_response):
38+
self.get_response = get_response
39+
40+
def __call__(self, request):
41+
tz = validate_timezone(request.COOKIES.get("client_timezone"))
42+
43+
if tz:
44+
timezone.activate(zoneinfo.ZoneInfo(tz))
45+
else:
46+
timezone.deactivate()
47+
48+
return self.get_response(request)

scancodeio/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
"django.contrib.auth.middleware.AuthenticationMiddleware",
208208
"django.contrib.messages.middleware.MessageMiddleware",
209209
"django.middleware.clickjacking.XFrameOptionsMiddleware",
210+
"scancodeio.middleware.TimezoneMiddleware",
210211
]
211212

212213
ROOT_URLCONF = "scancodeio.urls"
@@ -348,6 +349,8 @@
348349

349350
LANGUAGE_CODE = "en-us"
350351

352+
FORMAT_MODULE_PATH = ["scancodeio.formats"]
353+
351354
TIME_ZONE = env.str("TIME_ZONE", default="UTC")
352355

353356
USE_I18N = True

scancodeio/static/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ document.addEventListener('DOMContentLoaded', function () {
404404
});
405405
}
406406

407+
// Timezone
408+
// Detects the user's current timezone using the browser's API
409+
// and stores it in a cookie to be used by the backend for localization.
410+
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
411+
if (timezone) {
412+
document.cookie = `client_timezone=${timezone}; path=/; SameSite=Lax`;
413+
}
414+
407415
});
408416

409417
// Toasts (requires bulma-toast.js)

scanpipe/templates/scanpipe/includes/project_list_table.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load humanize %}
2+
{% now "Y-m-d" as today %}
23
<table class="table is-bordered is-striped is-hoverable is-fullwidth">
34
{% include 'scanpipe/includes/list_view_thead.html' with select_all=True %}
45
<tbody>
@@ -12,7 +13,17 @@
1213
<div>
1314
{% include 'scanpipe/includes/project_labels.html' with labels=project.labels.all only %}
1415
</div>
15-
<span class="has-text-grey is-size-7 is-block" title="{{ project.created_date|date:'N j, Y, P T' }}">Created {{ project.created_date|naturaltime }}</span>
16+
<span class="has-text-grey is-size-7 is-block">
17+
{% if project.created_date|date:"Y-m-d" == today %}
18+
<span title="{{ project.created_date }}">
19+
Created {{ project.created_date|naturaltime }}
20+
</span>
21+
{% else %}
22+
<span title="{{ project.created_date|naturaltime }}">
23+
Created {{ project.created_date }}
24+
</span>
25+
{% endif %}
26+
</span>
1627
</th>
1728
<td>
1829
{% if project.discoveredpackages_count %}

scanpipe/templates/scanpipe/modals/run_modal_content.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,22 @@
105105
<div class="control">
106106
<div class="tags has-addons">
107107
<span class="tag is-dark">Created date</span>
108-
<span class="tag is-info">{{ run.created_date|date:'N j, Y, P T' }}</span>
108+
<span class="tag is-info">{{ run.created_date }}</span>
109109
</div>
110110
</div>
111111
{% if run.task_start_date %}
112112
<div class="control">
113113
<div class="tags has-addons">
114114
<span class="tag is-dark">Start date</span>
115-
<span class="tag is-info">{{ run.task_start_date|date:'N j, Y, P T' }}</span>
115+
<span class="tag is-info">{{ run.task_start_date }}</span>
116116
</div>
117117
</div>
118118
{% endif %}
119119
{% if run.task_end_date %}
120120
<div class="control">
121121
<div class="tags has-addons">
122122
<span class="tag is-dark">End date</span>
123-
<span class="tag is-info">{{ run.task_end_date|date:'N j, Y, P T' }}</span>
123+
<span class="tag is-info">{{ run.task_end_date }}</span>
124124
</div>
125125
</div>
126126
{% endif %}

scanpipe/templates/scanpipe/project_detail.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@
2626
</nav>
2727
<div class="subtitle is-size-6">
2828
<span class="has-text-grey">
29-
<span title="{{ project.created_date|date:'N j, Y, P T' }}">
30-
Created {{ project.created_date|naturaltime }}
31-
</span>
29+
{% now "Y-m-d" as today %}
30+
{% if project.created_date|date:"Y-m-d" == today %}
31+
<span title="{{ project.created_date }}">
32+
Created {{ project.created_date|naturaltime }}
33+
</span>
34+
{% else %}
35+
<span title="{{ project.created_date|naturaltime }}">
36+
Created {{ project.created_date }}
37+
</span>
38+
{% endif %}
3239
</span>
3340
{% include 'scanpipe/includes/project_labels.html' with labels=labels only %}
3441
<a class="modal-button is-size-7" data-target="add-labels-modal" aria-haspopup="true">

0 commit comments

Comments
 (0)