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); + }); } }