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