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