Sometimes it is very nice to store time on the server in UTC and convert it to the user's local time when issuing a page. At least this task arose in the project in which I participate. Translation from utc to local time is not an easy task, but there is a lot of information and published algorithms on it. All that is needed for them is the data on the client's time zone, but as it turned out, there is not so much information about it.
All published below results of own developments
var now = new Date ();
function CheckDLT (sm, em)
{
return ((new Date (em)). getTimezoneOffset () - (new Date (sm)). getTimezoneOffset ());
}
function getTZLowerBound (start, end, cmp)
{
var nstart, count, count2;
count = end - start;
while (count> 0)
{
end = start + count;
nstart = start + (count2 = Math.floor (count / 2));
if (cmp (CheckDLT (nstart, end)))
{
start = nstart + 1;
count - = count2 + 1;
}
else
count = count2;
}
return [start, - (new Date (start)). getTimezoneOffset ()];
}
var dlt = Date.UTC (now.getFullYear (), 0, 1, 0, 0, 0, 0),
std = Date.UTC (now.getFullYear (), 6, 1, 0, 0, 0, 0);
if (SheckDLT (dlt, std))
{
dlt = getTZLowerBound (dlt, std, function (b) {return (b <0)});
std = getTZLowerBound (Date.UTC (now.getFullYear (), 6, 1, 0, 0, 0, 0),
Date.UTC (now.getFullYear (), 11, 1, 0, 0, 0, 0),
function (b) {return (b> 0)});
dlt [0] = dlt [0] + std [1] * 60000;
std [0] = std [0] + dlt [1] * 60000;
}
else
std = dlt = [0, -now.getTimezoneOffset ()];
')
This is what we have as a result of the execution of this code. At the output, we get two arrays dlt and std. The first contains data on the transition point to summer time, the second to the standard. The first element of each array is the timestamp of the transition point in local time, the second offset relative to GMT after the transition. If there is no daylight saving time in the client's time zone, then the first elements of both arrays will be zero, and the second elements of the time zone offset in GMT
Of course, it is impossible to unambiguously determine the client's time zone from these data, since there are many intersections among them at present, but there is a discrepancy in the past. Nevertheless, if you use them for automatic time conversion, then this is quite enough.