diff --git a/app/Dates/DateFactory.php b/app/Dates/DateFactory.php new file mode 100644 index 00000000..17c276f5 --- /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/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php index 08b88fdb..0e85d0e0 100644 --- a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -12,14 +12,32 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; -use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; class ReportIncidentCommandHandler { + /** + * The date factory instance. + * + * @var \CachetHQ\Cachet\Dates\DateFactory + */ + protected $dates; + + /** + * Create a new report incident command handler instance. + * + * @param \CachetHQ\Cachet\Dates\DateFactory $dates + * + * @return void + */ + public function __construct(DateFactory $dates) + { + $this->dates = $dates; + } + /** * Handle the report incident command. * @@ -43,7 +61,7 @@ class ReportIncidentCommandHandler // The incident occurred at a different time. if ($command->incident_date) { - $incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, config('cachet.timezone'))->setTimezone(Config::get('app.timezone')); + $incidentDate = $this->dates->createNormalized('d/m/Y H:i', $command->incident_date); $data['created_at'] = $incidentDate; $data['updated_at'] = $incidentDate; diff --git a/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php b/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php index 65029771..556d87f1 100644 --- a/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php +++ b/app/Handlers/Commands/Incident/ReportMaintenanceCommandHandler.php @@ -12,13 +12,31 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent; use CachetHQ\Cachet\Models\Incident; -use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; class ReportMaintenanceCommandHandler { + /** + * The date factory instance. + * + * @var \CachetHQ\Cachet\Dates\DateFactory + */ + protected $dates; + + /** + * Create a new report maintanance command handler instance. + * + * @param \CachetHQ\Cachet\Dates\DateFactory $dates + * + * @return void + */ + public function __construct(DateFactory $dates) + { + $this->dates = $dates; + } + /** * Handle the report maintenance command. * @@ -28,8 +46,7 @@ class ReportMaintenanceCommandHandler */ public function handle(ReportMaintenanceCommand $command) { - $scheduledAt = Date::createFromFormat('d/m/Y H:i', $command->timestamp, config('cachet.timezone')) - ->setTimezone(Config::get('app.timezone')); + $scheduledAt = $this->dates->createNormalized('d/m/Y H:i', $command->timestamp); $maintenanceEvent = Incident::create([ 'name' => $command->name, diff --git a/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php index e2e72b39..c9303bce 100644 --- a/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php @@ -12,14 +12,32 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Events\Incident\IncidentWasUpdatedEvent; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; -use Illuminate\Support\Facades\Config; -use Jenssegers\Date\Date; class UpdateIncidentCommandHandler { + /** + * The date factory instance. + * + * @var \CachetHQ\Cachet\Dates\DateFactory + */ + protected $dates; + + /** + * Create a new update incident command handler instance. + * + * @param \CachetHQ\Cachet\Dates\DateFactory $dates + * + * @return void + */ + public function __construct(DateFactory $dates) + { + $this->dates = $dates; + } + /** * Handle the update incident command. * @@ -34,7 +52,7 @@ class UpdateIncidentCommandHandler // The incident occurred at a different time. if ($command->incident_date) { - $incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, config('cachet.timezone'))->setTimezone(Config::get('app.timezone')); + $incidentDate = $this->dates->createNormalized('d/m/Y H:i', $command->incident_date); $incident->update([ 'created_at' => $incidentDate, diff --git a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php index 962b1623..048f85a0 100644 --- a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php +++ b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php @@ -12,12 +12,31 @@ namespace CachetHQ\Cachet\Handlers\Commands\Metric; use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Events\Metric\MetricPointWasAddedEvent; use CachetHQ\Cachet\Models\MetricPoint; -use Carbon\Carbon; class AddMetricPointCommandHandler { + /** + * The date factory instance. + * + * @var \CachetHQ\Cachet\Dates\DateFactory + */ + protected $dates; + + /** + * Create a new add metric point command handler instance. + * + * @param \CachetHQ\Cachet\Dates\DateFactory $dates + * + * @return void + */ + public function __construct(DateFactory $dates) + { + $this->dates = $dates; + } + /** * Handle the add metric point command. * @@ -36,7 +55,7 @@ class AddMetricPointCommandHandler ]; if ($createdAt) { - $data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s'); + $data['created_at'] = $this->dates->create('U', $createdAt)->format('Y-m-d H:i:s'); } $metricPoint = MetricPoint::create($data); diff --git a/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php index 823d254e..4a9fc156 100644 --- a/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php +++ b/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php @@ -12,11 +12,30 @@ namespace CachetHQ\Cachet\Handlers\Commands\Metric; use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Events\Metric\MetricPointWasUpdatedEvent; -use Carbon\Carbon; class UpdateMetricPointCommandHandler { + /** + * The date factory instance. + * + * @var \CachetHQ\Cachet\Dates\DateFactory + */ + protected $dates; + + /** + * Create a new update metric point command handler instance. + * + * @param \CachetHQ\Cachet\Dates\DateFactory $dates + * + * @return void + */ + public function __construct(DateFactory $dates) + { + $this->dates = $dates; + } + /** * Handle the update metric point command. * @@ -36,7 +55,7 @@ class UpdateMetricPointCommandHandler ]; if ($createdAt) { - $data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s'); + $data['created_at'] = $this->dates->create('U', $createdAt)->format('Y-m-d H:i:s'); } $point->update($data); diff --git a/app/Http/Controllers/Dashboard/ScheduleController.php b/app/Http/Controllers/Dashboard/ScheduleController.php index a49fe98b..3bab83a6 100644 --- a/app/Http/Controllers/Dashboard/ScheduleController.php +++ b/app/Http/Controllers/Dashboard/ScheduleController.php @@ -13,12 +13,12 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand; +use CachetHQ\Cachet\Dates\DateFactory; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\IncidentTemplate; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; -use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; use Illuminate\Support\MessageBag; @@ -130,16 +130,17 @@ class ScheduleController extends Controller /** * Updates the given incident. * - * @param \CachetHQ\Cachet\Models\Incident $schedule + * @param \CachetHQ\Cachet\Models\Incident $schedule + * @param \CachetHQ\Cachet\Dates\DateFactory $dates * * @return \Illuminate\Http\RedirectResponse */ - public function editScheduleAction(Incident $schedule) + public function editScheduleAction(Incident $schedule, DateFactory $dates) { $scheduleData = Binput::get('incident'); + // Parse the schedule date. - $scheduledAt = Date::createFromFormat('d/m/Y H:i', $scheduleData['scheduled_at'], config('cachet.timezone')) - ->setTimezone(Config::get('app.timezone')); + $scheduledAt = $dates->createNormalized('d/m/Y H:i', $scheduleData['scheduled_at']); if ($scheduledAt->isPast()) { $messageBag = new MessageBag(); 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); + }); } } diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index 2c7bb81a..29b20919 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -12,6 +12,7 @@ namespace CachetHQ\Tests\Cachet\Api; use CachetHQ\Tests\Cachet\AbstractTestCase; +use Carbon\Carbon; use Illuminate\Foundation\Testing\DatabaseMigrations; class MetricPointTest extends AbstractTestCase @@ -72,6 +73,28 @@ class MetricPointTest extends AbstractTestCase $this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime]); } + public function testPostMetricPointTimestampTimezone() + { + if (defined('HHVM_VERSION')) { + $this->markTestSkipped('Timezones are broken on HHVM.'); + } + + $this->beUser(); + + $timezone = 'America/Mexico_City'; + $metric = factory('CachetHQ\Cachet\Models\Metric')->create(); + $timestamp = Carbon::now()->timezone($timezone)->timestamp; + $datetime = Carbon::now()->toDateTimeString(); + $metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([ + 'metric_id' => $metric->id, + ]); + $postData = $metricPoint->toArray(); + $postData['timestamp'] = $timestamp; + + $this->post("/api/v1/metrics/{$metric->id}/points", $postData, ['Time-Zone' => $timezone]); + $this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime]); + } + public function testPutMetricPoint() { $this->beUser();