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