blob: bcb1477bcac61db9fb6d61e21a870b6f6c3460fc [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');
Andrey Andreev99b782d2012-06-09 22:24:46 +03008
9 $this->time = time();
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070010 }
11
Eric Barnes5d1e32b2011-05-03 22:28:59 -040012 // ------------------------------------------------------------------------
13
Taufan Aditya8749bc72012-03-11 05:43:45 +070014 public function test_now_local()
Eric Barnes5d1e32b2011-05-03 22:28:59 -040015 {
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030016 /*
17
Taufan Aditya8749bc72012-03-11 05:43:45 +070018 // This stub job, is simply to cater $config['time_reference']
19 $config = $this->getMock('CI_Config');
20 $config->expects($this->any())
21 ->method('item')
22 ->will($this->returnValue('local'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030023
Taufan Aditya8749bc72012-03-11 05:43:45 +070024 // Add the stub to our test instance
25 $this->ci_instance_var('config', $config);
26
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030027 */
28
29 $this->ci_set_config('time_reference', 'local');
30
Andrey Andreev1b60fda2012-06-09 21:07:40 +030031 $this->assertEquals(time(), now());
Taufan Aditya8749bc72012-03-11 05:43:45 +070032 }
33
34 // ------------------------------------------------------------------------
35
36 public function test_now_gmt()
37 {
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030038 /*
39
Taufan Aditya8749bc72012-03-11 05:43:45 +070040 // This stub job, is simply to cater $config['time_reference']
41 $config = $this->getMock('CI_Config');
42 $config->expects($this->any())
43 ->method('item')
44 ->will($this->returnValue('gmt'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030045
Taufan Aditya8749bc72012-03-11 05:43:45 +070046 // Add the stub to our stdClass
47 $this->ci_instance_var('config', $config);
48
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030049 */
50
51 $this->ci_set_config('time_reference', 'gmt');
52
Taufan Aditya8749bc72012-03-11 05:43:45 +070053 $t = time();
Andrey Andreev99b782d2012-06-09 22:24:46 +030054 $this->assertEquals(
55 mktime(gmdate('H', $t), gmdate('i', $t), gmdate('s', $t), gmdate('m', $t), gmdate('d', $t), gmdate('Y', $t)),
56 now()
57 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040058 }
59
60 // ------------------------------------------------------------------------
61
62 public function test_mdate()
63 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030064 $this->assertEquals(
65 date('Y-m-d - h:i a', $this->time),
66 mdate('%Y-%m-%d - %h:%i %a', $this->time)
67 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040068 }
69
70 // ------------------------------------------------------------------------
71
72 public function test_standard_date_rfc822()
73 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030074 $this->assertEquals(
75 date('D, d M y H:i:s O', $this->time),
76 standard_date('DATE_RFC822', $this->time)
77 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040078 }
79
80 // ------------------------------------------------------------------------
81
82 public function test_standard_date_atom()
83 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030084 $this->assertEquals(
85 date("Y-m-d\TH:i:sO", $this->time),
86 standard_date('DATE_ATOM', $this->time)
87 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040088 }
89
90 // ------------------------------------------------------------------------
91
92 public function test_standard_date_cookie()
93 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030094 $this->assertEquals(
95 date("l, d-M-y H:i:s \U\T\C", $this->time),
96 standard_date('DATE_COOKIE', $this->time)
97 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040098 }
99
100 // ------------------------------------------------------------------------
101
102 public function test_standard_date_iso8601()
103 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300104 $this->assertEquals(
105 date("Y-m-d\TH:i:sO", $this->time),
106 standard_date('DATE_ISO8601', $this->time)
107 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400108 }
109
110 // ------------------------------------------------------------------------
111
112 public function test_standard_date_rfc850()
113 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300114 $this->assertEquals(
115 date("l, d-M-y H:i:s \U\T\C", $this->time),
116 standard_date('DATE_RFC850', $this->time)
117 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400118 }
119
120 // ------------------------------------------------------------------------
121
122 public function test_standard_date_rfc1036()
123 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300124 $this->assertEquals(
125 date('D, d M y H:i:s O', $this->time),
126 standard_date('DATE_RFC1036', $this->time)
127 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400128 }
129
130 // ------------------------------------------------------------------------
131
132 public function test_standard_date_rfc1123()
133 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300134 $this->assertEquals(
135 date('D, d M Y H:i:s O', $this->time),
136 standard_date('DATE_RFC1123', $this->time)
137 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400138 }
139
140 // ------------------------------------------------------------------------
141
142 public function test_standard_date_rfc2822()
143 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300144 $this->assertEquals(
145 date('D, d M Y H:i:s O', $this->time),
146 standard_date('DATE_RFC2822', $this->time)
147 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400148 }
149
150 // ------------------------------------------------------------------------
151
152 public function test_standard_date_rss()
153 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300154 $this->assertEquals(
155 date('D, d M Y H:i:s O', $this->time),
156 standard_date('DATE_RSS', $this->time)
157 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400158 }
159
160 // ------------------------------------------------------------------------
161
162 public function test_standard_date_w3c()
163 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300164 $this->assertEquals(
165 date("Y-m-d\TH:i:sO", $this->time),
166 standard_date('DATE_W3C', $this->time)
167 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400168 }
169
170 // ------------------------------------------------------------------------
171
172 public function test_timespan()
173 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700174 $loader_cls = $this->ci_core_class('load');
175 $this->ci_instance_var('load', new $loader_cls);
176
177 $lang_cls = $this->ci_core_class('lang');
178 $this->ci_instance_var('lang', new $lang_cls);
179
180 $this->assertEquals('1 Second', timespan(time(), time()+1));
181 $this->assertEquals('1 Minute', timespan(time(), time()+60));
182 $this->assertEquals('1 Hour', timespan(time(), time()+3600));
183 $this->assertEquals('2 Hours', timespan(time(), time()+7200));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400184 }
185
186 // ------------------------------------------------------------------------
187
188 public function test_days_in_month()
189 {
190 $this->assertEquals(30, days_in_month(06, 2005));
191 $this->assertEquals(28, days_in_month(02, 2011));
192 $this->assertEquals(29, days_in_month(02, 2012));
193 }
194
195 // ------------------------------------------------------------------------
196
197 public function test_local_to_gmt()
198 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300199 $this->assertEquals(
200 mktime(
201 gmdate('H', $this->time), gmdate('i', $this->time), gmdate('s', $this->time),
202 gmdate('m', $this->time), gmdate('d', $this->time), gmdate('Y', $this->time)
203 ),
204 local_to_gmt($this->time)
205 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400206 }
207
208 // ------------------------------------------------------------------------
209
210 public function test_gmt_to_local()
211 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300212 $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400213 }
214
215 // ------------------------------------------------------------------------
216
217 public function test_mysql_to_unix()
218 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300219 $this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time)));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400220 }
221
222 // ------------------------------------------------------------------------
223
224 public function test_unix_to_human()
225 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300226 $this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time));
227 $this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us'));
228 $this->assertEquals(date('Y-m-d H:i:s', $this->time), unix_to_human($this->time, TRUE, 'eu'));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400229 }
230
231 // ------------------------------------------------------------------------
232
233 public function test_human_to_unix()
234 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700235 $date = '2000-12-31 10:00:00 PM';
Andrey Andreev99b782d2012-06-09 22:24:46 +0300236 $this->assertEquals(strtotime($date), human_to_unix($date));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700237 $this->assertFalse(human_to_unix());
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400238 }
239
240 // ------------------------------------------------------------------------
241
242 public function test_timezones()
243 {
244 $zones = array(
245 'UM12' => -12,
246 'UM11' => -11,
247 'UM10' => -10,
248 'UM95' => -9.5,
249 'UM9' => -9,
250 'UM8' => -8,
251 'UM7' => -7,
252 'UM6' => -6,
253 'UM5' => -5,
254 'UM45' => -4.5,
255 'UM4' => -4,
256 'UM35' => -3.5,
257 'UM3' => -3,
258 'UM2' => -2,
259 'UM1' => -1,
260 'UTC' => 0,
261 'UP1' => +1,
262 'UP2' => +2,
263 'UP3' => +3,
264 'UP35' => +3.5,
265 'UP4' => +4,
266 'UP45' => +4.5,
267 'UP5' => +5,
268 'UP55' => +5.5,
269 'UP575' => +5.75,
270 'UP6' => +6,
271 'UP65' => +6.5,
272 'UP7' => +7,
273 'UP8' => +8,
274 'UP875' => +8.75,
275 'UP9' => +9,
276 'UP95' => +9.5,
277 'UP10' => +10,
278 'UP105' => +10.5,
279 'UP11' => +11,
280 'UP115' => +11.5,
281 'UP12' => +12,
282 'UP1275' => +12.75,
283 'UP13' => +13,
284 'UP14' => +14
285 );
286
287 foreach ($zones AS $test => $expected)
288 {
289 $this->assertEquals($expected, timezones($test));
290 }
291
292 $this->assertArrayHasKey('UP3', timezones());
293 $this->assertEquals(0, timezones('non_existant'));
294 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300295
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400296}
297
298/* End of file date_helper_test.php */