summaryrefslogtreecommitdiffabout
authorulf69 <ulf69>2004-09-29 01:43:15 (UTC)
committer ulf69 <ulf69>2004-09-29 01:43:15 (UTC)
commitcd2cb3a9e2ea93909efc9efa16ddd80c2d2b5408 (patch) (unidiff)
treeab401f13f26d7f72ebd2a53deb3a67e97adad8a1
parentaf63a131d742aff8e448e5fe0f3e62b11a4d9a33 (diff)
downloadkdepimpi-cd2cb3a9e2ea93909efc9efa16ddd80c2d2b5408.zip
kdepimpi-cd2cb3a9e2ea93909efc9efa16ddd80c2d2b5408.tar.gz
kdepimpi-cd2cb3a9e2ea93909efc9efa16ddd80c2d2b5408.tar.bz2
needs to be fixed
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/klocale.cpp32
1 files changed, 28 insertions, 4 deletions
diff --git a/microkde/kdecore/klocale.cpp b/microkde/kdecore/klocale.cpp
index 4960b9a..7bd8a70 100644
--- a/microkde/kdecore/klocale.cpp
+++ b/microkde/kdecore/klocale.cpp
@@ -1,915 +1,939 @@
1#include <qregexp.h> 1#include <qregexp.h>
2#include <qapplication.h> 2#include <qapplication.h>
3 3
4#include "kdebug.h" 4#include "kdebug.h"
5#include "kcalendarsystemgregorian.h" 5#include "kcalendarsystemgregorian.h"
6 6
7#include "klocale.h" 7#include "klocale.h"
8 8
9 9
10QDict<QString> *mLocaleDict = 0; 10QDict<QString> *mLocaleDict = 0;
11void setLocaleDict( QDict<QString> * dict ) 11void setLocaleDict( QDict<QString> * dict )
12{ 12{
13 mLocaleDict = dict; 13 mLocaleDict = dict;
14 14
15} 15}
16QString i18n(const char *text) 16QString i18n(const char *text)
17{ 17{
18 if ( ! mLocaleDict ) 18 if ( ! mLocaleDict )
19 return QString( text ); 19 return QString( text );
20 else { 20 else {
21 QString* ret = mLocaleDict->find(QString(text)) ; 21 QString* ret = mLocaleDict->find(QString(text)) ;
22 if ( ret == 0 ) { 22 if ( ret == 0 ) {
23 return QString( text ); 23 return QString( text );
24 } 24 }
25 else { 25 else {
26 if ( (*ret).isEmpty() ) 26 if ( (*ret).isEmpty() )
27 return QString( text ); 27 return QString( text );
28 else 28 else
29 return (*ret); 29 return (*ret);
30 } 30 }
31 } 31 }
32 32
33} 33}
34 34
35QString i18n(const char *,const char *text) 35QString i18n(const char *,const char *text)
36{ 36{
37 return i18n( text ); 37 return i18n( text );
38} 38}
39 39
40QString i18n(const char *text1, const char *textn, int num) 40QString i18n(const char *text1, const char *textn, int num)
41{ 41{
42 if ( num == 1 ) return i18n( text1 ); 42 if ( num == 1 ) return i18n( text1 );
43 else { 43 else {
44 QString text = i18n( textn ); 44 QString text = i18n( textn );
45 int pos = text.find( "%n" ); 45 int pos = text.find( "%n" );
46 if ( pos >= 0 ) text.replace( pos, 2, QString::number( num ) ); 46 if ( pos >= 0 ) text.replace( pos, 2, QString::number( num ) );
47 return text; 47 return text;
48 } 48 }
49} 49}
50 50
51inline void put_it_in( QChar *buffer, uint& index, const QString &s ) 51inline void put_it_in( QChar *buffer, uint& index, const QString &s )
52{ 52{
53 for ( uint l = 0; l < s.length(); l++ ) 53 for ( uint l = 0; l < s.length(); l++ )
54 buffer[index++] = s.at( l ); 54 buffer[index++] = s.at( l );
55} 55}
56 56
57inline void put_it_in( QChar *buffer, uint& index, int number ) 57inline void put_it_in( QChar *buffer, uint& index, int number )
58{ 58{
59 buffer[index++] = number / 10 + '0'; 59 buffer[index++] = number / 10 + '0';
60 buffer[index++] = number % 10 + '0'; 60 buffer[index++] = number % 10 + '0';
61} 61}
62 62
63static int readInt(const QString &str, uint &pos) 63static int readInt(const QString &str, uint &pos)
64{ 64{
65 if (!str.at(pos).isDigit()) return -1; 65 if (!str.at(pos).isDigit()) return -1;
66 int result = 0; 66 int result = 0;
67 for (; str.length() > pos && str.at(pos).isDigit(); pos++) 67 for (; str.length() > pos && str.at(pos).isDigit(); pos++)
68 { 68 {
69 result *= 10; 69 result *= 10;
70 result += str.at(pos).digitValue(); 70 result += str.at(pos).digitValue();
71 } 71 }
72 72
73 return result; 73 return result;
74} 74}
75 75
76KLocale::KLocale() : mCalendarSystem( 0 ) 76KLocale::KLocale() : mCalendarSystem( 0 )
77{ 77{
78 78
79 m_decimalSymbol = "."; 79 m_decimalSymbol = ".";
80 m_positiveSign = ""; 80 m_positiveSign = "";
81 m_negativeSign = "-"; 81 m_negativeSign = "-";
82 m_thousandsSeparator = ","; 82 m_thousandsSeparator = ",";
83 83
84 84
85 85
86 86
87 mWeekStartsMonday = true; 87 mWeekStartsMonday = true;
88 mHourF24Format = true; 88 mHourF24Format = true;
89 mIntDateFormat = Default; 89 mIntDateFormat = Default;
90 mIntTimeFormat = Default; 90 mIntTimeFormat = Default;
91 mLanguage = 0; 91 mLanguage = 0;
92 mDateFormat = "%a %Y %b %d"; 92 mDateFormat = "%a %Y %b %d";
93 mDateFormatShort = "%Y-%m-%d"; 93 mDateFormatShort = "%Y-%m-%d";
94 mTimeZoneList << i18n ("-11:00 US/Samoa") 94 mTimeZoneList << i18n ("-11:00 US/Samoa")
95 << i18n ("-10:00 US/Hawaii") 95 << i18n ("-10:00 US/Hawaii")
96 << i18n ("-09:00 US/Alaska") 96 << i18n ("-09:00 US/Alaska")
97 << i18n ("-08:00 US/Pacific") 97 << i18n ("-08:00 US/Pacific")
98 << i18n ("-07:00 US/Mountain") 98 << i18n ("-07:00 US/Mountain")
99 << i18n ("-06:00 US/Central") 99 << i18n ("-06:00 US/Central")
100 << i18n ("-05:00 US/Eastern") 100 << i18n ("-05:00 US/Eastern")
101 << i18n ("-04:00 Brazil/West") 101 << i18n ("-04:00 Brazil/West")
102 << i18n ("-03:00 Brazil/East") 102 << i18n ("-03:00 Brazil/East")
103 << i18n ("-02:00 Brazil/DeNoronha") 103 << i18n ("-02:00 Brazil/DeNoronha")
104 << i18n ("-01:00 Atlantic/Azores") 104 << i18n ("-01:00 Atlantic/Azores")
105 << i18n (" 00:00 Europe/London(UTC)") 105 << i18n (" 00:00 Europe/London(UTC)")
106 << i18n ("+01:00 Europe/Oslo(CET)") 106 << i18n ("+01:00 Europe/Oslo(CET)")
107 << i18n ("+02:00 Europe/Helsinki") 107 << i18n ("+02:00 Europe/Helsinki")
108 << i18n ("+03:00 Europe/Moscow") 108 << i18n ("+03:00 Europe/Moscow")
109 << i18n ("+04:00 Indian/Mauritius") 109 << i18n ("+04:00 Indian/Mauritius")
110 << i18n ("+05:00 Indian/Maldives") 110 << i18n ("+05:00 Indian/Maldives")
111 << i18n ("+06:00 Indian/Chagos") 111 << i18n ("+06:00 Indian/Chagos")
112 << i18n ("+07:00 Asia/Bangkok") 112 << i18n ("+07:00 Asia/Bangkok")
113 << i18n ("+08:00 Asia/Hongkong") 113 << i18n ("+08:00 Asia/Hongkong")
114 << i18n ("+09:00 Asia/Tokyo") 114 << i18n ("+09:00 Asia/Tokyo")
115 << i18n ("+10:00 Asia/Vladivostok") 115 << i18n ("+10:00 Asia/Vladivostok")
116 << i18n ("+11:00 Asia/Magadan") 116 << i18n ("+11:00 Asia/Magadan")
117 << i18n ("+12:00 Asia/Kamchatka") 117 << i18n ("+12:00 Asia/Kamchatka")
118 // << i18n (" xx:xx User defined offset") 118 // << i18n (" xx:xx User defined offset")
119 << i18n (" Local Time"); 119 << i18n (" Local Time");
120 mSouthDaylight = false; 120 mSouthDaylight = false;
121 mTimeZoneOffset = 0; 121 mTimeZoneOffset = 0;
122 daylightEnabled = false; 122 daylightEnabled = false;
123} 123}
124 124
125void KLocale::setDateFormat( QString s ) 125void KLocale::setDateFormat( QString s )
126{ 126{
127 mDateFormat = s; 127 mDateFormat = s;
128} 128}
129 129
130void KLocale::setDateFormatShort( QString s ) 130void KLocale::setDateFormatShort( QString s )
131{ 131{
132 mDateFormatShort = s; 132 mDateFormatShort = s;
133} 133}
134 134
135void KLocale::setHore24Format ( bool b ) 135void KLocale::setHore24Format ( bool b )
136{ 136{
137 mHourF24Format = b; 137 mHourF24Format = b;
138} 138}
139void KLocale::setWeekStartMonday( bool b ) 139void KLocale::setWeekStartMonday( bool b )
140{ 140{
141 mWeekStartsMonday = b; 141 mWeekStartsMonday = b;
142} 142}
143 143
144KLocale::IntDateFormat KLocale::getIntDateFormat( ) 144KLocale::IntDateFormat KLocale::getIntDateFormat( )
145{ 145{
146 return mIntDateFormat; 146 return mIntDateFormat;
147 147
148} 148}
149void KLocale::setIntDateFormat( KLocale::IntDateFormat i ) 149void KLocale::setIntDateFormat( KLocale::IntDateFormat i )
150{ 150{
151 mIntDateFormat = i; 151 mIntDateFormat = i;
152} 152}
153KLocale::IntDateFormat KLocale::getIntTimeFormat( ) 153KLocale::IntDateFormat KLocale::getIntTimeFormat( )
154{ 154{
155 return mIntTimeFormat; 155 return mIntTimeFormat;
156 156
157} 157}
158void KLocale::setIntTimeFormat( KLocale::IntDateFormat i ) 158void KLocale::setIntTimeFormat( KLocale::IntDateFormat i )
159{ 159{
160 mIntTimeFormat = i; 160 mIntTimeFormat = i;
161} 161}
162 162
163void KLocale::setLanguage( int i ) 163void KLocale::setLanguage( int i )
164{ 164{
165 mLanguage = i; 165 mLanguage = i;
166} 166}
167QString KLocale::translate( const char *index ) const 167QString KLocale::translate( const char *index ) const
168{ 168{
169 return i18n( index ); 169 return i18n( index );
170} 170}
171 171
172QString KLocale::translate( const char *, const char *fallback) const 172QString KLocale::translate( const char *, const char *fallback) const
173{ 173{
174 return i18n( fallback ); 174 return i18n( fallback );
175} 175}
176 176
177QString KLocale::formatTime(const QTime &pTime, bool includeSecs, IntDateFormat intIntDateFormat) const 177QString KLocale::formatTime(const QTime &pTime, bool includeSecs, IntDateFormat intIntDateFormat) const
178{ 178{
179 const QString rst = timeFormat(intIntDateFormat); 179 const QString rst = timeFormat(intIntDateFormat);
180 180
181 // only "pm/am" here can grow, the rest shrinks, but 181 // only "pm/am" here can grow, the rest shrinks, but
182 // I'm rather safe than sorry 182 // I'm rather safe than sorry
183 QChar *buffer = new QChar[rst.length() * 3 / 2 + 30]; 183 QChar *buffer = new QChar[rst.length() * 3 / 2 + 30];
184 184
185 uint index = 0; 185 uint index = 0;
186 bool escape = false; 186 bool escape = false;
187 int number = 0; 187 int number = 0;
188 188
189 for ( uint format_index = 0; format_index < rst.length(); format_index++ ) 189 for ( uint format_index = 0; format_index < rst.length(); format_index++ )
190 { 190 {
191 if ( !escape ) 191 if ( !escape )
192 { 192 {
193 if ( rst.at( format_index ).unicode() == '%' ) 193 if ( rst.at( format_index ).unicode() == '%' )
194 escape = true; 194 escape = true;
195 else 195 else
196 buffer[index++] = rst.at( format_index ); 196 buffer[index++] = rst.at( format_index );
197 } 197 }
198 else 198 else
199 { 199 {
200 switch ( rst.at( format_index ).unicode() ) 200 switch ( rst.at( format_index ).unicode() )
201 { 201 {
202 case '%': 202 case '%':
203 buffer[index++] = '%'; 203 buffer[index++] = '%';
204 break; 204 break;
205 case 'H': 205 case 'H':
206 put_it_in( buffer, index, pTime.hour() ); 206 put_it_in( buffer, index, pTime.hour() );
207 break; 207 break;
208 case 'I': 208 case 'I':
209 put_it_in( buffer, index, ( pTime.hour() + 11) % 12 + 1 ); 209 put_it_in( buffer, index, ( pTime.hour() + 11) % 12 + 1 );
210 break; 210 break;
211 case 'M': 211 case 'M':
212 put_it_in( buffer, index, pTime.minute() ); 212 put_it_in( buffer, index, pTime.minute() );
213 break; 213 break;
214 case 'S': 214 case 'S':
215 if (includeSecs) 215 if (includeSecs)
216 put_it_in( buffer, index, pTime.second() ); 216 put_it_in( buffer, index, pTime.second() );
217 else 217 else
218 { 218 {
219 // we remove the seperator sign before the seconds and 219 // we remove the seperator sign before the seconds and
220 // assume that works everywhere 220 // assume that works everywhere
221 --index; 221 --index;
222 break; 222 break;
223 } 223 }
224 break; 224 break;
225 case 'k': 225 case 'k':
226 number = pTime.hour(); 226 number = pTime.hour();
227 case 'l': 227 case 'l':
228 // to share the code 228 // to share the code
229 if ( rst.at( format_index ).unicode() == 'l' ) 229 if ( rst.at( format_index ).unicode() == 'l' )
230 number = (pTime.hour() + 11) % 12 + 1; 230 number = (pTime.hour() + 11) % 12 + 1;
231 if ( number / 10 ) 231 if ( number / 10 )
232 buffer[index++] = number / 10 + '0'; 232 buffer[index++] = number / 10 + '0';
233 buffer[index++] = number % 10 + '0'; 233 buffer[index++] = number % 10 + '0';
234 break; 234 break;
235 case 'p': 235 case 'p':
236 { 236 {
237 QString s; 237 QString s;
238 if ( pTime.hour() >= 12 ) 238 if ( pTime.hour() >= 12 )
239 put_it_in( buffer, index, i18n("pm") ); 239 put_it_in( buffer, index, i18n("pm") );
240 else 240 else
241 put_it_in( buffer, index, i18n("am") ); 241 put_it_in( buffer, index, i18n("am") );
242 break; 242 break;
243 } 243 }
244 default: 244 default:
245 buffer[index++] = rst.at( format_index ); 245 buffer[index++] = rst.at( format_index );
246 break; 246 break;
247 } 247 }
248 escape = false; 248 escape = false;
249 } 249 }
250 } 250 }
251 QString ret( buffer, index ); 251 QString ret( buffer, index );
252 delete [] buffer; 252 delete [] buffer;
253 return ret; 253 return ret;
254} 254}
255 255
256QString KLocale::formatDate(const QDate &pDate, bool shortFormat, IntDateFormat intIntDateFormat) const 256QString KLocale::formatDate(const QDate &pDate, bool shortFormat, IntDateFormat intIntDateFormat) const
257{ 257{
258 const QString rst = shortFormat?dateFormatShort(intIntDateFormat):dateFormat(intIntDateFormat); 258 const QString rst = shortFormat?dateFormatShort(intIntDateFormat):dateFormat(intIntDateFormat);
259 259
260 // I'm rather safe than sorry 260 // I'm rather safe than sorry
261 QChar *buffer = new QChar[rst.length() * 3 / 2 + 50]; 261 QChar *buffer = new QChar[rst.length() * 3 / 2 + 50];
262 262
263 unsigned int index = 0; 263 unsigned int index = 0;
264 bool escape = false; 264 bool escape = false;
265 int number = 0; 265 int number = 0;
266 266
267 for ( uint format_index = 0; format_index < rst.length(); ++format_index ) 267 for ( uint format_index = 0; format_index < rst.length(); ++format_index )
268 { 268 {
269 if ( !escape ) 269 if ( !escape )
270 { 270 {
271 if ( rst.at( format_index ).unicode() == '%' ) 271 if ( rst.at( format_index ).unicode() == '%' )
272 escape = true; 272 escape = true;
273 else 273 else
274 buffer[index++] = rst.at( format_index ); 274 buffer[index++] = rst.at( format_index );
275 } 275 }
276 else 276 else
277 { 277 {
278 switch ( rst.at( format_index ).unicode() ) 278 switch ( rst.at( format_index ).unicode() )
279 { 279 {
280 case '%': 280 case '%':
281 buffer[index++] = '%'; 281 buffer[index++] = '%';
282 break; 282 break;
283 case 'Y': 283 case 'Y':
284 put_it_in( buffer, index, pDate.year() / 100 ); 284 put_it_in( buffer, index, pDate.year() / 100 );
285 case 'y': 285 case 'y':
286 put_it_in( buffer, index, pDate.year() % 100 ); 286 put_it_in( buffer, index, pDate.year() % 100 );
287 break; 287 break;
288 case 'n': 288 case 'n':
289 number = pDate.month(); 289 number = pDate.month();
290 case 'e': 290 case 'e':
291 // to share the code 291 // to share the code
292 if ( rst.at( format_index ).unicode() == 'e' ) 292 if ( rst.at( format_index ).unicode() == 'e' )
293 number = pDate.day(); 293 number = pDate.day();
294 if ( number / 10 ) 294 if ( number / 10 )
295 buffer[index++] = number / 10 + '0'; 295 buffer[index++] = number / 10 + '0';
296 buffer[index++] = number % 10 + '0'; 296 buffer[index++] = number % 10 + '0';
297 break; 297 break;
298 case 'm': 298 case 'm':
299 put_it_in( buffer, index, pDate.month() ); 299 put_it_in( buffer, index, pDate.month() );
300 break; 300 break;
301 case 'b': 301 case 'b':
302 put_it_in( buffer, index, monthName(pDate.month(), true) ); 302 put_it_in( buffer, index, monthName(pDate.month(), true) );
303 break; 303 break;
304 case 'B': 304 case 'B':
305 put_it_in( buffer, index, monthName(pDate.month(), false) ); 305 put_it_in( buffer, index, monthName(pDate.month(), false) );
306 break; 306 break;
307 case 'd': 307 case 'd':
308 put_it_in( buffer, index, pDate.day() ); 308 put_it_in( buffer, index, pDate.day() );
309 break; 309 break;
310 case 'a': 310 case 'a':
311 put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) ); 311 put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) );
312 break; 312 break;
313 case 'A': 313 case 'A':
314 put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) ); 314 put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) );
315 break; 315 break;
316 default: 316 default:
317 buffer[index++] = rst.at( format_index ); 317 buffer[index++] = rst.at( format_index );
318 break; 318 break;
319 } 319 }
320 escape = false; 320 escape = false;
321 } 321 }
322 } 322 }
323 QString ret( buffer, index ); 323 QString ret( buffer, index );
324 delete [] buffer; 324 delete [] buffer;
325 return ret; 325 return ret;
326} 326}
327 327
328QString KLocale::formatDateTime(const QDateTime &pDateTime, 328QString KLocale::formatDateTime(const QDateTime &pDateTime,
329 bool shortFormat, 329 bool shortFormat,
330 bool includeSeconds, 330 bool includeSeconds,
331 IntDateFormat intIntDateFormat) const 331 IntDateFormat intIntDateFormat) const
332{ 332{
333 return QString( "%1 %2") 333 QString format("%1 %2");
334 .arg( formatDate( pDateTime.date(), shortFormat, intIntDateFormat ) ) 334
335 .arg( formatTime( pDateTime.time(), includeSeconds , intIntDateFormat ) ); 335 if ( intIntDateFormat == Default )
336 format = "%1 %2";
337 else if ( intIntDateFormat == Format1 )
338 format = "%1 %2";
339 else if ( intIntDateFormat == ISODate )
340 format = "%1T%2";
341
342 return format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat ))
343 .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat ));
336} 344}
337 345
338QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const 346QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const
339{ 347{
340 return formatDateTime(pDateTime, true, intIntDateFormat); 348 return formatDateTime(pDateTime, true, true, intIntDateFormat);
341} 349}
342 350
343QDate KLocale::readDate(const QString &intstr, bool* ok) const 351QDate KLocale::readDate(const QString &intstr, bool* ok) const
344{ 352{
345 QDate date; 353 QDate date;
346 date = readDate(intstr, true, ok); 354 date = readDate(intstr, true, ok);
347 if (date.isValid()) return date; 355 if (date.isValid()) return date;
348 return readDate(intstr, false, ok); 356 return readDate(intstr, false, ok);
349} 357}
350 358
351QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const 359QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const
352{ 360{
353 QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace(); 361 QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace();
354 return readDate( intstr, fmt, ok ); 362 return readDate( intstr, fmt, ok );
355} 363}
356 364
357QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const 365QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const
358{ 366{
359 //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl; 367 //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl;
360 QString str = intstr.simplifyWhiteSpace().lower(); 368 QString str = intstr.simplifyWhiteSpace().lower();
361 int day = -1, month = -1; 369 int day = -1, month = -1;
362 // allow the year to be omitted if not in the format 370 // allow the year to be omitted if not in the format
363 int year = QDate::currentDate().year(); 371 int year = QDate::currentDate().year();
364 uint strpos = 0; 372 uint strpos = 0;
365 uint fmtpos = 0; 373 uint fmtpos = 0;
366 374
367 while (fmt.length() > fmtpos || str.length() > strpos) 375 while (fmt.length() > fmtpos || str.length() > strpos)
368 { 376 {
369 if ( !(fmt.length() > fmtpos && str.length() > strpos) ) 377 if ( !(fmt.length() > fmtpos && str.length() > strpos) )
370 goto error; 378 goto error;
371 379
372 QChar c = fmt.at(fmtpos++); 380 QChar c = fmt.at(fmtpos++);
373 381
374 if (c != '%') { 382 if (c != '%') {
375 if (c.isSpace()) 383 if (c.isSpace())
376 strpos++; 384 strpos++;
377 else if (c != str.at(strpos++)) 385 else if (c != str.at(strpos++))
378 goto error; 386 goto error;
379 continue; 387 continue;
380 } 388 }
381 389
382 // remove space at the begining 390 // remove space at the begining
383 if (str.length() > strpos && str.at(strpos).isSpace()) 391 if (str.length() > strpos && str.at(strpos).isSpace())
384 strpos++; 392 strpos++;
385 393
386 c = fmt.at(fmtpos++); 394 c = fmt.at(fmtpos++);
387 switch (c) 395 switch (c)
388 { 396 {
389 case 'a': 397 case 'a':
390 case 'A': 398 case 'A':
391 // this will just be ignored 399 // this will just be ignored
392 { // Cristian Tache: porting to Win: Block added because of "j" redefinition 400 { // Cristian Tache: porting to Win: Block added because of "j" redefinition
393 for (int j = 1; j < 8; j++) { 401 for (int j = 1; j < 8; j++) {
394 QString s = weekDayName(j, c == 'a').lower(); 402 QString s = weekDayName(j, c == 'a').lower();
395 int len = s.length(); 403 int len = s.length();
396 if (str.mid(strpos, len) == s) 404 if (str.mid(strpos, len) == s)
397 strpos += len; 405 strpos += len;
398 } 406 }
399 break; 407 break;
400 } 408 }
401 case 'b': 409 case 'b':
402 case 'B': 410 case 'B':
403 { // Cristian Tache: porting to Win: Block added because of "j" redefinition 411 { // Cristian Tache: porting to Win: Block added because of "j" redefinition
404 for (int j = 1; j < 13; j++) { 412 for (int j = 1; j < 13; j++) {
405 QString s = monthName(j, c == 'b').lower(); 413 QString s = monthName(j, c == 'b').lower();
406 int len = s.length(); 414 int len = s.length();
407 if (str.mid(strpos, len) == s) { 415 if (str.mid(strpos, len) == s) {
408 month = j; 416 month = j;
409 strpos += len; 417 strpos += len;
410 } 418 }
411 } 419 }
412 break; 420 break;
413 } 421 }
414 case 'd': 422 case 'd':
415 case 'e': 423 case 'e':
416 day = readInt(str, strpos); 424 day = readInt(str, strpos);
417 if (day < 1 || day > 31) 425 if (day < 1 || day > 31)
418 goto error; 426 goto error;
419 427
420 break; 428 break;
421 429
422 case 'n': 430 case 'n':
423 case 'm': 431 case 'm':
424 month = readInt(str, strpos); 432 month = readInt(str, strpos);
425 if (month < 1 || month > 12) 433 if (month < 1 || month > 12)
426 goto error; 434 goto error;
427 435
428 break; 436 break;
429 437
430 case 'Y': 438 case 'Y':
431 case 'y': 439 case 'y':
432 year = readInt(str, strpos); 440 year = readInt(str, strpos);
433 if (year < 0) 441 if (year < 0)
434 goto error; 442 goto error;
435 // Qt treats a year in the range 0-100 as 1900-1999. 443 // Qt treats a year in the range 0-100 as 1900-1999.
436 // It is nicer for the user if we treat 0-68 as 2000-2068 444 // It is nicer for the user if we treat 0-68 as 2000-2068
437 if (year < 69) 445 if (year < 69)
438 year += 2000; 446 year += 2000;
439 else if (c == 'y') 447 else if (c == 'y')
440 year += 1900; 448 year += 1900;
441 449
442 break; 450 break;
443 } 451 }
444 } 452 }
445 //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl; 453 //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl;
446 if ( year != -1 && month != -1 && day != -1 ) 454 if ( year != -1 && month != -1 && day != -1 )
447 { 455 {
448 if (ok) *ok = true; 456 if (ok) *ok = true;
449 return QDate(year, month, day); 457 return QDate(year, month, day);
450 } 458 }
451 error: 459 error:
452 if (ok) *ok = false; 460 if (ok) *ok = false;
453 return QDate(); // invalid date 461 return QDate(); // invalid date
454} 462}
455 463
456QTime KLocale::readTime(const QString &intstr, bool *ok) const 464QTime KLocale::readTime(const QString &intstr, bool *ok) const
457{ 465{
458 QTime _time; 466 QTime _time;
459 _time = readTime(intstr, true, ok); 467 _time = readTime(intstr, true, ok);
460 if (_time.isValid()) return _time; 468 if (_time.isValid()) return _time;
461 return readTime(intstr, false, ok); 469 return readTime(intstr, false, ok);
462} 470}
463 471
464QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const 472QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const
465{ 473{
466 QString str = intstr.simplifyWhiteSpace().lower(); 474 QString str = intstr.simplifyWhiteSpace().lower();
467 QString Format = timeFormat().simplifyWhiteSpace(); 475 QString Format = timeFormat().simplifyWhiteSpace();
468 if (!seconds) 476 if (!seconds)
469 Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null); 477 Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null);
470 478
471 int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds 479 int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds
472 480
473 bool g_12h = false; 481 bool g_12h = false;
474 bool pm = false; 482 bool pm = false;
475 uint strpos = 0; 483 uint strpos = 0;
476 uint Formatpos = 0; 484 uint Formatpos = 0;
477 485
478 while (Format.length() > Formatpos || str.length() > strpos) 486 while (Format.length() > Formatpos || str.length() > strpos)
479 { 487 {
480 if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error; 488 if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error;
481 489
482 QChar c = Format.at(Formatpos++); 490 QChar c = Format.at(Formatpos++);
483 491
484 if (c != '%') 492 if (c != '%')
485 { 493 {
486 if (c.isSpace()) 494 if (c.isSpace())
487 strpos++; 495 strpos++;
488 else if (c != str.at(strpos++)) 496 else if (c != str.at(strpos++))
489 goto error; 497 goto error;
490 continue; 498 continue;
491 } 499 }
492 500
493 // remove space at the begining 501 // remove space at the begining
494 if (str.length() > strpos && str.at(strpos).isSpace()) 502 if (str.length() > strpos && str.at(strpos).isSpace())
495 strpos++; 503 strpos++;
496 504
497 c = Format.at(Formatpos++); 505 c = Format.at(Formatpos++);
498 switch (c) 506 switch (c)
499 { 507 {
500 case 'p': 508 case 'p':
501 { 509 {
502 QString s; 510 QString s;
503 s = i18n("pm").lower(); 511 s = i18n("pm").lower();
504 int len = s.length(); 512 int len = s.length();
505 if (str.mid(strpos, len) == s) 513 if (str.mid(strpos, len) == s)
506 { 514 {
507 pm = true; 515 pm = true;
508 strpos += len; 516 strpos += len;
509 } 517 }
510 else 518 else
511 { 519 {
512 s = i18n("am").lower(); 520 s = i18n("am").lower();
513 len = s.length(); 521 len = s.length();
514 if (str.mid(strpos, len) == s) { 522 if (str.mid(strpos, len) == s) {
515 pm = false; 523 pm = false;
516 strpos += len; 524 strpos += len;
517 } 525 }
518 else 526 else
519 goto error; 527 goto error;
520 } 528 }
521 } 529 }
522 break; 530 break;
523 531
524 case 'k': 532 case 'k':
525 case 'H': 533 case 'H':
526 g_12h = false; 534 g_12h = false;
527 hour = readInt(str, strpos); 535 hour = readInt(str, strpos);
528 if (hour < 0 || hour > 23) 536 if (hour < 0 || hour > 23)
529 goto error; 537 goto error;
530 538
531 break; 539 break;
532 540
533 case 'l': 541 case 'l':
534 case 'I': 542 case 'I':
535 g_12h = true; 543 g_12h = true;
536 hour = readInt(str, strpos); 544 hour = readInt(str, strpos);
537 if (hour < 1 || hour > 12) 545 if (hour < 1 || hour > 12)
538 goto error; 546 goto error;
539 547
540 break; 548 break;
541 549
542 case 'M': 550 case 'M':
543 minute = readInt(str, strpos); 551 minute = readInt(str, strpos);
544 if (minute < 0 || minute > 59) 552 if (minute < 0 || minute > 59)
545 goto error; 553 goto error;
546 554
547 break; 555 break;
548 556
549 case 'S': 557 case 'S':
550 second = readInt(str, strpos); 558 second = readInt(str, strpos);
551 if (second < 0 || second > 59) 559 if (second < 0 || second > 59)
552 goto error; 560 goto error;
553 561
554 break; 562 break;
555 } 563 }
556 } 564 }
557 if (g_12h) 565 if (g_12h)
558 { 566 {
559 hour %= 12; 567 hour %= 12;
560 if (pm) hour += 12; 568 if (pm) hour += 12;
561 } 569 }
562 570
563 if (ok) *ok = true; 571 if (ok) *ok = true;
564 return QTime(hour, minute, second); 572 return QTime(hour, minute, second);
565 573
566 error: 574 error:
567 if (ok) *ok = false; 575 if (ok) *ok = false;
568 return QTime(-1, -1, -1); // return invalid date if it didn't work 576 return QTime(-1, -1, -1); // return invalid date if it didn't work
569 // This will be removed in the near future, since it gives a warning on stderr. 577 // This will be removed in the near future, since it gives a warning on stderr.
570 // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime. 578 // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime.
571} 579}
572 580
581QDateTime KLocale::readDateTime(const QString &intstr,
582 bool shortFormat,
583 bool includeSeconds,
584 IntDateFormat intIntDateFormat,
585 bool* ok) const
586{
587 bool ok1, ok2;
588
589 QDate m_date = readDate(date, &ok1);
590 QTime m_time = KGlobal::locale()->readTime(time, &ok2);
591 if ((ok1 == false) || (ok2 == false))
592 qDebug("Serializer::extractMeta invalid date or time !!!!!!!!!!!!!");
593
594}
595
596
573bool KLocale::use12Clock() const 597bool KLocale::use12Clock() const
574{ 598{
575 return !mHourF24Format ;; 599 return !mHourF24Format ;;
576} 600}
577 601
578bool KLocale::weekStartsMonday() const 602bool KLocale::weekStartsMonday() const
579{ 603{
580 return mWeekStartsMonday; 604 return mWeekStartsMonday;
581} 605}
582 606
583int KLocale::weekStartDay() const 607int KLocale::weekStartDay() const
584{ 608{
585 if ( mWeekStartsMonday ) 609 if ( mWeekStartsMonday )
586 return 1; 610 return 1;
587 return 7; 611 return 7;
588} 612}
589 613
590QString KLocale::weekDayName(int i,bool shortName) const 614QString KLocale::weekDayName(int i,bool shortName) const
591{ 615{
592 if ( shortName ) 616 if ( shortName )
593 switch ( i ) 617 switch ( i )
594 { 618 {
595 case 1: return i18n("Monday", "Mon"); 619 case 1: return i18n("Monday", "Mon");
596 case 2: return i18n("Tuesday", "Tue"); 620 case 2: return i18n("Tuesday", "Tue");
597 case 3: return i18n("Wednesday", "Wed"); 621 case 3: return i18n("Wednesday", "Wed");
598 case 4: return i18n("Thursday", "Thu"); 622 case 4: return i18n("Thursday", "Thu");
599 case 5: return i18n("Friday", "Fri"); 623 case 5: return i18n("Friday", "Fri");
600 case 6: return i18n("Saturday", "Sat"); 624 case 6: return i18n("Saturday", "Sat");
601 case 7: return i18n("Sunday", "Sun"); 625 case 7: return i18n("Sunday", "Sun");
602 } 626 }
603 else 627 else
604 switch ( i ) 628 switch ( i )
605 { 629 {
606 case 1: return i18n("Monday"); 630 case 1: return i18n("Monday");
607 case 2: return i18n("Tuesday"); 631 case 2: return i18n("Tuesday");
608 case 3: return i18n("Wednesday"); 632 case 3: return i18n("Wednesday");
609 case 4: return i18n("Thursday"); 633 case 4: return i18n("Thursday");
610 case 5: return i18n("Friday"); 634 case 5: return i18n("Friday");
611 case 6: return i18n("Saturday"); 635 case 6: return i18n("Saturday");
612 case 7: return i18n("Sunday"); 636 case 7: return i18n("Sunday");
613 } 637 }
614 638
615 return QString::null; 639 return QString::null;
616} 640}
617 641
618QString KLocale::monthName(int i,bool shortName) const 642QString KLocale::monthName(int i,bool shortName) const
619{ 643{
620 if ( shortName ) 644 if ( shortName )
621 switch ( i ) 645 switch ( i )
622 { 646 {
623 case 1: return i18n("January", "Jan"); 647 case 1: return i18n("January", "Jan");
624 case 2: return i18n("February", "Feb"); 648 case 2: return i18n("February", "Feb");
625 case 3: return i18n("March", "Mar"); 649 case 3: return i18n("March", "Mar");
626 case 4: return i18n("April", "Apr"); 650 case 4: return i18n("April", "Apr");
627 case 5: return i18n("May short", "May"); 651 case 5: return i18n("May short", "May");
628 case 6: return i18n("June", "Jun"); 652 case 6: return i18n("June", "Jun");
629 case 7: return i18n("July", "Jul"); 653 case 7: return i18n("July", "Jul");
630 case 8: return i18n("August", "Aug"); 654 case 8: return i18n("August", "Aug");
631 case 9: return i18n("September", "Sep"); 655 case 9: return i18n("September", "Sep");
632 case 10: return i18n("October", "Oct"); 656 case 10: return i18n("October", "Oct");
633 case 11: return i18n("November", "Nov"); 657 case 11: return i18n("November", "Nov");
634 case 12: return i18n("December", "Dec"); 658 case 12: return i18n("December", "Dec");
635 } 659 }
636 else 660 else
637 switch (i) 661 switch (i)
638 { 662 {
639 case 1: return i18n("January"); 663 case 1: return i18n("January");
640 case 2: return i18n("February"); 664 case 2: return i18n("February");
641 case 3: return i18n("March"); 665 case 3: return i18n("March");
642 case 4: return i18n("April"); 666 case 4: return i18n("April");
643 case 5: return i18n("May long", "May"); 667 case 5: return i18n("May long", "May");
644 case 6: return i18n("June"); 668 case 6: return i18n("June");
645 case 7: return i18n("July"); 669 case 7: return i18n("July");
646 case 8: return i18n("August"); 670 case 8: return i18n("August");
647 case 9: return i18n("September"); 671 case 9: return i18n("September");
648 case 10: return i18n("October"); 672 case 10: return i18n("October");
649 case 11: return i18n("November"); 673 case 11: return i18n("November");
650 case 12: return i18n("December"); 674 case 12: return i18n("December");
651 } 675 }
652 676
653 return QString::null; 677 return QString::null;
654} 678}
655 679
656QString KLocale::country() const 680QString KLocale::country() const
657{ 681{
658 return QString::null; 682 return QString::null;
659} 683}
660 684
661QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const 685QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const
662{ 686{
663 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat; 687 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
664 688
665 if ( QApplication::desktop()->width() < 480 ) { 689 if ( QApplication::desktop()->width() < 480 ) {
666 if ( dformat == Default ) 690 if ( dformat == Default )
667 return "%a %d %b %Y"; 691 return "%a %d %b %Y";
668 else if ( dformat == Format1 ) 692 else if ( dformat == Format1 )
669 return "%a %b %d %Y"; 693 return "%a %b %d %Y";
670 else if ( dformat == ISODate ) 694 else if ( dformat == ISODate )
671 return "%a %Y %b %d"; 695 return "%a %Y %b %d";
672 } else { 696 } else {
673 697
674 if ( dformat == Default ) 698 if ( dformat == Default )
675 return "%A %d %B %Y"; 699 return "%A %d %B %Y";
676 else if ( dformat == Format1 ) 700 else if ( dformat == Format1 )
677 return "%A %B %d %Y"; 701 return "%A %B %d %Y";
678 else if ( dformat == ISODate ) 702 else if ( dformat == ISODate )
679 return "%A %Y %B %d"; 703 return "%A %Y %B %d";
680 } 704 }
681 return mDateFormat ; 705 return mDateFormat ;
682} 706}
683 707
684QString KLocale::dateFormatShort(IntDateFormat intIntDateFormat) const 708QString KLocale::dateFormatShort(IntDateFormat intIntDateFormat) const
685{ 709{
686 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat; 710 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
687 711
688 if ( dformat == Default ) 712 if ( dformat == Default )
689 return "%d.%m.%Y"; 713 return "%d.%m.%Y";
690 else if ( dformat == Format1 ) 714 else if ( dformat == Format1 )
691 return "%m.%d.%Y"; 715 return "%m.%d.%Y";
692 else if ( dformat == ISODate ) // = Qt::ISODate 716 else if ( dformat == ISODate ) // = Qt::ISODate
693 return "%Y-%m-%d"; 717 return "%Y-%m-%d";
694 return mDateFormatShort ; 718 return mDateFormatShort ;
695 719
696} 720}
697 721
698 722
699QString KLocale::timeFormat(IntDateFormat intIntTimeFormat) const 723QString KLocale::timeFormat(IntDateFormat intIntTimeFormat) const
700{ 724{
701 const IntDateFormat tformat = (intIntTimeFormat == Undefined)?mIntTimeFormat:intIntTimeFormat; 725 const IntDateFormat tformat = (intIntTimeFormat == Undefined)?mIntTimeFormat:intIntTimeFormat;
702 726
703 if ( tformat == Default ) 727 if ( tformat == Default )
704 if ( mHourF24Format) 728 if ( mHourF24Format)
705 return "%H:%M:%S"; 729 return "%H:%M:%S";
706 else 730 else
707 return "%I:%M:%S%p"; 731 return "%I:%M:%S%p";
708 732
709 else if ( tformat == Format1 ) 733 else if ( tformat == Format1 )
710 if ( mHourF24Format) 734 if ( mHourF24Format)
711 return "%H:%M:%S"; 735 return "%H:%M:%S";
712 else 736 else
713 return "%I:%M:%S%p"; 737 return "%I:%M:%S%p";
714 738
715 else if ( tformat == ISODate ) // = Qt::ISODate 739 else if ( tformat == ISODate ) // = Qt::ISODate
716 if ( mHourF24Format) 740 if ( mHourF24Format)
717 return "%H:%M:%S"; 741 return "%H:%M:%S";
718 else 742 else
719 return "%I:%M:%S%p"; 743 return "%I:%M:%S%p";
720 744
721} 745}
722 746
723void KLocale::insertCatalogue ( const QString & ) 747void KLocale::insertCatalogue ( const QString & )
724{ 748{
725} 749}
726 750
727KCalendarSystem *KLocale::calendar() 751KCalendarSystem *KLocale::calendar()
728{ 752{
729 if ( !mCalendarSystem ) { 753 if ( !mCalendarSystem ) {
730 mCalendarSystem = new KCalendarSystemGregorian; 754 mCalendarSystem = new KCalendarSystemGregorian;
731 } 755 }
732 756
733 return mCalendarSystem; 757 return mCalendarSystem;
734} 758}
735 759
736int KLocale::timezoneOffset( QString timeZone ) 760int KLocale::timezoneOffset( QString timeZone )
737{ 761{
738 int ret = 1001; 762 int ret = 1001;
739 int index = mTimeZoneList.findIndex( timeZone ); 763 int index = mTimeZoneList.findIndex( timeZone );
740 if ( index < 24 ) 764 if ( index < 24 )
741 ret = ( index-11 ) * 60 ; 765 ret = ( index-11 ) * 60 ;
742 return ret; 766 return ret;
743} 767}
744 768
745QStringList KLocale::timeZoneList() const 769QStringList KLocale::timeZoneList() const
746{ 770{
747 return mTimeZoneList; 771 return mTimeZoneList;
748} 772}
749void KLocale::setTimezone( const QString &timeZone ) 773void KLocale::setTimezone( const QString &timeZone )
750{ 774{
751 mTimeZoneOffset = timezoneOffset( timeZone ); 775 mTimeZoneOffset = timezoneOffset( timeZone );
752} 776}
753 777
754void KLocale::setDaylightSaving( bool b, int start , int end ) 778void KLocale::setDaylightSaving( bool b, int start , int end )
755{ 779{
756 daylightEnabled = b; 780 daylightEnabled = b;
757 daylightStart = start; 781 daylightStart = start;
758 daylightEnd = end; 782 daylightEnd = end;
759 mSouthDaylight = (end < start); 783 mSouthDaylight = (end < start);
760 // qDebug("klocale daylight %d %d %d ", b, start , end ); 784 // qDebug("klocale daylight %d %d %d ", b, start , end );
761} 785}
762 786
763int KLocale::localTimeOffset( const QDateTime &dt ) 787int KLocale::localTimeOffset( const QDateTime &dt )
764{ 788{
765 bool addDaylight = false; 789 bool addDaylight = false;
766 if ( daylightEnabled ) { 790 if ( daylightEnabled ) {
767 int d_end, d_start; 791 int d_end, d_start;
768 int dayofyear = dt.date().dayOfYear(); 792 int dayofyear = dt.date().dayOfYear();
769 int year = dt.date().year(); 793 int year = dt.date().year();
770 int add = 0; 794 int add = 0;
771 if ( QDate::leapYear(year) ) 795 if ( QDate::leapYear(year) )
772 add = 1; 796 add = 1;
773 QDate date ( year,1,1 ); 797 QDate date ( year,1,1 );
774 if ( daylightEnd > 59 ) 798 if ( daylightEnd > 59 )
775 d_end = daylightEnd +add; 799 d_end = daylightEnd +add;
776 else 800 else
777 d_end = daylightEnd; 801 d_end = daylightEnd;
778 if ( daylightStart > 59 ) 802 if ( daylightStart > 59 )
779 d_start = daylightStart +add; 803 d_start = daylightStart +add;
780 else 804 else
781 d_start = daylightStart; 805 d_start = daylightStart;
782 QDate s_date = date.addDays( d_start -1 ); 806 QDate s_date = date.addDays( d_start -1 );
783 QDate e_date = date.addDays( d_end -1 ); 807 QDate e_date = date.addDays( d_end -1 );
784 int dof = s_date.dayOfWeek(); 808 int dof = s_date.dayOfWeek();
785 if ( dof < 7 ) 809 if ( dof < 7 )
786 s_date = s_date.addDays( -dof ); 810 s_date = s_date.addDays( -dof );
787 dof = e_date.dayOfWeek(); 811 dof = e_date.dayOfWeek();
788 if ( dof < 7 ) 812 if ( dof < 7 )
789 e_date = e_date.addDays( -dof ); 813 e_date = e_date.addDays( -dof );
790 QTime startTime ( 3,0,0 ); 814 QTime startTime ( 3,0,0 );
791 QDateTime startDt( s_date, startTime ); 815 QDateTime startDt( s_date, startTime );
792 QDateTime endDt( e_date, startTime ); 816 QDateTime endDt( e_date, startTime );
793 //qDebug("dayligt saving start %s end %s ",startDt.toString().latin1(),endDt.toString().latin1( )); 817 //qDebug("dayligt saving start %s end %s ",startDt.toString().latin1(),endDt.toString().latin1( ));
794 if ( mSouthDaylight ) { 818 if ( mSouthDaylight ) {
795 if ( ! ( endDt < dt && dt < startDt) ) 819 if ( ! ( endDt < dt && dt < startDt) )
796 addDaylight = true; 820 addDaylight = true;
797 } else { 821 } else {
798 if ( startDt < dt && dt < endDt ) 822 if ( startDt < dt && dt < endDt )
799 addDaylight = true; 823 addDaylight = true;
800 824
801 825
802 } 826 }
803 } 827 }
804 int addMin = 0; 828 int addMin = 0;
805 if ( addDaylight ) 829 if ( addDaylight )
806 addMin = 60; 830 addMin = 60;
807 return mTimeZoneOffset + addMin; 831 return mTimeZoneOffset + addMin;
808} 832}
809// ****************************************************************** 833// ******************************************************************
810// added LR 834// added LR
811QString KLocale::formatNumber(double num, int precision) const 835QString KLocale::formatNumber(double num, int precision) const
812{ 836{
813 bool neg = num < 0; 837 bool neg = num < 0;
814 if (precision == -1) precision = 2; 838 if (precision == -1) precision = 2;
815 QString res = QString::number(neg?-num:num, 'f', precision); 839 QString res = QString::number(neg?-num:num, 'f', precision);
816 int pos = res.find('.'); 840 int pos = res.find('.');
817 if (pos == -1) pos = res.length(); 841 if (pos == -1) pos = res.length();
818 else res.replace(pos, 1, decimalSymbol()); 842 else res.replace(pos, 1, decimalSymbol());
819 843
820 while (0 < (pos -= 3)) 844 while (0 < (pos -= 3))
821 res.insert(pos, thousandsSeparator()); // thousand sep 845 res.insert(pos, thousandsSeparator()); // thousand sep
822 846
823 // How can we know where we should put the sign? 847 // How can we know where we should put the sign?
824 res.prepend(neg?negativeSign():positiveSign()); 848 res.prepend(neg?negativeSign():positiveSign());
825 849
826 return res; 850 return res;
827} 851}
828QString KLocale::formatNumber(const QString &numStr) const 852QString KLocale::formatNumber(const QString &numStr) const
829{ 853{
830 return formatNumber(numStr.toDouble()); 854 return formatNumber(numStr.toDouble());
831} 855}
832double KLocale::readNumber(const QString &_str, bool * ok) const 856double KLocale::readNumber(const QString &_str, bool * ok) const
833{ 857{
834 QString str = _str.stripWhiteSpace(); 858 QString str = _str.stripWhiteSpace();
835 bool neg = str.find(negativeSign()) == 0; 859 bool neg = str.find(negativeSign()) == 0;
836 if (neg) 860 if (neg)
837 str.remove( 0, negativeSign().length() ); 861 str.remove( 0, negativeSign().length() );
838 862
839 /* will hold the scientific notation portion of the number. 863 /* will hold the scientific notation portion of the number.
840 Example, with 2.34E+23, exponentialPart == "E+23" 864 Example, with 2.34E+23, exponentialPart == "E+23"
841 */ 865 */
842 QString exponentialPart; 866 QString exponentialPart;
843 int EPos; 867 int EPos;
844 868
845 EPos = str.find('E', 0, false); 869 EPos = str.find('E', 0, false);
846 870
847 if (EPos != -1) 871 if (EPos != -1)
848 { 872 {
849 exponentialPart = str.mid(EPos); 873 exponentialPart = str.mid(EPos);
850 str = str.left(EPos); 874 str = str.left(EPos);
851 } 875 }
852 876
853 int pos = str.find(decimalSymbol()); 877 int pos = str.find(decimalSymbol());
854 QString major; 878 QString major;
855 QString minor; 879 QString minor;
856 if ( pos == -1 ) 880 if ( pos == -1 )
857 major = str; 881 major = str;
858 else 882 else
859 { 883 {
860 major = str.left(pos); 884 major = str.left(pos);
861 minor = str.mid(pos + decimalSymbol().length()); 885 minor = str.mid(pos + decimalSymbol().length());
862 } 886 }
863 887
864 // Remove thousand separators 888 // Remove thousand separators
865 int thlen = thousandsSeparator().length(); 889 int thlen = thousandsSeparator().length();
866 int lastpos = 0; 890 int lastpos = 0;
867 while ( ( pos = major.find( thousandsSeparator() ) ) > 0 ) 891 while ( ( pos = major.find( thousandsSeparator() ) ) > 0 )
868 { 892 {
869 // e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N 893 // e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N
870 int fromEnd = major.length() - pos; 894 int fromEnd = major.length() - pos;
871 if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error 895 if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error
872 || pos - lastpos > 3 // More than 3 digits between two separators -> error 896 || pos - lastpos > 3 // More than 3 digits between two separators -> error
873 || pos == 0 // Can't start with a separator 897 || pos == 0 // Can't start with a separator
874 || (lastpos>0 && pos-lastpos!=3)) // Must have exactly 3 digits between two separators 898 || (lastpos>0 && pos-lastpos!=3)) // Must have exactly 3 digits between two separators
875 { 899 {
876 if (ok) *ok = false; 900 if (ok) *ok = false;
877 return 0.0; 901 return 0.0;
878 } 902 }
879 903
880 lastpos = pos; 904 lastpos = pos;
881 major.remove( pos, thlen ); 905 major.remove( pos, thlen );
882 } 906 }
883 if (lastpos>0 && major.length()-lastpos!=3) // Must have exactly 3 digits after the last separator 907 if (lastpos>0 && major.length()-lastpos!=3) // Must have exactly 3 digits after the last separator
884 { 908 {
885 if (ok) *ok = false; 909 if (ok) *ok = false;
886 return 0.0; 910 return 0.0;
887 } 911 }
888 912
889 QString tot; 913 QString tot;
890 if (neg) tot = '-'; 914 if (neg) tot = '-';
891 915
892 tot += major + '.' + minor + exponentialPart; 916 tot += major + '.' + minor + exponentialPart;
893 917
894 return tot.toDouble(ok); 918 return tot.toDouble(ok);
895} 919}
896QString KLocale::decimalSymbol() const 920QString KLocale::decimalSymbol() const
897{ 921{
898 922
899 return m_decimalSymbol; 923 return m_decimalSymbol;
900} 924}
901 925
902QString KLocale::thousandsSeparator() const 926QString KLocale::thousandsSeparator() const
903{ 927{
904 928
905 return m_thousandsSeparator; 929 return m_thousandsSeparator;
906} 930}
907QString KLocale::positiveSign() const 931QString KLocale::positiveSign() const
908{ 932{
909 return m_positiveSign; 933 return m_positiveSign;
910} 934}
911 935
912QString KLocale::negativeSign() const 936QString KLocale::negativeSign() const
913{ 937{
914 return m_negativeSign; 938 return m_negativeSign;
915} 939}