| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2022 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class to make start/end times match units |
| 26 |
*/ |
| 27 |
class TimeSpanner { |
| 28 |
|
| 29 |
private $start_func = 'start_days'; |
| 30 |
private $end_func = 'end_days'; |
| 31 |
private $start_time = 0; |
| 32 |
|
| 33 |
public function __construct($units) |
| 34 |
{ |
| 35 |
switch($units) { |
| 36 |
case 'minute' : |
| 37 |
$this->start_func = 'start_minutes'; |
| 38 |
$this->end_func = 'end_minutes'; |
| 39 |
break; |
| 40 |
case 'hour' : |
| 41 |
$this->start_func = 'start_hours'; |
| 42 |
$this->end_func = 'end_hours'; |
| 43 |
break; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns the start time clamped to unit |
| 49 |
* $d = unix timestamp |
| 50 |
*/ |
| 51 |
public function start($d) |
| 52 |
{ |
| 53 |
$e = new \DateTime('@' . $d); |
| 54 |
$this->{$this->start_func}($e); |
| 55 |
$this->start_time = $e->format('U'); |
| 56 |
return $this->start_time; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the end time clamped to at least one unit after start |
| 61 |
* $d = unix timestamp |
| 62 |
*/ |
| 63 |
public function end($d) |
| 64 |
{ |
| 65 |
if($d < $this->start_time) |
| 66 |
$d = $this->start_time; |
| 67 |
|
| 68 |
$e = new \DateTime('@' . $d); |
| 69 |
$this->{$this->end_func}($e, $d - $this->start_time); |
| 70 |
return $e->format('U'); |
| 71 |
} |
| 72 |
|
| 73 |
public function start_minutes(\DateTime $e) |
| 74 |
{ |
| 75 |
$h = (int)$e->format('H'); |
| 76 |
$m = (int)$e->format('i'); |
| 77 |
$e->setTime($h, $m, 0); |
| 78 |
} |
| 79 |
|
| 80 |
public function end_minutes(\DateTime $e, $diff) |
| 81 |
{ |
| 82 |
if($diff < 60) |
| 83 |
$e->modify('+1 minute'); |
| 84 |
$h = (int)$e->format('H'); |
| 85 |
$m = (int)$e->format('i'); |
| 86 |
$e->setTime($h, $m, 0); |
| 87 |
|
| 88 |
// prevent ending at 00:00 on next day |
| 89 |
if($h == 0 && $m == 0) |
| 90 |
$e->modify('-1 second'); |
| 91 |
} |
| 92 |
|
| 93 |
public function start_hours(\DateTime $e) |
| 94 |
{ |
| 95 |
$h = (int)$e->format('H'); |
| 96 |
$e->setTime($h, 0, 0); |
| 97 |
} |
| 98 |
|
| 99 |
public function end_hours(\DateTime $e, $diff) |
| 100 |
{ |
| 101 |
if($diff < 3600) |
| 102 |
$e->modify('+1 hour'); |
| 103 |
$h = (int)$e->format('H'); |
| 104 |
$e->setTime($h, 0, 0); |
| 105 |
|
| 106 |
// prevent ending at 00:00 on next day |
| 107 |
if($h == 0) |
| 108 |
$e->modify('-1 second'); |
| 109 |
} |
| 110 |
|
| 111 |
public function start_days(\DateTime $e) |
| 112 |
{ |
| 113 |
$e->setTime(0, 0, 0); |
| 114 |
} |
| 115 |
|
| 116 |
public function end_days(\DateTime $e, $diff) |
| 117 |
{ |
| 118 |
$e->setTime(23, 59, 59); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|