blob: 1458acd3edcaace5dcee8ef22f55b03fd3bffbb7 [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
Andrey Andreev0ddff312012-06-14 13:18:07 +030036 public function test_now_utc()
Taufan Aditya8749bc72012-03-11 05:43:45 +070037 {
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')
Andrey Andreev0ddff312012-06-14 13:18:07 +030044 ->will($this->returnValue('UTC'));
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
Andrey Andreev99b782d2012-06-09 22:24:46 +030051 $this->assertEquals(
Andrey Andreeva84055b2012-06-20 14:37:59 +030052 mktime(gmdate('G'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y')),
53 now('UTC')
Andrey Andreev99b782d2012-06-09 22:24:46 +030054 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040055 }
56
57 // ------------------------------------------------------------------------
58
59 public function test_mdate()
60 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030061 $this->assertEquals(
62 date('Y-m-d - h:i a', $this->time),
63 mdate('%Y-%m-%d - %h:%i %a', $this->time)
64 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040065 }
66
67 // ------------------------------------------------------------------------
68
69 public function test_standard_date_rfc822()
70 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030071 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030072 date(DATE_RFC822, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030073 standard_date('DATE_RFC822', $this->time)
74 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040075 }
76
77 // ------------------------------------------------------------------------
78
79 public function test_standard_date_atom()
80 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030081 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030082 date(DATE_ATOM, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030083 standard_date('DATE_ATOM', $this->time)
84 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040085 }
86
87 // ------------------------------------------------------------------------
88
89 public function test_standard_date_cookie()
90 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030091 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030092 date(DATE_COOKIE, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030093 standard_date('DATE_COOKIE', $this->time)
94 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040095 }
96
97 // ------------------------------------------------------------------------
98
99 public function test_standard_date_iso8601()
100 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300101 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300102 date(DATE_ISO8601, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300103 standard_date('DATE_ISO8601', $this->time)
104 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400105 }
106
107 // ------------------------------------------------------------------------
108
109 public function test_standard_date_rfc850()
110 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300111 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300112 date(DATE_RFC850, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300113 standard_date('DATE_RFC850', $this->time)
114 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400115 }
116
117 // ------------------------------------------------------------------------
118
119 public function test_standard_date_rfc1036()
120 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300121 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300122 date(DATE_RFC1036, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300123 standard_date('DATE_RFC1036', $this->time)
124 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400125 }
126
127 // ------------------------------------------------------------------------
128
129 public function test_standard_date_rfc1123()
130 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300131 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300132 date(DATE_RFC1123, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300133 standard_date('DATE_RFC1123', $this->time)
134 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400135 }
136
137 // ------------------------------------------------------------------------
138
139 public function test_standard_date_rfc2822()
140 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300141 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300142 date(DATE_RFC2822, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300143 standard_date('DATE_RFC2822', $this->time)
144 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400145 }
146
147 // ------------------------------------------------------------------------
148
149 public function test_standard_date_rss()
150 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300151 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300152 date(DATE_RSS, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300153 standard_date('DATE_RSS', $this->time)
154 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400155 }
156
157 // ------------------------------------------------------------------------
158
159 public function test_standard_date_w3c()
160 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300161 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300162 date(DATE_W3C, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300163 standard_date('DATE_W3C', $this->time)
164 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400165 }
166
167 // ------------------------------------------------------------------------
168
169 public function test_timespan()
170 {
dchill427ecc5cd2012-10-12 16:25:51 -0400171 $this->ci_vfs_clone('system/language/english/date_lang.php');
172
Taufan Aditya8749bc72012-03-11 05:43:45 +0700173 $loader_cls = $this->ci_core_class('load');
174 $this->ci_instance_var('load', new $loader_cls);
175
176 $lang_cls = $this->ci_core_class('lang');
177 $this->ci_instance_var('lang', new $lang_cls);
178
179 $this->assertEquals('1 Second', timespan(time(), time()+1));
180 $this->assertEquals('1 Minute', timespan(time(), time()+60));
181 $this->assertEquals('1 Hour', timespan(time(), time()+3600));
182 $this->assertEquals('2 Hours', timespan(time(), time()+7200));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400183 }
184
185 // ------------------------------------------------------------------------
186
187 public function test_days_in_month()
188 {
189 $this->assertEquals(30, days_in_month(06, 2005));
190 $this->assertEquals(28, days_in_month(02, 2011));
191 $this->assertEquals(29, days_in_month(02, 2012));
192 }
193
194 // ------------------------------------------------------------------------
195
196 public function test_local_to_gmt()
197 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300198 $this->assertEquals(
Andrey Andreevb089e152012-06-16 19:11:40 +0300199 mktime(
200 gmdate('G', $this->time), gmdate('i', $this->time), gmdate('s', $this->time),
201 gmdate('n', $this->time), gmdate('j', $this->time), gmdate('Y', $this->time)
Andrey Andreev99b782d2012-06-09 22:24:46 +0300202 ),
203 local_to_gmt($this->time)
204 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400205 }
206
207 // ------------------------------------------------------------------------
208
209 public function test_gmt_to_local()
210 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300211 $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400212 }
213
214 // ------------------------------------------------------------------------
215
216 public function test_mysql_to_unix()
217 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300218 $this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time)));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400219 }
220
221 // ------------------------------------------------------------------------
222
223 public function test_unix_to_human()
224 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300225 $this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time));
226 $this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us'));
227 $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 -0400228 }
229
230 // ------------------------------------------------------------------------
231
232 public function test_human_to_unix()
233 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700234 $date = '2000-12-31 10:00:00 PM';
Andrey Andreev99b782d2012-06-09 22:24:46 +0300235 $this->assertEquals(strtotime($date), human_to_unix($date));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700236 $this->assertFalse(human_to_unix());
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400237 }
238
239 // ------------------------------------------------------------------------
240
241 public function test_timezones()
242 {
243 $zones = array(
244 'UM12' => -12,
245 'UM11' => -11,
246 'UM10' => -10,
247 'UM95' => -9.5,
248 'UM9' => -9,
249 'UM8' => -8,
250 'UM7' => -7,
251 'UM6' => -6,
252 'UM5' => -5,
253 'UM45' => -4.5,
254 'UM4' => -4,
255 'UM35' => -3.5,
256 'UM3' => -3,
257 'UM2' => -2,
258 'UM1' => -1,
259 'UTC' => 0,
260 'UP1' => +1,
261 'UP2' => +2,
262 'UP3' => +3,
263 'UP35' => +3.5,
264 'UP4' => +4,
265 'UP45' => +4.5,
266 'UP5' => +5,
267 'UP55' => +5.5,
268 'UP575' => +5.75,
269 'UP6' => +6,
270 'UP65' => +6.5,
271 'UP7' => +7,
272 'UP8' => +8,
273 'UP875' => +8.75,
274 'UP9' => +9,
275 'UP95' => +9.5,
276 'UP10' => +10,
277 'UP105' => +10.5,
278 'UP11' => +11,
279 'UP115' => +11.5,
280 'UP12' => +12,
281 'UP1275' => +12.75,
282 'UP13' => +13,
283 'UP14' => +14
284 );
285
286 foreach ($zones AS $test => $expected)
287 {
288 $this->assertEquals($expected, timezones($test));
289 }
290
291 $this->assertArrayHasKey('UP3', timezones());
292 $this->assertEquals(0, timezones('non_existant'));
293 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300294
Andrey Andreev0df35852012-10-05 23:10:23 +0300295 // ------------------------------------------------------------------------
296
297 public function test_date_range()
298 {
299 $dates = array(
300 '29-01-2012', '30-01-2012', '31-01-2012',
301 '01-02-2012', '02-02-2012', '03-02-2012',
302 '04-02-2012', '05-02-2012', '06-02-2012',
303 '07-02-2012', '08-02-2012', '09-02-2012',
304 '10-02-2012', '11-02-2012', '12-02-2012',
305 '13-02-2012', '14-02-2012', '15-02-2012',
306 '16-02-2012', '17-02-2012', '18-02-2012',
307 '19-02-2012', '20-02-2012', '21-02-2012',
308 '22-02-2012', '23-02-2012', '24-02-2012',
309 '25-02-2012', '26-02-2012', '27-02-2012',
310 '28-02-2012', '29-02-2012', '01-03-2012'
311 );
312
313 $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), mktime(12, 0, 0, 3, 1, 2012), TRUE, 'd-m-Y'));
314 array_pop($dates);
315 $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), 31, FALSE, 'd-m-Y'));
316 }
317
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400318}
319
320/* End of file date_helper_test.php */