-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves timezones from model to service
- Loading branch information
Showing
4 changed files
with
34 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
<?php namespace App\Http\Controllers; | ||
|
||
use App\Models\Timezones; | ||
use App\Services\Timezones; | ||
|
||
class TimezoneController extends BaseController | ||
{ | ||
/** | ||
* Assign dependencies. | ||
* | ||
* @param Timezones $timezones | ||
* @return void | ||
*/ | ||
public function __construct(Timezones $timezones) | ||
{ | ||
$this->timezones = $timezones; | ||
} | ||
|
||
/** | ||
* Get all entities. | ||
* | ||
* @return Response | ||
*/ | ||
public function getAll() | ||
{ | ||
return $this->collection(Timezones::getTimezones()); | ||
return $this->collection($this->timezones->all()); | ||
} | ||
} |
40 changes: 19 additions & 21 deletions
40
api/app/Models/Timezones.php → api/app/Services/Timezones.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,51 @@ | ||
<?php namespace App\Models; | ||
<?php namespace App\Services; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* Class Timezones | ||
* @package App\Models | ||
* | ||
* Note this model does not have an associated database table as it is an abstract | ||
* data model with generated token data. | ||
*/ | ||
class Timezones extends Model | ||
|
||
class Timezones | ||
{ | ||
/** | ||
* Get the collection of available timezones. | ||
* Get all timezones. | ||
* | ||
* @return Collection | ||
*/ | ||
public static function getTimezones() | ||
public function all() | ||
{ | ||
$allTimezones = \DateTimeZone::listIdentifiers(); | ||
$allTimezones = DateTimeZone::listIdentifiers(); | ||
|
||
$timezones = new Collection(); | ||
$now = time(); | ||
|
||
foreach ($allTimezones as $timezoneIdentifier) { | ||
$dateTimeZone = new \DateTimeZone($timezoneIdentifier); | ||
$dateTimeZone = new DateTimeZone($timezoneIdentifier); | ||
|
||
//Read the current transition to get if the timezone is currently in DST | ||
$transitions = $dateTimeZone->getTransitions($now, $now); | ||
|
||
$timezones->push(new Collection([ | ||
'timezone_identifier' => $dateTimeZone->getName(), | ||
'offset' => $offset = $dateTimeZone->getOffset(new \DateTime()), | ||
'offset' => $offset = $dateTimeZone->getOffset(new DateTime()), | ||
'is_dst' => $transitions[0]['isdst'], //only use the first transition | ||
'display_offset' => self::getDisplayOffset($offset), | ||
'display_offset' => $this->formatDisplayOffset($offset), | ||
])); | ||
} | ||
|
||
return $timezones; | ||
} | ||
|
||
private static function getDisplayOffset($offset) | ||
/** | ||
* Format the time offset for display. | ||
* | ||
* @param int $offset | ||
* @return string | ||
*/ | ||
protected function formatDisplayOffset($offset) | ||
{ | ||
$inital = new \DateTime(); | ||
$inital = new DateTime(); | ||
$inital->setTimestamp(abs($offset)); | ||
$hoursFormatted = $inital->format('H:i'); | ||
|
||
|
||
return ($offset >= 0 ? '+':'-') . $hoursFormatted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters