59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
4 | * date.php |
5 | * |
6 | * Copyright (c) 1999-2001 The SquirrelMail Development 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 | /*****************************************************************/ |
18 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
19 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
20 | /*** + Base level indent should begin at left margin, as ***/ |
21 | /*** the first line of the function definition below. ***/ |
22 | /*** + All identation should consist of four space blocks ***/ |
23 | /*** + Tab characters are evil. ***/ |
24 | /*** + all comments should use "slash-star ... star-slash" ***/ |
25 | /*** style -- no pound characters, no slash-slash style ***/ |
26 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
27 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
28 | /*** + Please use ' instead of ", when possible. Note " ***/ |
29 | /*** should always be used in _( ) function calls. ***/ |
30 | /*** Thank you for your help making the SM code more readable. ***/ |
31 | /*****************************************************************/ |
32 | |
33 | // corrects a time stamp to be the local time |
34 | function getGMTSeconds($stamp, $gmt) { |
2ed6f907 |
35 | global $invert_time; |
b9bfd165 |
36 | if (($gmt == 'Pacific') || ($gmt == 'PST')) |
37 | $gmt = '-0800'; |
38 | else if (($gmt == 'EDT')) |
39 | $gmt = '-0400'; |
40 | else if (($gmt == 'Eastern') || ($gmt == 'EST') || ($gmt == 'CDT')) |
41 | $gmt = '-0500'; |
42 | else if (($gmt == 'Central') || ($gmt == 'CST') || ($gmt == 'MDT')) |
43 | $gmt = '-0600'; |
44 | else if (($gmt == 'Mountain') || ($gmt == 'MST') || ($gmt == 'PDT')) |
45 | $gmt = '-0700'; |
46 | else if ($gmt == 'BST') |
47 | $gmt = '+0100'; |
48 | else if ($gmt == 'EET') |
49 | $gmt = '+0200'; |
50 | else if ($gmt == 'GMT') |
51 | $gmt = '+0000'; |
52 | else if ($gmt == 'HKT') |
53 | $gmt = '+0800'; |
54 | else if ($gmt == 'IST') |
55 | $gmt = '+0200'; |
56 | else if ($gmt == 'JST') |
57 | $gmt = '+0900'; |
f435778e |
58 | else if ($gmt == 'KST') |
59 | $gmt = "+0900"; |
b9bfd165 |
60 | else if ($gmt == 'MET') |
61 | $gmt = '+0100'; |
62 | else if ($gmt == 'MET DST' || $gmt == 'METDST') |
63 | $gmt = '+0200'; |
47654bfb |
64 | |
b9bfd165 |
65 | if (substr($gmt, 0, 1) == '-') { |
0f1835f3 |
66 | $neg = true; |
67 | $gmt = substr($gmt, 1, strlen($gmt)); |
b9bfd165 |
68 | } else if (substr($gmt, 0, 1) == '+') { |
0f1835f3 |
69 | $neg = false; |
70 | $gmt = substr($gmt, 1, strlen($gmt)); |
71 | } else |
72 | $neg = false; |
73 | |
74 | $gmt = substr($gmt, 0, 2); |
75 | $gmt = $gmt * 3600; |
76 | if ($neg == true) |
77 | $gmt = "-$gmt"; |
78 | else |
79 | $gmt = "+$gmt"; |
80 | |
81 | /** now find what the server is at **/ |
b9bfd165 |
82 | $current = date('Z', time()); |
d47b2518 |
83 | if ($invert_time) |
84 | $current = - $current; |
0f1835f3 |
85 | $stamp = (int)$stamp - (int)$gmt + (int)$current; |
86 | |
87 | return $stamp; |
88 | } |
89 | |
ca5b5e00 |
90 | /** |
91 | Switch system has been intentionaly choosed for the |
92 | internationalization of month and day names. The reason |
93 | is to make sure that _("") strings will go into the |
94 | main po. |
95 | **/ |
d59837cf |
96 | |
97 | function getDayName( $day_number ) { |
98 | |
ca5b5e00 |
99 | switch( $day_number ) { |
100 | case 0: |
101 | $ret = _("Sunday"); |
102 | break; |
103 | case 1: |
104 | $ret = _("Monday"); |
105 | break; |
106 | case 2: |
107 | $ret = _("Tuesday"); |
108 | break; |
109 | case 3: |
110 | $ret = _("Wednesday"); |
111 | break; |
112 | case 4: |
113 | $ret = _("Thursday"); |
114 | break; |
115 | case 5: |
116 | $ret = _("Friday"); |
117 | break; |
118 | case 6: |
119 | $ret = _("Saturday"); |
120 | break; |
121 | default: |
122 | $ret = ''; |
123 | } |
124 | return( $ret ); |
d59837cf |
125 | } |
126 | |
ca5b5e00 |
127 | function getMonthName( $month_number ) { |
128 | switch( $month_number ) { |
129 | case '01': |
130 | $ret = _("January"); |
131 | break; |
132 | case '02': |
133 | $ret = _("February"); |
134 | break; |
135 | case '03': |
136 | $ret = _("March"); |
137 | break; |
138 | case '04': |
139 | $ret = _("April"); |
140 | break; |
141 | case '05': |
142 | $ret = _("May"); |
143 | break; |
144 | case '06': |
145 | $ret = _("June"); |
146 | break; |
147 | case '07': |
148 | $ret = _("July"); |
149 | break; |
150 | case '08': |
151 | $ret = _("August"); |
152 | break; |
153 | case '09': |
154 | $ret = _("September"); |
155 | break; |
156 | case '10': |
157 | $ret = _("October"); |
158 | break; |
159 | case '11': |
160 | $ret = _("November"); |
161 | break; |
162 | case '12': |
163 | $ret = _("December"); |
164 | break; |
165 | default: |
166 | $ret = ''; |
167 | } |
168 | return( $ret ); |
169 | } |
d59837cf |
170 | |
ca5b5e00 |
171 | function date_intl( $date_format, $stamp ) { |
d59837cf |
172 | |
d205a7bf |
173 | $ret = str_replace( 'D', '$1', $date_format ); |
174 | $ret = str_replace( 'F', '$2', $ret ); |
175 | $ret = date( '$3'. $ret . '$3', $stamp ); // Workaround for a PHP 4.0.4 problem |
176 | $ret = str_replace( '$1', substr( getDayName( date( 'w', $stamp ) ), 0, 3 ), $ret ); |
177 | $ret = str_replace( '$2', getMonthName( date( 'm', $stamp ) ), $ret ); |
178 | $ret = str_replace( '$3', '', $ret ); |
179 | |
180 | return( $ret ); |
ca5b5e00 |
181 | } |
d59837cf |
182 | |
d205a7bf |
183 | function getLongDateString( $stamp ) { |
d59837cf |
184 | |
d205a7bf |
185 | return( date_intl( _("D, F j, Y g:i a"), $stamp ) ); |
d59837cf |
186 | |
0f1835f3 |
187 | } |
188 | |
d205a7bf |
189 | function getDateString( $stamp ) { |
d59837cf |
190 | |
2ed6f907 |
191 | global $invert_time; |
d205a7bf |
192 | |
d68a3926 |
193 | $now = time(); |
d205a7bf |
194 | |
195 | $dateZ = date('Z', $now ); |
d47b2518 |
196 | if ($invert_time) |
197 | $dateZ = - $dateZ; |
c57a3b1a |
198 | $midnight = $now - ($now % 86400) - $dateZ; |
d68a3926 |
199 | |
200 | if ($midnight < $stamp) { |
201 | // Today |
d59837cf |
202 | $date_format = _("g:i a"); |
203 | } else if ($midnight - 518400 < $stamp) { |
d68a3926 |
204 | // This week |
d59837cf |
205 | $date_format = _("D, g:i a"); |
d68a3926 |
206 | } else { |
207 | // before this week |
d59837cf |
208 | $date_format = _("M j, Y"); |
d68a3926 |
209 | } |
d59837cf |
210 | |
d205a7bf |
211 | return( date_intl( $date_format, $stamp ) ); |
212 | // return( date( $date_i, $stamp ) ); |
0f1835f3 |
213 | } |
214 | |
215 | function getTimeStamp($dateParts) { |
3302d0d4 |
216 | /** $dateParts[0] == <day of week> Mon, Tue, Wed |
217 | ** $dateParts[1] == <day of month> 23 |
218 | ** $dateParts[2] == <month> Jan, Feb, Mar |
219 | ** $dateParts[3] == <year> 1999 |
220 | ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS) |
221 | ** $dateParts[5] == <from GMT> +0100 |
222 | ** $dateParts[6] == <zone> (EDT) |
223 | ** |
224 | ** NOTE: In RFC 822, it states that <day of week> is optional. |
225 | ** In that case, dateParts[0] would be the <day of month> |
226 | ** and everything would be bumped up one. |
227 | **/ |
228 | |
d068c0ec |
229 | // Simply check to see if the first element in the dateParts |
230 | // array is an integer or not. |
5e90d34a |
231 | // Since the day of week is optional, this check is needed. |
232 | // |
b9bfd165 |
233 | // The old code used eregi('mon|tue|wed|thu|fri|sat|sun', |
d068c0ec |
234 | // $dateParts[0], $tmp) to find if the first element was the |
235 | // day of week or day of month. This is an expensive call |
236 | // (processing time) to have inside a loop. Doing it this way |
237 | // saves quite a bit of time for large mailboxes. |
5e90d34a |
238 | // |
d068c0ec |
239 | // It is also quicker to call explode only once rather than |
240 | // the 3 times it was getting called by calling the functions |
241 | // getHour, getMinute, and getSecond. |
5e90d34a |
242 | // |
af354e93 |
243 | if (! isset($dateParts[1])) $dateParts[1] = ''; |
244 | if (! isset($dateParts[2])) $dateParts[2] = ''; |
245 | if (! isset($dateParts[3])) $dateParts[3] = ''; |
246 | if (! isset($dateParts[4])) $dateParts[4] = ''; |
247 | if (! isset($dateParts[5])) $dateParts[5] = ''; |
5e90d34a |
248 | if (intval(trim($dateParts[0])) > 0) { |
b9bfd165 |
249 | $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' . |
250 | $dateParts[2] . ' ' . $dateParts[3]; |
fde32e3f |
251 | return getGMTSeconds(strtotime($string), $dateParts[4]); |
5b10f02a |
252 | } |
b9bfd165 |
253 | $string = $dateParts[0] . ' ' . $dateParts[1] . ' ' . |
254 | $dateParts[2] . ' ' . $dateParts[3] . ' ' . $dateParts[4]; |
ca5b5e00 |
255 | if (isset($dateParts[5])) |
256 | return getGMTSeconds(strtotime($string), $dateParts[5]); |
257 | else |
258 | return getGMTSeconds(strtotime($string), ''); |
5e90d34a |
259 | } |
260 | |
d068c0ec |
261 | // I use this function for profiling. Should never be called in |
262 | // actual versions of squirrelmail released to public. |
d205a7bf |
263 | /* |
5e90d34a |
264 | function getmicrotime() { |
265 | $mtime = microtime(); |
b9bfd165 |
266 | $mtime = explode(' ',$mtime); |
5e90d34a |
267 | $mtime = $mtime[1] + $mtime[0]; |
268 | return ($mtime); |
9774bdef |
269 | } |
d205a7bf |
270 | */ |
35586184 |
271 | ?> |