Browse Source

Reports: show times in the host's local time zone

PHP ignores the host time zone: without date.timezone in php.ini (the
installer does not set it) it uses UTC, so session labels, event times,
the hour nodes and the live log were shown in UTC while la_mojo names
report folders/files in local time.

- lib/php/host_timezone.php: sets PHP's default time zone from the host
  (/etc/localtime as maintained by timedatectl, then /etc/timezone, then
  TZ); loaded right after settings.php by ajax_asm, assembler, js_asm,
  soap_asm and ui_asm; added to files.list.
- Calendar day key: getHistoryDates returns the host-local noon of each
  day and getHistoryDay uses it directly. The old "local midnight +
  current UTC offset" (applied twice) only worked east of UTC and could be
  off by a day across a DST change.
- Video download file name uses the host-local time from getVideoInfo
  instead of the browser's time zone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/1/head
Claude 1 day ago
parent
commit
036be22fa4
Failed to extract signature
  1. 1
      files.list
  2. 1
      lib/ajax_asm.php
  3. 1
      lib/assembler.php
  4. 6
      lib/controller/getHistoryDates.php
  5. 2
      lib/controller/getHistoryDay.php
  6. 1
      lib/controller/getVideoInfo.php
  7. 4
      lib/javascript/jsapi_dynamic/mojo_reports.js
  8. 1
      lib/js_asm.php
  9. 30
      lib/php/host_timezone.php
  10. 1
      lib/soap_asm.php
  11. 1
      lib/ui_asm.php

1
files.list

@ -5712,6 +5712,7 @@
./lib/php/db/VS_Object_class.php
./lib/php/db/SQLite_Class.php
./lib/php/db/report_files.php
./lib/php/host_timezone.php
./lib/php/db/VS_Tree_class.php
./lib/php/db/language_class.php
./lib/php/db/external.php

1
lib/ajax_asm.php

@ -1,6 +1,7 @@
<?php
error_reporting( E_ALL );
require_once('settings.php');
require_once( __DIR__ . '/php/host_timezone.php'); // PHP must use the host's local time zone
require_once('defines.php');
// SET DATA CONNECTION AND GLOBAL USER VARIABLES

1
lib/assembler.php

@ -1,6 +1,7 @@
<?php
error_reporting( E_ALL );
require_once('settings.php');
require_once( __DIR__ . '/php/host_timezone.php'); // PHP must use the host's local time zone
require_once('defines.php');
// SET DATA CONNECTION AND GLOBAL USER VARIABLES

6
lib/controller/getHistoryDates.php

