Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
time.php
1<?php
2
4
6
7class Time implements \JsonSerializable
8{
9 protected int $timestamp = 0;
10 protected int $offset = 0;
11
12 public function __construct(string $timeFormatted, int $offset = 0)
13 {
14 if (!static::isCorrect($timeFormatted))
15 {
16 $this->timestamp = (new \Bitrix\Main\Type\Date('0000-00-00', 'Y-m-d'))->getTimestamp();
17 }
18 elseif (static::isSerialized($timeFormatted))
19 {
20 preg_match('#(\d{2}:\d{2})\s\[([0-9\-]+)\]#i', $timeFormatted, $matches);
21 $timeFormatted = $matches[1];
22 $userOffset = (int)$matches[2];
23 $dateTime = new \Bitrix\Main\Type\DateTime($timeFormatted, static::getRenderFormat());
24
25 $this->timestamp = $dateTime->getTimestamp() - $userOffset;
26 }
27 else
28 {
29 $format = static::isRenderFormat($timeFormatted) ? static::getRenderFormat() : static::getFormat();
30 $dateTime = new \Bitrix\Main\Type\DateTime($timeFormatted, $format);
31 $this->timestamp = $dateTime->getTimestamp() - $offset;
32 }
33
34 $this->offset = $offset;
35 }
36
37 public function getTimestamp(): int
38 {
39 return $this->timestamp;
40 }
41
42 public function getOffset(): int
43 {
44 return $this->offset;
45 }
46
47 public function __toString(): string
48 {
49 return ($this->toSystemObject()->format(static::getFormat()));
50 }
51
52 public function toSystemObject(): \Bitrix\Main\Type\DateTime
53 {
54 return \Bitrix\Main\Type\DateTime::createFromTimestamp($this->getTimestamp() + $this->getOffset());
55 }
56
57 public function toServerTime(): \Bitrix\Main\Type\DateTime
58 {
59 return \Bitrix\Main\Type\DateTime::createFromTimestamp($this->getTimestamp());
60 }
61
62 public function toUserTime(int $userOffset): \Bitrix\Main\Type\DateTime
63 {
64 return \Bitrix\Main\Type\DateTime::createFromTimestamp($this->getTimestamp() + $userOffset);
65 }
66
67 public static function getFormat(): ?string
68 {
69 return Application::getInstance()->getContext()->getCulture()?->getShortTimeFormat();
70 }
71
72 public static function getRenderFormat(): string
73 {
74 return 'H:i';
75 }
76
77 public static function isCorrect(string $timeFormatted): bool
78 {
79 return (
80 !\CBPHelper::isEmptyValue($timeFormatted)
81 && (
82 \Bitrix\Main\Type\DateTime::isCorrect($timeFormatted, static::getFormat())
83 || static::isRenderFormat($timeFormatted)
84 || static::isSerialized($timeFormatted)
85 )
86 );
87 }
88
89 public function jsonSerialize()
90 {
91 return $this->serialize();
92 }
93
94 public function serialize(): string
95 {
96 $timeFormatted = $this->toSystemObject()->format(static::getRenderFormat());
97
98 return sprintf('%s [%d]', $timeFormatted, $this->offset);
99 }
100
101 private static function isSerialized(string $timeFormatted): bool
102 {
103 if (preg_match('#(\d{2}:\d{2})\s\[([0-9\-]+)\]#i', $timeFormatted, $matches))
104 {
105 $timeFormatted = $matches[1];
106
107 return \Bitrix\Main\Type\DateTime::isCorrect($timeFormatted, static::getRenderFormat());
108 }
109
110 return false;
111 }
112
113 private static function isRenderFormat(string $timeFormatted): bool
114 {
115 $timeFormatted = trim($timeFormatted);
116 if (preg_match('#^\d{2}:\d{2}$#i', $timeFormatted, $matches))
117 {
118 return \Bitrix\Main\Type\DateTime::isCorrect($timeFormatted, static::getRenderFormat());
119 }
120
121 return false;
122 }
123
124 public static function tryMakeCorrectFormat(string $timeFormatted, int $offset = 0): string
125 {
126 $correct = $timeFormatted;
127
128 if (static::isCorrect($timeFormatted))
129 {
130 $time = new static($timeFormatted, $offset);
131 $correct = (string)$time;
132 }
133
134 return $correct;
135 }
136}
static isCorrect(string $timeFormatted)
Definition time.php:77
toUserTime(int $userOffset)
Definition time.php:62
__construct(string $timeFormatted, int $offset=0)
Definition time.php:12
static tryMakeCorrectFormat(string $timeFormatted, int $offset=0)
Definition time.php:124
static isCorrect($time, $format=null)
Definition date.php:356