blob: c4f99a97c3d77ae3f4803618776c8a81ab8e3cf1 [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 $this->time = time();
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07009 }
10
Eric Barnes5d1e32b2011-05-03 22:28:59 -040011 // ------------------------------------------------------------------------
12
Taufan Aditya8749bc72012-03-11 05:43:45 +070013 public function test_now_local()
Eric Barnes5d1e32b2011-05-03 22:28:59 -040014 {
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030015 /*
16
Taufan Aditya8749bc72012-03-11 05:43:45 +070017 // This stub job, is simply to cater $config['time_reference']
Andrey Andreev878e23f2016-08-10 15:15:49 +030018 $config = $this->getMockBuilder('CI_Config')->getMock();
Taufan Aditya8749bc72012-03-11 05:43:45 +070019 $config->expects($this->any())
20 ->method('item')
21 ->will($this->returnValue('local'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030022
Taufan Aditya8749bc72012-03-11 05:43:45 +070023 // Add the stub to our test instance
24 $this->ci_instance_var('config', $config);
25
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030026 */
27
28 $this->ci_set_config('time_reference', 'local');
29
Andrey Andreev1b60fda2012-06-09 21:07:40 +030030 $this->assertEquals(time(), now());
Taufan Aditya8749bc72012-03-11 05:43:45 +070031 }
32
33 // ------------------------------------------------------------------------
34
Andrey Andreev0ddff312012-06-14 13:18:07 +030035 public function test_now_utc()
Taufan Aditya8749bc72012-03-11 05:43:45 +070036 {
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030037 /*
38
Taufan Aditya8749bc72012-03-11 05:43:45 +070039 // This stub job, is simply to cater $config['time_reference']
Andrey Andreev878e23f2016-08-10 15:15:49 +030040 $config = $this->getMockBuilder('CI_Config')->getMock();
Taufan Aditya8749bc72012-03-11 05:43:45 +070041 $config->expects($this->any())
42 ->method('item')
Andrey Andreev0ddff312012-06-14 13:18:07 +030043 ->will($this->returnValue('UTC'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030044
Taufan Aditya8749bc72012-03-11 05:43:45 +070045 // Add the stub to our stdClass
46 $this->ci_instance_var('config', $config);
47
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030048 */
49
Andrey Andreev99b782d2012-06-09 22:24:46 +030050 $this->assertEquals(
Andrey Andreeva84055b2012-06-20 14:37:59 +030051 mktime(gmdate('G'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y')),
52 now('UTC')
Andrey Andreev99b782d2012-06-09 22:24:46 +030053 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040054 }
55
56 // ------------------------------------------------------------------------
57
58 public function test_mdate()
59 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030060 $this->assertEquals(
61 date('Y-m-d - h:i a', $this->time),
62 mdate('%Y-%m-%d - %h:%i %a', $this->time)
63 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040064 }
65
66 // ------------------------------------------------------------------------
67
68 public function test_standard_date_rfc822()
69 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030070 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030071 date(DATE_RFC822, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030072 standard_date('DATE_RFC822', $this->time)
73 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040074 }
75
76 // ------------------------------------------------------------------------
77
78 public function test_standard_date_atom()
79 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030080 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030081 date(DATE_ATOM, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030082 standard_date('DATE_ATOM', $this->time)
83 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040084 }
85
86 // ------------------------------------------------------------------------
87
88 public function test_standard_date_cookie()
89 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030090 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030091 date(DATE_COOKIE, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030092 standard_date('DATE_COOKIE', $this->time)
93 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040094 }
95
96 // ------------------------------------------------------------------------
97
98 public function test_standard_date_iso8601()
99 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300100 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300101 date(DATE_ISO8601, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300102 standard_date('DATE_ISO8601', $this->time)
103 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400104 }
105
106 // ------------------------------------------------------------------------
107
108 public function test_standard_date_rfc850()
109 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300110 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300111 date(DATE_RFC850, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300112 standard_date('DATE_RFC850', $this->time)
113 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400114 }
115
116 // ------------------------------------------------------------------------
117
118 public function test_standard_date_rfc1036()
119 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300120 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300121 date(DATE_RFC1036, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300122 standard_date('DATE_RFC1036', $this->time)
123 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400124 }
125
126 // ------------------------------------------------------------------------
127
128 public function test_standard_date_rfc1123()
129 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300130 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300131 date(DATE_RFC1123, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300132 standard_date('DATE_RFC1123', $this->time)
133 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400134 }
135
136 // ------------------------------------------------------------------------
137
138 public function test_standard_date_rfc2822()
139 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300140 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300141 date(DATE_RFC2822, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300142 standard_date('DATE_RFC2822', $this->time)
143 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400144 }
145
146 // ------------------------------------------------------------------------
147
148 public function test_standard_date_rss()
149 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300150 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300151 date(DATE_RSS, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300152 standard_date('DATE_RSS', $this->time)
153 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400154 }
155
156 // ------------------------------------------------------------------------
157
158 public function test_standard_date_w3c()
159 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300160 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300161 date(DATE_W3C, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300162 standard_date('DATE_W3C', $this->time)
163 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400164 }
165
166 // ------------------------------------------------------------------------
167
168 public function test_timespan()
169 {
dchill427ecc5cd2012-10-12 16:25:51 -0400170 $this->ci_vfs_clone('system/language/english/date_lang.php');
171
Taufan Aditya8749bc72012-03-11 05:43:45 +0700172 $loader_cls = $this->ci_core_class('load');
173 $this->ci_instance_var('load', new $loader_cls);
174
175 $lang_cls = $this->ci_core_class('lang');
176 $this->ci_instance_var('lang', new $lang_cls);
177
178 $this->assertEquals('1 Second', timespan(time(), time()+1));
179 $this->assertEquals('1 Minute', timespan(time(), time()+60));
180 $this->assertEquals('1 Hour', timespan(time(), time()+3600));
181 $this->assertEquals('2 Hours', timespan(time(), time()+7200));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400182 }
183
184 // ------------------------------------------------------------------------
185
186 public function test_days_in_month()
187 {
188 $this->assertEquals(30, days_in_month(06, 2005));
189 $this->assertEquals(28, days_in_month(02, 2011));
190 $this->assertEquals(29, days_in_month(02, 2012));
191 }
192
193 // ------------------------------------------------------------------------
194
195 public function test_local_to_gmt()
196 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300197 $this->assertEquals(
Andrey Andreevb089e152012-06-16 19:11:40 +0300198 mktime(
199 gmdate('G', $this->time), gmdate('i', $this->time), gmdate('s', $this->time),
200 gmdate('n', $this->time), gmdate('j', $this->time), gmdate('Y', $this->time)
Andrey Andreev99b782d2012-06-09 22:24:46 +0300201 ),
202 local_to_gmt($this->time)
203 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400204 }
205
206 // ------------------------------------------------------------------------
207
208 public function test_gmt_to_local()
209 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300210 $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400211 }
212
213 // ------------------------------------------------------------------------
214
215 public function test_mysql_to_unix()
216 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300217 $this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time)));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400218 }
219
220 // ------------------------------------------------------------------------
221
222 public function test_unix_to_human()
223 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300224 $this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time));
225 $this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us'));
226 $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 -0400227 }
228
229 // ------------------------------------------------------------------------
230
231 public function test_human_to_unix()
232 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700233 $date = '2000-12-31 10:00:00 PM';
Andrey Andreev99b782d2012-06-09 22:24:46 +0300234 $this->assertEquals(strtotime($date), human_to_unix($date));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700235 $this->assertFalse(human_to_unix());
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400236 }
237
238 // ------------------------------------------------------------------------
239
240 public function test_timezones()
241 {
242 $zones = array(
243 'UM12' => -12,
244 'UM11' => -11,
245 'UM10' => -10,
246 'UM95' => -9.5,
247 'UM9' => -9,
248 'UM8' => -8,
249 'UM7' => -7,
250 'UM6' => -6,
251 'UM5' => -5,
252 'UM45' => -4.5,
253 'UM4' => -4,
254 'UM35' => -3.5,
255 'UM3' => -3,
256 'UM2' => -2,
257 'UM1' => -1,
258 'UTC' => 0,
259 'UP1' => +1,
260 'UP2' => +2,
261 'UP3' => +3,
262 'UP35' => +3.5,
263 'UP4' => +4,
264 'UP45' => +4.5,
265 'UP5' => +5,
266 'UP55' => +5.5,
267 'UP575' => +5.75,
268 'UP6' => +6,
269 'UP65' => +6.5,
270 'UP7' => +7,
271 'UP8' => +8,
272 'UP875' => +8.75,
273 'UP9' => +9,
274 'UP95' => +9.5,
275 'UP10' => +10,
276 'UP105' => +10.5,
277 'UP11' => +11,
278 'UP115' => +11.5,
279 'UP12' => +12,
280 'UP1275' => +12.75,
281 'UP13' => +13,
282 'UP14' => +14
283 );
284
285 foreach ($zones AS $test => $expected)
286 {
287 $this->assertEquals($expected, timezones($test));
288 }
289
290 $this->assertArrayHasKey('UP3', timezones());
291 $this->assertEquals(0, timezones('non_existant'));
292 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300293
Andrey Andreev0df35852012-10-05 23:10:23 +0300294 // ------------------------------------------------------------------------
295
296 public function test_date_range()
297 {
298 $dates = array(
299 '29-01-2012', '30-01-2012', '31-01-2012',
300 '01-02-2012', '02-02-2012', '03-02-2012',
301 '04-02-2012', '05-02-2012', '06-02-2012',
302 '07-02-2012', '08-02-2012', '09-02-2012',
303 '10-02-2012', '11-02-2012', '12-02-2012',
304 '13-02-2012', '14-02-2012', '15-02-2012',
305 '16-02-2012', '17-02-2012', '18-02-2012',
306 '19-02-2012', '20-02-2012', '21-02-2012',
307 '22-02-2012', '23-02-2012', '24-02-2012',
308 '25-02-2012', '26-02-2012', '27-02-2012',
309 '28-02-2012', '29-02-2012', '01-03-2012'
310 );
311
312 $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), mktime(12, 0, 0, 3, 1, 2012), TRUE, 'd-m-Y'));
313 array_pop($dates);
314 $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), 31, FALSE, 'd-m-Y'));
315 }
316
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400317}