Forgot that $lsub_ibx[0] would be unset if LSUB didn't return INBOX, so
[squirrelmail.git] / functions / date.php
CommitLineData
59177427 1<?php
7350889b 2
35586184 3/**
4 * date.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Takes a date and parses it into a usable format. The form that a
10 * date SHOULD arrive in is:
11 * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
12 * (as specified in RFC 822) -- 'Tue' is optional
13 *
14 * $Id$
15 */
16
b68edc75 17require_once(SM_PATH . 'functions/constants.php');
35586184 18
dc06f88c 19/* corrects a time stamp to be the local time */
d5c4ae7c 20function getGMTSeconds($stamp, $tzc) {
dc06f88c 21 /* date couldn't be parsed */
913ed9a3 22 if ($stamp == -1) {
23 return -1;
24 }
d5c4ae7c 25 /* timezone correction, expressed as `shhmm' */
26 switch($tzc)
dc06f88c 27 {
28 case 'Pacific':
29 case 'PST':
d5c4ae7c 30 $tzc = '-0800';
dc06f88c 31 break;
32 case 'Mountain':
33 case 'MST':
34 case 'PDT':
d5c4ae7c 35 $tzc = '-0700';
dc06f88c 36 break;
37 case 'Central':
38 case 'CST':
39 case 'MDT':
d5c4ae7c 40 $tzc = '-0600';
dc06f88c 41 break;
42 case 'Eastern':
43 case 'EST':
44 case 'CDT':
d5c4ae7c 45 $tzc = '-0500';
dc06f88c 46 break;
47 case 'EDT':
d5c4ae7c 48 $tzc = '-0400';
dc06f88c 49 break;
50 case 'GMT':
d5c4ae7c 51 $tzc = '+0000';
dc06f88c 52 break;
53 case 'BST':
d5c4ae7c 54 case 'MET':
55 case 'CET':
56 $tzc = '+0100';
57 break;
dc06f88c 58 case 'EET':
59 case 'IST':
60 case 'MET DST':
61 case 'METDST':
d5c4ae7c 62 $tzc = '+0200';
dc06f88c 63 break;
64 case 'HKT':
d5c4ae7c 65 $tzc = '+0800';
dc06f88c 66 break;
67 case 'JST':
68 case 'KST':
d5c4ae7c 69 $tzc = '+0900';
dc06f88c 70 break;
0e4ae91a 71 }
d5c4ae7c 72 $neg = false;
73 if (substr($tzc, 0, 1) == '-') {
0e4ae91a 74 $neg = true;
d5c4ae7c 75 } else if (substr($tzc, 0, 1) != '+') {
76 $tzc = '+'.$tzc;
0e4ae91a 77 }
d5c4ae7c 78 $hh = substr($tzc,1,2);
79 $mm = substr($tzc,3,2);
80 $iTzc = ($hh * 60 + $mm) * 60;
81 if ($neg) $iTzc = -1 * (int) $iTzc;
82 /* stamp in gmt */
83 $stamp -= $iTzc;
0e4ae91a 84 /** now find what the server is at **/
85 $current = date('Z', time());
d5c4ae7c 86 /* stamp in local timezone */
87 $stamp += $current;
0e4ae91a 88
89 return $stamp;
90}
0f1835f3 91
0e4ae91a 92/**
dc06f88c 93 Switch system has been intentionaly chosen for the
0e4ae91a 94 internationalization of month and day names. The reason
95 is to make sure that _("") strings will go into the
96 main po.
97**/
0f1835f3 98
0e4ae91a 99function getDayName( $day_number ) {
0f1835f3 100
0e4ae91a 101 switch( $day_number ) {
102 case 0:
103 $ret = _("Sunday");
104 break;
105 case 1:
106 $ret = _("Monday");
107 break;
108 case 2:
109 $ret = _("Tuesday");
110 break;
111 case 3:
112 $ret = _("Wednesday");
113 break;
114 case 4:
115 $ret = _("Thursday");
116 break;
117 case 5:
118 $ret = _("Friday");
119 break;
120 case 6:
121 $ret = _("Saturday");
122 break;
123 default:
124 $ret = '';
125 }
126 return( $ret );
127}
d59837cf 128
22d73941 129function getDayAbrv( $day_number ) {
130
131 switch( $day_number ) {
132 case 0:
133 $ret = _("Sun");
134 break;
135 case 1:
136 $ret = _("Mon");
137 break;
138 case 2:
139 $ret = _("Tue");
140 break;
141 case 3:
142 $ret = _("Wed");
143 break;
144 case 4:
145 $ret = _("Thu");
146 break;
147 case 5:
148 $ret = _("Fri");
149 break;
150 case 6:
151 $ret = _("Sat");
152 break;
153 default:
154 $ret = '';
155 }
156 return( $ret );
157}
158
0e4ae91a 159function getMonthName( $month_number ) {
160 switch( $month_number ) {
161 case '01':
162 $ret = _("January");
163 break;
164 case '02':
165 $ret = _("February");
166 break;
167 case '03':
168 $ret = _("March");
169 break;
170 case '04':
171 $ret = _("April");
172 break;
173 case '05':
174 $ret = _("May");
175 break;
176 case '06':
177 $ret = _("June");
178 break;
179 case '07':
180 $ret = _("July");
181 break;
182 case '08':
183 $ret = _("August");
184 break;
185 case '09':
186 $ret = _("September");
187 break;
188 case '10':
189 $ret = _("October");
190 break;
191 case '11':
192 $ret = _("November");
193 break;
194 case '12':
195 $ret = _("December");
196 break;
197 default:
198 $ret = '';
199 }
200 return( $ret );
201}
d59837cf 202
22d73941 203function getMonthAbrv( $month_number ) {
204 switch( $month_number ) {
205 case '01':
206 $ret = _("Jan");
207 break;
208 case '02':
209 $ret = _("Feb");
210 break;
211 case '03':
212 $ret = _("Mar");
213 break;
214 case '04':
215 $ret = _("Apr");
216 break;
217 case '05':
218 $ret = _("Ma&#121;");
219 break;
220 case '06':
221 $ret = _("Jun");
222 break;
223 case '07':
224 $ret = _("Jul");
225 break;
226 case '08':
227 $ret = _("Aug");
228 break;
229 case '09':
230 $ret = _("Sep");
231 break;
232 case '10':
233 $ret = _("Oct");
234 break;
235 case '11':
236 $ret = _("Nov");
237 break;
238 case '12':
239 $ret = _("Dec");
240 break;
241 default:
242 $ret = '';
243 }
244 return( $ret );
245}
246
247
0e4ae91a 248function date_intl( $date_format, $stamp ) {
d59837cf 249
0e4ae91a 250 $ret = str_replace( 'D', '$1', $date_format );
251 $ret = str_replace( 'F', '$2', $ret );
ce628f00 252 $ret = str_replace( 'l', '$4', $ret );
60087c97 253 $ret = str_replace( 'M', '$5', $ret );
0e4ae91a 254 $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
22d73941 255 $ret = str_replace( '$1', getDayAbrv( date( 'w', $stamp ) ), $ret );
256 $ret = str_replace( '$5', getMonthAbrv( date( 'm', $stamp ) ), $ret );
0e4ae91a 257 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
ce628f00 258 $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret );
0e4ae91a 259 $ret = str_replace( '$3', '', $ret );
260
261 return( $ret );
262}
d59837cf 263
0e4ae91a 264function getLongDateString( $stamp ) {
d59837cf 265
0e4ae91a 266 global $hour_format;
267
913ed9a3 268 if ($stamp == -1) {
269 return '';
270 }
271
0e4ae91a 272 if ( $hour_format == SMPREF_TIME_12HR ) {
273 $date_format = _("D, F j, Y g:i a");
274 } else {
275 $date_format = _("D, F j, Y G:i");
276 }
277
278 return( date_intl( $date_format, $stamp ) );
0f1835f3 279
0e4ae91a 280}
d205a7bf 281
0e4ae91a 282function getDateString( $stamp ) {
d68a3926 283
0e4ae91a 284 global $invert_time, $hour_format;
dc06f88c 285
286 if ( $stamp == -1 ) {
287 return '';
288 }
0e4ae91a 289
290 $now = time();
291
292 $dateZ = date('Z', $now );
293 if ($invert_time) {
294 $dateZ = - $dateZ;
295 }
296 $midnight = $now - ($now % 86400) - $dateZ;
297
298 if ($midnight < $stamp) {
299 /* Today */
300 if ( $hour_format == SMPREF_TIME_12HR ) {
301 $date_format = _("g:i a");
302 } else {
303 $date_format = _("G:i");
304 }
305 } else if ($midnight - 518400 < $stamp) {
306 /* This week */
307 if ( $hour_format == SMPREF_TIME_12HR ) {
308 $date_format = _("D, g:i a");
309 } else {
310 $date_format = _("D, G:i");
311 }
312 } else {
313 /* before this week */
314 $date_format = _("M j, Y");
315 }
316
317 return( date_intl( $date_format, $stamp ) );
318}
0f1835f3 319
0e4ae91a 320function getTimeStamp($dateParts) {
321 /** $dateParts[0] == <day of week> Mon, Tue, Wed
322 ** $dateParts[1] == <day of month> 23
323 ** $dateParts[2] == <month> Jan, Feb, Mar
324 ** $dateParts[3] == <year> 1999
325 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
326 ** $dateParts[5] == <from GMT> +0100
327 ** $dateParts[6] == <zone> (EDT)
328 **
329 ** NOTE: In RFC 822, it states that <day of week> is optional.
330 ** In that case, dateParts[0] would be the <day of month>
331 ** and everything would be bumped up one.
332 **/
d5c4ae7c 333
0e4ae91a 334 /*
335 * Simply check to see if the first element in the dateParts
336 * array is an integer or not.
dc06f88c 337 * Since the day of week is optional, this check is needed.
0e4ae91a 338 */
50457d45 339 if (count($dateParts) <2) {
340 return 0;
341 }
3302d0d4 342
d5c4ae7c 343 /* remove day of week */
344 if (!is_numeric(trim($dateParts[0]))) {
345 $dataParts = array_shift($dateParts);
0e4ae91a 346 }
d5c4ae7c 347 /* calculate timestamp separated from the zone and obs-zone */
348 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
349 if (!isset($dateParts[0])) {
350 $dateParts[0] = '+0000';
0e4ae91a 351 }
dc06f88c 352
d5c4ae7c 353 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
354 /* zone in obs-zone format */
355 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
356 $obs_zone = $regs[1];
357 } else {
358 $obs_zone = $dateParts[0];
359 }
360 return getGMTSeconds($stamp, $obs_zone);
361 } else {
362 return getGMTSeconds($stamp, $dateParts[0]);
0e4ae91a 363 }
0e4ae91a 364}
5e90d34a 365
0e4ae91a 366/* I use this function for profiling. Should never be called in
367 actual versions of squirrelmail released to public. */
d205a7bf 368/*
5e90d34a 369 function getmicrotime() {
370 $mtime = microtime();
b9bfd165 371 $mtime = explode(' ',$mtime);
5e90d34a 372 $mtime = $mtime[1] + $mtime[0];
373 return ($mtime);
9774bdef 374 }
d205a7bf 375*/
35586184 376?>