!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Communicator client code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Alec Flett <alecf@netscape.com>
24 * Seth Spitzer <sspitzer@netscape.com>
25 * Magnus Melin <mkmelin+mozilla@iki.fi>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 var gSearchDateFormat = 0;
42 var gSearchDateSeparator;
43 var gSearchDateLeadingZeros;
44
45 /**
46 * Get the short date format option of the current locale.
47 * This supports the common case which the date separator is
48 * either '/', '-', '.' and using Christian year.
49 */
initLocaleShortDateFormat
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
50 function initLocaleShortDateFormat()
51 {
52 // default to mm/dd/yyyy
53 gSearchDateFormat = 3;
54 gSearchDateSeparator = "/";
55 gSearchDateLeadingZeros = true;
56
57 try {
58 var dateFormatService = Components.classes["@mozilla.org/intl/scriptabledateformat;1"]
59 .getService(Components.interfaces.nsIScriptableDateFormat);
60 var dateString = dateFormatService.FormatDate("",
61 dateFormatService.dateFormatShort,
62 1999,
63 12,
64 1);
65
66 // find out the separator
67 var possibleSeparators = "/-.";
68 var arrayOfStrings;
69 for ( var i = 0; i < possibleSeparators.length; ++i )
70 {
71 arrayOfStrings = dateString.split( possibleSeparators[i] );
72 if ( arrayOfStrings.length == 3 )
73 {
74 gSearchDateSeparator = possibleSeparators[i];
75 break;
76 }
77 }
78
79 // check the format option
80 if ( arrayOfStrings.length != 3 ) // no successfull split
81 {
82 dump("getLocaleShortDateFormat: could not analyze the date format, defaulting to mm/dd/yyyy\n");
83 }
84 else
85 {
86 // the date will contain a zero if that system settings include leading zeros
87 gSearchDateLeadingZeros = /0/.test(dateString);
88
89 // match 1 as number, since that will match both "1" and "01"
90 if ( arrayOfStrings[0] == 1 )
91 {
92 // 01.12.1999 or 01.1999.12
93 gSearchDateFormat = arrayOfStrings[1] == "12" ? 5 : 6;
94 }
95 else if ( arrayOfStrings[1] == 1 )
96 {
97 // 12.01.1999 or 1999.01.12
98 gSearchDateFormat = arrayOfStrings[0] == "12" ? 3 : 2;
99 }
100 else // implies arrayOfStrings[2] == 1
101 {
102 // 12.1999.01 or 1999.12.01
103 gSearchDateFormat = arrayOfStrings[0] == "12" ? 4 : 1;
104 }
105 }
106 }
107 catch (e)
108 {
109 dump("getLocaleShortDateFormat: caught an exception!\n");
110 }
111 }
112
initializeSearchDateFormat
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
113 function initializeSearchDateFormat()
114 {
115 if (gSearchDateFormat)
116 return;
117
118 // get a search date format option and a seprator
119 try {
120 var pref = Components.classes["@mozilla.org/preferences-service;1"]
121 .getService(Components.interfaces.nsIPrefBranch);
122 gSearchDateFormat =
123 pref.getComplexValue("mailnews.search_date_format",
124 Components.interfaces.nsIPrefLocalizedString);
125 gSearchDateFormat = parseInt(gSearchDateFormat);
126
127 // if the option is 0 then try to use the format of the current locale
128 if (gSearchDateFormat == 0)
129 initLocaleShortDateFormat();
130 else
131 {
132 // initialize the search date format based on preferences
133 if ( gSearchDateFormat < 1 || gSearchDateFormat > 6 )
134 gSearchDateFormat = 3;
135
136 gSearchDateSeparator = pref.getComplexValue("mailnews.search_date_separator",
137 Components.interfaces.nsIPrefLocalizedString);
138
139 gSearchDateLeadingZeros = (pref.getComplexValue("mailnews.search_date_leading_zeros",
140 Components.interfaces.nsIPrefLocalizedString).data == "true");
141 }
142 }
143 catch (e)
144 {
145 dump("initializeSearchDateFormat: caught an exception: "+e+"\n");
146 // set to mm/dd/yyyy in case of error
147 gSearchDateFormat = 3;
148 gSearchDateSeparator = "/";
149 gSearchDateLeadingZeros = true;
150 }
151 }
152
convertPRTimeToString
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
153 function convertPRTimeToString(tm)
154 {
155 var time = new Date();
156 // PRTime is in microseconds, JavaScript time is in milliseconds
157 // so divide by 1000 when converting
158 time.setTime(tm / 1000);
159
160 return convertDateToString(time);
161 }
162
convertDateToString
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
163 function convertDateToString(time)
164 {
165 initializeSearchDateFormat();
166
167 var year = time.getFullYear();
168 var month = time.getMonth() + 1; // since js month is 0-11
169 if ( gSearchDateLeadingZeros && month < 10 )
170 month = "0" + month;
171 var date = time.getDate();
172 if ( gSearchDateLeadingZeros && date < 10 )
173 date = "0" + date;
174
175 var dateStr;
176 var sep = gSearchDateSeparator;
177
178 switch (gSearchDateFormat)
179 {
180 case 1:
181 dateStr = year + sep + month + sep + date;
182 break;
183 case 2:
184 dateStr = year + sep + date + sep + month;
185 break;
186 case 3:
187 dateStr = month + sep + date + sep + year;
188 break;
189 case 4:
190 dateStr = month + sep + year + sep + date;
191 break;
192 case 5:
193 dateStr = date + sep + month + sep + year;
194 break;
195 case 6:
196 dateStr = date + sep + year + sep + month;
197 break;
198 default:
199 dump("valid search date format option is 1-6\n");
200 }
201
202 return dateStr;
203 }
204
convertStringToPRTime
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
205 function convertStringToPRTime(str)
206 {
207 initializeSearchDateFormat();
208
209 var arrayOfStrings = str.split(gSearchDateSeparator);
210 var year, month, date;
211
212 // set year, month, date based on the format option
213 switch (gSearchDateFormat)
214 {
215 case 1:
216 year = arrayOfStrings[0];
217 month = arrayOfStrings[1];
218 date = arrayOfStrings[2];
219 break;
220 case 2:
221 year = arrayOfStrings[0];
222 month = arrayOfStrings[2];
223 date = arrayOfStrings[1];
224 break;
225 case 3:
226 year = arrayOfStrings[2];
227 month = arrayOfStrings[0];
228 date = arrayOfStrings[1];
229 break;
230 case 4:
231 year = arrayOfStrings[1];
232 month = arrayOfStrings[0];
233 date = arrayOfStrings[2];
234 break;
235 case 5:
236 year = arrayOfStrings[2];
237 month = arrayOfStrings[1];
238 date = arrayOfStrings[0];
239 break;
240 case 6:
241 year = arrayOfStrings[1];
242 month = arrayOfStrings[2];
243 date = arrayOfStrings[0];
244 break;
245 default:
246 dump("valid search date format option is 1-6\n");
247 }
248
249 month -= 1; // since js month is 0-11
250
251 var time = new Date(year, month, date);
252
253 // JavaScript time is in milliseconds, PRTime is in microseconds
254 // so multiply by 1000 when converting
255 return (time.getTime() * 1000);
256 }