# Buggy code
def timesince_last_update(last_update, time_prefix="", use_shorthand=True):
    """
    Returns:
         - the time of update if last_update is today, if any prefix is supplied, the output will use it
         - time since last update othewise. Defaults to the simplified timesince,
           but can return the full string if needed
    """
    if last_update.date() == datetime.today().date():
        time_str = timezone.localtime(last_update).strftime("%H:%M")
        return (
            time_str
            if not time_prefix
            else "%(prefix)s %(formatted_time)s"
            % {"prefix": time_prefix, "formatted_time": time_str}
        )
    else:
        if use_shorthand:
            return timesince_simple(last_update)
        return _("%(time_period)s ago") % {"time_period": timesince(last_update)}


# Traceback (ValueError)
Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/pages/3/edit/

Django Version: 3.1
Python Version: 3.8.3
Installed Applications:
['home',
'search',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware']


Template error:
In template /Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/wagtail/admin/templates/wagtailadmin/shared/last_updated.html, error at line 4
localtime() cannot be applied to a naive datetime
1 : {% load i18n wagtailadmin_tags %}
2 : {% if last_updated %}
3 : <span title="{{ last_updated }}" data-wagtail-tooltip="{{ last_updated }}" {% if classname %}class="{{ classname }}"{% endif %}>
4 :     {% if since_text %}{{ since_text }}{% endif %}  {% timesince_last_update last_updated time_prefix=time_prefix %}
5 : </span>
6 : {% endif %}
7 :

Traceback (most recent call last):
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/core/handlers/base.py", line 202, in _get_response
response = response.render()
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 192, in render
return template.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 172, in render
return self._render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
return nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
return nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/loader_tags.py", line 192, in render
return template.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 172, in render
return self._render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
return nodelist.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/template/library.py", line 192, in render
output = self.func(*resolved_args, **resolved_kwargs)
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/wagtail/admin/templatetags/wagtailadmin_tags.py", line 587, in timesince_last_update
time_str = timezone.localtime(last_update).strftime("%H:%M")
File "/Users/stefan/.virtualenvs/test-wagtail/lib/python3.8/site-packages/django/utils/timezone.py", line 171, in localtime
raise ValueError("localtime() cannot be applied to a naive datetime")

Exception Type: ValueError at /admin/pages/3/edit/
Exception Value: localtime() cannot be applied to a naive datetime

# Fixed code
def timesince_last_update(last_update, time_prefix="", use_shorthand=True):
    """
    Returns:
         - the time of update if last_update is today, if any prefix is supplied, the output will use it
         - time since last update othewise. Defaults to the simplified timesince,
           but can return the full string if needed
    """
    if last_update.date() == datetime.today().date():
        if timezone.is_aware(last_update):
            time_str = timezone.localtime(last_update).strftime("%H:%M")
        else:
            time_str = last_update.strftime("%H:%M")

        return (
            time_str
            if not time_prefix
            else "%(prefix)s %(formatted_time)s"
            % {"prefix": time_prefix, "formatted_time": time_str}
        )
    else:
        if use_shorthand:
            return timesince_simple(last_update)
        return _("%(time_period)s ago") % {"time_period": timesince(last_update)}