summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/helpers/date_helper_test.php
blob: 4b747b8648816b2fd604dded5a50af83daea98b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php

class Date_helper_test extends CI_TestCase {

	public function set_up()
	{
		$this->helper('date');

		$this->time = time();
	}

	// ------------------------------------------------------------------------

	public function test_now_local()
	{
		// This stub job, is simply to cater $config['time_reference']
		$config = $this->getMock('CI_Config');
		$config->expects($this->any())
			   ->method('item')
			   ->will($this->returnValue('local'));

		// Add the stub to our test instance
		$this->ci_instance_var('config', $config);

		$this->assertEquals(time(), now());
	}

	// ------------------------------------------------------------------------

	public function test_now_gmt()
	{
		// This stub job, is simply to cater $config['time_reference']
		$config = $this->getMock('CI_Config');
		$config->expects($this->any())
			   ->method('item')
			   ->will($this->returnValue('gmt'));

		// Add the stub to our stdClass
		$this->ci_instance_var('config', $config);

		$t = time();
		$this->assertEquals(
			mktime(gmdate('H', $t), gmdate('i', $t), gmdate('s', $t), gmdate('m', $t), gmdate('d', $t), gmdate('Y', $t)),
			now()
		);
	}

	// ------------------------------------------------------------------------

	public function test_mdate()
	{
		$this->assertEquals(
			date('Y-m-d - h:i a', $this->time),
			mdate('%Y-%m-%d - %h:%i %a', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_rfc822()
	{
		$this->assertEquals(
			date('D, d M y H:i:s O', $this->time),
			standard_date('DATE_RFC822', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_atom()
	{
		$this->assertEquals(
			date("Y-m-d\TH:i:sO", $this->time),
			standard_date('DATE_ATOM', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_cookie()
	{
		$this->assertEquals(
			date("l, d-M-y H:i:s \U\T\C", $this->time),
			standard_date('DATE_COOKIE', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_iso8601()
	{
		$this->assertEquals(
			date("Y-m-d\TH:i:sO", $this->time),
			standard_date('DATE_ISO8601', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_rfc850()
	{
		$this->assertEquals(
			date("l, d-M-y H:i:s \U\T\C", $this->time),
			standard_date('DATE_RFC850', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_rfc1036()
	{
		$this->assertEquals(
			date('D, d M y H:i:s O', $this->time),
			standard_date('DATE_RFC1036', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_rfc1123()
	{
		$this->assertEquals(
			date('D, d M Y H:i:s O', $this->time),
			standard_date('DATE_RFC1123', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_rfc2822()
	{
		$this->assertEquals(
			date('D, d M Y H:i:s O', $this->time),
			standard_date('DATE_RFC2822', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_rss()
	{
		$this->assertEquals(
			date('D, d M Y H:i:s O', $this->time),
			standard_date('DATE_RSS', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_standard_date_w3c()
	{
		$this->assertEquals(
			date("Y-m-d\TH:i:sO", $this->time),
			standard_date('DATE_W3C', $this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_timespan()
	{
		$loader_cls = $this->ci_core_class('load');
		$this->ci_instance_var('load', new $loader_cls);

		$lang_cls = $this->ci_core_class('lang');
		$this->ci_instance_var('lang', new $lang_cls);

		$this->assertEquals('1 Second', timespan(time(), time()+1));
		$this->assertEquals('1 Minute', timespan(time(), time()+60));
		$this->assertEquals('1 Hour', timespan(time(), time()+3600));
		$this->assertEquals('2 Hours', timespan(time(), time()+7200));
	}

	// ------------------------------------------------------------------------

	public function test_days_in_month()
	{
		$this->assertEquals(30, days_in_month(06, 2005));
		$this->assertEquals(28, days_in_month(02, 2011));
		$this->assertEquals(29, days_in_month(02, 2012));
	}

	// ------------------------------------------------------------------------

	public function test_local_to_gmt()
	{
		$this->assertEquals(
			mktime(
				gmdate('H', $this->time), gmdate('i', $this->time), gmdate('s', $this->time),
				gmdate('m', $this->time), gmdate('d', $this->time), gmdate('Y', $this->time)
			),
			local_to_gmt($this->time)
		);
	}

	// ------------------------------------------------------------------------

	public function test_gmt_to_local()
	{
		$this->assertEquals(1140128493, gmt_to_local('1140153693', 'UM8', TRUE));
	}

	// ------------------------------------------------------------------------

	public function test_mysql_to_unix()
	{
		$this->assertEquals($this->time, mysql_to_unix(date('Y-m-d H:i:s', $this->time)));
	}

	// ------------------------------------------------------------------------

	public function test_unix_to_human()
	{
		$this->assertEquals(date('Y-m-d h:i A', $this->time), unix_to_human($this->time));
		$this->assertEquals(date('Y-m-d h:i:s A', $this->time), unix_to_human($this->time, TRUE, 'us'));
		$this->assertEquals(date('Y-m-d H:i:s', $this->time), unix_to_human($this->time, TRUE, 'eu'));
	}

	// ------------------------------------------------------------------------

	public function test_human_to_unix()
	{
		$date = '2000-12-31 10:00:00 PM';
		$this->assertEquals(strtotime($date), human_to_unix($date));
		$this->assertFalse(human_to_unix());
	}

	// ------------------------------------------------------------------------

	public function test_timezones()
	{
		$zones = array(
			'UM12'		=> -12,
			'UM11'		=> -11,
			'UM10'		=> -10,
			'UM95'		=> -9.5,
			'UM9'		=> -9,
			'UM8'		=> -8,
			'UM7'		=> -7,
			'UM6'		=> -6,
			'UM5'		=> -5,
			'UM45'		=> -4.5,
			'UM4'		=> -4,
			'UM35'		=> -3.5,
			'UM3'		=> -3,
			'UM2'		=> -2,
			'UM1'		=> -1,
			'UTC'		=> 0,
			'UP1'		=> +1,
			'UP2'		=> +2,
			'UP3'		=> +3,
			'UP35'		=> +3.5,
			'UP4'		=> +4,
			'UP45'		=> +4.5,
			'UP5'		=> +5,
			'UP55'		=> +5.5,
			'UP575'		=> +5.75,
			'UP6'		=> +6,
			'UP65'		=> +6.5,
			'UP7'		=> +7,
			'UP8'		=> +8,
			'UP875'		=> +8.75,
			'UP9'		=> +9,
			'UP95'		=> +9.5,
			'UP10'		=> +10,
			'UP105'		=> +10.5,
			'UP11'		=> +11,
			'UP115'		=> +11.5,
			'UP12'		=> +12,
			'UP1275'	=> +12.75,
			'UP13'		=> +13,
			'UP14'		=> +14
		);

		foreach ($zones AS $test => $expected)
		{
			$this->assertEquals($expected, timezones($test));
		}

		$this->assertArrayHasKey('UP3', timezones());
		$this->assertEquals(0, timezones('non_existant'));
	}

}

/* End of file date_helper_test.php */