blob: 4b747b8648816b2fd604dded5a50af83daea98b2 [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 {
Taufan Aditya8749bc72012-03-11 05:43:45 +070016 // This stub job, is simply to cater $config['time_reference']
17 $config = $this->getMock('CI_Config');
18 $config->expects($this->any())
19 ->method('item')
20 ->will($this->returnValue('local'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030021
Taufan Aditya8749bc72012-03-11 05:43:45 +070022 // Add the stub to our test instance
23 $this->ci_instance_var('config', $config);
24
Andrey Andreev1b60fda2012-06-09 21:07:40 +030025 $this->assertEquals(time(), now());
Taufan Aditya8749bc72012-03-11 05:43:45 +070026 }
27
28 // ------------------------------------------------------------------------
29
30 public function test_now_gmt()
31 {
32 // This stub job, is simply to cater $config['time_reference']
33 $config = $this->getMock('CI_Config');
34 $config->expects($this->any())
35 ->method('item')
36 ->will($this->returnValue('gmt'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030037
Taufan Aditya8749bc72012-03-11 05:43:45 +070038 // Add the stub to our stdClass
39 $this->ci_instance_var('config', $config);
40
41 $t = time();
Andrey Andreev99b782d2012-06-09 22:24:46 +030042 $this->assertEquals(
43 mktime(gmdate('H', $t), gmdate('i', $t), gmdate('s', $t), gmdate('m', $t), gmdate('d', $t), gmdate('Y', $t)),
44 now()
45 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040046 }
47
48 // ------------------------------------------------------------------------
49
50 public function test_mdate()
51 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030052 $this->assertEquals(
53 date('Y-m-d - h:i a', $this->time),
54 mdate('%Y-%m-%d - %h:%i %a', $this->time)
55 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040056 }
57
58 // ------------------------------------------------------------------------
59
60 public function test_standard_date_rfc822()
61 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030062 $this->assertEquals(
63 date('D, d M y H:i:s O', $this->time),
64 standard_date('DATE_RFC822', $this->time)
65 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040066 }
67
68 // ------------------------------------------------------------------------
69
70 public function test_standard_date_atom()
71 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030072 $this->assertEquals(
73 date("Y-m-d\TH:i:sO", $this->time),
74 standard_date('DATE_ATOM', $this->time)
75 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040076 }
77
78 // ------------------------------------------------------------------------
79
80 public function test_standard_date_cookie()
81 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030082 $this->assertEquals(
83 date("l, d-M-y H:i:s \U\T\C", $this->time),
84 standard_date('DATE_COOKIE', $this->time)
85 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040086 }
87
88 // ------------------------------------------------------------------------
89
90 public function test_standard_date_iso8601()
91 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030092 $this->assertEquals(
93 date("Y-m-d\TH:i:sO", $this->time),
94 standard_date('DATE_ISO8601', $this->time)
95 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040096 }
97
98 // ------------------------------------------------------------------------
99
100 public function test_standard_date_rfc850()
101 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300102 $this->assertEquals(
103 date("l, d-M-y H:i:s \U\T\C", $this->time),
104 standard_date('DATE_RFC850', $this->time)
105 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400106 }
107
108 // ------------------------------------------------------------------------
109
110 public function test_standard_date_rfc1036()
111 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300112 $this->assertEquals(
113 date('D, d M y H:i:s O', $this->time),
114 standard_date('DATE_RFC1036', $this->time)
115 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400116 }
117
118 // ------------------------------------------------------------------------
119
120 public function test_standard_date_rfc1123()
121 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300122 $this->assertEquals(
123 date('D, d M Y H:i:s O', $this->time),
124 standard_date('DATE_RFC1123', $this->time)
125 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400126 }
127
128 // ------------------------------------------------------------------------
129
130 public function test_standard_date_rfc2822()
131 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300132 $this->assertEquals(
133 date('D, d M Y H:i:s O', $this->time),
134 standard_date('DATE_RFC2822', $this->time)
135 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400136 }
137
138 // ------------------------------------------------------------------------
139
140 public function test_standard_date_rss()
141 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300142 $this->assertEquals(
143 date('D, d M Y H:i:s O', $this->time),
144 standard_date('DATE_RSS', $this->time)
145 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400146 }
147
148 // ------------------------------------------------------------------------
149
150 public function test_standard_date_w3c()
151 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300152 $this->assertEquals(
153 date("Y-m-d\TH:i:sO", $this->time),
154 standard_date('DATE_W3C', $this->time)
155 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400156 }
157
158 // ------------------------------------------------------------------------
159
160 public function test_timespan()
161 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700162 $loader_cls = $this->ci_core_class('load');
163 $this->ci_instance_var('load', new $loader_cls);
164
165 $lang_cls = $this->ci_core_class('lang');
166 $this->ci_instance_var('lang', new $lang_cls);
167
168 $this->assertEquals('1 Second', timespan(time(), time()+1));
169 $this->assertEquals('1 Minute', timespan(time(), time()+60));
170 $this->assertEquals('1 Hour', timespan(time(), time()+3600));
171 $this->assertEquals('2 Hours', timespan(time(), time()+7200));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400172 }
173
174 // ------------------------------------------------------------------------
175
176 public function test_days_in_month()
177 {
178 $this->assertEquals(30, days_in_month(06, 2005));
179 $this->assertEquals(28, days_in_month(02, 2011));
180 $this->assertEquals(29, days_in_month(02, 2012));
181 }
182
183 // ------------------------------------------------------------------------
184
185 public function test_local_to_gmt()
186 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300187 $this->assertEquals(
188 mktime(
189 gmdate('H', $this->time), gmdate('i', $this->time), gmdate('s', $this->time),
190 gmdate('m', $this->time), gmdate('d', $this->time), gmdate('Y', $this->time)
191 ),
192 local_to_gmt($this->time)
193 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400194 }
195
196 // ------------------------------------------------------------------------
197
198 public function test_gmt_to_local()
199 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300200 $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400201 }
202
203 // ------------------------------------------------------------------------
204
205 public function test_mysql_to_unix()
206 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300207 $this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time)));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400208 }
209
210 // ------------------------------------------------------------------------
211
212 public function test_unix_to_human()
213 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300214 $this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time));
215 $this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us'));
216 $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 -0400217 }
218
219 // ------------------------------------------------------------------------
220
221 public function test_human_to_unix()
222 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700223 $date = '2000-12-31 10:00:00 PM';
Andrey Andreev99b782d2012-06-09 22:24:46 +0300224 $this->assertEquals(strtotime($date), human_to_unix($date));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700225 $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 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300283
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400284}
285
286/* End of file date_helper_test.php */