Exemple #1
0
int KstSettingsDlg::utcOffset(const QString& timezone) {
  int seconds = 0;

  if (timezone.startsWith("UTC")) {
    bool ok;
    int hours = timezone.mid(3).toInt(&ok);

    if (ok) {
      seconds = int( ( double(hours) / 100.0 ) * 3600.0);
    } else {
      seconds = 0;
    }
  } else {
    KstTimezones db;
    const KstTimezones::ZoneMap zones = db.allZones();
    KstTimezones::ZoneMap::const_iterator it;

    for (it = zones.begin(); it != zones.end(); ++it) {
      if ((*it)->name() == timezone) {
        seconds = -(*it)->offset();
      }
    }
  }

  return seconds;
}
KTimezoneCombo::KTimezoneCombo(QWidget *parent, const char *name, KstTimezones *db)
: KComboBox(parent, name), d(new Private) {
  bool userDb = db != 0L;
  if (!userDb) {
    db = new KstTimezones;
  }

  if (listBox()) {
    listBox()->setVScrollBarMode(QScrollView::AlwaysOn);
    listBox()->setColumnMode(QListBox::FixedNumber);
    listBox()->setRowMode(QListBox::Variable);
  }

  insertItem("UTC");
  const KstTimezones::ZoneMap zones = db->allZones();
  d->_offsets.resize(zones.count()+1);
  d->_offsets[0] = 0;
  d->_names += "UTC";
  int i = 0;

  for (KstTimezones::ZoneMap::ConstIterator it = zones.begin(); it != zones.end(); ++it) {
    int offset = (*it)->offset();
    d->_offsets[++i] = offset;
    int hours = offset / 3600;
    int minutes = 100 * offset / 3600 % 100;
    bool negative = false;
    if (hours < 0) {
      hours *= -1;
      negative = true;
    }
    if (minutes < 0) {
      minutes *= -1;
    }
    QString offnum;
    if (hours < 10) {
      offnum += '0';
    }
    offnum += QString::number(hours);
    if (minutes < 10) {
      offnum += '0';
    }
    offnum += QString::number(minutes);
    if ((*it)->name() != "UTC") {
      insertItem(i18n("%3 (UTC%1%2)").arg(negative ? '-' : '+').arg(offnum).arg((*it)->name()));
      d->_names += (*it)->name();
    }
  }

  if (!userDb) {
    delete db;
  }
}