1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* A parser for relative time in "now[/<yMwdhm>][<+->N<yMwdhms>[/<yMwdhm>]]" format.
*/
class CRelativeTimeParser extends CParser {
const ZBX_TOKEN_PRECISION = 0;
const ZBX_TOKEN_OFFSET = 1;
/**
* @var array $tokens An array of tokens for relative date.
*/
private $tokens;
/**
* Parse the given period.
*
* @param string $source Source string that needs to be parsed.
* @param int $pos Position offset.
*/
public function parse($source, $pos = 0) {
$this->length = 0;
$this->match = '';
$this->tokens = [];
$p = $pos;
if (!$this->parseRelativeTime($source, $p)) {
return self::PARSE_FAIL;
}
$this->length = $p - $pos;
$this->match = substr($source, $pos, $this->length);
return isset($source[$p]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS;
}
/**
* Parse relative time.
*
* @param string $source
* @param int $pos
*
* @return bool
*/
private function parseRelativeTime($source, &$pos) {
if (strncmp(substr($source, $pos), 'now', 3) != 0) {
return false;
}
$pos += 3;
while ($this->parsePrecision($source, $pos) || $this->parseOffset($source, $pos)) {
}
return true;
}
/**
* Parse precision.
*
* @param string $source
* @param int $pos
*
* @return bool
*/
private function parsePrecision($source, &$pos) {
$pattern = '(\/[yMwdhm])';
if (!preg_match('/^'.$pattern.'/', substr($source, $pos), $matches)) {
return false;
}
$this->tokens[] = [
'type' => self::ZBX_TOKEN_PRECISION,
'suffix' => substr($matches[0], 1)
];
$pos += strlen($matches[0]);
return true;
}
/**
* Parse offset.
*
* @param string $source
* @param int $pos
*
* @return bool
*/
private function parseOffset($source, &$pos) {
$pattern = '(?P<offset_sign>[+-])(?P<offset_value>[0-9]+)(?P<offset_suffix>[yMwdhms])?';
if (!preg_match('/^'.$pattern.'/', substr($source, $pos), $matches)) {
return false;
}
$this->tokens[] = [
'type' => self::ZBX_TOKEN_OFFSET,
'sign' => $matches['offset_sign'],
'value' => $matches['offset_value'],
'suffix' => array_key_exists('offset_suffix', $matches) ? $matches['offset_suffix'] : 's'
];
$pos += strlen($matches[0]);
return true;
}
/**
* Returns an array of tokens.
*
* @return array
*/
public function getTokens() {
return $this->tokens;
}
/**
* Timestamp is returned as initialized DateTime object. Returns null when timestamp is not valid.
*
* @param bool $is_start If set to true date will be modified to lowest value, example (now/w) will be returned
* as Monday of this week. When set to false precisiion will modify date to highest value,
* same example will return Sunday of this week.
*
* @return DateTime|null
*/
public function getDateTime($is_start) {
if ($this->match === '') {
return null;
}
$date = new DateTime('now');
foreach ($this->getTokens() as $token) {
switch ($token['type']) {
case CRelativeTimeParser::ZBX_TOKEN_PRECISION:
if ($token['suffix'] === 'm' || $token['suffix'] === 'h' || $token['suffix'] === 'd') {
$formats = $is_start
? [
'd' => 'Y-m-d 00:00:00',
'm' => 'Y-m-d H:i:00',
'h' => 'Y-m-d H:00:00'
]
: [
'd' => 'Y-m-d 23:59:59',
'm' => 'Y-m-d H:i:59',
'h' => 'Y-m-d H:59:59'
];
$date = new DateTime($date->format($formats[$token['suffix']]));
}
else {
$modifiers = $is_start
? [
'w' => 'Monday this week 00:00:00',
'M' => 'first day of this month 00:00:00',
'y' => 'first day of January this year 00:00:00'
]
: [
'w' => 'Sunday this week 23:59:59',
'M' => 'last day of this month 23:59:59',
'y' => 'last day of December this year 23:59:59'
];
$date->modify($modifiers[$token['suffix']]);
}
break;
case CRelativeTimeParser::ZBX_TOKEN_OFFSET:
$units = [
's' => 'second',
'm' => 'minute',
'h' => 'hour',
'd' => 'day',
'w' => 'week',
'M' => 'month',
'y' => 'year'
];
$date->modify($token['sign'].$token['value'].' '.$units[$token['suffix']]);
break;
}
}
return $date;
}
}
|