Set Anonymous timezone to match site default

Created on 14 August 2024, 5 months ago
Updated 15 August 2024, 5 months ago

In Drupal timezone for anonymous users appears to be determined by the server time.

Also see https://www.drupal.org/forum/support/installing-drupal/2003-07-26/how-to... β†’

It is worth mentioning that Timezone can be set by each user (including admin) and also the default timezone can be set from the sites local and regional settings /admin/config/regional/settings

Sometimes the server may be in a different timezone. e.g if server timezone is 1 hour ahead, then the date shown to anon users will be 1 hour behind.

✨ Feature request
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom 2dareis2do

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @2dareis2do
  • πŸ‡¬πŸ‡§United Kingdom 2dareis2do
  • πŸ‡¬πŸ‡§United Kingdom 2dareis2do
  • πŸ‡¬πŸ‡§United Kingdom 2dareis2do

    Just looking at this and from what I can see the behaviour seems to be related to the use of date_default_timezone_set() and date_default_timezone_get()

    date_default_timezone_get β€” Gets the default timezone used by all date/time functions in a script

    Description ΒΆ

    date_default_timezone_get(): string
    In order of preference, this function returns the default timezone by:

    Reading the timezone set using the date_default_timezone_set() function (if any)

    Reading the value of the date.timezone ini option (if set)

    If none of the above succeed, date_default_timezone_get() will return a default timezone of UTC.

    https://www.php.net/manual/en/function.date-default-timezone-get.php

    date_default_timezone_set() is called correctly for logged in users. In fact when tested this it appears to be called 4 times on a single page with a single node. (submitted on), compared to just once for an anonymous user! ?

    Looking at php.info at has a value called date.timezone and this seems to be the same value that is being returned for anonymous user, despite date_default_timezone_set being called in an almost identical fashion to logged in users.

    Now when I step through the code as an anonymous user, I can see that date_default_timezone_set() is called in
    web/core/modules/system/src/TimeZoneResolver.php

    Call stack:

    Drupal\system\TimeZoneResolver->setDefaultTimeZone (/web/core/modules/system/src/TimeZoneResolver.php:53)
    call_user_func:{/var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111} (/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch (/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw (/vendor/symfony/http-kernel/HttpKernel.php:157)
    Symfony\Component\HttpKernel\HttpKernel->handle (/vendor/symfony/http-kernel/HttpKernel.php:76)
    Drupal\Core\StackMiddleware\Session->handle (/web/core/lib/Drupal/Core/StackMiddleware/Session.php:58)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle (/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php:48)
    Drupal\Core\StackMiddleware\ContentLength->handle (/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php:28)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle (/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php:32)
    Drupal\page_cache\StackMiddleware\PageCache->fetch (/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:191)
    Drupal\page_cache\StackMiddleware\PageCache->lookup (/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:128)
    Drupal\page_cache\StackMiddleware\PageCache->handle (/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:82)
    Drupal\ban\BanMiddleware->handle (/web/core/modules/ban/src/BanMiddleware.php:50)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle (/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php:48)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle (/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php:51)
    Drupal\Core\StackMiddleware\AjaxPageState->handle (/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php:36)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle (/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php:51)
    Drupal\Core\DrupalKernel->handle (/web/core/lib/Drupal/Core/DrupalKernel.php:704)
    {main} (/web/index.php:19)

    However, When I am logged in setDefaultTimeZone is called 4 times. Here is the call stack the last time it is called

    Drupal\system\TimeZoneResolver->setDefaultTimeZone (/web/core/modules/system/src/TimeZoneResolver.php:53)
    call_user_func:{/var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111} (/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch (/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw (/vendor/symfony/http-kernel/HttpKernel.php:157)
    Symfony\Component\HttpKernel\HttpKernel->handle (/vendor/symfony/http-kernel/HttpKernel.php:76)
    Drupal\Core\StackMiddleware\Session->handle (/web/core/lib/Drupal/Core/StackMiddleware/Session.php:58)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle (/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php:48)
    Drupal\Core\StackMiddleware\ContentLength->handle (/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php:28)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle (/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php:32)
    Drupal\page_cache\StackMiddleware\PageCache->pass (/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:106)
    Drupal\page_cache\StackMiddleware\PageCache->handle (/web/core/modules/page_cache/src/StackMiddleware/PageCache.php:85)
    Drupal\ban\BanMiddleware->handle (/web/core/modules/ban/src/BanMiddleware.php:50)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle (/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php:48)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle (/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php:51)
    Drupal\Core\StackMiddleware\AjaxPageState->handle (/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php:36)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle (/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php:51)
    Drupal\Core\DrupalKernel->handle (/web/core/lib/Drupal/Core/DrupalKernel.php:704)
    {main} (/web/index.php:19)

    Also, I did see this clue in web/core/lib/Drupal/Core/Cache/Context/TimeZoneCacheContext.php

      /**
       * {@inheritdoc}
       */
      public function getContext() {
        // date_default_timezone_set() is called in AccountProxy::setAccount(), so
        // we can safely retrieve the timezone.
        return date_default_timezone_get();
      }
  • πŸ‡¬πŸ‡§United Kingdom 2dareis2do

    OK events for logged in user are

    account.set
    kernel.request

    account.set
    kernel.request

    for anonymous user it is just kernel.request

  • Status changed to Closed: works as designed 5 months ago
  • πŸ‡¬πŸ‡§United Kingdom 2dareis2do

    Ok this is a bit embarrassing.

    I have looked at this again and it seems to be working as expected now. 😊

    Updated time for anonymous user seems to be the same as that for logged in user both with user timezone and or site timezone set to local under local and regional settings/config.

    Thanks.

Production build 0.71.5 2024