summaryrefslogtreecommitdiffabout
authorulf69 <ulf69>2004-10-18 03:01:17 (UTC)
committer ulf69 <ulf69>2004-10-18 03:01:17 (UTC)
commit1712d92ea25b220273859d985bc7211fa7a97a39 (patch) (unidiff)
tree00b603af10b0da8d757ffec5c29f73953fbf03e0
parent2fd3f09238a624b1a91793d43b5f3653e2b34763 (diff)
downloadkdepimpi-1712d92ea25b220273859d985bc7211fa7a97a39.zip
kdepimpi-1712d92ea25b220273859d985bc7211fa7a97a39.tar.gz
kdepimpi-1712d92ea25b220273859d985bc7211fa7a97a39.tar.bz2
return a real empty value in readDateTime
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/klocale.cpp20
1 files changed, 15 insertions, 5 deletions
diff --git a/microkde/kdecore/klocale.cpp b/microkde/kdecore/klocale.cpp
index 8b4513e..27acfec 100644
--- a/microkde/kdecore/klocale.cpp
+++ b/microkde/kdecore/klocale.cpp
@@ -1,978 +1,988 @@
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 QString format("%1 %2"); 333 QString format("%1 %2");
334 334
335 if ( intIntDateFormat == Default ) 335 if ( intIntDateFormat == Default )
336 format = "%1 %2"; 336 format = "%1 %2";
337 else if ( intIntDateFormat == Format1 ) 337 else if ( intIntDateFormat == Format1 )
338 format = "%1 %2"; 338 format = "%1 %2";
339 else if ( intIntDateFormat == ISODate ) 339 else if ( intIntDateFormat == ISODate )
340 format = "%1T%2"; 340 format = "%1T%2";
341 341
342 return format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat )) 342 QString res = format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat ))
343 .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat )); 343 .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat ));
344
345 //qDebug("KLocale::formatDateTime transformed %s, into %s", pDateTime.toString().latin1(), res.latin1() );
346
347 return res;
344} 348}
345 349
346QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const 350QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const
347{ 351{
348 return formatDateTime(pDateTime, true, true, intIntDateFormat); 352 return formatDateTime(pDateTime, true, true, intIntDateFormat);
349} 353}
350 354
351QDate KLocale::readDate(const QString &intstr, bool* ok) const 355QDate KLocale::readDate(const QString &intstr, bool* ok) const
352{ 356{
353 QDate date; 357 QDate date;
354 date = readDate(intstr, true, ok); 358 date = readDate(intstr, true, ok);
355 if (date.isValid()) return date; 359 if (date.isValid()) return date;
356 return readDate(intstr, false, ok); 360 return readDate(intstr, false, ok);
357} 361}
358 362
359QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const 363QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const
360{ 364{
361 QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace(); 365 QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace();
362 return readDate( intstr, fmt, ok ); 366 return readDate( intstr, fmt, ok );
363} 367}
364 368
365QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const 369QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const
366{ 370{
367 //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl; 371 //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl;
368 QString str = intstr.simplifyWhiteSpace().lower(); 372 QString str = intstr.simplifyWhiteSpace().lower();
369 int day = -1, month = -1; 373 int day = -1, month = -1;
370 // allow the year to be omitted if not in the format 374 // allow the year to be omitted if not in the format
371 int year = QDate::currentDate().year(); 375 int year = QDate::currentDate().year();
372 uint strpos = 0; 376 uint strpos = 0;
373 uint fmtpos = 0; 377 uint fmtpos = 0;
374 378
375 while (fmt.length() > fmtpos || str.length() > strpos) 379 while (fmt.length() > fmtpos || str.length() > strpos)
376 { 380 {
377 if ( !(fmt.length() > fmtpos && str.length() > strpos) ) 381 if ( !(fmt.length() > fmtpos && str.length() > strpos) )
378 goto error; 382 goto error;
379 383
380 QChar c = fmt.at(fmtpos++); 384 QChar c = fmt.at(fmtpos++);
381 385
382 if (c != '%') { 386 if (c != '%') {
383 if (c.isSpace()) 387 if (c.isSpace())
384 strpos++; 388 strpos++;
385 else if (c != str.at(strpos++)) 389 else if (c != str.at(strpos++))
386 goto error; 390 goto error;
387 continue; 391 continue;
388 } 392 }
389 393
390 // remove space at the begining 394 // remove space at the begining
391 if (str.length() > strpos && str.at(strpos).isSpace()) 395 if (str.length() > strpos && str.at(strpos).isSpace())
392 strpos++; 396 strpos++;
393 397
394 c = fmt.at(fmtpos++); 398 c = fmt.at(fmtpos++);
395 switch (c) 399 switch (c)
396 { 400 {
397 case 'a': 401 case 'a':
398 case 'A': 402 case 'A':
399 // this will just be ignored 403 // this will just be ignored
400 { // Cristian Tache: porting to Win: Block added because of "j" redefinition 404 { // Cristian Tache: porting to Win: Block added because of "j" redefinition
401 for (int j = 1; j < 8; j++) { 405 for (int j = 1; j < 8; j++) {
402 QString s = weekDayName(j, c == 'a').lower(); 406 QString s = weekDayName(j, c == 'a').lower();
403 int len = s.length(); 407 int len = s.length();
404 if (str.mid(strpos, len) == s) 408 if (str.mid(strpos, len) == s)
405 strpos += len; 409 strpos += len;
406 } 410 }
407 break; 411 break;
408 } 412 }
409 case 'b': 413 case 'b':
410 case 'B': 414 case 'B':
411 { // Cristian Tache: porting to Win: Block added because of "j" redefinition 415 { // Cristian Tache: porting to Win: Block added because of "j" redefinition
412 for (int j = 1; j < 13; j++) { 416 for (int j = 1; j < 13; j++) {
413 QString s = monthName(j, c == 'b').lower(); 417 QString s = monthName(j, c == 'b').lower();
414 int len = s.length(); 418 int len = s.length();
415 if (str.mid(strpos, len) == s) { 419 if (str.mid(strpos, len) == s) {
416 month = j; 420 month = j;
417 strpos += len; 421 strpos += len;
418 } 422 }
419 } 423 }
420 break; 424 break;
421 } 425 }
422 case 'd': 426 case 'd':
423 case 'e': 427 case 'e':
424 day = readInt(str, strpos); 428 day = readInt(str, strpos);
425 if (day < 1 || day > 31) 429 if (day < 1 || day > 31)
426 goto error; 430 goto error;
427 431
428 break; 432 break;
429 433
430 case 'n': 434 case 'n':
431 case 'm': 435 case 'm':
432 month = readInt(str, strpos); 436 month = readInt(str, strpos);
433 if (month < 1 || month > 12) 437 if (month < 1 || month > 12)
434 goto error; 438 goto error;
435 439
436 break; 440 break;
437 441
438 case 'Y': 442 case 'Y':
439 case 'y': 443 case 'y':
440 year = readInt(str, strpos); 444 year = readInt(str, strpos);
441 if (year < 0) 445 if (year < 0)
442 goto error; 446 goto error;
443 // Qt treats a year in the range 0-100 as 1900-1999. 447 // Qt treats a year in the range 0-100 as 1900-1999.
444 // It is nicer for the user if we treat 0-68 as 2000-2068 448 // It is nicer for the user if we treat 0-68 as 2000-2068
445 if (year < 69) 449 if (year < 69)
446 year += 2000; 450 year += 2000;
447 else if (c == 'y') 451 else if (c == 'y')
448 year += 1900; 452 year += 1900;
449 453
450 break; 454 break;
451 } 455 }
452 } 456 }
453 //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl; 457 //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl;
454 if ( year != -1 && month != -1 && day != -1 ) 458 if ( year != -1 && month != -1 && day != -1 )
455 { 459 {
456 if (ok) *ok = true; 460 if (ok) *ok = true;
457 return QDate(year, month, day); 461 return QDate(year, month, day);
458 } 462 }
459 error: 463 error:
460 if (ok) *ok = false; 464 if (ok) *ok = false;
461 return QDate(); // invalid date 465 return QDate(); // invalid date
462} 466}
463 467
464QTime KLocale::readTime(const QString &intstr, bool *ok) const 468QTime KLocale::readTime(const QString &intstr, bool *ok) const
465{ 469{
466 QTime _time; 470 QTime _time;
467 _time = readTime(intstr, true, ok); 471 _time = readTime(intstr, true, ok);
468 if (_time.isValid()) return _time; 472 if (_time.isValid()) return _time;
469 return readTime(intstr, false, ok); 473 return readTime(intstr, false, ok);
470} 474}
471 475
472QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const 476QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const
473{ 477{
474 QString str = intstr.simplifyWhiteSpace().lower(); 478 QString str = intstr.simplifyWhiteSpace().lower();
475 QString Format = timeFormat().simplifyWhiteSpace(); 479 QString Format = timeFormat().simplifyWhiteSpace();
476 if (!seconds) 480 if (!seconds)
477 Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null); 481 Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null);
478 482
479 int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds 483 int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds
480 484
481 bool g_12h = false; 485 bool g_12h = false;
482 bool pm = false; 486 bool pm = false;
483 uint strpos = 0; 487 uint strpos = 0;
484 uint Formatpos = 0; 488 uint Formatpos = 0;
485 489
486 while (Format.length() > Formatpos || str.length() > strpos) 490 while (Format.length() > Formatpos || str.length() > strpos)
487 { 491 {
488 if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error; 492 if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error;
489 493
490 QChar c = Format.at(Formatpos++); 494 QChar c = Format.at(Formatpos++);
491 495
492 if (c != '%') 496 if (c != '%')
493 { 497 {
494 if (c.isSpace()) 498 if (c.isSpace())
495 strpos++; 499 strpos++;
496 else if (c != str.at(strpos++)) 500 else if (c != str.at(strpos++))
497 goto error; 501 goto error;
498 continue; 502 continue;
499 } 503 }
500 504
501 // remove space at the begining 505 // remove space at the begining
502 if (str.length() > strpos && str.at(strpos).isSpace()) 506 if (str.length() > strpos && str.at(strpos).isSpace())
503 strpos++; 507 strpos++;
504 508
505 c = Format.at(Formatpos++); 509 c = Format.at(Formatpos++);
506 switch (c) 510 switch (c)
507 { 511 {
508 case 'p': 512 case 'p':
509 { 513 {
510 QString s; 514 QString s;
511 s = i18n("pm").lower(); 515 s = i18n("pm").lower();
512 int len = s.length(); 516 int len = s.length();
513 if (str.mid(strpos, len) == s) 517 if (str.mid(strpos, len) == s)
514 { 518 {
515 pm = true; 519 pm = true;
516 strpos += len; 520 strpos += len;
517 } 521 }
518 else 522 else
519 { 523 {
520 s = i18n("am").lower(); 524 s = i18n("am").lower();
521 len = s.length(); 525 len = s.length();
522 if (str.mid(strpos, len) == s) { 526 if (str.mid(strpos, len) == s) {
523 pm = false; 527 pm = false;
524 strpos += len; 528 strpos += len;
525 } 529 }
526 else 530 else
527 goto error; 531 goto error;
528 } 532 }
529 } 533 }
530 break; 534 break;
531 535
532 case 'k': 536 case 'k':
533 case 'H': 537 case 'H':
534 g_12h = false; 538 g_12h = false;
535 hour = readInt(str, strpos); 539 hour = readInt(str, strpos);
536 if (hour < 0 || hour > 23) 540 if (hour < 0 || hour > 23)
537 goto error; 541 goto error;
538 542
539 break; 543 break;
540 544
541 case 'l': 545 case 'l':
542 case 'I': 546 case 'I':
543 g_12h = true; 547 g_12h = true;
544 hour = readInt(str, strpos); 548 hour = readInt(str, strpos);
545 if (hour < 1 || hour > 12) 549 if (hour < 1 || hour > 12)
546 goto error; 550 goto error;
547 551
548 break; 552 break;
549 553
550 case 'M': 554 case 'M':
551 minute = readInt(str, strpos); 555 minute = readInt(str, strpos);
552 if (minute < 0 || minute > 59) 556 if (minute < 0 || minute > 59)
553 goto error; 557 goto error;
554 558
555 break; 559 break;
556 560
557 case 'S': 561 case 'S':
558 second = readInt(str, strpos); 562 second = readInt(str, strpos);
559 if (second < 0 || second > 59) 563 if (second < 0 || second > 59)
560 goto error; 564 goto error;
561 565
562 break; 566 break;
563 } 567 }
564 } 568 }
565 if (g_12h) 569 if (g_12h)
566 { 570 {
567 hour %= 12; 571 hour %= 12;
568 if (pm) hour += 12; 572 if (pm) hour += 12;
569 } 573 }
570 574
571 if (ok) *ok = true; 575 if (ok) *ok = true;
572 return QTime(hour, minute, second); 576 return QTime(hour, minute, second);
573 577
574 error: 578 error:
575 if (ok) *ok = false; 579 if (ok) *ok = false;
576 return QTime(-1, -1, -1); // return invalid date if it didn't work 580 return QTime(-1, -1, -1); // return invalid date if it didn't work
577 // This will be removed in the near future, since it gives a warning on stderr. 581 // This will be removed in the near future, since it gives a warning on stderr.
578 // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime. 582 // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime.
579} 583}
580 584
581QDateTime KLocale::readDateTime(const QString &intstr, 585QDateTime KLocale::readDateTime(const QString &intstr,
582 IntDateFormat intIntDateFormat, 586 IntDateFormat intIntDateFormat,
583 bool* ok) const 587 bool* ok) const
584{ 588{
585 bool ok1, ok2; 589 bool ok1, ok2;
586 590
587 // AT the moment we can not read any other format then ISODate 591 // AT the moment we can not read any other format then ISODate
588 if ( intIntDateFormat != ISODate ) 592 if ( intIntDateFormat != ISODate )
589 { 593 {
590 qDebug("KLocale::readDateTime, only ISODate is supported."); 594 qDebug("KLocale::readDateTime, only ISODate is supported.");
591 return QDateTime(); 595 return QDateTime();
592 } 596 }
593 597
594 int pos = intstr.find("T"); 598 int pos = intstr.find("T");
595 QString date = intstr.left(pos); 599 QString date = intstr.left(pos);
596 QString time = intstr.mid(pos+1); 600 QString time = intstr.mid(pos+1);
597 601
598 QString dformat = dateFormat(intIntDateFormat); 602 QString dformat = dateFormat(intIntDateFormat);
599 QString tformat = timeFormat(intIntDateFormat); 603 QString tformat = timeFormat(intIntDateFormat);
600 604
601 QDate m_date = readDate(date, dformat, &ok1); 605 QDate m_date = readDate(date, dformat, &ok1);
602 QTime m_time = readTime(time, tformat, &ok2); 606 QTime m_time = readTime(time, tformat, &ok2);
603 607
608 QDateTime m_dt;
609
604 if (ok) 610 if (ok)
605 { 611 {
606 if ((ok1 == false) || (ok2 == false)) 612 if ((ok1 == false) || (ok2 == false))
607 *ok = false; 613 *ok = false;
608 else 614 else
609 *ok = true; 615 *ok = true;
610 } 616 }
611 QDateTime m_dt;
612 m_dt.setDate(m_date);
613 m_dt.setTime(m_time);
614 617
615 qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); 618 //only set values if both operations returned true.
619 if ((ok1 == true) && (ok2 == true))
620 {
621 m_dt.setDate(m_date);
622 m_dt.setTime(m_time);
623 }
624
625 //qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2);
616 return m_dt; 626 return m_dt;
617} 627}
618 628
619QDate KLocale::readDate(const QString &intstr, 629QDate KLocale::readDate(const QString &intstr,
620 IntDateFormat intIntDateFormat, 630 IntDateFormat intIntDateFormat,
621 bool* ok) const 631 bool* ok) const
622{ 632{
623 bool ok1; 633 bool ok1;
624 634
625 QString dformat = dateFormat(intIntDateFormat); 635 QString dformat = dateFormat(intIntDateFormat);
626 636
627 QDate m_date = readDate(intstr, dformat, &ok1); 637 QDate m_date = readDate(intstr, dformat, &ok1);
628 638
629 if (ok) 639 if (ok)
630 *ok = ok1; 640 *ok = ok1;
631 641
632 //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); 642 //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2);
633 return m_date; 643 return m_date;
634} 644}
635 645
636 646
637bool KLocale::use12Clock() const 647bool KLocale::use12Clock() const
638{ 648{
639 return !mHourF24Format ;; 649 return !mHourF24Format ;;
640} 650}
641 651
642bool KLocale::weekStartsMonday() const 652bool KLocale::weekStartsMonday() const
643{ 653{
644 return mWeekStartsMonday; 654 return mWeekStartsMonday;
645} 655}
646 656
647int KLocale::weekStartDay() const 657int KLocale::weekStartDay() const
648{ 658{
649 if ( mWeekStartsMonday ) 659 if ( mWeekStartsMonday )
650 return 1; 660 return 1;
651 return 7; 661 return 7;
652} 662}
653 663
654QString KLocale::weekDayName(int i,bool shortName) const 664QString KLocale::weekDayName(int i,bool shortName) const
655{ 665{
656 if ( shortName ) 666 if ( shortName )
657 switch ( i ) 667 switch ( i )
658 { 668 {
659 case 1: return i18n("Monday", "Mon"); 669 case 1: return i18n("Monday", "Mon");
660 case 2: return i18n("Tuesday", "Tue"); 670 case 2: return i18n("Tuesday", "Tue");
661 case 3: return i18n("Wednesday", "Wed"); 671 case 3: return i18n("Wednesday", "Wed");
662 case 4: return i18n("Thursday", "Thu"); 672 case 4: return i18n("Thursday", "Thu");
663 case 5: return i18n("Friday", "Fri"); 673 case 5: return i18n("Friday", "Fri");
664 case 6: return i18n("Saturday", "Sat"); 674 case 6: return i18n("Saturday", "Sat");
665 case 7: return i18n("Sunday", "Sun"); 675 case 7: return i18n("Sunday", "Sun");
666 } 676 }
667 else 677 else
668 switch ( i ) 678 switch ( i )
669 { 679 {
670 case 1: return i18n("Monday"); 680 case 1: return i18n("Monday");
671 case 2: return i18n("Tuesday"); 681 case 2: return i18n("Tuesday");
672 case 3: return i18n("Wednesday"); 682 case 3: return i18n("Wednesday");
673 case 4: return i18n("Thursday"); 683 case 4: return i18n("Thursday");
674 case 5: return i18n("Friday"); 684 case 5: return i18n("Friday");
675 case 6: return i18n("Saturday"); 685 case 6: return i18n("Saturday");
676 case 7: return i18n("Sunday"); 686 case 7: return i18n("Sunday");
677 } 687 }
678 688
679 return QString::null; 689 return QString::null;
680} 690}
681 691
682QString KLocale::monthName(int i,bool shortName) const 692QString KLocale::monthName(int i,bool shortName) const
683{ 693{
684 if ( shortName ) 694 if ( shortName )
685 switch ( i ) 695 switch ( i )
686 { 696 {
687 case 1: return i18n("January", "Jan"); 697 case 1: return i18n("January", "Jan");
688 case 2: return i18n("February", "Feb"); 698 case 2: return i18n("February", "Feb");
689 case 3: return i18n("March", "Mar"); 699 case 3: return i18n("March", "Mar");
690 case 4: return i18n("April", "Apr"); 700 case 4: return i18n("April", "Apr");
691 case 5: return i18n("May short", "May"); 701 case 5: return i18n("May short", "May");
692 case 6: return i18n("June", "Jun"); 702 case 6: return i18n("June", "Jun");
693 case 7: return i18n("July", "Jul"); 703 case 7: return i18n("July", "Jul");
694 case 8: return i18n("August", "Aug"); 704 case 8: return i18n("August", "Aug");
695 case 9: return i18n("September", "Sep"); 705 case 9: return i18n("September", "Sep");
696 case 10: return i18n("October", "Oct"); 706 case 10: return i18n("October", "Oct");
697 case 11: return i18n("November", "Nov"); 707 case 11: return i18n("November", "Nov");
698 case 12: return i18n("December", "Dec"); 708 case 12: return i18n("December", "Dec");
699 } 709 }
700 else 710 else
701 switch (i) 711 switch (i)
702 { 712 {
703 case 1: return i18n("January"); 713 case 1: return i18n("January");
704 case 2: return i18n("February"); 714 case 2: return i18n("February");
705 case 3: return i18n("March"); 715 case 3: return i18n("March");
706 case 4: return i18n("April"); 716 case 4: return i18n("April");
707 case 5: return i18n("May long", "May"); 717 case 5: return i18n("May long", "May");
708 case 6: return i18n("June"); 718 case 6: return i18n("June");
709 case 7: return i18n("July"); 719 case 7: return i18n("July");
710 case 8: return i18n("August"); 720 case 8: return i18n("August");
711 case 9: return i18n("September"); 721 case 9: return i18n("September");
712 case 10: return i18n("October"); 722 case 10: return i18n("October");
713 case 11: return i18n("November"); 723 case 11: return i18n("November");
714 case 12: return i18n("December"); 724 case 12: return i18n("December");
715 } 725 }
716 726
717 return QString::null; 727 return QString::null;
718} 728}
719 729
720QString KLocale::country() const 730QString KLocale::country() const
721{ 731{
722 return QString::null; 732 return QString::null;
723} 733}
724 734
725QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const 735QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const
726{ 736{
727 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat; 737 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
728 738
729 if ( dformat == ISODate ) 739 if ( dformat == ISODate )
730 return "%Y-%m-%d"; 740 return "%Y-%m-%d";
731 741
732 if ( QApplication::desktop()->width() < 480 ) { 742 if ( QApplication::desktop()->width() < 480 ) {
733 if ( dformat == Default ) 743 if ( dformat == Default )
734 return "%a %d %b %Y"; 744 return "%a %d %b %Y";
735 else if ( dformat == Format1 ) 745 else if ( dformat == Format1 )
736 return "%a %b %d %Y"; 746 return "%a %b %d %Y";
737 } else { 747 } else {
738 if ( dformat == Default ) 748 if ( dformat == Default )
739 return "%A %d %B %Y"; 749 return "%A %d %B %Y";
740 else if ( dformat == Format1 ) 750 else if ( dformat == Format1 )
741 return "%A %B %d %Y"; 751 return "%A %B %d %Y";
742 752
743 } 753 }
744 return mDateFormat ; 754 return mDateFormat ;
745} 755}
746 756
747QString KLocale::dateFormatShort(IntDateFormat intIntDateFormat) const 757QString KLocale::dateFormatShort(IntDateFormat intIntDateFormat) const
748{ 758{
749 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat; 759 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
750 760
751 if ( dformat == Default ) 761 if ( dformat == Default )
752 return "%d.%m.%Y"; 762 return "%d.%m.%Y";
753 else if ( dformat == Format1 ) 763 else if ( dformat == Format1 )
754 return "%m.%d.%Y"; 764 return "%m.%d.%Y";
755 else if ( dformat == ISODate ) // = Qt::ISODate 765 else if ( dformat == ISODate ) // = Qt::ISODate
756 return "%Y-%m-%d"; 766 return "%Y-%m-%d";
757 return mDateFormatShort ; 767 return mDateFormatShort ;
758 768
759} 769}
760 770
761 771
762QString KLocale::timeFormat(IntDateFormat intIntTimeFormat) const 772QString KLocale::timeFormat(IntDateFormat intIntTimeFormat) const
763{ 773{
764 const IntDateFormat tformat = (intIntTimeFormat == Undefined)?mIntTimeFormat:intIntTimeFormat; 774 const IntDateFormat tformat = (intIntTimeFormat == Undefined)?mIntTimeFormat:intIntTimeFormat;
765 775
766 if ( tformat == Default ) 776 if ( tformat == Default )
767 if ( mHourF24Format) 777 if ( mHourF24Format)
768 return "%H:%M:%S"; 778 return "%H:%M:%S";
769 else 779 else
770 return "%I:%M:%S%p"; 780 return "%I:%M:%S%p";
771 781
772 else if ( tformat == Format1 ) 782 else if ( tformat == Format1 )
773 if ( mHourF24Format) 783 if ( mHourF24Format)
774 return "%H:%M:%S"; 784 return "%H:%M:%S";
775 else 785 else
776 return "%I:%M:%S%p"; 786 return "%I:%M:%S%p";
777 787
778 else if ( tformat == ISODate ) // = Qt::ISODate 788 else if ( tformat == ISODate ) // = Qt::ISODate
779 if ( mHourF24Format) 789 if ( mHourF24Format)
780 return "%H:%M:%S"; 790 return "%H:%M:%S";
781 else 791 else
782 return "%I:%M:%S%p"; 792 return "%I:%M:%S%p";
783 793
784} 794}
785 795
786void KLocale::insertCatalogue ( const QString & ) 796void KLocale::insertCatalogue ( const QString & )
787{ 797{
788} 798}
789 799
790KCalendarSystem *KLocale::calendar() 800KCalendarSystem *KLocale::calendar()
791{ 801{
792 if ( !mCalendarSystem ) { 802 if ( !mCalendarSystem ) {
793 mCalendarSystem = new KCalendarSystemGregorian; 803 mCalendarSystem = new KCalendarSystemGregorian;
794 } 804 }
795 805
796 return mCalendarSystem; 806 return mCalendarSystem;
797} 807}
798 808
799int KLocale::timezoneOffset( QString timeZone ) 809int KLocale::timezoneOffset( QString timeZone )
800{ 810{
801 int ret = 1001; 811 int ret = 1001;
802 int index = mTimeZoneList.findIndex( timeZone ); 812 int index = mTimeZoneList.findIndex( timeZone );
803 if ( index < 24 ) 813 if ( index < 24 )
804 ret = ( index-11 ) * 60 ; 814 ret = ( index-11 ) * 60 ;
805 return ret; 815 return ret;
806} 816}
807 817
808QStringList KLocale::timeZoneList() const 818QStringList KLocale::timeZoneList() const
809{ 819{
810 return mTimeZoneList; 820 return mTimeZoneList;
811} 821}
812void KLocale::setTimezone( const QString &timeZone ) 822void KLocale::setTimezone( const QString &timeZone )
813{ 823{
814 mTimeZoneOffset = timezoneOffset( timeZone ); 824 mTimeZoneOffset = timezoneOffset( timeZone );
815} 825}
816 826
817void KLocale::setDaylightSaving( bool b, int start , int end ) 827void KLocale::setDaylightSaving( bool b, int start , int end )
818{ 828{
819 daylightEnabled = b; 829 daylightEnabled = b;
820 daylightStart = start; 830 daylightStart = start;
821 daylightEnd = end; 831 daylightEnd = end;
822 mSouthDaylight = (end < start); 832 mSouthDaylight = (end < start);
823 // qDebug("klocale daylight %d %d %d ", b, start , end ); 833 // qDebug("klocale daylight %d %d %d ", b, start , end );
824} 834}
825 835
826int KLocale::localTimeOffset( const QDateTime &dt ) 836int KLocale::localTimeOffset( const QDateTime &dt )
827{ 837{
828 bool addDaylight = false; 838 bool addDaylight = false;
829 if ( daylightEnabled ) { 839 if ( daylightEnabled ) {
830 int d_end, d_start; 840 int d_end, d_start;
831 int dayofyear = dt.date().dayOfYear(); 841 int dayofyear = dt.date().dayOfYear();
832 int year = dt.date().year(); 842 int year = dt.date().year();
833 int add = 0; 843 int add = 0;
834 if ( QDate::leapYear(year) ) 844 if ( QDate::leapYear(year) )
835 add = 1; 845 add = 1;
836 QDate date ( year,1,1 ); 846 QDate date ( year,1,1 );
837 if ( daylightEnd > 59 ) 847 if ( daylightEnd > 59 )
838 d_end = daylightEnd +add; 848 d_end = daylightEnd +add;
839 else 849 else
840 d_end = daylightEnd; 850 d_end = daylightEnd;
841 if ( daylightStart > 59 ) 851 if ( daylightStart > 59 )
842 d_start = daylightStart +add; 852 d_start = daylightStart +add;
843 else 853 else
844 d_start = daylightStart; 854 d_start = daylightStart;
845 QDate s_date = date.addDays( d_start -1 ); 855 QDate s_date = date.addDays( d_start -1 );
846 QDate e_date = date.addDays( d_end -1 ); 856 QDate e_date = date.addDays( d_end -1 );
847 int dof = s_date.dayOfWeek(); 857 int dof = s_date.dayOfWeek();
848 if ( dof < 7 ) 858 if ( dof < 7 )
849 s_date = s_date.addDays( -dof ); 859 s_date = s_date.addDays( -dof );
850 dof = e_date.dayOfWeek(); 860 dof = e_date.dayOfWeek();
851 if ( dof < 7 ) 861 if ( dof < 7 )
852 e_date = e_date.addDays( -dof ); 862 e_date = e_date.addDays( -dof );
853 QTime startTime ( 3,0,0 ); 863 QTime startTime ( 3,0,0 );
854 QDateTime startDt( s_date, startTime ); 864 QDateTime startDt( s_date, startTime );
855 QDateTime endDt( e_date, startTime ); 865 QDateTime endDt( e_date, startTime );
856 //qDebug("dayligt saving start %s end %s ",startDt.toString().latin1(),endDt.toString().latin1( )); 866 //qDebug("dayligt saving start %s end %s ",startDt.toString().latin1(),endDt.toString().latin1( ));
857 if ( mSouthDaylight ) { 867 if ( mSouthDaylight ) {
858 if ( ! ( endDt < dt && dt < startDt) ) 868 if ( ! ( endDt < dt && dt < startDt) )
859 addDaylight = true; 869 addDaylight = true;
860 } else { 870 } else {
861 if ( startDt < dt && dt < endDt ) 871 if ( startDt < dt && dt < endDt )
862 addDaylight = true; 872 addDaylight = true;
863 873
864 874
865 } 875 }
866 } 876 }
867 int addMin = 0; 877 int addMin = 0;
868 if ( addDaylight ) 878 if ( addDaylight )
869 addMin = 60; 879 addMin = 60;
870 return mTimeZoneOffset + addMin; 880 return mTimeZoneOffset + addMin;
871} 881}
872// ****************************************************************** 882// ******************************************************************
873// added LR 883// added LR
874QString KLocale::formatNumber(double num, int precision) const 884QString KLocale::formatNumber(double num, int precision) const
875{ 885{
876 bool neg = num < 0; 886 bool neg = num < 0;
877 if (precision == -1) precision = 2; 887 if (precision == -1) precision = 2;
878 QString res = QString::number(neg?-num:num, 'f', precision); 888 QString res = QString::number(neg?-num:num, 'f', precision);
879 int pos = res.find('.'); 889 int pos = res.find('.');
880 if (pos == -1) pos = res.length(); 890 if (pos == -1) pos = res.length();
881 else res.replace(pos, 1, decimalSymbol()); 891 else res.replace(pos, 1, decimalSymbol());
882 892
883 while (0 < (pos -= 3)) 893 while (0 < (pos -= 3))
884 res.insert(pos, thousandsSeparator()); // thousand sep 894 res.insert(pos, thousandsSeparator()); // thousand sep
885 895
886 // How can we know where we should put the sign? 896 // How can we know where we should put the sign?
887 res.prepend(neg?negativeSign():positiveSign()); 897 res.prepend(neg?negativeSign():positiveSign());
888 898
889 return res; 899 return res;
890} 900}
891QString KLocale::formatNumber(const QString &numStr) const 901QString KLocale::formatNumber(const QString &numStr) const
892{ 902{
893 return formatNumber(numStr.toDouble()); 903 return formatNumber(numStr.toDouble());
894} 904}
895double KLocale::readNumber(const QString &_str, bool * ok) const 905double KLocale::readNumber(const QString &_str, bool * ok) const
896{ 906{
897 QString str = _str.stripWhiteSpace(); 907 QString str = _str.stripWhiteSpace();
898 bool neg = str.find(negativeSign()) == 0; 908 bool neg = str.find(negativeSign()) == 0;
899 if (neg) 909 if (neg)
900 str.remove( 0, negativeSign().length() ); 910 str.remove( 0, negativeSign().length() );
901 911
902 /* will hold the scientific notation portion of the number. 912 /* will hold the scientific notation portion of the number.
903 Example, with 2.34E+23, exponentialPart == "E+23" 913 Example, with 2.34E+23, exponentialPart == "E+23"
904 */ 914 */
905 QString exponentialPart; 915 QString exponentialPart;
906 int EPos; 916 int EPos;
907 917
908 EPos = str.find('E', 0, false); 918 EPos = str.find('E', 0, false);
909 919
910 if (EPos != -1) 920 if (EPos != -1)
911 { 921 {
912 exponentialPart = str.mid(EPos); 922 exponentialPart = str.mid(EPos);
913 str = str.left(EPos); 923 str = str.left(EPos);
914 } 924 }
915 925
916 int pos = str.find(decimalSymbol()); 926 int pos = str.find(decimalSymbol());
917 QString major; 927 QString major;
918 QString minor; 928 QString minor;
919 if ( pos == -1 ) 929 if ( pos == -1 )
920 major = str; 930 major = str;
921 else 931 else
922 { 932 {
923 major = str.left(pos); 933 major = str.left(pos);
924 minor = str.mid(pos + decimalSymbol().length()); 934 minor = str.mid(pos + decimalSymbol().length());
925 } 935 }
926 936
927 // Remove thousand separators 937 // Remove thousand separators
928 int thlen = thousandsSeparator().length(); 938 int thlen = thousandsSeparator().length();
929 int lastpos = 0; 939 int lastpos = 0;
930 while ( ( pos = major.find( thousandsSeparator() ) ) > 0 ) 940 while ( ( pos = major.find( thousandsSeparator() ) ) > 0 )
931 { 941 {
932 // e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N 942 // e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N
933 int fromEnd = major.length() - pos; 943 int fromEnd = major.length() - pos;
934 if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error 944 if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error
935 || pos - lastpos > 3 // More than 3 digits between two separators -> error 945 || pos - lastpos > 3 // More than 3 digits between two separators -> error
936 || pos == 0 // Can't start with a separator 946 || pos == 0 // Can't start with a separator
937 || (lastpos>0 && pos-lastpos!=3)) // Must have exactly 3 digits between two separators 947 || (lastpos>0 && pos-lastpos!=3)) // Must have exactly 3 digits between two separators
938 { 948 {
939 if (ok) *ok = false; 949 if (ok) *ok = false;
940 return 0.0; 950 return 0.0;
941 } 951 }
942 952
943 lastpos = pos; 953 lastpos = pos;
944 major.remove( pos, thlen ); 954 major.remove( pos, thlen );
945 } 955 }
946 if (lastpos>0 && major.length()-lastpos!=3) // Must have exactly 3 digits after the last separator 956 if (lastpos>0 && major.length()-lastpos!=3) // Must have exactly 3 digits after the last separator
947 { 957 {
948 if (ok) *ok = false; 958 if (ok) *ok = false;
949 return 0.0; 959 return 0.0;
950 } 960 }
951 961
952 QString tot; 962 QString tot;
953 if (neg) tot = '-'; 963 if (neg) tot = '-';
954 964
955 tot += major + '.' + minor + exponentialPart; 965 tot += major + '.' + minor + exponentialPart;
956 966
957 return tot.toDouble(ok); 967 return tot.toDouble(ok);
958} 968}
959QString KLocale::decimalSymbol() const 969QString KLocale::decimalSymbol() const
960{ 970{
961 971
962 return m_decimalSymbol; 972 return m_decimalSymbol;
963} 973}
964 974
965QString KLocale::thousandsSeparator() const 975QString KLocale::thousandsSeparator() const
966{ 976{
967 977
968 return m_thousandsSeparator; 978 return m_thousandsSeparator;
969} 979}
970QString KLocale::positiveSign() const 980QString KLocale::positiveSign() const
971{ 981{
972 return m_positiveSign; 982 return m_positiveSign;
973} 983}
974 984
975QString KLocale::negativeSign() const 985QString KLocale::negativeSign() const
976{ 986{
977 return m_negativeSign; 987 return m_negativeSign;
978} 988}