-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNetCommonsTime.php
More file actions
261 lines (236 loc) · 8.07 KB
/
NetCommonsTime.php
File metadata and controls
261 lines (236 loc) · 8.07 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* NetCommonsTime
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('Current', 'NetCommons.Utility');
/**
* Class NetCommonsTime
*
* タイムゾーンを考慮した日時を算出します。
* コンポーネントとヘルパーを提供しています。
*
* [NetCommonsTimeComponent](./NetCommonsTimeComponent.html)<br>
* [NetCommonsTimeHelper](./NetCommonsTimeHelper.html)
*
* ## テストで時刻を差し替えたいときのサンプルコード
* ```
* $nowProperty = new ReflectionProperty('NetCommonsTime', '_now');
* $nowProperty->setAccessible(true);
* $nowProperty->setValue(strtotime('2000-01-01 00:00:00'));
* // test code ..
* $nowProperty->setValue(null); // 現在日時変更が他のテストに影響を与えないようにnullにもどしておく
* ```
*
* ## 現在時刻を得る
* ```
* $nowDatetime = (new NetCommonsTime())->getNowDatetime();
* ```
*/
class NetCommonsTime {
/**
* @var int 現在日時 unixtime
*/
protected static $_now = null;
/**
* Unitテストで自由に変更できるようするため、publicにする
*
* @var string サイトデフォルトタイムゾーン
*/
public static $siteTimezone = null;
/**
* サイトのデフォルトタイムゾーンを返す
*
* @return string タイムゾーン
*/
public function getSiteTimezone() {
if (self::$siteTimezone === null) {
$SiteSetting = ClassRegistry::init('SiteManager.SiteSetting');
self::$siteTimezone = $SiteSetting->getSiteTimezone();
}
return self::$siteTimezone;
}
/**
* サーバタイムゾーンの日時をユーザタイムゾーンの日時に変換する
*
* @param string $serverDatetime server timezone datetime
* @return string ユーザタイムゾーンの日時
*/
public function toUserDatetime($serverDatetime) {
if (! $this->isDatetime($serverDatetime)) {
//日時型じゃない場合、変換できないので、そのまま返す。
return $serverDatetime;
}
$userTimezone = $this->getUserTimezone();
$date = new DateTime($serverDatetime, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone($userTimezone));
return $date->format('Y-m-d H:i:s');
}
/**
* サーバタイムゾーンの日時が含まれる連想配列から指定された配列添え字の値だけをユーザタイムゾーンに変換した配列を返す
*
* @param array $data サーバタイムゾーンの日時が含まれた配列
* @param array $convertKeyNameList ユーザタイムゾーンに変換する配列添え字
* @return array ユーザタイムゾーンに変換済みの配列
*/
public function toUserDatetimeArray(array $data, array $convertKeyNameList) {
$userDatetimeData = $data;
$convertKeyNameList = Hash::filter($convertKeyNameList);
foreach ($convertKeyNameList as $keyName) {
if (Hash::get($data, $keyName)) {
$_userDatetime = $this->toUserDatetime(Hash::get($data, $keyName));
$userDatetimeData = Hash::insert($userDatetimeData, $keyName, $_userDatetime);
}
}
//
//foreach ($userDatetimeData as $key => $value) {
// if (is_array($value)) {
// $userDatetimeData[$key] = $this->toUserDatetimeArray($value, $convertKeyNameList);
// } else {
// if (in_array($key, $convertKeyNameList)) {
// $userDatetimeData[$key] = $this->toUserDatetime($value);
// }
// }
//}
return $userDatetimeData;
}
/**
* ユーザタイムゾーンからサーバタイムゾーンに日時を変換する
*
* @param string $userDatetime ユーザタイムゾーンの日時
* @param null|string $userTimezone ユーザタイムゾーンを指定したい時にユーザタイムゾーンを渡す。指定しないとアクセスユーザのタイムゾーンを対象に変換する
* @return string サーバタイムゾーンに変換された日時
*/
public function toServerDatetime($userDatetime, $userTimezone = null) {
if (! $this->isDatetime($userDatetime)) {
//日時型じゃない場合、変換できないので、そのまま返す。
return $userDatetime;
}
if (! $userTimezone) {
$userTimezone = $this->getUserTimezone();
}
$date = new DateTime($userDatetime, new DateTimeZone($userTimezone));
$date->setTimezone(new DateTimeZone('UTC'));
return $date->format('Y-m-d H:i:s');
}
/**
* ユーザタイムゾーンの日時が含まれる連想配列から指定された配列添え字の値だけをサーバタイムゾーンに変換した配列を返す
*
* @param array $data ユーザタイムゾーンの日時が含まれた配列
* @param array $convertKeyNameList サーバタイムゾーンに変換する配列添え字
* @param null|string $userTimezone ユーザタイムゾーンを指定したい時にユーザタイムゾーンを渡す。指定しないとアクセスユーザのタイムゾーンを対象に変換する
* @return array サーバタイムゾーンに変換済みの配列
*/
public function toServerDatetimeArray($data, array $convertKeyNameList, $userTimezone = null) {
$serverDatetimeData = $data;
$convertKeyNameList = Hash::filter($convertKeyNameList);
foreach ($convertKeyNameList as $keyName) {
$date = Hash::get($data, $keyName);
if ($date) {
$_serverDatetime = $this->toServerDatetime($date, $userTimezone);
$serverDatetimeData = Hash::insert($serverDatetimeData, $keyName, $_serverDatetime);
}
}
//foreach ($serverDatetimeData as $key => $value) {
// if (is_array($value)) {
// $serverDatetimeData[$key] = $this->toServerDatetimeArray($value, $convertKeyNameList, $userTimezone);
// } else {
// if (in_array($key, $convertKeyNameList)) {
// $serverDatetimeData[$key] = $this->toServerDatetime($value, $userTimezone);
// }
// }
//}
return $serverDatetimeData;
}
/**
* 現在日時を返す
*
* @return string
*/
static public function getNowDatetime() {
if (self::$_now === null) {
self::$_now = time();
}
return date('Y-m-d H:i:s', self::$_now);
}
/**
* アクセスしたユーザのタイムゾーンを返す
* ゲストならサイトタイムゾーンを返す
*
* @return string タイムゾーン
*/
public function getUserTimezone() {
$userTimezone = Current::read('User.timezone');
if (! $userTimezone) {
$userTimezone = $this->getSiteTimezone();
}
return $userTimezone;
}
/**
* DateTime型チェック
*
* @param string $value datetimeの値
* @return bool DateTime型
*/
public function isDatetime($value) {
try {
// DateTime値チェック($value=20150716150000 or 2018-06-26 11:37:39いずれにも対応)
new DateTime($value, new DateTimeZone('UTC'));
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Date Format
*
* @param datetime $date datetime
* @param null|string $format フォーマット
* @return array
*/
public function dateFormat($date, $format = null) {
if (! Validation::datetime($date)) {
return null;
}
// ユーザタイムゾーンに変換
$date = $this->toUserDatetime($date);
if ($format) {
return date($format, strtotime($date));
} elseif ($this->_isToday($date)) {
return date('G:i', strtotime($date));
} elseif (! $this->_isThisYear($date)) {
return date('Y/m/d', strtotime($date));
} else {
return date('m/d', strtotime($date));
}
}
/**
* ユーザタイムゾーンで今日の日付かの判定
*
* @param string $date user timezone datetime
* @return bool
*/
protected function _isToday($date) {
$now = $this->getNowDatetime();
$nowUserDatetime = $this->toUserDatetime($now);
$dateYmd = date('Y-m-d', strtotime($date));
$nowYmd = date('Y-m-d', strtotime($nowUserDatetime));
return ($dateYmd === $nowYmd);
}
/**
* ユーザタイムゾーンで今年の日時かの判定
*
* @param string $date user timezone datetime
* @return bool
*/
protected function _isThisYear($date) {
$now = $this->getNowDatetime();
$nowUserDatetime = $this->toUserDatetime($now);
$dateYear = date('Y', strtotime($date));
$nowYear = date('Y', strtotime($nowUserDatetime));
return ($dateYear === $nowYear);
}
}