@ -11,8 +11,10 @@
$DD = array();
foreach( $RR as $R ){
$CC = intval( $R['evt_count'] );
$DS = $R['year'] . '-' . $R['month'] . '-' . $R['day'] . ' 00:00:00';
$TS = strtotime( $DS ) + $DIDI;
// Day key = local noon of the day in the host time zone: maps back to the same date on the
// server in any time zone, and shows the right day in a browser within 12 h of the host
$DS = sprintf( "%04d-%02d-%02d 12:00:00", intval( $R['year'] ), intval( $R['month'] ), intval( $R['day'] ) );
$TS = strtotime( $DS );
if( $CC > 100 ) $CC = 100;
$DD[] = '{"t": '.( $TS * 1000 ).',"v":'.$CC.'}';
//echo '[{"t": 1285891200000,"v": 20},{"t": 1285804800000,"v": 100},{"t": 1672249854000,"v": 50},{"t": 1672279854000,"v": 80}]';

2
lib/controller/getHistoryDay.php

@ -7,7 +7,7 @@
$DT = new DateTime("now", $TZ );
$DIDI = $TZ->getOffset( $DT );
$Data['tz_offset']=$DIDI;
$DTS += $DIDI;
// DTS is the day key from getHistoryDates (host local noon of the day) - no offset correction needed
$Y = date("Y", $DTS );
$M = date("m", $DTS );
$D = date("d", $DTS );

1
lib/controller/getVideoInfo.php

@ -18,6 +18,7 @@
$Pack['videos'] = array();
foreach( $LREC as $Data ){
$Data['id'] = reportEncodeId( $SLOT, $Data['id'] );
$Data['fname'] = date( "Y_m_d_H-i-s", intval( $Data['ts'] ) ); // download file name in host local time
$Pack['videos'][] = $Data;
};
echo json_encode( $Pack );

4
lib/javascript/jsapi_dynamic/mojo_reports.js

@ -1047,6 +1047,7 @@ function loadEventData( record ){
videoNfo[data.videos[i].media_stream_id][1] = data.videos[i].md5;
videoNfo[data.videos[i].media_stream_id][2] = data.videos[i].fps;
videoNfo[data.videos[i].media_stream_id][3] = data.videos[i].ext;
videoNfo[data.videos[i].media_stream_id][4] = data.videos[i].fname; // host local time stamp for file name
data.videos[i].frame -= 20;
if( data.videos[i].frame < 0 ) data.videos[i].frame = 0;
@ -1898,7 +1899,8 @@ function showVideos(){
function downloadCurrentVideo( stid ){
var d = new Date( videoNfo[stid][0] * 1000 );
var videoFileDownloadTarget = d.getFullYear().toString().padStart(4, '0')+'_'+(d.getMonth()+1).toString().padStart(2, '0')+'_'+d.getDate().toString().padStart(2, '0')+'_'+d.getHours().toString().padStart(2, '0')+'-'+d.getMinutes().toString().padStart(2, '0')+'-'+d.getSeconds().toString().padStart(2, '0');
// File name in the host's local time (from the server); browser local time only as a fallback
var videoFileDownloadTarget = videoNfo[stid][4] ? videoNfo[stid][4] : d.getFullYear().toString().padStart(4, '0')+'_'+(d.getMonth()+1).toString().padStart(2, '0')+'_'+d.getDate().toString().padStart(2, '0')+'_'+d.getHours().toString().padStart(2, '0')+'-'+d.getMinutes().toString().padStart(2, '0')+'-'+d.getSeconds().toString().padStart(2, '0');
// TS, MD5, FPS, EXT
downloadVideoFile( videoNfo[stid][0], videoNfo[stid][1], videoNfo[stid][2], videoFileDownloadTarget, videoNfo[stid][3] );
};

1
lib/js_asm.php

@ -1,6 +1,7 @@
<?php
error_reporting( E_ALL );
require_once('settings.php');
require_once( __DIR__ . '/php/host_timezone.php'); // PHP must use the host's local time zone
require_once('defines.php');
// SET DATA CONNECTION AND GLOBAL USER VARIABLES

30
lib/php/host_timezone.php

@ -0,0 +1,30 @@
<?php
/*
* Use the host system's local time zone for all PHP date/time formatting.
*
* PHP does not follow the host time zone: without date.timezone in php.ini it uses UTC, so
* report days, session and event times were shown in UTC instead of the local time that
* la_mojo uses for report folders/files (data/reports/YYYY/MM/DD/report_HH.db).
*
* Source of the host zone, in order:
* /etc/localtime symlink (what timedatectl set-timezone maintains), /etc/timezone, TZ variable.
*/
function hostTimezoneName(){
$Candidates = array();
$L = @readlink( '/etc/localtime' );
if( $L !== false && ( $P = strpos( $L, 'zoneinfo/' ) ) !== false ){
$Candidates[] = substr( $L, $P + 9 );
};
$T = @file_get_contents( '/etc/timezone' );
if( $T !== false ) $Candidates[] = trim( $T );
$E = getenv( 'TZ' );
if( $E !== false ) $Candidates[] = ltrim( trim( $E ), ':' );
foreach( $Candidates as $C ){
if( $C !== '' && in_array( $C, timezone_identifiers_list( DateTimeZone::ALL_WITH_BC ) ) ) return $C;
};
return NULL;
}
$HOST_TZ = hostTimezoneName();
if( $HOST_TZ !== NULL ) date_default_timezone_set( $HOST_TZ );
?>

1
lib/soap_asm.php

@ -1,6 +1,7 @@
<?php
error_reporting( E_ALL );
require_once('settings.php');
require_once( __DIR__ . '/php/host_timezone.php'); // PHP must use the host's local time zone
require_once('defines.php');
require_once('php/onvif/toto.php');

1
lib/ui_asm.php

@ -1,6 +1,7 @@
<?php
require_once('settings.php');
require_once( __DIR__ . '/php/host_timezone.php'); // PHP must use the host's local time zone
require_once('defines.php');
Loading…
Cancel
Save