File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 3
3
namespace Whitecube \LaravelTimezones ;
4
4
5
5
use Carbon \CarbonTimeZone ;
6
+ use Carbon \CarbonInterface ;
6
7
7
8
class Timezone
8
9
{
@@ -65,6 +66,16 @@ public function getStorage(): CarbonTimeZone
65
66
return $ this ->storage ;
66
67
}
67
68
69
+ public function convertToCurrent (CarbonInterface $ date ): CarbonInterface
70
+ {
71
+ return $ date ->copy ()->setTimezone ($ this ->getCurrent ());
72
+ }
73
+
74
+ public function convertToStorage (CarbonInterface $ date ): CarbonInterface
75
+ {
76
+ return $ date ->copy ()->setTimezone ($ this ->getStorage ());
77
+ }
78
+
68
79
protected function makeTimezone ($ value ): CarbonTimeZone
69
80
{
70
81
if (! is_a ($ value , CarbonTimeZone::class)) {
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ use Carbon \Carbon ;
3
4
use Carbon \CarbonTimeZone ;
4
5
use Whitecube \LaravelTimezones \Timezone ;
5
6
9
10
expect ($ instance ->getCurrent ())->toBeInstanceOf (CarbonTimeZone::class);
10
11
expect ($ instance ->getStorage ())->toBeInstanceOf (CarbonTimeZone::class);
11
12
});
13
+
14
+ it ('can convert date with defined timezone to current & storage timezones ' , function () {
15
+ $ instance = new Timezone ('UTC ' );
16
+ $ instance ->setCurrent ('Europe/Brussels ' );
17
+
18
+ $ date = new Carbon (null , 'Asia/Phnom_Penh ' );
19
+
20
+ expect ($ instance ->convertToStorage ($ date )->getTimezone ()->getName () ?? null )->toBe ('UTC ' );
21
+ expect ($ instance ->convertToCurrent ($ date )->getTimezone ()->getName () ?? null )->toBe ('Europe/Brussels ' );
22
+ expect ($ date ->getTimezone ()->getName () ?? null )->toBe ('Asia/Phnom_Penh ' );
23
+ });
24
+
25
+ it ('can convert date with unset timezone to current & storage timezones ' , function () {
26
+ $ instance = new Timezone ('UTC ' );
27
+ $ instance ->setCurrent ('Europe/Brussels ' );
28
+
29
+ $ date = new Carbon ();
30
+
31
+ expect ($ instance ->convertToStorage ($ date )->getTimezone ()->getName () ?? null )->toBe ('UTC ' );
32
+ expect ($ instance ->convertToCurrent ($ date )->getTimezone ()->getName () ?? null )->toBe ('Europe/Brussels ' );
33
+ expect ($ date ->getTimezone ()->getName () ?? null )->toBe (date_default_timezone_get ());
34
+ });
You can’t perform that action at this time.
0 commit comments