1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22/**
23 * A parser for time used in the time selector.
24 */
25class CRangeTimeParser extends CParser {
26
27	const ZBX_TIME_UNKNOWN = 0;
28	const ZBX_TIME_ABSOLUTE = 1;
29	const ZBX_TIME_RELATIVE = 2;
30
31	/**
32	 * @var int $time_type
33	 */
34	private $time_type;
35
36	/**
37	 * @var CRelativeTimeParser
38	 */
39	private $relative_time_parser;
40
41	/**
42	 * @var CAbsoluteTimeParser
43	 */
44	private $absolute_time_parser;
45
46	public function __construct() {
47		$this->relative_time_parser = new CRelativeTimeParser();
48		$this->absolute_time_parser = new CAbsoluteTimeParser();
49	}
50
51	/**
52	 * Parse the given period.
53	 *
54	 * @param string $source  Source string that needs to be parsed.
55	 * @param int    $pos     Position offset.
56	 */
57	public function parse($source, $pos = 0) {
58		$this->length = 0;
59		$this->match = '';
60		$this->time_type = self::ZBX_TIME_UNKNOWN;
61
62		$p = $pos;
63
64		if ($this->relative_time_parser->parse($source, $p) != self::PARSE_FAIL) {
65			$p += $this->relative_time_parser->getLength();
66			$this->time_type = self::ZBX_TIME_RELATIVE;
67		}
68		elseif ($this->absolute_time_parser->parse($source, $p) != self::PARSE_FAIL) {
69			$p += $this->absolute_time_parser->getLength();
70			$this->time_type = self::ZBX_TIME_ABSOLUTE;
71		}
72		else {
73			return self::PARSE_FAIL;
74		}
75
76		$this->length = $p - $pos;
77		$this->match = substr($source, $pos, $this->length);
78
79		return isset($source[$p]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS;
80	}
81
82	public function getTimeType() {
83		return $this->time_type;
84	}
85
86	/**
87	 * Timestamp is returned as initialized DateTime object. Returns null when timestamp is not valid.
88	 *
89	 * @param bool   $is_start  If set to true date will be modified to lowest value, example (now/w) will be returned
90	 *                          as Monday of this week. When set to false precisiion will modify date to highest value,
91	 *                          same example will return Sunday of this week.
92	 *
93	 * @return DateTime|null
94	 */
95	public function getDateTime($is_start) {
96		switch ($this->time_type) {
97			case self::ZBX_TIME_ABSOLUTE:
98				return $this->absolute_time_parser->getDateTime($is_start);
99
100			case self::ZBX_TIME_RELATIVE:
101				return $this->relative_time_parser->getDateTime($is_start);
102
103			default:
104				return null;
105		}
106	}
107}
108