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