Time and TimeZones
Hi all... seeking advice on time and timezones... the bane of my existence. I have a multi time zone application. Transactions occur 24 hours a day, and fall into defined "sessions". Each session is defined in a database table with a start time, and end time and a timezone. Because the session is "local" to a part of the world. So I have six sessions and some of them are in Asia tz's, some are in German timezones, some are in US timezones..... they are loaded into my "UTC" only application using this quaint piece of code,
protected static function boot()
{
parent::boot();
static::retrieved(function ($model) {
// Trading sessions are stored in local time for the exchange the session relates to.
$tz = new DateTimeZone($model->tz);
$start = new DateTime($model->start, $tz);
$end = new DateTime($model->end, $tz);
$model->start = $start;
$model->end = $end;
$model->tz = $tz;
});
}
That way everything can work nicely regardless of where in the world you are using the app. But every time I go to do something, I end up running into weird convoluted things to make the timezones work, which makes me think I have everything not quite right from a design perspective.
The latest thing, is that given a particular time of day, I need to find the start of the session, closest to that time. It sounds simple, yet the timezones mean that sometimes I end up with the conversions going across date lines and something that should seem close, measured in a few thousand seconds, ends up being nearly a day away because its on the wrong side of midnight in UTC, but not in Asia.....
Anyway, people must do this all the time... yet somehow everything I do ends up being a Carbon object with a Date and a time and then things get moved to "the wrong day" when I try and do simple Carbon->diffInSeconds() calls......
All advice warmly received.
Thanks in advance.
Please or to participate in this conversation.