Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datetime.php
1<?php
2
3namespace Bitrix\Main\Type;
4
7
8class DateTime extends Date
9{
11 protected $userTimeEnabled = true;
12
20 public function __construct($time = null, $format = null, \DateTimeZone $timezone = null)
21 {
22 if ($timezone === null)
23 {
24 $this->value = new \DateTime();
25 }
26 else
27 {
28 $this->value = new \DateTime(null, $timezone);
29 }
30
31 if ($time !== null && $time !== "")
32 {
33 if ($format === null)
34 {
35 $format = static::getFormat();
36 }
37
38 $parsedValue = $this->parse($format, $time);
39
40 if($parsedValue === false)
41 {
42 throw new Main\ObjectException("Incorrect date/time: ".$time);
43 }
44
45 if(isset($parsedValue["timestamp"]))
46 {
47 $this->value->setTimestamp($parsedValue["timestamp"]);
48 }
49 else
50 {
51 if(isset($parsedValue["zone_type"]) && $parsedValue["zone_type"] == 1)
52 {
53 if(isset($parsedValue["zone"]))
54 {
55 $this->setTimeZone(new \DateTimeZone(static::secondsToOffset($parsedValue["zone"])));
56 }
57 }
58
59 $microseconds = 0;
60 if($parsedValue['fraction'] > 0)
61 {
62 $microseconds = intval($parsedValue['fraction'] * 1000000);
63 }
64
65 $this->value->setDate($parsedValue['year'], $parsedValue['month'], $parsedValue['day']);
66 $this->value->setTime($parsedValue['hour'], $parsedValue['minute'], $parsedValue['second'], $microseconds);
67 }
68 }
69 }
70
71 public static function secondsToOffset($seconds, $delimiter = '')
72 {
73 $absSeconds = abs($seconds);
74 $hours = sprintf("%02d", floor($absSeconds / 3600));
75 $minutes = gmdate("i", $absSeconds % 3600);
76 return ($seconds < 0 ? "-" : "+") . $hours . $delimiter . $minutes;
77 }
78
86 public function toString(Context\Culture $culture = null)
87 {
88 if ($this->userTimeEnabled && \CTimeZone::Enabled())
89 {
90 $userTime = clone $this;
91 $userTime->toUserTime();
92
93 $format = static::getFormat($culture);
94
95 return $userTime->format($format);
96 }
97
98 return parent::toString($culture);
99 }
100
106 public function getTimeZone()
107 {
108 return $this->value->getTimezone();
109 }
110
118 public function setTimeZone(\DateTimeZone $timezone)
119 {
120 $this->value->setTimezone($timezone);
121 return $this;
122 }
123
129 public function setDefaultTimeZone()
130 {
131 $time = new \DateTime();
132 $this->setTimezone($time->getTimezone());
133 return $this;
134 }
135
144 public function setTime($hour, $minute, $second = 0, $microseconds = 0)
145 {
146 $this->value->setTime($hour, $minute, $second, $microseconds);
147 return $this;
148 }
149
155 public function toUserTime()
156 {
157 //first, move to server timezone
158 $this->setDefaultTimeZone();
159
160 //second, adjust time according global timezone offset
161 static $diff = null;
162 if($diff === null)
163 {
164 $diff = \CTimeZone::GetOffset();
165 }
166 if($diff <> 0)
167 {
168 $this->add(($diff < 0? "-":"")."PT".abs($diff)."S");
169 }
170 return $this;
171 }
172
180 public static function createFromUserTime($timeString)
181 {
182 try
183 {
184 //try full datetime format
185 $time = new static($timeString);
186 }
187 catch(Main\ObjectException $e)
188 {
189 //try short date format
190 $time = new static($timeString, Date::getFormat());
191 $time->setTime(0, 0);
192 }
193
194 if(\CTimeZone::Enabled())
195 {
196 static $diff = null;
197 if($diff === null)
198 {
199 $diff = \CTimeZone::GetOffset();
200 }
201 if($diff <> 0)
202 {
203 $time->add(($diff > 0? "-":"")."PT".abs($diff)."S");
204 }
205 }
206 return $time;
207 }
208
216 protected static function getCultureFormat(Context\Culture $culture = null)
217 {
218 if($culture)
219 {
220 return $culture->getDateTimeFormat();
221 }
222 return "DD.MM.YYYY HH:MI:SS";
223 }
224
232 public static function createFromPhp(\DateTime $datetime)
233 {
234 $d = new static();
235 $d->value = clone $datetime;
236 return $d;
237 }
238
246 public static function createFromTimestamp($timestamp)
247 {
248 $d = new static();
249 $d->value->setTimestamp($timestamp);
250 return $d;
251 }
252
260 public static function tryParse($timeString, $format = null)
261 {
262 if($timeString === '')
263 {
264 return null;
265 }
266
267 try
268 {
269 $time = new static($timeString, $format);
270 }
271 catch(Main\ObjectException $e)
272 {
273 $time = null;
274 }
275 return $time;
276 }
277
281 public function isUserTimeEnabled()
282 {
284 }
285
289 public function disableUserTime()
290 {
291 $this->userTimeEnabled = false;
292
293 return $this;
294 }
295
299 public function enableUserTime()
300 {
301 $this->userTimeEnabled = true;
302
303 return $this;
304 }
305}
static getFormat(Context\Culture $culture=null)
Definition date.php:261
add($interval)
Definition date.php:145
parse($format, $time)
Definition date.php:53
static createFromTimestamp($timestamp)
Definition datetime.php:246
static tryParse($timeString, $format=null)
Definition datetime.php:260
toString(Context\Culture $culture=null)
Definition datetime.php:86
static secondsToOffset($seconds, $delimiter='')
Definition datetime.php:71
static getCultureFormat(Context\Culture $culture=null)
Definition datetime.php:216
__construct($time=null, $format=null, \DateTimeZone $timezone=null)
Definition datetime.php:20
setTime($hour, $minute, $second=0, $microseconds=0)
Definition datetime.php:144
static createFromUserTime($timeString)
Definition datetime.php:180
setTimeZone(\DateTimeZone $timezone)
Definition datetime.php:118
static createFromPhp(\DateTime $datetime)
Definition datetime.php:232