void WorkInfoCacheTester::timeZone() { Calendar cal("Test"); // local zone: Europe/Berlin ( 9 hours from America/Los_Angeles ) KTimeZone la = KSystemTimeZones::zone("America/Los_Angeles"); QVERIFY( la.isValid() ); cal.setTimeZone( la ); QDate wdate(2012,1,2); DateTime before = DateTime(wdate.addDays(-1), QTime()); DateTime after = DateTime(wdate.addDays(1), QTime()); QTime t1(14,0,0); // 23 LA QTime t2(16,0,0); // 01 LA next day DateTime wdt1(wdate, t1); DateTime wdt2(wdate, t2); int length = t1.msecsTo( t2 ); CalendarDay *day = new CalendarDay(wdate, CalendarDay::Working); day->addInterval(TimeInterval(t1, length)); cal.addDay(day); QVERIFY(cal.findDay(wdate) == day); Debug::print( &cal, "America/Los_Angeles" ); Resource r; r.setCalendar( &cal ); const Resource::WorkInfoCache &wic = r.workInfoCache(); QVERIFY( ! wic.isValid() ); r.calendarIntervals( before, after ); qDebug()<<wic.intervals.map(); QCOMPARE( wic.intervals.map().count(), 2 ); QCOMPARE( wic.intervals.map().value( wdate ).startTime(), DateTime( wdate, QTime( 23, 0, 0 ) ) ); QCOMPARE( wic.intervals.map().value( wdate ).endTime(), DateTime( wdate.addDays( 1 ), QTime( 0, 0, 0 ) ) ); wdate = wdate.addDays( 1 ); QCOMPARE( wic.intervals.map().value( wdate ).startTime(), DateTime( wdate, QTime( 0, 0, 0 ) ) ); QCOMPARE( wic.intervals.map().value( wdate ).endTime(), DateTime( wdate, QTime( 1, 0, 0 ) ) ); }
void KSystemTimeZonesPrivate::updateTimezoneInformation(bool update) { if (!m_source) m_source = new KSystemTimeZoneSourceWindows; QStringList newZones; Q_FOREACH(const QString & tz, KSystemTimeZoneWindows::listTimeZones()) { // const std::wstring wstr = tz.toStdWString(); // const KTimeZone info = make_time_zone( wstr.c_str() ); KSystemTimeZoneWindows stz(m_source, tz); if (update) { // Update the existing collection with the new zone definition newZones += stz.name(); KTimeZone oldTz = zone(stz.name()); if (oldTz.isValid()) oldTz.updateBase(stz); // the zone previously existed, so update its definition else add(stz); // the zone didn't previously exist, so add it } else add(stz); }
KTimeZone KTimeZones::remove(const KTimeZone &zone) { if (zone.isValid()) { for (ZoneMap::Iterator it = d->zones.begin(), end = d->zones.end(); it != end; ++it) { if (it.value() == zone) { d->zones.erase(it); return zone; } } } return KTimeZone(); }
// Perform initialization, create the unique KSystemTimeZones instance, // whose only function is to receive D-Bus signals from KTimeZoned, // and create the unique KSystemTimeZonesPrivate instance. KSystemTimeZonesPrivate *KSystemTimeZonesPrivate::instance() { if (!m_instance) { m_instance = new KSystemTimeZonesPrivate; // A KSystemTimeZones instance is required only to catch D-Bus signals. m_parent = new KSystemTimeZones; // Ensure that the KDED time zones module has initialized. The call loads the module on demand. if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.kded"))) KToolInvocation::klauncher(); // this calls startKdeinit, and blocks until it returns const QString dbusIface = QString::fromLatin1(KTIMEZONED_DBUS_IFACE); QDBusInterface *ktimezoned = new QDBusInterface(QLatin1String("org.kde.kded"), QLatin1String("/modules/ktimezoned"), dbusIface); QDBusReply<void> reply = ktimezoned->call(QLatin1String("initialize"), false); m_ktimezonedError = !reply.isValid(); if (m_ktimezonedError) kError(161) << "KSystemTimeZones: ktimezoned initialize() D-Bus call failed: " << reply.error().message() << endl; kDebug(161)<<"instance(): ... initialised"; delete ktimezoned; // Read the time zone config written by ktimezoned readConfig(true); // Go read the database. #ifdef Q_OS_WIN // On Windows, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones // is the place to look. The TZI binary value is the TIME_ZONE_INFORMATION structure. m_instance->updateTimezoneInformation(false); #else // For Unix, read zone.tab. if (!m_zonetab.isEmpty()) m_instance->readZoneTab(false); #endif setLocalZone(); if (!m_localZone.isValid()) m_localZone = KTimeZone::utc(); // ensure a time zone is always returned qAddPostRoutine(KSystemTimeZonesPrivate::cleanup); } return m_instance; }
// Perform initialization, create the unique KSystemTimeZones instance, // whose only function is to receive D-Bus signals from KTimeZoned, // and create the unique KSystemTimeZonesPrivate instance. KSystemTimeZonesPrivate *KSystemTimeZonesPrivate::instance() { if (!m_instance) { m_instance = new KSystemTimeZonesPrivate; #if !defined(TIMED_SUPPORT) && !defined(KCALCORE_FOR_MEEGO) // A KSystemTimeZones instance is required only to catch D-Bus signals. m_parent = new KSystemTimeZones; // Ensure that the KDED time zones module has initialized. The call loads the module on demand. QDBusInterface *ktimezoned = new QDBusInterface("org.kde.kded", "/modules/ktimezoned", KTIMEZONED_DBUS_IFACE); QDBusReply<void> reply = ktimezoned->call("initialize", false); if (!reply.isValid()) kError(161) << "KSystemTimeZones: ktimezoned initialize() D-Bus call failed: " << reply.error().message() << endl; kDebug(161)<<"instance(): ... initialised"; delete ktimezoned; #endif // Read the time zone config written by ktimezoned readConfig(true); // Go read the database. #ifdef Q_OS_WIN // On Windows, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones // is the place to look. The TZI binary value is the TIME_ZONE_INFORMATION structure. #else // For Unix, read zone.tab. if (!m_zonetab.isEmpty()) m_instance->readZoneTab(false); #endif setLocalZone(); if (!m_localZone.isValid()) { kDebug() << "m_localZone invalid"; m_localZone = KTimeZone::utc(); // ensure a time zone is always returned } qAddPostRoutine(KSystemTimeZonesPrivate::cleanup); } return m_instance; }
/* * Find the location of the zoneinfo files and store in mZoneinfoDir. * Parse zone.tab and for each time zone, create a KSystemTimeZone instance. */ void KSystemTimeZonesPrivate::readZoneTab(bool update) { kDebug(161) << "readZoneTab(" << m_zonetab<< ")"; QStringList newZones; QFile f; f.setFileName(m_zonetab); if (!f.open(QIODevice::ReadOnly)) return; QTextStream str(&f); QRegExp lineSeparator("[ \t]"); QRegExp ordinateSeparator("[+-]"); if (!m_source) m_source = new KSystemTimeZoneSource; while (!str.atEnd()) { QString line = str.readLine(); if (line.isEmpty() || line[0] == '#') continue; QStringList tokens = KStringHandler::perlSplit(lineSeparator, line, 4); int n = tokens.count(); if (n < 3) { kError(161) << "readZoneTab(): invalid record: " << line << endl; continue; } // Got three tokens. Now check for two ordinates plus first one is "". int i = tokens[1].indexOf(ordinateSeparator, 1); if (i < 0) { kError(161) << "readZoneTab() " << tokens[2] << ": invalid coordinates: " << tokens[1] << endl; continue; } float latitude = convertCoordinate(tokens[1].left(i)); float longitude = convertCoordinate(tokens[1].mid(i)); // Add entry to list. if (tokens[0] == "??") tokens[0] = ""; // Solaris sets the empty Comments field to '-', making it not empty. // Clean it up. if (n > 3 && tokens[3] == "-") tokens[3] = ""; KSystemTimeZone tz(m_source, tokens[2], tokens[0], latitude, longitude, (n > 3 ? tokens[3] : QString())); if (update) { // Update the existing collection with the new zone definition newZones += tz.name(); KTimeZone oldTz = zone(tz.name()); if (oldTz.isValid()) oldTz.updateBase(tz); // the zone previously existed, so update its definition else add(tz); // the zone didn't previously exist, so add it } else add(tz); } f.close(); if (update) { // Remove any zones from the collection which no longer exist const ZoneMap oldZones = zones(); for (ZoneMap::ConstIterator it = oldZones.constBegin(); it != oldZones.constEnd(); ++it) { if (newZones.indexOf(it.key()) < 0) remove(it.value()); } } }