blob: df03fe32c2665522841ef3e259917555c3e15273 [file] [log] [blame]
Eric Barnes5d1e32b2011-05-03 22:28:59 -04001<?php
Eric Barnes5d1e32b2011-05-03 22:28:59 -04002
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07003class Date_helper_test extends CI_TestCase {
4
5 public function set_up()
6 {
7 $this->helper('date');
8 }
9
Eric Barnes5d1e32b2011-05-03 22:28:59 -040010 // ------------------------------------------------------------------------
11
Taufan Aditya8749bc72012-03-11 05:43:45 +070012 public function test_now_local()
Eric Barnes5d1e32b2011-05-03 22:28:59 -040013 {
Taufan Aditya8749bc72012-03-11 05:43:45 +070014 // This stub job, is simply to cater $config['time_reference']
15 $config = $this->getMock('CI_Config');
16 $config->expects($this->any())
17 ->method('item')
18 ->will($this->returnValue('local'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030019
Taufan Aditya8749bc72012-03-11 05:43:45 +070020 // Add the stub to our test instance
21 $this->ci_instance_var('config', $config);
22
Andrey Andreev1b60fda2012-06-09 21:07:40 +030023 $this->assertEquals(time(), now());
Taufan Aditya8749bc72012-03-11 05:43:45 +070024 }
25
26 // ------------------------------------------------------------------------
27
28 public function test_now_gmt()
29 {
30 // This stub job, is simply to cater $config['time_reference']
31 $config = $this->getMock('CI_Config');
32 $config->expects($this->any())
33 ->method('item')
34 ->will($this->returnValue('gmt'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030035
Taufan Aditya8749bc72012-03-11 05:43:45 +070036 // Add the stub to our stdClass
37 $this->ci_instance_var('config', $config);
38
39 $t = time();
40 $expected = mktime(gmdate("H", $t), gmdate("i", $t), gmdate("s", $t), gmdate("m", $t), gmdate("d", $t), gmdate("Y", $t));
41 $test = now();
42 $this->assertEquals($expected, $test);
Eric Barnes5d1e32b2011-05-03 22:28:59 -040043 }
44
45 // ------------------------------------------------------------------------
46
47 public function test_mdate()
48 {
49 $time = time();
50 $expected = date("Y-m-d - h:i a", $time);
51 $test = mdate("%Y-%m-%d - %h:%i %a", $time);
52 $this->assertEquals($expected, $test);
53 }
54
55 // ------------------------------------------------------------------------
56
57 public function test_standard_date_rfc822()
58 {
59 $time = time();
60 $format = 'DATE_RFC822';
Eric Barnes71644d62011-07-12 21:45:40 -040061 $expected = date("D, d M y H:i:s O", $time);
Eric Barnes5d1e32b2011-05-03 22:28:59 -040062 $this->assertEquals($expected, standard_date($format, $time));
63 }
64
65 // ------------------------------------------------------------------------
66
67 public function test_standard_date_atom()
68 {
69 $time = time();
70 $format = 'DATE_ATOM';
71 $expected = date("Y-m-d\TH:i:sO", $time);
72 $this->assertEquals($expected, standard_date($format, $time));
73 }
74
75 // ------------------------------------------------------------------------
76
77 public function test_standard_date_cookie()
78 {
79 $time = time();
80 $format = 'DATE_COOKIE';
81 $expected = date("l, d-M-y H:i:s \U\T\C", $time);
82 $this->assertEquals($expected, standard_date($format, $time));
83 }
84
85 // ------------------------------------------------------------------------
86
87 public function test_standard_date_iso8601()
88 {
89 $time = time();
90 $format = 'DATE_ISO8601';
91 $expected = date("Y-m-d\TH:i:sO", $time);
92 $this->assertEquals($expected, standard_date($format, $time));
93 }
94
95 // ------------------------------------------------------------------------
96
97 public function test_standard_date_rfc850()
98 {
99 $time = time();
100 $format = 'DATE_RFC850';
101 $expected = date("l, d-M-y H:i:s \U\T\C", $time);
102 $this->assertEquals($expected, standard_date($format, $time));
103 }
104
105 // ------------------------------------------------------------------------
106
107 public function test_standard_date_rfc1036()
108 {
109 $time = time();
110 $format = 'DATE_RFC1036';
111 $expected = date("D, d M y H:i:s O", $time);
112 $this->assertEquals($expected, standard_date($format, $time));
113 }
114
115 // ------------------------------------------------------------------------
116
117 public function test_standard_date_rfc1123()
118 {
119 $time = time();
120 $format = 'DATE_RFC1123';
121 $expected = date("D, d M Y H:i:s O", $time);
122 $this->assertEquals($expected, standard_date($format, $time));
123 }
124
125 // ------------------------------------------------------------------------
126
127 public function test_standard_date_rfc2822()
128 {
129 $time = time();
130 $format = 'DATE_RFC2822';
131 $expected = date("D, d M Y H:i:s O", $time);
132 $this->assertEquals($expected, standard_date($format, $time));
133 }
134
135 // ------------------------------------------------------------------------
136
137 public function test_standard_date_rss()
138 {
139 $time = time();
140 $format = 'DATE_RSS';
141 $expected = date("D, d M Y H:i:s O", $time);
142 $this->assertEquals($expected, standard_date($format, $time));
143 }
144
145 // ------------------------------------------------------------------------
146
147 public function test_standard_date_w3c()
148 {
149 $time = time();
150 $format = 'DATE_W3C';
151 $expected = date("Y-m-d\TH:i:sO", $time);
152 $this->assertEquals($expected, standard_date($format, $time));
153 }
154
155 // ------------------------------------------------------------------------
156
157 public function test_timespan()
158 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700159 $loader_cls = $this->ci_core_class('load');
160 $this->ci_instance_var('load', new $loader_cls);
161
162 $lang_cls = $this->ci_core_class('lang');
163 $this->ci_instance_var('lang', new $lang_cls);
164
165 $this->assertEquals('1 Second', timespan(time(), time()+1));
166 $this->assertEquals('1 Minute', timespan(time(), time()+60));
167 $this->assertEquals('1 Hour', timespan(time(), time()+3600));
168 $this->assertEquals('2 Hours', timespan(time(), time()+7200));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400169 }
170
171 // ------------------------------------------------------------------------
172
173 public function test_days_in_month()
174 {
175 $this->assertEquals(30, days_in_month(06, 2005));
176 $this->assertEquals(28, days_in_month(02, 2011));
177 $this->assertEquals(29, days_in_month(02, 2012));
178 }
179
180 // ------------------------------------------------------------------------
181
182 public function test_local_to_gmt()
183 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700184 $t = time();
185 $expected = mktime(gmdate("H", $t), gmdate("i", $t), gmdate("s", $t), gmdate("m", $t), gmdate("d", $t), gmdate("Y", $t));
186 $this->assertEquals($expected, local_to_gmt($t));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400187 }
188
189 // ------------------------------------------------------------------------
190
191 public function test_gmt_to_local()
192 {
193 $timestamp = '1140153693';
194 $timezone = 'UM8';
195 $daylight_saving = TRUE;
196
197 $this->assertEquals(1140128493, gmt_to_local($timestamp, $timezone, $daylight_saving));
198 }
199
200 // ------------------------------------------------------------------------
201
202 public function test_mysql_to_unix()
203 {
Greg Aker2e0f8252011-08-21 16:19:16 -0500204 $time = time();
Andrey Andreev1b60fda2012-06-09 21:07:40 +0300205 $this->assertEquals($time, mysql_to_unix(date("Y-m-d H:i:s", $time)));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400206 }
207
208 // ------------------------------------------------------------------------
209
210 public function test_unix_to_human()
211 {
212 $time = time();
Greg Aker2e0f8252011-08-21 16:19:16 -0500213 $this->assertEquals(date("Y-m-d h:i A", $time), unix_to_human($time));
214 $this->assertEquals(date("Y-m-d h:i:s A", $time), unix_to_human($time, TRUE, 'us'));
215 $this->assertEquals(date("Y-m-d H:i:s", $time), unix_to_human($time, TRUE, 'eu'));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400216 }
217
218 // ------------------------------------------------------------------------
219
220 public function test_human_to_unix()
221 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700222 $date = '2000-12-31 10:00:00 PM';
223 $expected = strtotime($date);
224 $this->assertEquals($expected, human_to_unix($date));
225 $this->assertFalse(human_to_unix());
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400226 }
227
228 // ------------------------------------------------------------------------
229
230 public function test_timezones()
231 {
232 $zones = array(
233 'UM12' => -12,
234 'UM11' => -11,
235 'UM10' => -10,
236 'UM95' => -9.5,
237 'UM9' => -9,
238 'UM8' => -8,
239 'UM7' => -7,
240 'UM6' => -6,
241 'UM5' => -5,
242 'UM45' => -4.5,
243 'UM4' => -4,
244 'UM35' => -3.5,
245 'UM3' => -3,
246 'UM2' => -2,
247 'UM1' => -1,
248 'UTC' => 0,
249 'UP1' => +1,
250 'UP2' => +2,
251 'UP3' => +3,
252 'UP35' => +3.5,
253 'UP4' => +4,
254 'UP45' => +4.5,
255 'UP5' => +5,
256 'UP55' => +5.5,
257 'UP575' => +5.75,
258 'UP6' => +6,
259 'UP65' => +6.5,
260 'UP7' => +7,
261 'UP8' => +8,
262 'UP875' => +8.75,
263 'UP9' => +9,
264 'UP95' => +9.5,
265 'UP10' => +10,
266 'UP105' => +10.5,
267 'UP11' => +11,
268 'UP115' => +11.5,
269 'UP12' => +12,
270 'UP1275' => +12.75,
271 'UP13' => +13,
272 'UP14' => +14
273 );
274
275 foreach ($zones AS $test => $expected)
276 {
277 $this->assertEquals($expected, timezones($test));
278 }
279
280 $this->assertArrayHasKey('UP3', timezones());
281 $this->assertEquals(0, timezones('non_existant'));
282 }
283}
284
285/* End of file date_helper_test.php */