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 | |
| 9 | $this->time = time(); |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 10 | } |
| 11 | |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 12 | // ------------------------------------------------------------------------ |
| 13 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 14 | public function test_now_local() |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 15 | { |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame^] | 16 | /* |
| 17 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 18 | // 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 Andreev | 1b60fda | 2012-06-09 21:07:40 +0300 | [diff] [blame] | 23 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 24 | // Add the stub to our test instance |
| 25 | $this->ci_instance_var('config', $config); |
| 26 | |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame^] | 27 | */ |
| 28 | |
| 29 | $this->ci_set_config('time_reference', 'local'); |
| 30 | |
Andrey Andreev | 1b60fda | 2012-06-09 21:07:40 +0300 | [diff] [blame] | 31 | $this->assertEquals(time(), now()); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | |
| 36 | public function test_now_gmt() |
| 37 | { |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame^] | 38 | /* |
| 39 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 40 | // This stub job, is simply to cater $config['time_reference'] |
| 41 | $config = $this->getMock('CI_Config'); |
| 42 | $config->expects($this->any()) |
| 43 | ->method('item') |
| 44 | ->will($this->returnValue('gmt')); |
Andrey Andreev | 1b60fda | 2012-06-09 21:07:40 +0300 | [diff] [blame] | 45 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 46 | // Add the stub to our stdClass |
| 47 | $this->ci_instance_var('config', $config); |
| 48 | |
Andrey Andreev | e1c8ee7 | 2012-06-14 11:35:11 +0300 | [diff] [blame^] | 49 | */ |
| 50 | |
| 51 | $this->ci_set_config('time_reference', 'gmt'); |
| 52 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 53 | $t = time(); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 54 | $this->assertEquals( |
| 55 | mktime(gmdate('H', $t), gmdate('i', $t), gmdate('s', $t), gmdate('m', $t), gmdate('d', $t), gmdate('Y', $t)), |
| 56 | now() |
| 57 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // ------------------------------------------------------------------------ |
| 61 | |
| 62 | public function test_mdate() |
| 63 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 64 | $this->assertEquals( |
| 65 | date('Y-m-d - h:i a', $this->time), |
| 66 | mdate('%Y-%m-%d - %h:%i %a', $this->time) |
| 67 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | |
| 72 | public function test_standard_date_rfc822() |
| 73 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 74 | $this->assertEquals( |
| 75 | date('D, d M y H:i:s O', $this->time), |
| 76 | standard_date('DATE_RFC822', $this->time) |
| 77 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | |
| 82 | public function test_standard_date_atom() |
| 83 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 84 | $this->assertEquals( |
| 85 | date("Y-m-d\TH:i:sO", $this->time), |
| 86 | standard_date('DATE_ATOM', $this->time) |
| 87 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // ------------------------------------------------------------------------ |
| 91 | |
| 92 | public function test_standard_date_cookie() |
| 93 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 94 | $this->assertEquals( |
| 95 | date("l, d-M-y H:i:s \U\T\C", $this->time), |
| 96 | standard_date('DATE_COOKIE', $this->time) |
| 97 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // ------------------------------------------------------------------------ |
| 101 | |
| 102 | public function test_standard_date_iso8601() |
| 103 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 104 | $this->assertEquals( |
| 105 | date("Y-m-d\TH:i:sO", $this->time), |
| 106 | standard_date('DATE_ISO8601', $this->time) |
| 107 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // ------------------------------------------------------------------------ |
| 111 | |
| 112 | public function test_standard_date_rfc850() |
| 113 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 114 | $this->assertEquals( |
| 115 | date("l, d-M-y H:i:s \U\T\C", $this->time), |
| 116 | standard_date('DATE_RFC850', $this->time) |
| 117 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // ------------------------------------------------------------------------ |
| 121 | |
| 122 | public function test_standard_date_rfc1036() |
| 123 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 124 | $this->assertEquals( |
| 125 | date('D, d M y H:i:s O', $this->time), |
| 126 | standard_date('DATE_RFC1036', $this->time) |
| 127 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // ------------------------------------------------------------------------ |
| 131 | |
| 132 | public function test_standard_date_rfc1123() |
| 133 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 134 | $this->assertEquals( |
| 135 | date('D, d M Y H:i:s O', $this->time), |
| 136 | standard_date('DATE_RFC1123', $this->time) |
| 137 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // ------------------------------------------------------------------------ |
| 141 | |
| 142 | public function test_standard_date_rfc2822() |
| 143 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 144 | $this->assertEquals( |
| 145 | date('D, d M Y H:i:s O', $this->time), |
| 146 | standard_date('DATE_RFC2822', $this->time) |
| 147 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // ------------------------------------------------------------------------ |
| 151 | |
| 152 | public function test_standard_date_rss() |
| 153 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 154 | $this->assertEquals( |
| 155 | date('D, d M Y H:i:s O', $this->time), |
| 156 | standard_date('DATE_RSS', $this->time) |
| 157 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // ------------------------------------------------------------------------ |
| 161 | |
| 162 | public function test_standard_date_w3c() |
| 163 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 164 | $this->assertEquals( |
| 165 | date("Y-m-d\TH:i:sO", $this->time), |
| 166 | standard_date('DATE_W3C', $this->time) |
| 167 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // ------------------------------------------------------------------------ |
| 171 | |
| 172 | public function test_timespan() |
| 173 | { |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 174 | $loader_cls = $this->ci_core_class('load'); |
| 175 | $this->ci_instance_var('load', new $loader_cls); |
| 176 | |
| 177 | $lang_cls = $this->ci_core_class('lang'); |
| 178 | $this->ci_instance_var('lang', new $lang_cls); |
| 179 | |
| 180 | $this->assertEquals('1 Second', timespan(time(), time()+1)); |
| 181 | $this->assertEquals('1 Minute', timespan(time(), time()+60)); |
| 182 | $this->assertEquals('1 Hour', timespan(time(), time()+3600)); |
| 183 | $this->assertEquals('2 Hours', timespan(time(), time()+7200)); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // ------------------------------------------------------------------------ |
| 187 | |
| 188 | public function test_days_in_month() |
| 189 | { |
| 190 | $this->assertEquals(30, days_in_month(06, 2005)); |
| 191 | $this->assertEquals(28, days_in_month(02, 2011)); |
| 192 | $this->assertEquals(29, days_in_month(02, 2012)); |
| 193 | } |
| 194 | |
| 195 | // ------------------------------------------------------------------------ |
| 196 | |
| 197 | public function test_local_to_gmt() |
| 198 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 199 | $this->assertEquals( |
| 200 | mktime( |
| 201 | gmdate('H', $this->time), gmdate('i', $this->time), gmdate('s', $this->time), |
| 202 | gmdate('m', $this->time), gmdate('d', $this->time), gmdate('Y', $this->time) |
| 203 | ), |
| 204 | local_to_gmt($this->time) |
| 205 | ); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // ------------------------------------------------------------------------ |
| 209 | |
| 210 | public function test_gmt_to_local() |
| 211 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 212 | $this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE)); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // ------------------------------------------------------------------------ |
| 216 | |
| 217 | public function test_mysql_to_unix() |
| 218 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 219 | $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] | 220 | } |
| 221 | |
| 222 | // ------------------------------------------------------------------------ |
| 223 | |
| 224 | public function test_unix_to_human() |
| 225 | { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 226 | $this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time)); |
| 227 | $this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us')); |
| 228 | $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] | 229 | } |
| 230 | |
| 231 | // ------------------------------------------------------------------------ |
| 232 | |
| 233 | public function test_human_to_unix() |
| 234 | { |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 235 | $date = '2000-12-31 10:00:00 PM'; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 236 | $this->assertEquals(strtotime($date), human_to_unix($date)); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 237 | $this->assertFalse(human_to_unix()); |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // ------------------------------------------------------------------------ |
| 241 | |
| 242 | public function test_timezones() |
| 243 | { |
| 244 | $zones = array( |
| 245 | 'UM12' => -12, |
| 246 | 'UM11' => -11, |
| 247 | 'UM10' => -10, |
| 248 | 'UM95' => -9.5, |
| 249 | 'UM9' => -9, |
| 250 | 'UM8' => -8, |
| 251 | 'UM7' => -7, |
| 252 | 'UM6' => -6, |
| 253 | 'UM5' => -5, |
| 254 | 'UM45' => -4.5, |
| 255 | 'UM4' => -4, |
| 256 | 'UM35' => -3.5, |
| 257 | 'UM3' => -3, |
| 258 | 'UM2' => -2, |
| 259 | 'UM1' => -1, |
| 260 | 'UTC' => 0, |
| 261 | 'UP1' => +1, |
| 262 | 'UP2' => +2, |
| 263 | 'UP3' => +3, |
| 264 | 'UP35' => +3.5, |
| 265 | 'UP4' => +4, |
| 266 | 'UP45' => +4.5, |
| 267 | 'UP5' => +5, |
| 268 | 'UP55' => +5.5, |
| 269 | 'UP575' => +5.75, |
| 270 | 'UP6' => +6, |
| 271 | 'UP65' => +6.5, |
| 272 | 'UP7' => +7, |
| 273 | 'UP8' => +8, |
| 274 | 'UP875' => +8.75, |
| 275 | 'UP9' => +9, |
| 276 | 'UP95' => +9.5, |
| 277 | 'UP10' => +10, |
| 278 | 'UP105' => +10.5, |
| 279 | 'UP11' => +11, |
| 280 | 'UP115' => +11.5, |
| 281 | 'UP12' => +12, |
| 282 | 'UP1275' => +12.75, |
| 283 | 'UP13' => +13, |
| 284 | 'UP14' => +14 |
| 285 | ); |
| 286 | |
| 287 | foreach ($zones AS $test => $expected) |
| 288 | { |
| 289 | $this->assertEquals($expected, timezones($test)); |
| 290 | } |
| 291 | |
| 292 | $this->assertArrayHasKey('UP3', timezones()); |
| 293 | $this->assertEquals(0, timezones('non_existant')); |
| 294 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 295 | |
Eric Barnes | 5d1e32b | 2011-05-03 22:28:59 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* End of file date_helper_test.php */ |