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 |
17 | require_once(SM_PATH . 'functions/constants.php'); |
35586184 |
18 | |
dc06f88c |
19 | /* corrects a time stamp to be the local time */ |
d5c4ae7c |
20 | function 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 |
99 | function 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 | |
0e4ae91a |
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 | } |
d59837cf |
172 | |
0e4ae91a |
173 | function date_intl( $date_format, $stamp ) { |
d59837cf |
174 | |
0e4ae91a |
175 | $ret = str_replace( 'D', '$1', $date_format ); |
176 | $ret = str_replace( 'F', '$2', $ret ); |
ce628f00 |
177 | $ret = str_replace( 'l', '$4', $ret ); |
60087c97 |
178 | $ret = str_replace( 'M', '$5', $ret ); |
0e4ae91a |
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 ); |
60087c97 |
181 | $ret = str_replace( '$5', substr( getMonthName( date( 'm', $stamp ) ), 0, 3 ), $ret ); |
0e4ae91a |
182 | $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret ); |
ce628f00 |
183 | $ret = str_replace( '$4', getDayName( date( 'w', $stamp ) ), $ret ); |
0e4ae91a |
184 | $ret = str_replace( '$3', '', $ret ); |
185 | |
186 | return( $ret ); |
187 | } |
d59837cf |
188 | |
0e4ae91a |
189 | function getLongDateString( $stamp ) { |
d59837cf |
190 | |
0e4ae91a |
191 | global $hour_format; |
192 | |
913ed9a3 |
193 | if ($stamp == -1) { |
194 | return ''; |
195 | } |
196 | |
0e4ae91a |
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 ) ); |
0f1835f3 |
204 | |
0e4ae91a |
205 | } |
d205a7bf |
206 | |
0e4ae91a |
207 | function getDateString( $stamp ) { |
d68a3926 |
208 | |
0e4ae91a |
209 | global $invert_time, $hour_format; |
dc06f88c |
210 | |
211 | if ( $stamp == -1 ) { |
212 | return ''; |
213 | } |
0e4ae91a |
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 | } |
0f1835f3 |
244 | |
0e4ae91a |
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 | **/ |
d5c4ae7c |
258 | |
0e4ae91a |
259 | /* |
260 | * Simply check to see if the first element in the dateParts |
261 | * array is an integer or not. |
dc06f88c |
262 | * Since the day of week is optional, this check is needed. |
0e4ae91a |
263 | */ |
50457d45 |
264 | if (count($dateParts) <2) { |
265 | return 0; |
266 | } |
3302d0d4 |
267 | |
d5c4ae7c |
268 | /* remove day of week */ |
269 | if (!is_numeric(trim($dateParts[0]))) { |
270 | $dataParts = array_shift($dateParts); |
0e4ae91a |
271 | } |
d5c4ae7c |
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'; |
0e4ae91a |
276 | } |
dc06f88c |
277 | |
d5c4ae7c |
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]); |
0e4ae91a |
288 | } |
0e4ae91a |
289 | } |
5e90d34a |
290 | |
0e4ae91a |
291 | /* I use this function for profiling. Should never be called in |
292 | actual versions of squirrelmail released to public. */ |
d205a7bf |
293 | /* |
5e90d34a |
294 | function getmicrotime() { |
295 | $mtime = microtime(); |
b9bfd165 |
296 | $mtime = explode(' ',$mtime); |
5e90d34a |
297 | $mtime = $mtime[1] + $mtime[0]; |
298 | return ($mtime); |
9774bdef |
299 | } |
d205a7bf |
300 | */ |
35586184 |
301 | ?> |