forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsTime.php
More file actions
159 lines (146 loc) · 5.35 KB
/
NetCommonsTime.php
File metadata and controls
159 lines (146 loc) · 5.35 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
<?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.md#netcommonstimecomponent)<br>
* [NetCommonsTimeHelper](./NetCommonsTimeHelper.md#netcommonstimehelper)
*
* ## テストで時刻を差し替えたいときのサンプルコード
* ```
* $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
*/
static protected $_now = null;
/**
* @var string サイトデフォルトタイムゾーン
*/
static protected $_siteTimezone = null;
/**
* サイトのデフォルトタイムゾーンを返す
*
* @return string タイムゾーン
*/
public function getSiteTimezone() {
if (self::$_siteTimezone === null) {
$SiteSetting = ClassRegistry::init('NetCommons.SiteSetting');
self::$_siteTimezone = $SiteSetting->getSiteTimezone();
}
return self::$_siteTimezone;
}
/**
* サーバタイムゾーンの日時をユーザタイムゾーンの日時に変換する
*
* @param string $serverDatetime server timezone datetime
* @return string ユーザタイムゾーンの日時
*/
public function toUserDatetime($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;
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 ($userTimezone === null) {
$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;
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 === null) {
$userTimezone = $this->getSiteTimezone();
}
return $userTimezone;
}
}