blob: b419418bf9641b429d0121118b43efadb326a13f [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
Andrey Andreev820d9cd2016-11-23 13:27:42 +020013 public function test_nice_date()
14 {
15 $this->assertEquals('2016-11-01', nice_date('201611', 'Y-m-d'));
16 $this->assertEquals('2016-11-23', nice_date('20161123', 'Y-m-d'));
17 }
18
19 // ------------------------------------------------------------------------
20
Taufan Aditya8749bc72012-03-11 05:43:45 +070021 public function test_now_local()
Eric Barnes5d1e32b2011-05-03 22:28:59 -040022 {
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030023 /*
24
Taufan Aditya8749bc72012-03-11 05:43:45 +070025 // This stub job, is simply to cater $config['time_reference']
Andrey Andreev878e23f2016-08-10 15:15:49 +030026 $config = $this->getMockBuilder('CI_Config')->getMock();
Taufan Aditya8749bc72012-03-11 05:43:45 +070027 $config->expects($this->any())
28 ->method('item')
29 ->will($this->returnValue('local'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030030
Taufan Aditya8749bc72012-03-11 05:43:45 +070031 // Add the stub to our test instance
32 $this->ci_instance_var('config', $config);
33
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030034 */
35
36 $this->ci_set_config('time_reference', 'local');
37
Andrey Andreev1b60fda2012-06-09 21:07:40 +030038 $this->assertEquals(time(), now());
Taufan Aditya8749bc72012-03-11 05:43:45 +070039 }
40
41 // ------------------------------------------------------------------------
42
Andrey Andreev0ddff312012-06-14 13:18:07 +030043 public function test_now_utc()
Taufan Aditya8749bc72012-03-11 05:43:45 +070044 {
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030045 /*
46
Taufan Aditya8749bc72012-03-11 05:43:45 +070047 // This stub job, is simply to cater $config['time_reference']
Andrey Andreev878e23f2016-08-10 15:15:49 +030048 $config = $this->getMockBuilder('CI_Config')->getMock();
Taufan Aditya8749bc72012-03-11 05:43:45 +070049 $config->expects($this->any())
50 ->method('item')
Andrey Andreev0ddff312012-06-14 13:18:07 +030051 ->will($this->returnValue('UTC'));
Andrey Andreev1b60fda2012-06-09 21:07:40 +030052
Taufan Aditya8749bc72012-03-11 05:43:45 +070053 // Add the stub to our stdClass
54 $this->ci_instance_var('config', $config);
55
Andrey Andreeve1c8ee72012-06-14 11:35:11 +030056 */
57
Andrey Andreev99b782d2012-06-09 22:24:46 +030058 $this->assertEquals(
Andrey Andreeva84055b2012-06-20 14:37:59 +030059 mktime(gmdate('G'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y')),
60 now('UTC')
Andrey Andreev99b782d2012-06-09 22:24:46 +030061 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040062 }
63
64 // ------------------------------------------------------------------------
65
66 public function test_mdate()
67 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030068 $this->assertEquals(
69 date('Y-m-d - h:i a', $this->time),
70 mdate('%Y-%m-%d - %h:%i %a', $this->time)
71 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040072 }
73
74 // ------------------------------------------------------------------------
75
76 public function test_standard_date_rfc822()
77 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030078 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030079 date(DATE_RFC822, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030080 standard_date('DATE_RFC822', $this->time)
81 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040082 }
83
84 // ------------------------------------------------------------------------
85
86 public function test_standard_date_atom()
87 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030088 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030089 date(DATE_ATOM, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +030090 standard_date('DATE_ATOM', $this->time)
91 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -040092 }
93
94 // ------------------------------------------------------------------------
95
96 public function test_standard_date_cookie()
97 {
Andrey Andreev99b782d2012-06-09 22:24:46 +030098 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +030099 date(DATE_COOKIE, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300100 standard_date('DATE_COOKIE', $this->time)
101 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400102 }
103
104 // ------------------------------------------------------------------------
105
106 public function test_standard_date_iso8601()
107 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300108 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300109 date(DATE_ISO8601, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300110 standard_date('DATE_ISO8601', $this->time)
111 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400112 }
113
114 // ------------------------------------------------------------------------
115
116 public function test_standard_date_rfc850()
117 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300118 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300119 date(DATE_RFC850, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300120 standard_date('DATE_RFC850', $this->time)
121 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400122 }
123
124 // ------------------------------------------------------------------------
125
126 public function test_standard_date_rfc1036()
127 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300128 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300129 date(DATE_RFC1036, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300130 standard_date('DATE_RFC1036', $this->time)
131 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400132 }
133
134 // ------------------------------------------------------------------------
135
136 public function test_standard_date_rfc1123()
137 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300138 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300139 date(DATE_RFC1123, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300140 standard_date('DATE_RFC1123', $this->time)
141 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400142 }
143
144 // ------------------------------------------------------------------------
145
146 public function test_standard_date_rfc2822()
147 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300148 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300149 date(DATE_RFC2822, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300150 standard_date('DATE_RFC2822', $this->time)
151 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400152 }
153
154 // ------------------------------------------------------------------------
155
156 public function test_standard_date_rss()
157 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300158 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300159 date(DATE_RSS, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300160 standard_date('DATE_RSS', $this->time)
161 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400162 }
163
164 // ------------------------------------------------------------------------
165
166 public function test_standard_date_w3c()
167 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300168 $this->assertEquals(
Andrey Andreevdd6f32b2012-07-04 10:08:54 +0300169 date(DATE_W3C, $this->time),
Andrey Andreev99b782d2012-06-09 22:24:46 +0300170 standard_date('DATE_W3C', $this->time)
171 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400172 }
173
174 // ------------------------------------------------------------------------
175
176 public function test_timespan()
177 {
dchill427ecc5cd2012-10-12 16:25:51 -0400178 $this->ci_vfs_clone('system/language/english/date_lang.php');
179
Taufan Aditya8749bc72012-03-11 05:43:45 +0700180 $loader_cls = $this->ci_core_class('load');
181 $this->ci_instance_var('load', new $loader_cls);
182
183 $lang_cls = $this->ci_core_class('lang');
184 $this->ci_instance_var('lang', new $lang_cls);
185
186 $this->assertEquals('1 Second', timespan(time(), time()+1));
187 $this->assertEquals('1 Minute', timespan(time(), time()+60));
188 $this->assertEquals('1 Hour', timespan(time(), time()+3600));
189 $this->assertEquals('2 Hours', timespan(time(), time()+7200));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400190 }
191
192 // ------------------------------------------------------------------------
193
194 public function test_days_in_month()
195 {
196 $this->assertEquals(30, days_in_month(06, 2005));
197 $this->assertEquals(28, days_in_month(02, 2011));
198 $this->assertEquals(29, days_in_month(02, 2012));
199 }
200
201 // ------------------------------------------------------------------------
202
203 public function test_local_to_gmt()
204 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300205 $this->assertEquals(
Andrey Andreevb089e152012-06-16 19:11:40 +0300206 mktime(
207 gmdate('G', $this->time), gmdate('i', $this->time), gmdate('s', $this->time),
208 gmdate('n', $this->time), gmdate('j', $this->time), gmdate('Y', $this->time)
Andrey Andreev99b782d2012-06-09 22:24:46 +0300209 ),
210 local_to_gmt($this->time)
211 );
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400212 }
213
214 // ------------------------------------------------------------------------
215
216 public function test_gmt_to_local()
217 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300218 $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400219 }
220
221 // ------------------------------------------------------------------------
222
223 public function test_mysql_to_unix()
224 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300225 $this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time)));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400226 }
227
228 // ------------------------------------------------------------------------
229
230 public function test_unix_to_human()
231 {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300232 $this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time));
233 $this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us'));
234 $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 -0400235 }
236
237 // ------------------------------------------------------------------------
238
239 public function test_human_to_unix()
240 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700241 $date = '2000-12-31 10:00:00 PM';
Andrey Andreev99b782d2012-06-09 22:24:46 +0300242 $this->assertEquals(strtotime($date), human_to_unix($date));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700243 $this->assertFalse(human_to_unix());
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400244 }
245
246 // ------------------------------------------------------------------------
247
248 public function test_timezones()
249 {
250 $zones = array(
251 'UM12' => -12,
252 'UM11' => -11,
253 'UM10' => -10,
254 'UM95' => -9.5,
255 'UM9' => -9,
256 'UM8' => -8,
257 'UM7' => -7,
258 'UM6' => -6,
259 'UM5' => -5,
260 'UM45' => -4.5,
261 'UM4' => -4,
262 'UM35' => -3.5,
263 'UM3' => -3,
264 'UM2' => -2,
265 'UM1' => -1,
266 'UTC' => 0,
267 'UP1' => +1,
268 'UP2' => +2,
269 'UP3' => +3,
270 'UP35' => +3.5,
271 'UP4' => +4,
272 'UP45' => +4.5,
273 'UP5' => +5,
274 'UP55' => +5.5,
275 'UP575' => +5.75,
276 'UP6' => +6,
277 'UP65' => +6.5,
278 'UP7' => +7,
279 'UP8' => +8,
280 'UP875' => +8.75,
281 'UP9' => +9,
282 'UP95' => +9.5,
283 'UP10' => +10,
284 'UP105' => +10.5,
285 'UP11' => +11,
286 'UP115' => +11.5,
287 'UP12' => +12,
288 'UP1275' => +12.75,
289 'UP13' => +13,
290 'UP14' => +14
291 );
292
293 foreach ($zones AS $test => $expected)
294 {
295 $this->assertEquals($expected, timezones($test));
296 }
297
298 $this->assertArrayHasKey('UP3', timezones());
Andrey Andreev71d8f722017-01-17 12:01:00 +0200299 $this->assertEquals(0, timezones('non_existent'));
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400300 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300301
Andrey Andreev0df35852012-10-05 23:10:23 +0300302 // ------------------------------------------------------------------------
303
304 public function test_date_range()
305 {
306 $dates = array(
307 '29-01-2012', '30-01-2012', '31-01-2012',
308 '01-02-2012', '02-02-2012', '03-02-2012',
309 '04-02-2012', '05-02-2012', '06-02-2012',
310 '07-02-2012', '08-02-2012', '09-02-2012',
311 '10-02-2012', '11-02-2012', '12-02-2012',
312 '13-02-2012', '14-02-2012', '15-02-2012',
313 '16-02-2012', '17-02-2012', '18-02-2012',
314 '19-02-2012', '20-02-2012', '21-02-2012',
315 '22-02-2012', '23-02-2012', '24-02-2012',
316 '25-02-2012', '26-02-2012', '27-02-2012',
317 '28-02-2012', '29-02-2012', '01-03-2012'
318 );
319
320 $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), mktime(12, 0, 0, 3, 1, 2012), TRUE, 'd-m-Y'));
321 array_pop($dates);
322 $this->assertEquals($dates, date_range(mktime(12, 0, 0, 1, 29, 2012), 31, FALSE, 'd-m-Y'));
323 }
324
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400325}