Skip to content

Commit

Permalink
Initial README, function rename & unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clnt committed Sep 11, 2018
1 parent ab525ae commit d109541
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,62 @@
<a href="https://codeclimate.com/github/Laralabs/timezone/test_coverage"><img src="https://api.codeclimate.com/v1/badges/8112c5d1026cf4a01570/test_coverage" /></a>
</p>

## WORK IN PROGRESS
The timezone package for Laravel provides an easy bi-directional conversion of DateTime into a variety of formats and locales.

Laravel package to make managing timezones and locales easy.
## :rocket: Quick Start

### Installation
Require the package in the `composer.json` of your project.
```
composer require laralabs/timezone
```
Publish the configuration file.
```
php artisan vendor:publish --tag=timezone-config
```
Edit the configuration file and set your desired default display timezone and format.

### Usage
A helper function and facade is available, choose your preferred method.
For the following examples the default timezone will be `Europe/London` and `d/m/Y H:i:s` as the default format.

**Converting from storage**
```php
$date = '2018-09-11 11:00:00';
$result = timezone()->fromStorage($date);
$result->formatToDefault();

Output: 11/09/2018 12:00:00
```

**Converting to storage**
```php
$date = '11/09/2018 12:00:00';
$result = timezone()->toStorage($date);

Output: 2018-09-11 11:00:00
```

The package will check for a `timezone` key in the session before defaulting to the configuration value, alternatively it is possible to override the timezone with a second argument.

**Overriding timezone**
```php
$toTimezone = 'Europe/London';
timezone()->fromStorage($date, $toTimezone);

$fromTimezone = 'Europe/London';
timezone()->toStorage($date, $fromTimezone);
```

**Model Presenter**

The package also comes with a presenter that can be added to models as a trait, for more information on this see the full documentation available below.

## :orange-book: Documentation
Full documentation can be found [on the website](https://docs.laralabs.uk/timezone/).

## :speech-balloon: Support
Please raise an issue on GitHub if there is a problem.

## :key: License
This is open-sourced software licensed under the [MIT License](http://opensource.org/licenses/MIT).
6 changes: 3 additions & 3 deletions src/Presenter/TimezonePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ private function formatDate(string $property, string $original, TimezoneDate $co
if (timezone()->isTimestamp($original)) {
return $converted->format('Y-m-d H:i:s');
}
if (timezone()->isDate($original)) {
return $converted->format('Y-m-d');
}
if (timezone()->isTime($original)) {
return $converted->format('H:i:s');
}
if (timezone()->isDate($original)) {
return $converted->format('Y-m-d');
}

return $this->model->$property;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TimezoneDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function formatToLocale(string $format, string $locale)
/**
* @return mixed|string
*/
public function formatDefault()
public function formatToDefault()
{
return $this->format($this->defaultFormat);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/TimezoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Laralabs\Timezone\Exceptions\TimezoneException;
use Laralabs\Timezone\Presenter\TimezonePresenter;
use Laralabs\Timezone\Tests\Model\TestModel;
Expand Down Expand Up @@ -66,6 +67,20 @@ public function facade_it_can_check_if_timestamp(): void
$this->assertTrue(\Laralabs\Timezone\Facades\Timezone::isTimestamp($this->testUTC));
}

/** @test */
public function facade_it_can_check_if_date(): void
{
$this->assertFalse(\Laralabs\Timezone\Facades\Timezone::isDate($this->testTimeUTC));
$this->assertTrue(\Laralabs\Timezone\Facades\Timezone::isDate($this->testDate));
}

/** @test */
public function facade_it_can_check_if_time(): void
{
$this->assertFalse(\Laralabs\Timezone\Facades\Timezone::isTime($this->testDate));
$this->assertTrue(\Laralabs\Timezone\Facades\Timezone::isTime($this->testTimeUTC));
}

/** @test */
public function it_can_format_to_locale(): void
{
Expand All @@ -74,6 +89,18 @@ public function it_can_format_to_locale(): void
$this->assertEquals($this->testLocaleResult, $converted);
}

/** @test */
public function it_can_format_to_default(): void
{
Config::set('timezone.format', 'd/m/Y H:i:s');

$converted = timezone()->fromStorage($this->testUTC)->formatToDefault();

$this->assertEquals($this->testUKParse, $converted);

Config::set('timezone.format', 'Y-m-d H:i:s');
}

/** @test */
public function it_caches_timezone_array(): void
{
Expand Down

0 comments on commit d109541

Please sign in to comment.