/*! Offset in seconds from Coordinated Universal Time The offset depends on the time specification of dateTime: - Qt::UTC 0, dateTime has no offset - Qt::OffsetFromUTC returns dateTime.utcOffset() - Qt::LocalTime: number of seconds from the UTC For Qt::LocalTime the offset depends on the timezone and daylight savings. \param dateTime Datetime value \return Offset in seconds */ int QwtDate::utcOffset( const QDateTime &dateTime ) { int seconds = 0; switch( dateTime.timeSpec() ) { case Qt::UTC: { break; } case Qt::OffsetFromUTC: { #ifdef WORKBENCH_REPLACE_QT_DEPRECATED seconds = dateTime.offsetFromUtc(); #else seconds = dateTime.utcOffset(); #endif } default: { const QDateTime dt1( dateTime.date(), dateTime.time(), Qt::UTC ); seconds = dateTime.secsTo( dt1 ); } } return seconds; }
void AbstractDateTime::copyTimeSpec(const QDateTime &from, QDateTime &to) { switch(from.timeSpec()) { case Qt::UTC: /* Fallthrough. */ case Qt::LocalTime: { to.setTimeSpec(from.timeSpec()); return; } case Qt::OffsetFromUTC: { to.setOffsetFromUtc(from.offsetFromUtc()); Q_ASSERT(to.timeSpec() == Qt::OffsetFromUTC); return; } case Qt::TimeZone: { to.setTimeZone(from.timeZone()); return; } } }
bool SortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { if (filterRole() == "fromNow" && sourceModel()->roleNames().values().contains("start")) { QModelIndex index = sourceModel()->index(source_row, 0, source_parent); QDateTime date = sourceModel()->data(index, sourceModel()->roleNames().key("start")).toDateTime(); QDateTime currentDateTime = QDateTime::currentDateTime(); int localTimeZoneOffset = currentDateTime.offsetFromUtc(); currentDateTime.setMSecsSinceEpoch(currentDateTime.currentMSecsSinceEpoch() + localTimeZoneOffset*1000); return date >= currentDateTime; } else { if (m_hide && sourceModel()->roleNames().values().contains("hideInSchedule")) { QModelIndex index = sourceModel()->index(source_row, 0, source_parent); bool willBeHidden = sourceModel()->data(index, sourceModel()->roleNames().key("hideInSchedule")).toBool(); if (willBeHidden) return false; } return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); } }
template <> RubyValue Conversion<QDateTime>::to(const QDateTime &dateTime) { RubyValue sec = rb_rational_new(RubyValue::from(dateTime.toMSecsSinceEpoch()), RubyValue::from(1000)); RubyValue time = rb_time_num_new(sec, RubyValue::from(dateTime.offsetFromUtc())); return time.send(RUBYQML_INTERN("to_datetime")); }
QVector<struct timetick> TimeAxis::getTicks() { uint64_t delta = (uint64_t) (this->domainHi - this->domainLo); uint64_t deltatick; Timescale granularity; /* First find deltatick. In the case of months and years, which are * of variable length, just find the number of months or years. */ if (delta < MAX_NANOTICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(NANOTICK_INTERVALS, NANOTICK_INTERVALS_LEN, delta); granularity = Timescale::NANOSECOND; } else if (delta < MAX_MILLITICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(MILLITICK_INTERVALS, MILLITICK_INTERVALS_LEN, delta); granularity = Timescale::MILLISECOND; } else if (delta < MAX_SECTICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(SECTICK_INTERVALS, SECTICK_INTERVALS_LEN, delta); granularity = Timescale::SECOND; } else if (delta < MAX_MINUTETICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(MINUTETICK_INTERVALS, MINUTETICK_INTERVALS_LEN, delta); granularity = Timescale::MINUTE; } else if (delta < MAX_HOURTICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(HOURTICK_INTERVALS, HOURTICK_INTERVALS_LEN, delta); granularity = Timescale::HOUR; } else if (delta < MAX_DAYTICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(DAYTICK_INTERVALS, DAYTICK_INTERVALS_LEN, delta); granularity = Timescale::DAY; } else if (delta < MAX_MONTHTICK * TIMEAXIS_MAXTICKS) { deltatick = getTimeTickDelta(MONTHTICK_INTERVALS, MONTHTICK_INTERVALS_LEN, delta); granularity = Timescale::MONTH; } else { deltatick = getTimeTickDelta(YEARTICK_INTERVALS, YEARTICK_INTERVALS_LEN, delta); granularity = Timescale::YEAR; } QVector<struct timetick> ticks; int64_t domainLoMSecs = this->domainLo / MILLISECOND_NS; int64_t domainLoNSecs = this->domainLo % MILLISECOND_NS; if (domainLoNSecs < 0) { domainLoMSecs -= 1; domainLoNSecs += MILLISECOND_NS; } int64_t starttime, prevstart; switch(granularity) { case Timescale::YEAR: { int yeardelta = (int) (deltatick / YEAR_NS); Q_ASSERT((deltatick % YEAR_NS) == 0); QDateTime date = QDateTime::fromMSecsSinceEpoch(domainLoMSecs - 1, this->tz); int curryear = ceildiv(date.date().year(), yeardelta) * yeardelta; date.setDate(QDate(curryear, 1, 1)); date.setTime(QTime()); if (domainLoMSecs == date.toMSecsSinceEpoch() && domainLoNSecs > 0) { date = date.addYears(yeardelta); } /* TODO: this multiplication might overflow */ starttime = date.toMSecsSinceEpoch() * MILLISECOND_NS; prevstart = starttime; /* This is the lowest granularity, so we need to check for overflow. */ while (starttime <= this->domainHi && starttime >= prevstart) { ticks.append({ starttime, getTimeTickLabel(starttime, date, granularity, !this->promoteTicks) }); date = date.addYears(yeardelta); prevstart = starttime; starttime = date.toMSecsSinceEpoch() * MILLISECOND_NS; } break; } case Timescale::MONTH: { int monthdelta = (int) (deltatick / MONTH_NS); Q_ASSERT((deltatick % MONTH_NS) == 0); QDateTime date = QDateTime::fromMSecsSinceEpoch(domainLoMSecs - 1, this->tz); date.setTime(QTime()); /* currmonth is an int from 0 to 11. */ int currmonth = ceildiv(date.date().month() - 1, monthdelta) * monthdelta; int curryear = date.date().year() + (currmonth / 12); currmonth %= 12; date.setDate(QDate(curryear, currmonth + 1, 1)); date.setTime(QTime()); if (domainLoMSecs == date.toMSecsSinceEpoch() && domainLoNSecs > 0) { date = date.addMonths(monthdelta); } starttime = date.toMSecsSinceEpoch() * (int64_t) MILLISECOND_NS; prevstart = starttime; while (starttime <= this->domainHi && starttime >= prevstart) { ticks.append({ starttime, getTimeTickLabel(starttime, date, granularity, !this->promoteTicks) }); date = date.addMonths(monthdelta); prevstart = starttime; starttime = date.toMSecsSinceEpoch() * (int64_t) MILLISECOND_NS; } break; } default: starttime = ceildiv(this->domainLo, (int64_t) deltatick) * deltatick; if (granularity == Timescale::DAY || granularity == Timescale::HOUR) { /* I'm assuming that the timezone offset is never in smaller granularity than minutes. */ QDateTime d = QDateTime::fromMSecsSinceEpoch(ceildiv(starttime, MILLISECOND_NS), this->tz); starttime -= SECOND_NS * (int64_t) d.offsetFromUtc(); while (starttime > this->domainLo) { starttime -= deltatick; } while (starttime < this->domainLo) { starttime += deltatick; } } prevstart = starttime; while (starttime <= this->domainHi && starttime >= prevstart) { // Add the tick to ticks QDateTime date = QDateTime::fromMSecsSinceEpoch(ceildiv(starttime, MILLISECOND_NS), this->tz); ticks.append({ starttime, getTimeTickLabel(starttime, date, granularity, !this->promoteTicks) }); prevstart = starttime; starttime += deltatick; } break; } return ticks; }