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