From 592b62b39999155a3aca1ec6d0e12e2f8e1e0de7 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sat, 7 Nov 2015 13:15:28 +0000 Subject: [PATCH] Added date factory --- app/Dates/DateFactory.php | 77 ++++++++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 18 ++++++- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 app/Dates/DateFactory.php diff --git a/app/Dates/DateFactory.php b/app/Dates/DateFactory.php new file mode 100644 index 00000000..0df460b5 --- /dev/null +++ b/app/Dates/DateFactory.php @@ -0,0 +1,77 @@ +appTimezone = $appTimezone; + $this->cachetTimezone = $cachetTimezone; + } + + /** + * Create a Carbon instance from a specific format. + * + * @param string $format + * @param string $time + * + * @throws \InvalidArgumentException + * + * @return \Carbon\Carbon + */ + public function create($format, $time) + { + return Date::createFromFormat($format, $time, $this->cachetTimezone)->setTimezone($this->appTimezone); + } + + /** + * Create a Carbon instance from a specific format. + * + * We're also going to make sure the timezone information is correct. + * + * @param string $format + * @param string $time + * + * @throws \InvalidArgumentException + * + * @return \Carbon\Carbon + */ + public function createNormalized($format, $time) + { + return $this->createFromFormat($format, $time)->setTimezone($this->appTimezone); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 61a874ac..b6542bcb 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -11,6 +11,7 @@ namespace CachetHQ\Cachet\Providers; +use CachetHQ\Cachet\Dates\DateFactory; use Illuminate\Bus\Dispatcher; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; @@ -40,6 +41,21 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // + $this->registerDateFactory(); + } + + /** + * Register the date factory. + * + * @return void + */ + protected function registerDateFactory() + { + $this->app->singleton(DateFactory::class, function ($app) { + $appTimezone = $app->config->get('app.timezone'); + $cacheTimezone = $app->config->get('cachet.timezone'); + + return new DateFactory($appTimezone, $cacheTimezone); + }); } }