24 #include <kalarmcal/datetime.h>
26 #include <ksystemtimezone.h>
30 #include <qapplication.h>
32 using namespace KAlarmCal;
34 int AlarmTime::mTimeHourPos = -2;
41 if (!dateTime.isValid())
42 return i18nc(
"@info/plain Alarm never occurs",
"Never");
43 KLocale* locale = KGlobal::locale();
45 QString dateTimeText = locale->formatDate(kdt.date(), KLocale::ShortDate);
46 if (!dateTime.isDateOnly()
47 || (!dateTime.isClockTime() && kdt.utcOffset() != dateTime.utcOffset()))
51 dateTimeText += QLatin1Char(
' ');
52 QString time = locale->formatTime(kdt.time());
53 if (mTimeHourPos == -2)
58 if (QApplication::isLeftToRight())
60 QString fmt = locale->timeFormat();
61 int i = fmt.indexOf(QRegExp(QLatin1String(
"%[kl]")));
62 if (i >= 0 && i == fmt.indexOf(QLatin1Char(
'%')))
66 if (mTimeHourPos >= 0 && (
int)time.length() > mTimeHourPos + 1
67 && time[mTimeHourPos].isDigit() && !time[mTimeHourPos + 1].isDigit())
68 dateTimeText += QLatin1Char(
'~');
71 return dateTimeText + QLatin1Char(
' ');
79 if (!dateTime.isValid())
80 return i18nc(
"@info/plain Alarm never occurs",
"Never");
81 KDateTime now = KDateTime::currentUtcDateTime();
82 if (dateTime.isDateOnly())
84 int days = now.date().daysTo(dateTime.date());
86 return i18nc(
"@info/plain n days",
"%1d", days);
88 int mins = (now.secsTo(dateTime.effectiveKDateTime()) + 59) / 60;
91 char minutes[3] =
"00";
92 minutes[0] = (mins%60) / 10 +
'0';
93 minutes[1] = (mins%60) % 10 +
'0';
95 return i18nc(
"@info/plain hours:minutes",
"%1:%2", mins/60, QLatin1String(minutes));
96 int days = mins / (24*60);
97 mins = mins % (24*60);
98 return i18nc(
"@info/plain days hours:minutes",
"%1d %2:%3", days, mins/60, QLatin1String(minutes));
113 #define MAX_DT_LEN 19
114 int i = timeString.indexOf(
' ');
117 QString zone = (i >= 0) ? QString::fromLatin1(timeString.mid(i)) : QString();
119 strcpy(timeStr, timeString.left(i >= 0 ? i :
MAX_DT_LEN));
120 int dt[5] = { -1, -1, -1, -1, -1 };
125 if ((s = strchr(timeStr,
':')) == 0)
131 dt[4] = strtoul(s, &end, 10);
132 if (end == s || *end || dt[4] >= 60)
135 if ((s = strrchr(timeStr,
'-')) == 0)
139 dt[3] = strtoul(s, &end, 10);
140 if (end == s || *end || dt[3] >= 24)
148 if ((s = strrchr(timeStr,
'-')) == 0)
152 dt[2] = strtoul(s, &end, 10);
153 if (end == s || *end || dt[2] == 0 || dt[2] > 31)
158 if ((s = strrchr(timeStr,
'-')) == 0)
162 dt[1] = strtoul(s, &end, 10);
163 if (end == s || *end || dt[1] == 0 || dt[1] > 12)
168 dt[0] = strtoul(timeStr, &end, 10);
169 if (end == timeStr || *end)
177 date = QDate(dt[0], dt[1], dt[2]);
182 if (dt[0] < 0 || !date.isValid())
184 dateTime = applyTimeZone(zone, date, time,
false, defaultDt);
189 time.setHMS(dt[3], dt[4], 0);
194 if (defaultDt.isValid())
196 dt[0] = defaultDt.date().year();
198 (dt[1] < 0 ? defaultDt.date().month() : dt[1]),
199 (dt[2] < 0 ? defaultDt.date().day() : dt[2]));
202 date.setYMD(2000, 1, 1);
204 dateTime = applyTimeZone(zone, date, time,
true, defaultDt);
205 if (!dateTime.isValid())
211 KDateTime now = KDateTime::currentDateTime(dateTime.timeSpec());
212 date = dateTime.date();
213 date.setYMD(now.date().year(),
214 (dt[1] < 0 ? now.date().month() : dt[1]),
215 (dt[2] < 0 ? now.date().day() : dt[2]));
218 if (noDate && time < now.time())
219 date = date.addDays(1);
220 dateTime.setDate(date);
223 return dateTime.isValid();
234 bool haveTime,
const KDateTime& defaultDt)
237 KDateTime::Spec spec = KDateTime::LocalZone;
238 QString zone = tzstring.trimmed();
241 if (zone == QLatin1String(
"Clock"))
242 spec = KDateTime::ClockTime;
243 else if (zone == QLatin1String(
"UTC"))
244 spec = KDateTime::UTC;
247 KTimeZone tz = KSystemTimeZones::zone(zone);
248 error = !tz.isValid();
253 else if (defaultDt.isValid())
254 spec = defaultDt.timeSpec();
262 if (defaultDt.isValid())
263 result = KDateTime(defaultDt.date(), time, spec);
264 else if (spec == KDateTime::LocalZone || spec == KDateTime::ClockTime)
265 result = KDateTime(KDateTime::currentLocalDate(), time, spec);
270 result = KDateTime(date, time, spec);
275 result = KDateTime(date, spec);
static KDateTime applyTimeZone(const QString &tzstring, const QDate &date, const QTime &time, bool haveTime, const KDateTime &defaultDt=KDateTime())
static bool convertTimeString(const QByteArray &timeString, KDateTime &dateTime, const KDateTime &defaultDt=KDateTime(), bool allowTZ=true)
static KTimeZone timeZone(bool reload=false)
static QString timeToAlarmText(const KAlarmCal::DateTime &dateTime)
static QString alarmTimeText(const KAlarmCal::DateTime &dateTime)