From 21e184f373ed266c2eecd1f11c371186d3955634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Carodej?= Date: Sun, 6 Feb 2022 14:26:42 +0100 Subject: [PATCH] resolved timezones --- config/language-translations.php | 2 +- src/App/Users/Resources/UserResource.php | 4 ++-- .../Users/Resources/UserStorageResource.php | 4 ++-- src/App/Users/Resources/UserSubscription.php | 6 ++--- .../Dashboard/GetDashboardDataController.php | 4 ++-- src/Domain/Files/Resources/FileResource.php | 14 +++++------- .../Folders/Resources/FolderResource.php | 14 +++++------- .../Resources/TransactionResource.php | 2 +- src/Support/helpers.php | 22 ++++++------------- tests/App/Users/UserAccountTest.php | 19 ++++++++-------- 10 files changed, 37 insertions(+), 54 deletions(-) diff --git a/config/language-translations.php b/config/language-translations.php index 198b78d7..60b271fb 100644 --- a/config/language-translations.php +++ b/config/language-translations.php @@ -667,7 +667,7 @@ return [ 'app_description' => 'Your self-hosted storage cloud software powered by Laravel and Vue', 'user_not_fount' => 'We can\'t find a user with that e-mail address.', 'incorrect_password' => 'Sorry, your password is incorrect.', - 'time' => '%d. %B. %Y at %H:%M', + 'time' => 'd. M. Y, h:i', 'home' => 'Home', 'shared_link_email_subject' => '🙋 :user share some files with you. Look at it!', 'shared_link_email_greeting' => 'Hello!', diff --git a/src/App/Users/Resources/UserResource.php b/src/App/Users/Resources/UserResource.php index beea3700..65ff453f 100644 --- a/src/App/Users/Resources/UserResource.php +++ b/src/App/Users/Resources/UserResource.php @@ -37,8 +37,8 @@ class UserResource extends JsonResource 'two_factor_authentication' => $this->two_factor_secret ? true : false, 'socialite_account' => $this->password ? false : true, 'storage' => $this->storage, - 'created_at' => format_date($this->created_at, '%d. %b. %Y'), - 'updated_at' => format_date($this->updated_at, '%d. %B. %Y'), + 'created_at' => format_date($this->created_at, 'd. M. Y'), + 'updated_at' => format_date($this->updated_at, 'd. M. Y'), ], 'relationships' => [ 'settings' => new SettingsResource($this->settings), diff --git a/src/App/Users/Resources/UserStorageResource.php b/src/App/Users/Resources/UserStorageResource.php index 135d2656..de2886f0 100644 --- a/src/App/Users/Resources/UserStorageResource.php +++ b/src/App/Users/Resources/UserStorageResource.php @@ -133,13 +133,13 @@ class UserStorageResource extends JsonResource ->get(); $upload = $trafficRecords->map(fn ($record) => [ - 'created_at' => format_date($record->created_at, '%d. %B'), + 'created_at' => format_date($record->created_at, 'd. M. '), 'percentage' => $uploadMax !== 0 ? round(($record->upload / $uploadMax) * 100, 2) : 0, 'amount' => Metric::bytes($record->upload)->format(), ]); $download = $trafficRecords->map(fn ($record) => [ - 'created_at' => format_date($record->created_at, '%d. %B'), + 'created_at' => format_date($record->created_at, 'd. M. '), 'percentage' => $downloadMax !== 0 ? round(($record->download / $downloadMax) * 100, 2) : 0, 'amount' => Metric::bytes($record->download)->format(), ]); diff --git a/src/App/Users/Resources/UserSubscription.php b/src/App/Users/Resources/UserSubscription.php index 759ba4d5..81cbe05e 100644 --- a/src/App/Users/Resources/UserSubscription.php +++ b/src/App/Users/Resources/UserSubscription.php @@ -32,9 +32,9 @@ class UserSubscription extends JsonResource 'capacity' => (int) $subscription['product']['metadata']['capacity'], 'capacity_formatted' => format_gigabytes($subscription['product']['metadata']['capacity']), 'slug' => $subscription['plan']['id'], - 'canceled_at' => format_date($active_subscription['canceled_at'], '%d. %B. %Y'), - 'created_at' => format_date($active_subscription['current_period_start'], '%d. %B. %Y'), - 'ends_at' => format_date($active_subscription['current_period_end'], '%d. %B. %Y'), + 'canceled_at' => format_date($active_subscription['canceled_at'], 'd. M. Y'), + 'created_at' => format_date($active_subscription['current_period_start'], 'd. M. Y'), + 'ends_at' => format_date($active_subscription['current_period_end'], 'd. M. Y'), ], ], ]; diff --git a/src/Domain/Admin/Controllers/Dashboard/GetDashboardDataController.php b/src/Domain/Admin/Controllers/Dashboard/GetDashboardDataController.php index 94a97360..76705996 100644 --- a/src/Domain/Admin/Controllers/Dashboard/GetDashboardDataController.php +++ b/src/Domain/Admin/Controllers/Dashboard/GetDashboardDataController.php @@ -66,13 +66,13 @@ class GetDashboardDataController extends Controller ->get(); $upload = $trafficRecords->map(fn ($record) => [ - 'created_at' => format_date($record->created_at, '%d. %B'), + 'created_at' => format_date($record->created_at, 'd. M. '), 'percentage' => intval($trafficRecords->max('upload')) !== 0 ? round(($record->upload / $trafficRecords->max('upload')) * 100, 2) : 0, 'amount' => Metric::bytes($record->upload)->format(), ]); $download = $trafficRecords->map(fn ($record) => [ - 'created_at' => format_date($record->created_at, '%d. %B'), + 'created_at' => format_date($record->created_at, 'd. M. '), 'percentage' => intval($trafficRecords->max('download')) !== 0 ? round(($record->download / $trafficRecords->max('download')) * 100, 2) : 0, 'amount' => Metric::bytes($record->download)->format(), ]); diff --git a/src/Domain/Files/Resources/FileResource.php b/src/Domain/Files/Resources/FileResource.php index c89a66c3..9961909b 100644 --- a/src/Domain/Files/Resources/FileResource.php +++ b/src/Domain/Files/Resources/FileResource.php @@ -33,15 +33,11 @@ class FileResource extends JsonResource 'thumbnail' => $this->thumbnail, 'metadata' => $this->metadata, 'parent_id' => $this->parent_id, - 'updated_at' => $this->updated_at, - 'created_at' => Carbon::parse($this->created_at)->diffForHumans(), - 'deleted_at' => $this->deleted_at ? Carbon::parse($this->deleted_at)->diffForHumans() : null, - /*'updated_at' => format_date( - set_time_by_user_timezone($this->updated_at), __t('time') - ), - 'created_at' => format_date( - set_time_by_user_timezone($this->created_at), __t('time') - ),*/ + 'created_at' => set_time_by_user_timezone($this->owner, $this->created_at), + 'updated_at' => set_time_by_user_timezone($this->owner, $this->updated_at), + 'deleted_at' => $this->deleted_at + ? set_time_by_user_timezone($this->owner, $this->deleted_at) + : null, ], 'relationships' => [ $this->mergeWhen($this->shared, fn () => [ diff --git a/src/Domain/Folders/Resources/FolderResource.php b/src/Domain/Folders/Resources/FolderResource.php index f7bacc90..9019c269 100644 --- a/src/Domain/Folders/Resources/FolderResource.php +++ b/src/Domain/Folders/Resources/FolderResource.php @@ -24,15 +24,11 @@ class FolderResource extends JsonResource 'isTeamFolder' => $this->team_folder, 'items' => $this->items, 'trashed_items' => $this->trashed_items, - 'updated_at' => $this->updated_at, - 'created_at' => Carbon::parse($this->created_at)->diffForHumans(), - 'deleted_at' => $this->deleted_at ? Carbon::parse($this->deleted_at)->diffForHumans() : null, - /*'updated_at' => format_date( - set_time_by_user_timezone($this->updated_at), __t('time') - ), - 'created_at' => format_date( - set_time_by_user_timezone($this->created_at), __t('time') - ),*/ + 'created_at' => set_time_by_user_timezone($this->owner, $this->created_at), + 'updated_at' => set_time_by_user_timezone($this->owner, $this->updated_at), + 'deleted_at' => $this->deleted_at + ? set_time_by_user_timezone($this->owner, $this->deleted_at) + : null, ], 'relationships' => [ $this->mergeWhen($this->teamMembers, fn () => [ diff --git a/src/Domain/Transactions/Resources/TransactionResource.php b/src/Domain/Transactions/Resources/TransactionResource.php index 70c14360..4674ccb4 100644 --- a/src/Domain/Transactions/Resources/TransactionResource.php +++ b/src/Domain/Transactions/Resources/TransactionResource.php @@ -30,7 +30,7 @@ class TransactionResource extends JsonResource 'metadata' => $this->metadata ? resolve(FormatUsageEstimatesAction::class)($this->currency, $this->metadata) : null, - 'created_at' => $this->created_at->formatLocalized('%d. %b. %Y'), + 'created_at' => $this->created_at->formatLocalized('d. M. Y'), 'updated_at' => $this->updated_at, ], 'relationships' => [ diff --git a/src/Support/helpers.php b/src/Support/helpers.php index 18f219dd..e6e01e5a 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -580,16 +580,12 @@ if (! function_exists('filter_folders_ids')) { if (! function_exists('format_date')) { /** * Format localized date - * - * @param $date - * @param string $format - * @return string */ - function format_date($date, $format = '%d. %B. %Y, %H:%M') + function format_date($date, string $format = 'd. M. Y, h:i'): string { $start = Carbon::parse($date); - return $start->formatLocalized($format); + return $start->translatedFormat($format); } } @@ -949,17 +945,13 @@ if (! function_exists('set_time_by_user_timezone')) { /** * Set time by user timezone GMT */ - function set_time_by_user_timezone(string $time): string | Carbon + function set_time_by_user_timezone($user, $createdAt): string | Carbon { - $user = Auth::user(); + $timezone = $user->settings->timezone ?? 0; - if ($user) { - $time_zone = $user->settings->timezone ?? 0; - - return Carbon::parse($time)->addMinutes($time_zone * 60); - } - - return $time; + return Carbon::parse($createdAt) + ->addMinutes($timezone * 60) + ->translatedFormat('d. M. Y, h:i'); } } diff --git a/tests/App/Users/UserAccountTest.php b/tests/App/Users/UserAccountTest.php index 39a1f5c6..31bdecb0 100644 --- a/tests/App/Users/UserAccountTest.php +++ b/tests/App/Users/UserAccountTest.php @@ -35,28 +35,27 @@ class UserAccountTest extends TestCase } /** - * todo: finish test + * @test */ public function it_test_user_timezone() { $user = User::factory() - ->create(['role' => 'user']); + ->hasSettings([ + 'timezone' => '2.0', + ]) + ->create(); - Folder::factory(Folder::class) + Folder::factory() ->create([ 'user_id' => $user->id, 'created_at' => now(), ]); - $user->settings()->update([ - 'timezone' => '2.0', - ]); - $this ->actingAs($user) ->getJson('/api/browse/folders/undefined') ->assertJsonFragment([ - 'created_at' => '01. January. 2021 at 02:00', + 'created_at' => '01. Jan. 2021, 02:00', ]); } @@ -167,8 +166,8 @@ class UserAccountTest extends TestCase 'capacity' => '1', 'capacity_formatted' => '1GB', ], - 'created_at' => format_date($user->created_at, '%d. %b. %Y'), - 'updated_at' => format_date($user->updated_at, '%d. %B. %Y'), + 'created_at' => format_date($user->created_at, 'd. M. Y'), + 'updated_at' => format_date($user->updated_at, 'd. M. Y'), ], 'meta' => [ 'restrictions' => [