Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 1 | <?php |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 2 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 3 | class Date_helper_test extends CI_TestCase { |
| 4 | |
| 5 | public function set_up() |
| 6 | { |
| 7 | $this->helper('date'); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 8 | $this->time = time(); |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 9 | } |
| 10 | |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 11 | // ------------------------------------------------------------------------ |
| 12 | |
Andrey Andreev | 820d9cd | 2016-11-23 13:27:42 +0200 | [diff] [blame] | 13 | 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 Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 21 | public function test_now_local() |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 22 | { |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame] | 23 | /* |
| 24 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 25 | // This stub job, is simply to cater $config['time_reference'] |
Andrey Andreev | 878e23f | 2016-08-10 15:15:49 +0300 | [diff] [blame] | 26 | $config = $this->getMockBuilder('CI_Config')->getMock(); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 27 | $config->expects($this->any()) |
| 28 | ->method('item') |
| 29 | ->will($this->returnValue('local')); |
Andrey Andreev | 1b60fda | 2012-06-09 21:07:40 +0300 | [diff] [blame] | 30 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 31 | // Add the stub to our test instance |
| 32 | $this->ci_instance_var('config', $config); |
| 33 | |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | $this->ci_set_config('time_reference', 'local'); |
| 37 | |
Andrey Andreev | 1b60fda | 2012-06-09 21:07:40 +0300 | [diff] [blame] | 38 | $this->assertEquals(time(), now()); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
Andrey Andreev | 0ddff31 | 2012-06-14 13:18:07 +0300 | [diff] [blame] | 43 | public function test_now_utc() |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 44 | { |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame] | 45 | /* |
| 46 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 47 | // This stub job, is simply to cater $config['time_reference'] |
Andrey Andreev | 878e23f | 2016-08-10 15:15:49 +0300 | [diff] [blame] | 48 | $config = $this->getMockBuilder('CI_Config')->getMock(); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 49 | $config->expects($this->any()) |
| 50 | ->method('item') |
Andrey Andreev | 0ddff31 | 2012-06-14 13:18:07 +0300 | [diff] [blame] | 51 | ->will($this->returnValue('UTC')); |
Andrey Andreev | 1b60fda | 2012-06-09 21:07:40 +0300 | [diff] [blame] | 52 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 53 | // Add the stub to our stdClass |
| 54 | $this->ci_instance_var('config', $config); |
| 55 | |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame] | 56 | */ |
| 57 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 58 | $this->assertEquals( |
Andrey Andreev | a84055b | 2012-06-20 14:37:59 +0300 | [diff] [blame] | 59 | mktime(gmdate('G'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y')), |
| 60 | now('UTC') |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 61 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | public function test_mdate() |
| 67 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 68 | $this->assertEquals( |
| 69 | date('Y-m-d - h:i a', $this->time), |
| 70 | mdate('%Y-%m-%d - %h:%i %a', $this->time) |
| 71 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // ------------------------------------------------------------------------ |
| 75 | |
| 76 | public function test_standard_date_rfc822() |
| 77 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 78 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 79 | date(DATE_RFC822, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 80 | standard_date('DATE_RFC822', $this->time) |
| 81 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | |
| 86 | public function test_standard_date_atom() |
| 87 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 88 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 89 | date(DATE_ATOM, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 90 | standard_date('DATE_ATOM', $this->time) |
| 91 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // ------------------------------------------------------------------------ |
| 95 | |
| 96 | public function test_standard_date_cookie() |
| 97 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 98 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 99 | date(DATE_COOKIE, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 100 | standard_date('DATE_COOKIE', $this->time) |
| 101 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // ------------------------------------------------------------------------ |
| 105 | |
| 106 | public function test_standard_date_iso8601() |
| 107 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 108 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 109 | date(DATE_ISO8601, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 110 | standard_date('DATE_ISO8601', $this->time) |
| 111 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | public function test_standard_date_rfc850() |
| 117 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 118 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 119 | date(DATE_RFC850, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 120 | standard_date('DATE_RFC850', $this->time) |
| 121 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // ------------------------------------------------------------------------ |
| 125 | |
| 126 | public function test_standard_date_rfc1036() |
| 127 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 128 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 129 | date(DATE_RFC1036, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 130 | standard_date('DATE_RFC1036', $this->time) |
| 131 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------------ |
| 135 | |
| 136 | public function test_standard_date_rfc1123() |
| 137 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 138 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 139 | date(DATE_RFC1123, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 140 | standard_date('DATE_RFC1123', $this->time) |
| 141 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // ------------------------------------------------------------------------ |
| 145 | |
| 146 | public function test_standard_date_rfc2822() |
| 147 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 148 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 149 | date(DATE_RFC2822, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 150 | standard_date('DATE_RFC2822', $this->time) |
| 151 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------ |
| 155 | |
| 156 | public function test_standard_date_rss() |
| 157 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 158 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 159 | date(DATE_RSS, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 160 | standard_date('DATE_RSS', $this->time) |
| 161 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // ------------------------------------------------------------------------ |
| 165 | |
| 166 | public function test_standard_date_w3c() |
| 167 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 168 | $this->assertEquals( |
Andrey Andreev | dd6f32b | 2012-07-04 10:08:54 +0300 | [diff] [blame] | 169 | date(DATE_W3C, $this->time), |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 170 | standard_date('DATE_W3C', $this->time) |
| 171 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // ------------------------------------------------------------------------ |
| 175 | |
| 176 | public function test_timespan() |
| 177 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 178 | $this->ci_vfs_clone('system/language/english/date_lang.php'); |
| 179 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 180 | $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 Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 190 | } |
| 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 Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 205 | $this->assertEquals( |
Andrey Andreev | b089e15 | 2012-06-16 19:11:40 +0300 | [diff] [blame] | 206 | 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 Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 209 | ), |
| 210 | local_to_gmt($this->time) |
| 211 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // ------------------------------------------------------------------------ |
| 215 | |
| 216 | public function test_gmt_to_local() |
| 217 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 218 | $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE)); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // ------------------------------------------------------------------------ |
| 222 | |
| 223 | public function test_mysql_to_unix() |
| 224 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 225 | $this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time))); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // ------------------------------------------------------------------------ |
| 229 | |
| 230 | public function test_unix_to_human() |
| 231 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 232 | $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 Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // ------------------------------------------------------------------------ |
| 238 | |
| 239 | public function test_human_to_unix() |
| 240 | { |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 241 | $date = '2000-12-31 10:00:00 PM'; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 242 | $this->assertEquals(strtotime($date), human_to_unix($date)); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 243 | $this->assertFalse(human_to_unix()); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 244 | } |
| 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 Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 299 | $this->assertEquals(0, timezones('non_existent')); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 300 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 301 | |
Andrey Andreev | 0df3585 | 2012-10-05 23:10:23 +0300 | [diff] [blame] | 302 | // ------------------------------------------------------------------------ |
| 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 Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 325 | } |