1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
time.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Bizproc\BaseType\Value;
4
5
use Bitrix\Main\Application;
6
7
class
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
}
Bitrix\Bizproc\BaseType\Value\Time
Определения
time.php:8
Bitrix\Bizproc\BaseType\Value\Time\isCorrect
static isCorrect(string $timeFormatted)
Определения
time.php:77
Bitrix\Bizproc\BaseType\Value\Time\toSystemObject
toSystemObject()
Определения
time.php:52
Bitrix\Bizproc\BaseType\Value\Time\serialize
serialize()
Определения
time.php:94
Bitrix\Bizproc\BaseType\Value\Time\$timestamp
int $timestamp
Определения
time.php:9
Bitrix\Bizproc\BaseType\Value\Time\__toString
__toString()
Определения
time.php:47
Bitrix\Bizproc\BaseType\Value\Time\getTimestamp
getTimestamp()
Определения
time.php:37
Bitrix\Bizproc\BaseType\Value\Time\getRenderFormat
static getRenderFormat()
Определения
time.php:72
Bitrix\Bizproc\BaseType\Value\Time\getFormat
static getFormat()
Определения
time.php:67
Bitrix\Bizproc\BaseType\Value\Time\toUserTime
toUserTime(int $userOffset)
Определения
time.php:62
Bitrix\Bizproc\BaseType\Value\Time\$offset
int $offset
Определения
time.php:10
Bitrix\Bizproc\BaseType\Value\Time\__construct
__construct(string $timeFormatted, int $offset=0)
Определения
time.php:12
Bitrix\Bizproc\BaseType\Value\Time\toServerTime
toServerTime()
Определения
time.php:57
Bitrix\Bizproc\BaseType\Value\Time\jsonSerialize
jsonSerialize()
Определения
time.php:89
Bitrix\Bizproc\BaseType\Value\Time\tryMakeCorrectFormat
static tryMakeCorrectFormat(string $timeFormatted, int $offset=0)
Определения
time.php:124
Bitrix\Bizproc\BaseType\Value\Time\getOffset
getOffset()
Определения
time.php:42
Bitrix\Main\Type\Date\isCorrect
static isCorrect($time, $format=null)
Определения
date.php:359
Bitrix\Main\Type\DateTime
Определения
datetime.php:9
Bitrix\Main\Type
Определения
collection.php:2
Bitrix\Main
Bitrix
$time
$time
Определения
payment.php:61
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$matches
$matches
Определения
index.php:22
bitrix
modules
bizproc
lib
basetype
value
time.php
Создано системой
1.14.0