Exemplo n.º 1
0
void
GregorianCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status)
{
    if((amount == 0) || U_FAILURE(status)) {
        return;
    }

    // J81 processing. (gregorian cutover)
    UBool inCutoverMonth = FALSE;
    int32_t cMonthLen=0; // 'c' for cutover; in days
    int32_t cDayOfMonth=0; // no discontinuity: [0, cMonthLen)
    double cMonthStart=0.0; // in ms

    // Common code - see if we're in the cutover month of the cutover year
    if(get(UCAL_EXTENDED_YEAR, status) == fGregorianCutoverYear) {
        switch (field) {
        case UCAL_DAY_OF_MONTH:
        case UCAL_WEEK_OF_MONTH:
            {
                int32_t max = monthLength(internalGet(UCAL_MONTH));
                UDate t = internalGetTime();
                // We subtract 1 from the DAY_OF_MONTH to make it zero-based, and an
                // additional 10 if we are after the cutover. Thus the monthStart
                // value will be correct iff we actually are in the cutover month.
                cDayOfMonth = internalGet(UCAL_DAY_OF_MONTH) - ((t >= fGregorianCutover) ? 10 : 0);
                cMonthStart = t - ((cDayOfMonth - 1) * kOneDay);
                // A month containing the cutover is 10 days shorter.
                if ((cMonthStart < fGregorianCutover) &&
                    (cMonthStart + (cMonthLen=(max-10))*kOneDay >= fGregorianCutover)) {
                        inCutoverMonth = TRUE;
                    }
            }
        default:
            ;
        }
    }

    switch (field) {
    case UCAL_WEEK_OF_YEAR: {
        // Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the
        // week.  Also, rolling the week of the year can have seemingly
        // strange effects simply because the year of the week of year
        // may be different from the calendar year.  For example, the
        // date Dec 28, 1997 is the first day of week 1 of 1998 (if
        // weeks start on Sunday and the minimal days in first week is
        // <= 3).
        int32_t woy = get(UCAL_WEEK_OF_YEAR, status);
        // Get the ISO year, which matches the week of year.  This
        // may be one year before or after the calendar year.
        int32_t isoYear = get(UCAL_YEAR_WOY, status);
        int32_t isoDoy = internalGet(UCAL_DAY_OF_YEAR);
        if (internalGet(UCAL_MONTH) == UCAL_JANUARY) {
            if (woy >= 52) {
                isoDoy += handleGetYearLength(isoYear);
            }
        } else {
            if (woy == 1) {
                isoDoy -= handleGetYearLength(isoYear - 1);
            }
        }
        woy += amount;
        // Do fast checks to avoid unnecessary computation:
        if (woy < 1 || woy > 52) {
            // Determine the last week of the ISO year.
            // We do this using the standard formula we use
            // everywhere in this file.  If we can see that the
            // days at the end of the year are going to fall into
            // week 1 of the next year, we drop the last week by
            // subtracting 7 from the last day of the year.
            int32_t lastDoy = handleGetYearLength(isoYear);
            int32_t lastRelDow = (lastDoy - isoDoy + internalGet(UCAL_DAY_OF_WEEK) -
                getFirstDayOfWeek()) % 7;
            if (lastRelDow < 0) lastRelDow += 7;
            if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7;
            int32_t lastWoy = weekNumber(lastDoy, lastRelDow + 1);
            woy = ((woy + lastWoy - 1) % lastWoy) + 1;
        }
        set(UCAL_WEEK_OF_YEAR, woy);
        set(UCAL_YEAR_WOY,isoYear);
        return;
                            }

    case UCAL_DAY_OF_MONTH:
        if( !inCutoverMonth ) { 
            Calendar::roll(field, amount, status);
            return;
        } else {
            // [j81] 1582 special case for DOM
            // The default computation works except when the current month
            // contains the Gregorian cutover.  We handle this special case
            // here.  [j81 - aliu]
            double monthLen = cMonthLen * kOneDay;
            double msIntoMonth = uprv_fmod(internalGetTime() - cMonthStart +
                amount * kOneDay, monthLen);
            if (msIntoMonth < 0) {
                msIntoMonth += monthLen;
            }
#if defined (U_DEBUG_CAL)
            fprintf(stderr, "%s:%d: roll DOM %d  -> %.0lf ms  \n", 
                __FILE__, __LINE__,amount, cMonthLen, cMonthStart+msIntoMonth);
#endif
            setTimeInMillis(cMonthStart + msIntoMonth, status);
            return;
        }

    case UCAL_WEEK_OF_MONTH:
        if( !inCutoverMonth ) { 
            Calendar::roll(field, amount, status);
            return;
        } else {
#if defined (U_DEBUG_CAL)
            fprintf(stderr, "%s:%d: roll WOM %d ??????????????????? \n", 
                __FILE__, __LINE__,amount);
#endif
            // NOTE: following copied from  the old
            //     GregorianCalendar::roll( WEEK_OF_MONTH )  code 

            // This is tricky, because during the roll we may have to shift
            // to a different day of the week.  For example:

            //    s  m  t  w  r  f  s
            //          1  2  3  4  5
            //    6  7  8  9 10 11 12

            // When rolling from the 6th or 7th back one week, we go to the
            // 1st (assuming that the first partial week counts).  The same
            // thing happens at the end of the month.

            // The other tricky thing is that we have to figure out whether
            // the first partial week actually counts or not, based on the
            // minimal first days in the week.  And we have to use the
            // correct first day of the week to delineate the week
            // boundaries.

            // Here's our algorithm.  First, we find the real boundaries of
            // the month.  Then we discard the first partial week if it
            // doesn't count in this locale.  Then we fill in the ends with
            // phantom days, so that the first partial week and the last
            // partial week are full weeks.  We then have a nice square
            // block of weeks.  We do the usual rolling within this block,
            // as is done elsewhere in this method.  If we wind up on one of
            // the phantom days that we added, we recognize this and pin to
            // the first or the last day of the month.  Easy, eh?

            // Another wrinkle: To fix jitterbug 81, we have to make all this
            // work in the oddball month containing the Gregorian cutover.
            // This month is 10 days shorter than usual, and also contains
            // a discontinuity in the days; e.g., the default cutover month
            // is Oct 1582, and goes from day of month 4 to day of month 15.

            // Normalize the DAY_OF_WEEK so that 0 is the first day of the week
            // in this locale.  We have dow in 0..6.
            int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek();
            if (dow < 0) 
                dow += 7;

            // Find the day of month, compensating for cutover discontinuity.
            int32_t dom = cDayOfMonth;

            // Find the day of the week (normalized for locale) for the first
            // of the month.
            int32_t fdm = (dow - dom + 1) % 7;
            if (fdm < 0) 
                fdm += 7;

            // Get the first day of the first full week of the month,
            // including phantom days, if any.  Figure out if the first week
            // counts or not; if it counts, then fill in phantom days.  If
            // not, advance to the first real full week (skip the partial week).
            int32_t start;
            if ((7 - fdm) < getMinimalDaysInFirstWeek())
                start = 8 - fdm; // Skip the first partial week
            else
                start = 1 - fdm; // This may be zero or negative

            // Get the day of the week (normalized for locale) for the last
            // day of the month.
            int32_t monthLen = cMonthLen;
            int32_t ldm = (monthLen - dom + dow) % 7;
            // We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here.

            // Get the limit day for the blocked-off rectangular month; that
            // is, the day which is one past the last day of the month,
            // after the month has already been filled in with phantom days
            // to fill out the last week.  This day has a normalized DOW of 0.
            int32_t limit = monthLen + 7 - ldm;

            // Now roll between start and (limit - 1).
            int32_t gap = limit - start;
            int32_t newDom = (dom + amount*7 - start) % gap;
            if (newDom < 0) 
                newDom += gap;
            newDom += start;

            // Finally, pin to the real start and end of the month.
            if (newDom < 1) 
                newDom = 1;
            if (newDom > monthLen) 
                newDom = monthLen;

            // Set the DAY_OF_MONTH.  We rely on the fact that this field
            // takes precedence over everything else (since all other fields
            // are also set at this point).  If this fact changes (if the
            // disambiguation algorithm changes) then we will have to unset
            // the appropriate fields here so that DAY_OF_MONTH is attended
            // to.

            // If we are in the cutover month, manipulate ms directly.  Don't do
            // this in general because it doesn't work across DST boundaries
            // (details, details).  This takes care of the discontinuity.
            setTimeInMillis(cMonthStart + (newDom-1)*kOneDay, status);                
            return;
        }

    default:
        Calendar::roll(field, amount, status);
        return;
    }
}
Exemplo n.º 2
0
void SessionsBox::gotAuthorizations(const MTPaccount_Authorizations &result) {
	_shortPollRequest = 0;
	setLoading(false);

	auto availCurrent = st::boxWideWidth - st::sessionPadding.left() - st::sessionTerminateSkip;
	auto availOther = availCurrent - st::sessionTerminate.iconPosition.x();// -st::sessionTerminate.width - st::sessionTerminateSkip;

	_list.clear();
	if (result.type() != mtpc_account_authorizations) {
		return;
	}
	auto &v = result.c_account_authorizations().vauthorizations.v;
	_list.reserve(v.size());

	const CountriesByISO2 &countries(countriesByISO2());

	for_const (auto &auth, v) {
		if (auth.type() != mtpc_authorization) {
			continue;
		}
		auto &d = auth.c_authorization();
		Data data;
		data.hash = d.vhash.v;

		QString appName, appVer = qs(d.vapp_version), systemVer = qs(d.vsystem_version), deviceModel = qs(d.vdevice_model);
		if (d.vapi_id.v == 2040 || d.vapi_id.v == 17349) {
			appName = (d.vapi_id.v == 2040) ? qstr("Telegram Desktop") : qstr("Telegram Desktop (GitHub)");
		//	if (systemVer == qstr("windows")) {
		//		deviceModel = qsl("Windows");
		//	} else if (systemVer == qstr("os x")) {
		//		deviceModel = qsl("OS X");
		//	} else if (systemVer == qstr("linux")) {
		//		deviceModel = qsl("Linux");
		//	}
			if (appVer == QString::number(appVer.toInt())) {
				int32 ver = appVer.toInt();
				appVer = QString("%1.%2").arg(ver / 1000000).arg((ver % 1000000) / 1000) + ((ver % 1000) ? ('.' + QString::number(ver % 1000)) : QString());
			//} else {
			//	appVer = QString();
			}
		} else {
			appName = qs(d.vapp_name);// +qsl(" for ") + qs(d.vplatform);
			if (appVer.indexOf('(') >= 0) appVer = appVer.mid(appVer.indexOf('('));
		}
		data.name = appName;
		if (!appVer.isEmpty()) data.name += ' ' + appVer;
		data.nameWidth = st::sessionNameFont->width(data.name);

		QString country = qs(d.vcountry), platform = qs(d.vplatform);
		//CountriesByISO2::const_iterator j = countries.constFind(country);
		//if (j != countries.cend()) country = QString::fromUtf8(j.value()->name);

		const auto active = data.activeTime = d.vdate_active.v
			? d.vdate_active.v
			: d.vdate_created.v;

		data.info = qs(d.vdevice_model) + qstr(", ") + (platform.isEmpty() ? QString() : platform + ' ') + qs(d.vsystem_version);
		data.ip = qs(d.vip) + (country.isEmpty() ? QString() : QString::fromUtf8(" \xe2\x80\x93 ") + country);
		if (!data.hash || (d.vflags.v & 1)) {
			data.active = lang(lng_sessions_header);
			data.activeWidth = st::sessionWhenFont->width(lang(lng_sessions_header));
			int32 availForName = availCurrent - st::sessionPadding.right() - data.activeWidth;
			if (data.nameWidth > availForName) {
				data.name = st::sessionNameFont->elided(data.name, availForName);
				data.nameWidth = st::sessionNameFont->width(data.name);
			}
			data.infoWidth = st::sessionInfoFont->width(data.info);
			if (data.infoWidth > availCurrent) {
				data.info = st::sessionInfoFont->elided(data.info, availCurrent);
				data.infoWidth = st::sessionInfoFont->width(data.info);
			}
			data.ipWidth = st::sessionInfoFont->width(data.ip);
			if (data.ipWidth > availCurrent) {
				data.ip = st::sessionInfoFont->elided(data.ip, availCurrent);
				data.ipWidth = st::sessionInfoFont->width(data.ip);
			}
			_current = data;
		} else {
			const auto now = QDateTime::currentDateTime();
			const auto lastTime = ParseDateTime(active);
			const auto nowDate = now.date();
			const auto lastDate = lastTime.date();
			QString dt;
			if (lastDate == nowDate) {
				data.active = lastTime.toString(cTimeFormat());
			} else if (lastDate.year() == nowDate.year()
				&& lastDate.weekNumber() == nowDate.weekNumber()) {
				data.active = langDayOfWeek(lastDate);
			} else {
				data.active = lastDate.toString(qsl("d.MM.yy"));
			}
			data.activeWidth = st::sessionWhenFont->width(data.active);
			int32 availForName = availOther - st::sessionPadding.right() - data.activeWidth;
			if (data.nameWidth > availForName) {
				data.name = st::sessionNameFont->elided(data.name, availForName);
				data.nameWidth = st::sessionNameFont->width(data.name);
			}
			data.infoWidth = st::sessionInfoFont->width(data.info);
			if (data.infoWidth > availOther) {
				data.info = st::sessionInfoFont->elided(data.info, availOther);
				data.infoWidth = st::sessionInfoFont->width(data.info);
			}
			data.ipWidth = st::sessionInfoFont->width(data.ip);
			if (data.ipWidth > availOther) {
				data.ip = st::sessionInfoFont->elided(data.ip, availOther);
				data.ipWidth = st::sessionInfoFont->width(data.ip);
			}

			_list.push_back(data);
			for (int32 i = _list.size(); i > 1;) {
				--i;
				if (_list.at(i).activeTime > _list.at(i - 1).activeTime) {
					qSwap(_list[i], _list[i - 1]);
				}
			}
		}
	}
	_inner->listUpdated();

	update();

	_shortPollTimer->start(SessionsShortPollTimeout);
}
Exemplo n.º 3
0
void
kMyMoneyDateTbl::paintCell(QPainter *painter, int row, int col)
{
  QRect rect;
  QString text;
  QPen pen;
  int w=cellWidth();
  int h=cellHeight();
  QBrush brushBlue(KGlobalSettings::activeTitleColor());
  QBrush brushLightblue(KGlobalSettings::baseColor());
  QFont font=KGlobalSettings::generalFont();

  // -----
  font.setPointSize(fontsize);

#if KDE_VERSION < 310
  int firstWeekDay = KGlobal::locale()->weekStartsMonday() ? 1 : 0;
#else
  int firstWeekDay = KGlobal::locale()->weekStartDay();
#endif

  if (row==0)
  { // we are drawing the headline
    if (m_type == MONTHLY)
    {
      font.setBold(true);
      painter->setFont(font);
      bool normalday = true;
      QString daystr;
      if ( col+firstWeekDay < 8 )
        daystr = WEEK_DAY_NAME(col+firstWeekDay, true);
      else
        daystr = WEEK_DAY_NAME(col+firstWeekDay-7, true);

      if ( daystr==i18n("Sunday", "Sun") || daystr==i18n("Saturday", "Sat") )
        normalday=false;

      if (!normalday)
      {
        painter->setPen(KGlobalSettings::baseColor());
        painter->setBrush(brushLightblue);
        painter->drawRect(0, 0, w, h);
        painter->setPen(KGlobalSettings::activeTitleColor());
      } else {
        painter->setPen(KGlobalSettings::activeTitleColor());
        painter->setBrush(brushBlue);
        painter->drawRect(0, 0, w, h);
        painter->setPen(KGlobalSettings::activeTextColor());
      }
      painter->drawText(0, 0, w, h-1, AlignCenter,
                        daystr, -1, &rect);
      painter->setPen(KGlobalSettings::textColor());
      painter->moveTo(0, h-1);
      painter->lineTo(w-1, h-1);

      if(rect.width()>maxCell.width())
        maxCell.setWidth(rect.width());

      if(rect.height()>maxCell.height())
        maxCell.setHeight(rect.height());
    }
    else if (m_type == WEEKLY)
    {
      painter->setPen(KGlobalSettings::activeTitleColor());
      painter->setBrush(brushBlue);
      painter->drawRect(0, 0, w, h);
      painter->setPen(KGlobalSettings::activeTextColor());

      int year=date.year();
      QString headerText;
#if QT_VERSION > 0x030005
      // FIXME: Shouldn't that be i18n()'ed as well
      QString weekStr = QString::number(date.weekNumber(&year));
      QString yearStr = QString::number(year);
      headerText = i18n("Week %1 for year %2.")
        .arg(weekStr)
        .arg(yearStr);
#else
      // FIXME: include code to display the same as for KDE >= 3.0.5
      QString weekStr = QString::number(weekNumber(date, &year));
      QString yearStr = QString::number(year);
      headerText = i18n("Week %1 for year %2.")
        .arg(weekStr)
        .arg(yearStr);
#endif

      painter->drawText(0, 0, w, h-1, AlignCenter, headerText, -1, &rect);

      maxCell.setWidth(width());

      if(rect.height()>maxCell.height())
        maxCell.setHeight(rect.height());
    }
    else if (m_type == QUARTERLY)
    {
      int athird = width()/3;

      painter->setPen(KGlobalSettings::activeTitleColor());
      painter->setBrush(brushBlue);
      painter->setPen(/*KGlobalSettings::activeTextColor()*/black);

      if (col == 0)
      {
        painter->drawRect(0, 0, athird, h);
        painter->drawText(0, 0, athird, h-1, AlignCenter, "Month 1", -1, &rect);

        painter->drawRect(athird, 0, athird, h);
        painter->drawText(athird, 0, athird, h-1, AlignCenter, "Month 2", -1, &rect);

        painter->drawRect(athird*2, 0, athird, h);
        painter->drawText(athird*2, 0, athird, h-1, AlignCenter, "Month 3", -1, &rect);
      }
    }
  }
  else
  {
    int pos;

    QDate drawDate(date);

    if (m_type == MONTHLY)
    {
      pos=7*(row-1)+col;
      if ( firstWeekDay < 4 )
        pos += firstWeekDay;
      else
        pos += firstWeekDay - 7;

      if (pos<firstday || (firstday+numdays<=pos))
      { // we are either
        //  painting a day of the previous month or
        //  painting a day of the following month

        if (pos<firstday)
        { // previous month
          drawDate = drawDate.addMonths(-1);
          text.setNum(numDaysPrevMonth+pos-firstday+1);
          drawDate.setYMD(drawDate.year(), drawDate.month(), text.toInt());
        } else { // following month
          drawDate = drawDate.addMonths(1);
          text.setNum(pos-firstday-numdays+1);
          drawDate.setYMD(drawDate.year(), drawDate.month(), text.toInt());
        }
      } else { // paint a day of the current month
        text.setNum(pos-firstday+1);
        drawDate.setYMD(drawDate.year(), drawDate.month(), text.toInt());
      }
    }
    else if (m_type == WEEKLY)
    {
      // TODO: Handle other start weekdays than Monday
      text = QDate::shortDayName(row);
      text += " ";

      int dayOfWeek = date.dayOfWeek();
      int diff;

      if (row < dayOfWeek)
      {
        diff = -(dayOfWeek - row);
      }
      else
      {
        diff = row - dayOfWeek;
      }

      drawDate = date.addDays(diff);
    }
    else if (m_type == QUARTERLY)
    {
    }

    drawCellContents(painter, row, col, drawDate);
  }
}