Skip to content

Commit 08475b3

Browse files
committed
Added default timezone instanciation
1 parent e0570af commit 08475b3

File tree

8 files changed

+244
-1
lines changed

8 files changed

+244
-1
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/vendor
2+
.idea
3+
.DS_Store
4+
Thumbs.db
5+
composer.lock
6+
.phpunit.result.cache
7+
.swp
8+
*.map

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
# laravel-timezones
1+
# Laravel Timezones
2+
3+
## Usage
4+
5+
All database dates should be stored using the `app.timezone` config setting. We highly suggest keeping it as `UTC` since it's a global standard for dates storage.
6+
7+
For in-app date manipulation and display, you can define the timezone all dates should cast to using one of the following methods. Depending on you app's context, choose the one that best suits your situation.
8+
9+
### 1. Using middleware
10+
11+
Useful when the app's timezone should be set by ther user's settings.
12+
13+
### 2. Using a Service Provider
14+
15+
Useful when the app's timezone should be set by the application itself.

composer.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "whitecube/laravel-prices",
3+
"description": "Store UTC dates in the database and work with custom timezones in the application.",
4+
"keywords": [
5+
"whitecube",
6+
"laravel",
7+
"utc",
8+
"local",
9+
"time",
10+
"conversion"
11+
],
12+
"authors": [
13+
{
14+
"name": "Whitecube",
15+
"email": "hello@whitecube.be"
16+
},
17+
{
18+
"name": "Toon Van den Bos",
19+
"email": "toon@whitecube.be"
20+
}
21+
],
22+
"require": {
23+
"php": ">=8.1",
24+
"laravel/framework": "^9",
25+
"nesbot/carbon": "^2.64"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Whitecube\\LaravelTimezones\\": "src/",
30+
"Whitecube\\LaravelTimezones\\Tests\\": "tests/"
31+
}
32+
},
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"Whitecube\\LaravelTimezones\\ServiceProvider"
37+
]
38+
}
39+
},
40+
"require-dev": {
41+
"pestphp/pest": "^1.22"
42+
},
43+
"config": {
44+
"allow-plugins": {
45+
"pestphp/pest-plugin": true
46+
}
47+
},
48+
"minimum-stability": "dev",
49+
"prefer-stable": true
50+
}

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./app</directory>
15+
<directory suffix=".php">./src</directory>
16+
</include>
17+
</coverage>
18+
</phpunit>

src/ServiceProvider.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Whitecube\LaravelTimezones;
4+
5+
use Whitecube\LaravelTimezones\Timezone;
6+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
8+
class ServiceProvider extends BaseServiceProvider
9+
{
10+
/**
11+
* Register any application services.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
$this->app->singleton(Timezone::class, function ($app) {
18+
return new Timezone(config('app.timezone'));
19+
});
20+
}
21+
}

src/Timezone.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Whitecube\LaravelTimezones;
4+
5+
use Carbon\CarbonTimeZone;
6+
7+
class Timezone
8+
{
9+
/**
10+
* The app's current display & manipulation timezone
11+
*
12+
* @var \Carbon\CarbonTimeZone
13+
*/
14+
protected CarbonTimeZone $current;
15+
/**
16+
* The app's current storage timezone
17+
*
18+
* @var \Carbon\CarbonTimeZone
19+
*/
20+
protected CarbonTimeZone $storage;
21+
22+
public static function set($timezone = null)
23+
{
24+
static::instance()->setCurrent($timezone);
25+
}
26+
27+
public static function current(): CarbonTimeZone
28+
{
29+
return static::instance()->getCurrent();
30+
}
31+
32+
public static function storage(): CarbonTimeZone
33+
{
34+
return static::instance()->getStorage();
35+
}
36+
37+
public function instance(): static
38+
{
39+
return app()->make(self::class);
40+
}
41+
42+
public function __construct(string $default)
43+
{
44+
$this->setStorage($default);
45+
$this->setCurrent($default);
46+
}
47+
48+
public function setCurrent($timezone)
49+
{
50+
$this->current = $this->makeTimezone($timezone);
51+
}
52+
53+
public function getCurrent(): CarbonTimeZone
54+
{
55+
return $this->current;
56+
}
57+
58+
public function setStorage($timezone)
59+
{
60+
$this->storage = $this->makeTimezone($timezone);
61+
}
62+
63+
public function getStorage(): CarbonTimeZone
64+
{
65+
return $this->storage;
66+
}
67+
68+
protected function makeTimezone($value): CarbonTimeZone
69+
{
70+
if(! is_a($value, CarbonTimeZone::class)) {
71+
$value = new CarbonTimeZone($value);
72+
}
73+
74+
return $value;
75+
}
76+
}

tests/Pest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
|
8+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10+
| need to change it using the "uses()" function to bind a different classes or traits.
11+
|
12+
*/
13+
14+
// uses(Tests\TestCase::class)->in('Feature');
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Expectations
19+
|--------------------------------------------------------------------------
20+
|
21+
| When you're writing tests, you often need to check that values meet certain conditions. The
22+
| "expect()" function gives you access to a set of "expectations" methods that you can use
23+
| to assert different things. Of course, you may extend the Expectation API at any time.
24+
|
25+
*/
26+
27+
// expect()->extend('toBeOne', function () {
28+
// return $this->toBe(1);
29+
// });
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Functions
34+
|--------------------------------------------------------------------------
35+
|
36+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
37+
| project that you don't want to repeat in every file. Here you can also expose helpers as
38+
| global functions to help you to reduce the number of lines of code in your test files.
39+
|
40+
*/
41+
42+
// function something()
43+
// {
44+
// // ..
45+
// }

tests/TimezoneSingletonTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Carbon\CarbonTimeZone;
4+
use Whitecube\LaravelTimezones\Timezone;
5+
6+
it('can create a default Timezone instance and access its current & storage settings', function() {
7+
$instance = new Timezone('Europe/Brussels');
8+
9+
expect($instance->getCurrent())->toBeInstanceOf(CarbonTimeZone::class);
10+
expect($instance->getStorage())->toBeInstanceOf(CarbonTimeZone::class);
11+
});

0 commit comments

Comments
 (0)