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