Skip to content

Commit 3e7a0cf

Browse files
author
TrystanLea
committed
remove fixed user timezone
1 parent eb485ee commit 3e7a0cf

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

apps/OpenEnergyMonitor/myheatpump/myheatpump_controller.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414

1515
function myheatpump_app_controller($route,$app,$appconfig,$apikey)
1616
{
17-
global $path, $session, $settings, $mysqli, $redis;
17+
global $path, $session, $settings, $mysqli, $redis, $user;
1818
$v = 1;
1919

20+
if (!$user_timezone = $user->get_timezone($session['userid'])) {
21+
$user_timezone = 'Europe/London';
22+
}
23+
if (is_numeric($user_timezone)) $user_timezone = "Europe/London";
24+
// if timezone UTC set to Europe/London
25+
if ($user_timezone == "UTC") $user_timezone = "Europe/London";
26+
2027
require_once "Modules/feed/feed_model.php";
2128
$settings['feed']['max_datapoints'] = 100000;
2229
$settings['feed']['max_datapoints'] = 1200000;
@@ -25,7 +32,7 @@ function myheatpump_app_controller($route,$app,$appconfig,$apikey)
2532
require_once "Modules/app/apps/OpenEnergyMonitor/myheatpump/myheatpump_process.php";
2633
require_once "Modules/app/apps/OpenEnergyMonitor/myheatpump/myheatpump_waft.php";
2734
require_once "Modules/app/apps/OpenEnergyMonitor/myheatpump/myheatpump_model.php";
28-
$myheatpump = new MyHeatPump($mysqli,$redis,$feed,$appconfig);
35+
$myheatpump = new MyHeatPump($mysqli,$redis,$feed,$appconfig, $user_timezone);
2936

3037
if ($route->action == "view" || $route->action == "") {
3138
$route->format = "html";
@@ -128,7 +135,7 @@ function myheatpump_app_controller($route,$app,$appconfig,$apikey)
128135

129136
if (!$start || !$end) {
130137
$date = new DateTime();
131-
$date->setTimezone(new DateTimeZone("Europe/London"));
138+
$date->setTimezone(new DateTimeZone($user_timezone));
132139
$date->setTime(0,0,0);
133140
$end = $date->getTimestamp();
134141
$date->modify("-1 day");
@@ -212,7 +219,7 @@ function myheatpump_app_controller($route,$app,$appconfig,$apikey)
212219
if (!$start || !$end) {
213220
$date = new DateTime();
214221
// Europe/London is UTC+1
215-
$date->setTimezone(new DateTimeZone('Europe/London'));
222+
$date->setTimezone(new DateTimeZone($user_timezone));
216223
// Set end to midnight start of today
217224
$date->setTime(0, 0, 0);
218225
$end = $date->getTimestamp();

apps/OpenEnergyMonitor/myheatpump/myheatpump_model.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ class MyHeatPump {
77
private $feed;
88
private $appconfig;
99
private $schema;
10+
private $user_timezone;
1011

1112
// constructor
12-
function __construct($mysqli,$redis,$feed,$appconfig) {
13+
function __construct($mysqli,$redis,$feed,$appconfig,$user_timezone = 'Europe/London') {
1314
$this->mysqli = $mysqli;
1415
$this->redis = $redis;
1516
$this->feed = $feed;
1617
$this->appconfig = $appconfig;
18+
$this->user_timezone = $user_timezone;
1719

1820
// Load schema
1921
$schema = array();
@@ -184,7 +186,7 @@ public function process_daily($id, $timeout = 3) {
184186

185187
// Calculate start and end time aligned to midnight
186188
$date = new DateTime();
187-
$date->setTimezone(new DateTimeZone('Europe/London'));
189+
$date->setTimezone(new DateTimeZone($this->user_timezone));
188190

189191
$date->setTimestamp($end_time);
190192
$date->modify("midnight");
@@ -217,7 +219,7 @@ public function process_daily($id, $timeout = 3) {
217219
while ($time<$end) {
218220

219221
// Get stats for the day
220-
$stats = get_heatpump_stats($this->feed,$app,$time,$time+(3600*24),$starting_power);
222+
$stats = get_heatpump_stats($this->feed,$app,$time,$time+(3600*24),$starting_power,$this->user_timezone);
221223

222224
// Translate for database field compatibility
223225
$row = $this->format_flat_keys($stats);

apps/OpenEnergyMonitor/myheatpump/myheatpump_process.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,19 @@ function auto_detect_cooling($data, $interval) {
140140
return $data_heatpump_cooling;
141141
}
142142

143-
function get_heatpump_stats($feed,$app,$start,$end,$starting_power) {
143+
function get_heatpump_stats($feed,$app,$start,$end,$starting_power,$timezone = 'Europe/London') {
144144

145145
// --------------------------------------------------------------------------------------------------------------
146146
// Validate params
147147
// --------------------------------------------------------------------------------------------------------------
148148
if ($end===null || $start===null) {
149149
$date = new DateTime();
150-
$date->setTimezone(new DateTimeZone("Europe/London"));
150+
$date->setTimezone(new DateTimeZone($timezone));
151151
$date->modify("midnight");
152152
$end = $date->getTimestamp();
153153
$date->modify("-30 day");
154154
$start = $date->getTimestamp();
155155
} else {
156-
$timezone = 'Europe/London';
157156
$start = convert_time($start,$timezone);
158157
$end = convert_time($end,$timezone);
159158
}
@@ -187,7 +186,7 @@ function get_heatpump_stats($feed,$app,$start,$end,$starting_power) {
187186
foreach ($feeds as $key) {
188187
$data[$key] = false;
189188
if (isset($app->config->$key) && $app->config->$key>0) {
190-
$data[$key] = $feed->get_data($app->config->$key,$start,$end-$interval,$interval,1,"Europe/London","notime");
189+
$data[$key] = $feed->get_data($app->config->$key,$start,$end-$interval,$interval,1,$timezone,"notime");
191190
$data[$key] = remove_null_values($data[$key],$interval);
192191
}
193192
}

module.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "App",
3-
"version" : "3.1.3",
3+
"version" : "3.1.4",
44
"location" : "/var/www/emoncms/Modules",
55
"branches_available": ["stable","master"]
66
}

0 commit comments

Comments
 (0)