Skip to content

Commit 8537969

Browse files
committed
Added proper Timezone facade
1 parent cd329a2 commit 8537969

File tree

6 files changed

+175
-73
lines changed

6 files changed

+175
-73
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ protected $casts = [
3030
];
3131
```
3232

33-
In other scenarios, use the `Timezone`'s static methods directly for conversion:
33+
In other scenarios, use the `Timezone` Facade directly for conversion:
3434

3535
```php
3636
use Carbon\Carbon;
37-
use Whitecube\LaravelTimezones\Timezone;
37+
use Whitecube\LaravelTimezones\Facades\Timezone;
3838

3939
// Get the current date configured with the current timezone:
4040
$now = Timezone::now();

src/Casts/TimezonedDatetime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Whitecube\LaravelTimezones\Casts;
44

5-
use Whitecube\LaravelTimezones\Timezone;
65
use Illuminate\Support\Facades\Date;
6+
use Whitecube\LaravelTimezones\Facades\Timezone;
77
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
88

99
class TimezonedDatetime implements CastsAttributes

src/Facades/Timezone.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Whitecube\LaravelTimezones\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Timezone extends Facade
8+
{
9+
public static function getFacadeAccessor()
10+
{
11+
return 'timezone';
12+
}
13+
}

src/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ServiceProvider extends BaseServiceProvider
1414
*/
1515
public function register()
1616
{
17-
$this->app->singleton(Timezone::class, function ($app) {
17+
$this->app->singleton('timezone', function ($app) {
1818
return new Timezone(config('app.timezone'));
1919
});
2020
}

src/Timezone.php

Lines changed: 122 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,106 +21,182 @@ class Timezone
2121
*/
2222
protected CarbonTimeZone $storage;
2323

24+
/**
25+
* Create a new singleton instance.
26+
*
27+
* @param string $default
28+
* @return void
29+
*/
2430
public function __construct(string $default)
2531
{
2632
$this->setStorage($default);
2733
$this->setCurrent($default);
2834
}
2935

30-
public static function instance(): static
36+
/**
37+
* @alias setCurrent
38+
*
39+
* Set the current application timezone.
40+
*
41+
* @param mixed $timezone
42+
* @return void
43+
*/
44+
public function set($timezone = null)
3145
{
32-
return app()->make(self::class);
46+
$this->setCurrent($timezone);
3347
}
3448

35-
public static function set($timezone = null)
49+
/**
50+
* Set the current application timezone.
51+
*
52+
* @param mixed $timezone
53+
* @return void
54+
*/
55+
public function setCurrent($timezone)
3656
{
37-
static::instance()->setCurrent($timezone);
57+
$this->current = $this->makeTimezone($timezone);
3858
}
3959

40-
public static function current(): CarbonTimeZone
60+
/**
61+
* Return the current application timezone.
62+
*
63+
* @return \Carbon\CarbonTimeZone
64+
*/
65+
public function current(): CarbonTimeZone
4166
{
42-
return static::instance()->getCurrent();
67+
return $this->current;
4368
}
4469

45-
public static function storage(): CarbonTimeZone
70+
/**
71+
* Set the current database timezone.
72+
*
73+
* @param mixed $timezone
74+
* @return void
75+
*/
76+
public function setStorage($timezone)
4677
{
47-
return static::instance()->getStorage();
78+
$this->storage = $this->makeTimezone($timezone);
4879
}
4980

50-
public static function now(): CarbonInterface
81+
/**
82+
* Return the current application timezone.
83+
*
84+
* @return \Carbon\CarbonTimeZone
85+
*/
86+
public function storage(): CarbonTimeZone
5187
{
52-
return static::instance()->convertToCurrent(now());
88+
return $this->storage;
5389
}
5490

55-
public static function date($value, callable $maker = null): CarbonInterface
91+
/**
92+
* Get the current timezoned date.
93+
*
94+
* @return \Carbon\CarbonInterface
95+
*/
96+
public function now(): CarbonInterface
5697
{
57-
$instance = static::instance();
58-
59-
return $instance->convertToCurrent(
60-
$instance->makeDateWithStorage($value, $maker)
61-
);
98+
return $this->convertToCurrent(Date::now());
6299
}
63100

64-
public static function store($value, callable $maker = null): CarbonInterface
101+
/**
102+
* Configure given date for the application's current timezone.
103+
*
104+
* @param mixed $value
105+
* @param null|callable $maker
106+
* @return \Carbon\CarbonInterface
107+
*/
108+
public function date($value, callable $maker = null): CarbonInterface
65109
{
66-
$instance = static::instance();
67-
68-
return $instance->convertToStorage(
69-
$instance->makeDateWithCurrent($value, $maker)
110+
return $this->convertToCurrent(
111+
$this->makeDateWithStorage($value, $maker)
70112
);
71113
}
72114

73-
public function setCurrent($timezone)
74-
{
75-
$this->current = $this->makeTimezone($timezone);
76-
}
77-
78-
public function getCurrent(): CarbonTimeZone
79-
{
80-
return $this->current;
81-
}
82-
83-
public function setStorage($timezone)
84-
{
85-
$this->storage = $this->makeTimezone($timezone);
86-
}
87-
88-
public function getStorage(): CarbonTimeZone
115+
/**
116+
* Configure given date for the database storage timezone.
117+
*
118+
* @param mixed $value
119+
* @param null|callable $maker
120+
* @return \Carbon\CarbonInterface
121+
*/
122+
public function store($value, callable $maker = null): CarbonInterface
89123
{
90-
return $this->storage;
124+
return $this->convertToStorage(
125+
$this->makeDateWithCurrent($value, $maker)
126+
);
91127
}
92128

93-
public function convertToCurrent(CarbonInterface $date): CarbonInterface
129+
/**
130+
* Duplicate the given date and shift its timezone to the application's current timezone.
131+
*
132+
* @param \Carbon\CarbonInterface
133+
* @return \Carbon\CarbonInterface
134+
*/
135+
protected function convertToCurrent(CarbonInterface $date): CarbonInterface
94136
{
95-
return $date->copy()->setTimezone($this->getCurrent());
137+
return $date->copy()->setTimezone($this->current());
96138
}
97139

98-
public function convertToStorage(CarbonInterface $date): CarbonInterface
140+
/**
141+
* Duplicate the given date and shift its timezone to the database's storage timezone.
142+
*
143+
* @param \Carbon\CarbonInterface
144+
* @return \Carbon\CarbonInterface
145+
*/
146+
protected function convertToStorage(CarbonInterface $date): CarbonInterface
99147
{
100-
return $date->copy()->setTimezone($this->getStorage());
148+
return $date->copy()->setTimezone($this->storage());
101149
}
102150

103-
public function makeDateWithCurrent($value, callable $maker = null): CarbonInterface
151+
/**
152+
* Create or configure date using the application's current timezone.
153+
*
154+
* @param mixed $value
155+
* @param null|callable $maker
156+
* @return \Carbon\CarbonInterface
157+
*/
158+
protected function makeDateWithCurrent($value, callable $maker = null): CarbonInterface
104159
{
105160
return is_a($value, CarbonInterface::class)
106161
? $this->convertToCurrent($value)
107-
: $this->makeDate($value, $this->getCurrent(), $maker);
162+
: $this->makeDate($value, $this->current(), $maker);
108163
}
109164

110-
public function makeDateWithStorage($value, callable $maker = null): CarbonInterface
165+
/**
166+
* Create or configure date using the database's storage timezone.
167+
*
168+
* @param mixed $value
169+
* @param null|callable $maker
170+
* @return \Carbon\CarbonInterface
171+
*/
172+
protected function makeDateWithStorage($value, callable $maker = null): CarbonInterface
111173
{
112174
return is_a($value, CarbonInterface::class)
113175
? $this->convertToStorage($value)
114-
: $this->makeDate($value, $this->getStorage(), $maker);
176+
: $this->makeDate($value, $this->storage(), $maker);
115177
}
116178

179+
/**
180+
* Create a date using the provided timezone.
181+
*
182+
* @param mixed $value
183+
* @param \Carbon\CarbonTimeZone $timezone
184+
* @param null|callable $maker
185+
* @return \Carbon\CarbonInterface
186+
*/
117187
protected function makeDate($value, CarbonTimeZone $timezone, callable $maker = null): CarbonInterface
118188
{
119189
return ($maker)
120190
? call_user_func($maker, $value, $timezone)
121191
: Date::create($value, $timezone);
122192
}
123193

194+
/**
195+
* Create a Carbon timezone from given value.
196+
*
197+
* @param mixed $value
198+
* @return \Carbon\CarbonTimeZone
199+
*/
124200
protected function makeTimezone($value): CarbonTimeZone
125201
{
126202
if(! is_a($value, CarbonTimeZone::class)) {

tests/TimezoneSingletonTest.php

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,51 @@
99
it('can create a default Timezone instance and access its current & storage settings', function() {
1010
$instance = new Timezone('Europe/Brussels');
1111

12-
expect($instance->getCurrent())->toBeInstanceOf(CarbonTimeZone::class);
13-
expect($instance->getStorage())->toBeInstanceOf(CarbonTimeZone::class);
12+
expect($instance->current())->toBeInstanceOf(CarbonTimeZone::class);
13+
expect($instance->storage())->toBeInstanceOf(CarbonTimeZone::class);
1414
});
1515

16-
it('can convert date with defined timezone to current & storage timezones', function() {
16+
it('can set the application timezone', function() {
1717
$instance = new Timezone('UTC');
18-
$instance->setCurrent('Europe/Brussels');
1918

20-
$date = new Carbon(null, 'Asia/Phnom_Penh');
19+
expect($instance->current()->getName())->toBe('UTC');
20+
21+
$instance->set('Europe/Brussels');
22+
23+
expect($instance->current()->getName())->toBe('Europe/Brussels');
24+
25+
$instance->setCurrent('Europe/Paris');
2126

22-
expect($instance->convertToStorage($date)->getTimezone()->getName() ?? null)->toBe('UTC');
23-
expect($instance->convertToCurrent($date)->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
24-
expect($date->getTimezone()->getName() ?? null)->toBe('Asia/Phnom_Penh');
27+
expect($instance->current()->getName())->toBe('Europe/Paris');
2528
});
2629

27-
it('can convert date with unset timezone to current & storage timezones', function() {
30+
it('can set the database timezone', function() {
2831
$instance = new Timezone('UTC');
29-
$instance->setCurrent('Europe/Brussels');
3032

31-
$date = new Carbon();
33+
expect($instance->storage()->getName())->toBe('UTC');
3234

33-
expect($instance->convertToStorage($date)->getTimezone()->getName() ?? null)->toBe('UTC');
34-
expect($instance->convertToCurrent($date)->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
35-
expect($date->getTimezone()->getName() ?? null)->toBe(date_default_timezone_get());
35+
$instance->setStorage('Europe/Brussels');
36+
37+
expect($instance->storage()->getName())->toBe('Europe/Brussels');
3638
});
3739

38-
it('can create date with current timezone', function() {
40+
it('can get the current date using the application\'s timezone', function() {
3941
$instance = new Timezone('UTC');
40-
$instance->setCurrent('Europe/Brussels');
42+
$instance->set('Europe/Brussels');
4143

42-
$string = $instance->makeDateWithCurrent('1993-03-16 03:00:00');
43-
$date = $instance->makeDateWithCurrent(new Carbon('1993-03-16 03:00:00', 'UTC'));
44-
$custom = $instance->makeDateWithCurrent('1993-03-16 03:00:00', fn($value, $tz) => new CarbonImmutable($value, $tz));
44+
$date = $instance->now();
45+
46+
expect($date)->toBeInstanceOf(CarbonInterface::class);
47+
expect($date->getTimezone()->getName())->toBe('Europe/Brussels');
48+
});
49+
50+
it('can create or convert a date using the application\'s current timezone', function() {
51+
$instance = new Timezone('UTC');
52+
$instance->set('Europe/Brussels');
53+
54+
$string = $instance->date('1993-03-16 03:00:00');
55+
$date = $instance->date(new Carbon('1993-03-16 03:00:00', 'UTC'));
56+
$custom = $instance->date('1993-03-16 03:00:00', fn($value, $tz) => new CarbonImmutable($value, $tz));
4557

4658
expect($string)->toBeInstanceOf(CarbonInterface::class);
4759
expect($string->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
@@ -51,13 +63,14 @@
5163
expect($custom->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
5264
});
5365

54-
it('can create date with storage timezone', function() {
66+
67+
it('can create or convert a date using the database\'s storage timezone', function() {
5568
$instance = new Timezone('UTC');
5669
$instance->setCurrent('Europe/Brussels');
5770

58-
$string = $instance->makeDateWithStorage('1993-03-16 03:00:00');
59-
$date = $instance->makeDateWithStorage(new Carbon('1993-03-16 03:00:00', 'Europe/Brussels'));
60-
$custom = $instance->makeDateWithStorage('1993-03-16 03:00:00', fn($value, $tz) => new CarbonImmutable($value, $tz));
71+
$string = $instance->store('1993-03-16 03:00:00');
72+
$date = $instance->store(new Carbon('1993-03-16 03:00:00', 'Europe/Brussels'));
73+
$custom = $instance->store('1993-03-16 03:00:00', fn($value, $tz) => new CarbonImmutable($value, $tz));
6174

6275
expect($string)->toBeInstanceOf(CarbonInterface::class);
6376
expect($string->getTimezone()->getName() ?? null)->toBe('UTC');

0 commit comments

Comments
 (0)