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