From 9ed42b52eedb307e6115204ee0ea9453f81c65c2 Mon Sep 17 00:00:00 2001 From: Amandio Magalhaes Date: Tue, 19 May 2020 14:25:51 -0700 Subject: [PATCH 1/2] Returning Carbon instances when using Model Casting --- README.md | 37 +++++++++++++++++++++++------ src/Casts/Timezone.php | 45 ++++++++++++++++++++++++++++++++++++ src/Timezone.php | 20 ++++++++-------- src/Traits/TimezoneTrait.php | 14 +++++++++++ 4 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 src/Casts/Timezone.php create mode 100644 src/Traits/TimezoneTrait.php diff --git a/README.md b/README.md index 03b9416..3fa1d61 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ # Laravel Timezone -[![Latest Version on Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) -[![Total Downloads](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) -[![Licence](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) -[![Quality Score](https://img.shields.io/scrutinizer/g/jamesmills/laravel-timezone.svg?style=flat-square)](https://scrutinizer-ci.com/g/jamesmills/laravel-timezone) -[![StyleCI](https://github.styleci.io/repos/142882574/shield?branch=master)](https://github.styleci.io/repos/142882574) -[![Buy us a tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone) -[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone) +[![Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=for-the-badge)](https://packagist.org/packages/jamesmills/laravel-timezone) +![Packagist](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=for-the-badge) +![Packagist](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=for-the-badge) +[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone) +[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone) An easy way to set a timezone for a user in your application and then show date/times to them in their local timezone. @@ -90,6 +88,31 @@ And with custom formatting // 2018-07-04 3:32 New York, America ``` +### Using models casting class + +#### Basic usage + +``` + Timezone::class, + ]; +} +``` + ### Saving the users input to the database in UTC This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance. diff --git a/src/Casts/Timezone.php b/src/Casts/Timezone.php new file mode 100644 index 0000000..2a145f0 --- /dev/null +++ b/src/Casts/Timezone.php @@ -0,0 +1,45 @@ +setTimezone($this->getUserTimezone()); + } + + /** + * Transform the attribute to its underlying model values. + * + * @param Model $model + * @param string $key + * @param mixed $value + * @param array $attributes + * @return Carbon + */ + public function set($model, string $key, $value, array $attributes) + { + return TimezoneFacade::convertFromLocal($value); + } +} diff --git a/src/Timezone.php b/src/Timezone.php index 7e9c9d9..ff69cb7 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -3,24 +3,25 @@ namespace JamesMills\LaravelTimezone; use Carbon\Carbon; +use JamesMills\LaravelTimezone\Traits\TimezoneTrait; class Timezone { + use TimezoneTrait; + /** * @param Carbon|null $date * @param null $format * @param bool $format_timezone * @return string */ - public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string + public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string { if (is_null($date)) { return 'Empty'; } - $timezone = (auth()->user()->timezone) ?? config('app.timezone'); - - $date->setTimezone($timezone); + $date->setTimezone($this->getUserTimezone()); if (is_null($format)) { return $date->format(config('timezone.format')); @@ -29,7 +30,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = $formatted_date_time = $date->format($format); if ($format_timezone) { - return $formatted_date_time . ' ' . $this->formatTimezone($date); + return $formatted_date_time.' '.$this->formatTimezone($date); } return $formatted_date_time; @@ -39,22 +40,23 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = * @param $date * @return Carbon */ - public function convertFromLocal($date) : Carbon + public function convertFromLocal($date): Carbon { - return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC'); + return Carbon::parse($date, auth()->user()->timezone) + ->setTimezone('UTC'); } /** * @param Carbon $date * @return string */ - private function formatTimezone(Carbon $date) : string + private function formatTimezone(Carbon $date): string { $timezone = $date->format('e'); $parts = explode('/', $timezone); if (count($parts) > 1) { - return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0]; + return str_replace('_', ' ', $parts[1]).', '.$parts[0]; } return str_replace('_', ' ', $parts[0]); diff --git a/src/Traits/TimezoneTrait.php b/src/Traits/TimezoneTrait.php new file mode 100644 index 0000000..75f77d5 --- /dev/null +++ b/src/Traits/TimezoneTrait.php @@ -0,0 +1,14 @@ +user()->timezone) ?? config('app.timezone'); + } +} From 309c0ed229868ded16b5f6b41a0c5cb8d5616ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amandio=20Magalh=C3=A3es?= <2334321+amandiobm@users.noreply.github.com> Date: Mon, 8 Jun 2020 09:29:42 -0700 Subject: [PATCH 2/2] Update README.md --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3fa1d61..59ebc53 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # Laravel Timezone -[![Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=for-the-badge)](https://packagist.org/packages/jamesmills/laravel-timezone) -![Packagist](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=for-the-badge) -![Packagist](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=for-the-badge) -[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone) -[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) +[![Total Downloads](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) +[![Licence](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) +[![Quality Score](https://img.shields.io/scrutinizer/g/jamesmills/laravel-timezone.svg?style=flat-square)](https://scrutinizer-ci.com/g/jamesmills/laravel-timezone) +[![StyleCI](https://github.styleci.io/repos/142882574/shield?branch=master)](https://github.styleci.io/repos/142882574) +[![Buy us a tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone) +[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone) An easy way to set a timezone for a user in your application and then show date/times to them in their local timezone.