static KTimeZone toKTimeZone(Soprano::Node n)
{
    QString dt = n.dataType().toString();
    dt.remove(QRegExp("#tz$"));
    int idx = dt.lastIndexOf('/');
    if (idx > 0) {
        idx = dt.lastIndexOf('/', idx - 1);
    }
    if (idx > 0) {
        dt.remove(0, idx + 1);
    }
    KTimeZone kt = KSystemTimeZones::zone(dt);
    kDebug(30015) << "input:" << n.dataType().toString()
        << " output tz.valid:" << kt.isValid()
        << " timezone:" << dt;
    if (!kt.isValid()) {
        // UTC "Zulu" Time
        if (dt == "2001/XMLSchema#dateTime" && n.toString().endsWith('Z')) {
            kDebug(30015) << "input:" << n.dataType().toString()
            << " is UTC...";
            kt = KSystemTimeZones::zone("UTC");
        }
    }

    if (!kt.isValid()) {
        kt = KSystemTimeZones::zone("UTC");
    }
    return kt;
}
示例#2
0
void KSystemTimeZonesPrivate::setLocalZone()
{
    QString filename;
    if (m_localZoneName.startsWith(QLatin1Char('/'))) {
        // The time zone is specified by a file outside the zoneinfo directory
        filename = m_localZoneName;
    } else {
        // The zone name is either a known zone, or it's a relative file name
        // in zoneinfo directory which isn't in zone.tab.
        m_localZone = m_instance->zone(m_localZoneName);
        if (m_localZone.isValid())
            return;
        // It's a relative file name
        filename = m_zoneinfoDir + QLatin1Char('/') + m_localZoneName;
    }

    // Parse the specified time zone data file
    QString zonename = filename;
    if (zonename.startsWith(m_zoneinfoDir + QLatin1Char('/')))
        zonename = zonename.mid(m_zoneinfoDir.length() + 1);
    m_localZone = KTzfileTimeZone(KSystemTimeZonesPrivate::tzfileSource(), zonename);
    if (m_localZone.isValid() && m_instance)
    {
        // Add the new time zone to the list
        const KTimeZone oldzone = m_instance->zone(zonename);
        if (!oldzone.isValid() || oldzone.type() != "KTzfileTimeZone")
        {
            m_instance->remove(oldzone);
            m_instance->add(m_localZone);
        }
    }
}
/******************************************************************************
* Set the date/time.
*/
void AlarmTimeWidget::setDateTime(const DateTime& dt)
{
	// Set the time zone first so that the call to dateTimeChanged() works correctly.
	if (mDeferring)
		mTimeSpec = dt.timeSpec().isValid() ? dt.timeSpec() : KDateTime::LocalZone;
	else
	{
		KTimeZone tz = dt.timeZone();
		mNoTimeZone->setChecked(!tz.isValid());
		mTimeZone->setTimeZone(tz.isValid() ? tz : Preferences::timeZone());
		slotTimeZoneChanged();
	}

	if (dt.date().isValid())
	{
		mTimeEdit->setValue(dt.effectiveTime());
		mDateEdit->setDate(dt.date());
		dateTimeChanged();     // update the delay time edit box
	}
	else
	{
		mTimeEdit->setValid(false);
		mDateEdit->setInvalid();
		mDelayTimeEdit->setValid(false);
	}
	if (mAnyTimeCheckBox)
	{
		bool dateOnly = dt.isDateOnly();
		if (dateOnly)
			mAnyTimeAllowed = true;
		mAnyTimeCheckBox->setChecked(dateOnly);
		setAnyTime();
	}
}
void CalendarTester::testTimezone()
{
    Calendar t("Test");
    QDate wdate(2006,1,2);
    DateTime before = DateTime(wdate.addDays(-1), QTime());
    DateTime after = DateTime(wdate.addDays(1), QTime());
    QTime t1(8,0,0);
    QTime t2(10,0,0);
    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));
    t.addDay(day);
    Debug::print( &t, "Time zone testing" );
    QVERIFY(t.findDay(wdate) == day);

    // local zone: Europe/Berlin ( 1 hours from London )
    KTimeZone lo = KSystemTimeZones::zone("Europe/London");
    QVERIFY( lo.isValid() );
    KDateTime dt1 = KDateTime( wdate, t1, lo ).addSecs( -2 * 3600 );
    KDateTime dt2 = KDateTime( wdate, t2, lo ).addSecs( 0 * 3600 );

    qDebug()<<KDateTime( wdt1 )<<KDateTime( wdt2 );
    qDebug()<<dt1<<dt2<<"("<<dt1.toLocalZone()<<dt2.toLocalZone()<<")";
    QCOMPARE(t.firstAvailableAfter( DateTime( dt1 ), after ), wdt1 );
    QCOMPARE(t.firstAvailableBefore( DateTime( dt2 ), before ), wdt2 );

    Duration e(0, 2, 0);
    QCOMPARE( t.effort( DateTime( dt1 ), DateTime( dt2 ) ).toString(), e.toString() );

    // local zone: Europe/Berlin ( 9 hours from America/Los_Angeles )
    KTimeZone la = KSystemTimeZones::zone("America/Los_Angeles");
    QVERIFY( la.isValid() );
    KDateTime dt3 = KDateTime( wdate, t1, la ).addSecs( -10 * 3600 );
    KDateTime dt4 = KDateTime( wdate, t2, la ).addSecs( -8 * 3600 );

    qDebug()<<KDateTime( wdt1 )<<KDateTime( wdt2 );
    qDebug()<<dt3<<dt4<<"("<<dt3.toLocalZone()<<dt4.toLocalZone()<<")";
    QCOMPARE(t.firstAvailableAfter( DateTime( dt3 ), after ), wdt1 );
    QCOMPARE(t.firstAvailableBefore( DateTime( dt4 ), before ), wdt2 );

    QCOMPARE( t.effort( DateTime( dt3 ), DateTime( dt4 ) ).toString(), e.toString() );

    QString s = "Test Cairo:";
    qDebug()<<s;
    // local zone: Europe/Berlin ( 1 hour from cairo )
    KTimeZone ca = KSystemTimeZones::zone("Africa/Cairo");
    KDateTime dt5 = KDateTime( wdate, t1, ca ).addSecs( 0 * 3600 );
    KDateTime dt6 = KDateTime( wdate, t2, ca ).addSecs( 2 * 3600 );

    qDebug()<<KDateTime( wdt1 )<<KDateTime( wdt2 );
    qDebug()<<dt5<<dt6<<"("<<dt5.toLocalZone()<<dt6.toLocalZone()<<")";
    QCOMPARE(t.firstAvailableAfter( DateTime( dt5 ), after ), wdt1 );
    QCOMPARE(t.firstAvailableBefore( DateTime( dt6 ), before ), wdt2 );

    QCOMPARE( t.effort( DateTime( dt5 ), DateTime( dt6 ) ).toString(), e.toString() );
}
void TimeZoneCombo::setTimeZone(const KTimeZone& tz)
{
	if (!tz.isValid())
		return;
	int index = mZoneNames.indexOf(tz.name());
	if (index >= 0)
		setCurrentIndex(index);
}
示例#6
0
bool KTimeZones::add(const KTimeZone &zone)
{
    if (!zone.isValid())
        return false;
    if (d->zones.find(zone.name()) != d->zones.end())
        return false;    // name already exists
    d->zones.insert(zone.name(), zone);
    return true;
}
KOAlarmClient::KOAlarmClient( QObject *parent )
  : QObject( parent ), mDocker( 0 ), mDialog( 0 )
{
  new KOrgacAdaptor( this );
  QDBusConnection::sessionBus().registerObject( "/ac", this );
  kDebug();

  KConfig korgConfig( KStandardDirs::locate( "config", "korganizerrc" ) );
  KConfigGroup generalGroup( &korgConfig, "General" );
  bool showDock = generalGroup.readEntry( "ShowReminderDaemon", true );

  if ( showDock ) {
    mDocker = new AlarmDockWindow;

    connect( this, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
    connect( mDocker, SIGNAL(quitSignal()), SLOT(slotQuit()) );
  }

  const KTimeZone zone = KSystemTimeZones::local();
  mCalendar = new CalendarResources( zone.isValid() ?
                                     KDateTime::Spec( zone ) :
                                     KDateTime::ClockTime );
  mCalendar->readConfig();
  mCalendar->load();

  connect( &mCheckTimer, SIGNAL(timeout()), SLOT(checkAlarms()) );

  KConfigGroup alarmGroup( KGlobal::config(), "Alarms" );
  int interval = alarmGroup.readEntry( "Interval", 60 );
  kDebug() << "KOAlarmClient check interval:" << interval << "seconds.";
  mLastChecked = alarmGroup.readEntry( "CalendarsLastChecked", QDateTime() );

  // load reminders that were active when quitting
  KConfigGroup genGroup( KGlobal::config(), "General" );
  int numReminders = genGroup.readEntry( "Reminders", 0 );
  for ( int i=1; i<=numReminders; ++i ) {
    const QString group( QString( "Incidence-%1" ).arg( i ) );
    const KConfigGroup incGroup( KGlobal::config(), group );
    const QString uid = incGroup.readEntry( "UID" );
    const QDateTime dt = incGroup.readEntry( "RemindAt", QDateTime() );
    if ( !uid.isEmpty() ) {
      Incidence *i = mCalendar->incidence( uid );
      if ( i && !i->alarms().isEmpty() ) {
        createReminder( mCalendar, i, dt, QString() );
      }
    }
  }
  if ( numReminders ) {
     genGroup.writeEntry( "Reminders", 0 );
     genGroup.sync();
  }

  checkAlarms();
  mCheckTimer.start( 1000 * interval );  // interval in seconds
}
示例#8
0
void KSystemTimeZonesPrivate::setLocalZone()
{
    if (m_localZoneName.startsWith('/'))
    {
        // The time zone is specified by a file outside the zoneinfo directory
        m_localZone = KTzfileTimeZone(KSystemTimeZonesPrivate::tzfileSource(), m_localZoneName);
        if (m_localZone.isValid() && m_instance)
        {
            // Add the new time zone to the list
            KTimeZone oldzone = m_instance->zone(m_localZoneName);
            if (!oldzone.isValid() || oldzone.type() != "KTzfileTimeZone")
            {
                m_instance->remove(oldzone);
                m_instance->add(m_localZone);
            }
        }
    }
    else
        m_localZone = m_instance->zone(m_localZoneName);
}
示例#9
0
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();
}
/******************************************************************************
* Called after a new selection has been made in the time zone combo box.
* Re-evaluates the time specification to use.
*/
void AlarmTimeWidget::slotTimeZoneChanged()
{
	if (mNoTimeZone->isChecked())
		mTimeSpec = KDateTime::ClockTime;
	else
	{
		KTimeZone tz = mTimeZone->timeZone();
		mTimeSpec = tz.isValid() ? KDateTime::Spec(tz) : KDateTime::LocalZone;
	}
	if (!mTimeZoneBox->isVisible()  &&  mTimeSpec != Preferences::timeZone())
	{
		// The current time zone is not the default one, so
		// show the time zone selection controls
		showTimeZoneSelector();
	}
	mMinDateTime = mMinDateTime.toTimeSpec(mTimeSpec);
	mMaxDateTime = mMaxDateTime.toTimeSpec(mTimeSpec);
	updateTimes();
}
示例#11
0
// 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;
}
示例#12
0
// 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;
}
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 ) ) );
}
示例#14
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);
    }
示例#15
0
/*
 * 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());
        }
    }
}