moved address-parsing to the displayable headers
[squirrelmail.git] / functions / date.php
1 <?php
2
3 /**
4 * date.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
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
17 require_once(SM_PATH . 'functions/constants.php');
18
19 /* corrects a time stamp to be the local time */
20 function getGMTSeconds($stamp, $tzc) {
21 /* date couldn't be parsed */
22 if ($stamp == -1) {
23 return -1;
24 }
25 /* timezone correction, expressed as `shhmm' */
26 switch($tzc)
27 {
28 case 'Pacific':
29 case 'PST':
30 $tzc = '-0800';
31 break;
32 case 'Mountain':
33 case 'MST':
34 case 'PDT':
35 $tzc = '-0700';
36 break;
37 case 'Central':
38 case 'CST':
39 case 'MDT':
40 $tzc = '-0600';
41 break;
42 case 'Eastern':
43 case 'EST':
44 case 'CDT':
45 $tzc = '-0500';
46 break;
47 case 'EDT':
48 $tzc = '-0400';
49 break;
50 case 'GMT':
51 $tzc = '+0000';
52 break;
53 case 'BST':
54 case 'MET':
55 case 'CET':
56 $tzc = '+0100';
57 break;
58 case 'EET':
59 case 'IST':
60 case 'MET DST':
61 case 'METDST':
62 $tzc = '+0200';
63 break;
64 case 'HKT':
65 $tzc = '+0800';
66 break;
67 case 'JST':
68 case 'KST':
69 $tzc = '+0900';
70 break;
71 }
72 $neg = false;
73 if (substr($tzc, 0, 1) == '-') {
74 $neg = true;
75 } else if (substr($tzc, 0, 1) != '+') {
76 $tzc = '+'.$tzc;
77 }
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;
84 /** now find what the server is at **/
85 $current = date('Z', time());
86 /* stamp in local timezone */
87 $stamp += $current;
88
89 return $stamp;
90 }
91
92 /**
93 Switch system has been intentionaly chosen for the
94 internationalization of month and day names. The reason
95 is to make sure that _("") strings will go into the
96 main po.
97 **/
98
99 function getDayName( $day_number ) {
100
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 }
128
129 function getMonthName( $month_number ) {
130 switch( $month_number ) {
131 case '01':
132 $ret = _("January");
133 break;
134 case '02':
135 $ret = _("February");
136 break;
137 case '03':
138 $ret = _("March");
139 break;
140 case '04':
141 $ret = _("April");
142 break;
143 case '05':
144 $ret = _("May");
145 break;
146 case '06':
147 $ret = _("June");
148 break;
149 case '07':
150 $ret = _("July");
151 break;
152 case '08':
153 $ret = _("August");
154 break;
155 case '09':
156 $ret = _("September");
157 break;
158 case '10':
159 $ret = _("October");
160 break;
161 case '11':
162 $ret = _("November");
163 break;
164 case '12':
165 $ret = _("December");
166 break;
167 default:
168 $ret = '';
169 }
170 return( $ret );
171 }
172
173 function date_intl( $date_format, $stamp ) {
174
175 $ret = str_replace( 'D', '$1', $date_format );
176 $ret = str_replace( 'F', '$2', $ret );
177 $ret = str_replace( 'l', '$4', $ret );
178 $ret = str_replace( 'M', '$5', $ret );
179 $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem
180 $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret );
181 $ret = str_replace( '$5', substr( getMonthName( date( 'm', $stamp ) ), 0, 3 ), $ret );
182 $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret );
183 $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret );
184 $ret = str_replace( '$3', '', $ret );
185
186 return( $ret );
187 }
188
189 function getLongDateString( $stamp ) {
190
191 global $hour_format;
192
193 if ($stamp == -1) {
194 return '';
195 }
196
197 if ( $hour_format == SMPREF_TIME_12HR ) {
198 $date_format = _("D, F j, Y g:i a");
199 } else {
200 $date_format = _("D, F j, Y G:i");
201 }
202
203 return( date_intl( $date_format, $stamp ) );
204
205 }
206
207 function getDateString( $stamp ) {
208
209 global $invert_time, $hour_format;
210
211 if ( $stamp == -1 ) {
212 return '';
213 }
214
215 $now = time();
216
217 $dateZ = date('Z', $now );
218 if ($invert_time) {
219 $dateZ = - $dateZ;
220 }
221 $midnight = $now - ($now % 86400) - $dateZ;
222
223 if ($midnight < $stamp) {
224 /* Today */
225 if ( $hour_format == SMPREF_TIME_12HR ) {
226 $date_format = _("g:i a");
227 } else {
228 $date_format = _("G:i");
229 }
230 } else if ($midnight - 518400 < $stamp) {
231 /* This week */
232 if ( $hour_format == SMPREF_TIME_12HR ) {
233 $date_format = _("D, g:i a");
234 } else {
235 $date_format = _("D, G:i");
236 }
237 } else {
238 /* before this week */
239 $date_format = _("M j, Y");
240 }
241
242 return( date_intl( $date_format, $stamp ) );
243 }
244
245 function getTimeStamp($dateParts) {
246 /** $dateParts[0] == <day of week> Mon, Tue, Wed
247 ** $dateParts[1] == <day of month> 23
248 ** $dateParts[2] == <month> Jan, Feb, Mar
249 ** $dateParts[3] == <year> 1999
250 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
251 ** $dateParts[5] == <from GMT> +0100
252 ** $dateParts[6] == <zone> (EDT)
253 **
254 ** NOTE: In RFC 822, it states that <day of week> is optional.
255 ** In that case, dateParts[0] would be the <day of month>
256 ** and everything would be bumped up one.
257 **/
258
259 /*
260 * Simply check to see if the first element in the dateParts
261 * array is an integer or not.
262 * Since the day of week is optional, this check is needed.
263 */
264 if (count($dateParts) <2) {
265 return 0;
266 }
267
268 /* remove day of week */
269 if (!is_numeric(trim($dateParts[0]))) {
270 $dataParts = array_shift($dateParts);
271 }
272 /* calculate timestamp separated from the zone and obs-zone */
273 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
274 if (!isset($dateParts[0])) {
275 $dateParts[0] = '+0000';
276 }
277
278 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
279 /* zone in obs-zone format */
280 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
281 $obs_zone = $regs[1];
282 } else {
283 $obs_zone = $dateParts[0];
284 }
285 return getGMTSeconds($stamp, $obs_zone);
286 } else {
287 return getGMTSeconds($stamp, $dateParts[0]);
288 }
289 }
290
291 /* I use this function for profiling. Should never be called in
292 actual versions of squirrelmail released to public. */
293 /*
294 function getmicrotime() {
295 $mtime = microtime();
296 $mtime = explode(' ',$mtime);
297 $mtime = $mtime[1] + $mtime[0];
298 return ($mtime);
299 }
300 */
301 ?>