laravel / nova-issues

557 stars 34 forks source link

Value Metrics doesn't respect users timezone function #5021

Closed jsmit99 closed 2 years ago

jsmit99 commented 2 years ago

Description:

It seems the Value metrics don't respect the users timezone (even though this is set in the serviceprovider). I'm trying to show the number of created users for Today and Yesterday.

This Value metric should show 2 new users for Today and 1 new user for Yesterday. But because of the mysql is in UTC the Value metric is showing 1 new user for Today and 2 new users for Yesterday.

So it seems to respect the mysql timezone (UTC) instead of the nova users timezone (Europe/Amsterdam) which is an 1 hour difference for this case.

For the example; the user with ID 2 was created at 2022-11-04 00:42:33 in the Europe/Amsterdam timezone.

Note: The index view of the resource is showing the corrected (Europe/Amsterdam TZ) created_at timestamps.

Detailed steps to reproduce the issue on a fresh Nova installation:

Create a fresh Nova installation and add a few users to the users table in the database. In the example below I'm showing the ID's with the timestamps. Mysql defaults to UTC timestamps, even though my server (Laravel Forge) timezone is set to Europe/Amsterdam.

id  created_at  updated_at
3   2022-11-04 02:46:21 2022-11-04 02:46:21
2   2022-11-04 01:42:33 2022-11-03 23:42:33
1   2022-11-03 14:22:27 2022-11-03 14:22:27

NovaServiceProvider.php (rest of file is default nova)


public function boot()
{
    parent::boot();

    Nova::userTimezone(function () {
        return 'Europe/Amsterdam';
    });
}

Nova/Metrics/NewUsers.php (rest of file is default by php artisan nova:metric NewUsers)


public function calculate(NovaRequest $request)
{
    return $this->count($request, User::class);
}

public function ranges()
{
    return [
        'TODAY' => __('Today'),
        'YESTERDAY' => __('Yesterday'),
    ];
}
crynobone commented 2 years ago

Unable to reproduce the issue, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

jsmit99 commented 2 years ago

It's actually quite simple, change the user timezone to something different from UTC in Nova and check value metrics which are saved with a different value (UTC value) in mysql.

crynobone commented 2 years ago

No, it is not, and we have multiple feature tests already covering these features (timezone and DST). The only way we going to able to replicate your issue is by replicating what exactly what you are seeing

davidhemphill commented 2 years ago

Closing this issue since it's become stale. Feel free to open a new issue if your problem persists, and you can provide details on how to reproduce this in a fresh Nova install, or link this issue in your description. Thanks!