/
DateTime.php
169 lines (159 loc) · 4.06 KB
/
DateTime.php
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
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Stdlib\DateTime;
/**
* Date conversion model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class DateTime
{
/**
* Current config offset in seconds
*
* @var int
*/
private $_offset = 0;
/**
* @var TimezoneInterface
*/
protected $_localeDate;
/**
* @param TimezoneInterface $localeDate
*/
public function __construct(TimezoneInterface $localeDate)
{
$this->_localeDate = $localeDate;
$this->_offset = $this->calculateOffset($this->_localeDate->getConfigTimezone());
}
/**
* Calculates timezone offset
*
* @param string|null $timezone
* @return int offset between timezone and gmt
* @api
*/
public function calculateOffset($timezone = null)
{
$result = true;
$offset = 0;
if ($timezone !== null) {
$oldZone = @date_default_timezone_get();
$result = date_default_timezone_set($timezone);
}
if ($result === true) {
$offset = (int)date('Z');
}
if ($timezone !== null) {
date_default_timezone_set($oldZone);
}
return $offset;
}
/**
* Forms GMT date
*
* @param string $format
* @param int|string $input date in current timezone
* @return string
*/
public function gmtDate($format = null, $input = null)
{
if ($format === null) {
$format = 'Y-m-d H:i:s';
}
$date = $this->gmtTimestamp($input);
if ($date === false) {
return false;
}
$result = date($format, $date);
return $result;
}
/**
* Converts input date into date with timezone offset
* Input date must be in GMT timezone
*
* @param string $format
* @param int|string $input date in GMT timezone
* @return string
* @api
*/
public function date($format = null, $input = null)
{
if ($format === null) {
$format = 'Y-m-d H:i:s';
}
$result = date($format, $this->timestamp($input));
return $result;
}
/**
* Forms GMT timestamp
*
* @param int|string $input date in current timezone
* @return int
*/
public function gmtTimestamp($input = null)
{
if ($input === null) {
return (int)gmdate('U');
} elseif (is_numeric($input)) {
$result = $input;
} else {
$result = strtotime($input);
}
if ($result === false) {
// strtotime() unable to parse string (it's not a date or has incorrect format)
return false;
}
$date = $this->_localeDate->date($result);
$timestamp = $date->getTimestamp();
unset($date);
return $timestamp;
}
/**
* Converts input date into timestamp with timezone offset
* Input date must be in GMT timezone
*
* @param int|string $input date in GMT timezone
* @return int
*/
public function timestamp($input = null)
{
if ($input === null) {
$result = $this->gmtTimestamp();
} elseif (is_numeric($input)) {
$result = $input;
} else {
$result = strtotime($input);
}
$date = $this->_localeDate->date($result);
$timestamp = $date->getTimestamp();
unset($date);
return $timestamp;
}
/**
* Get current timezone offset in seconds/minutes/hours
*
* @param string $type
* @return int
* @api
*/
public function getGmtOffset($type = 'seconds')
{
$result = $this->_offset;
switch ($type) {
case 'seconds':
default:
break;
case 'minutes':
$result = $result / 60;
break;
case 'hours':
$result = $result / 60 / 60;
break;
}
return $result;
}
}