int kMyMoneyCalendar::weekOfYear(const QDate& date) { // Calculate ISO 8601 week number (taken from glibc/Gnumeric) int year, week, jan1wday, nextjan1wday; QDate jan1date, nextjan1date; year = date.year(); jan1date = QDate(year, 1, 1); jan1wday = jan1date.dayOfWeek(); week = (date.dayOfYear() - 1 + jan1wday - 1) / 7 + ((jan1wday - 1) == 0 ? 1 : 0); /* Does date belong to last week of previous year? */ if ((week == 0) && (jan1wday > 4 /*THURSDAY*/)) { QDate tmpdate = QDate(year - 1, 12, 31); return weekOfYear(tmpdate); } if ((jan1wday <= 4 /*THURSDAY*/) && (jan1wday > 1 /*MONDAY*/)) week++; if (week == 53) { nextjan1date = QDate(year + 1, 1, 1); nextjan1wday = nextjan1date.dayOfWeek(); if (nextjan1wday <= 4 /*THURSDAY*/) week = 1; } return week; }
void kMyMoneyCalendar::selectWeekClicked() { const KCalendarSystem* calendar = KGlobal::locale()->calendar(); KPopupFrame *popup = new KPopupFrame(this); KDatePickerPrivateWeekSelector *picker = new KDatePickerPrivateWeekSelector(calendar, date(), popup); picker->resize(picker->sizeHint()); picker->setWeek(weekOfYear(date())); picker->selectAll(); popup->setMainWidget(picker); connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int))); picker->setFocus(); if (popup->exec(d->selectWeek->mapToGlobal(QPoint(0, d->selectWeek->height())))) { QDate newDate; int week = picker->week(); // check if new week will lead to a valid date calendar->setDate(newDate, calendar->year(date()), 1, 1); while (weekOfYear(newDate) > 50) newDate = newDate.addDays(1); while (weekOfYear(newDate) < week && (week != 53 || (week == 53 && (weekOfYear(newDate) != 52 || weekOfYear(newDate.addDays(1)) != 1)))) newDate = newDate.addDays(1); if (week == 53 && weekOfYear(newDate) == 52) while (weekOfYear(newDate.addDays(-1)) == 52) newDate = newDate.addDays(-1); // Set the date, if it's invalid in any way then alert user and don't update if (!setDate(newDate)) { KNotification::beep(); } } delete popup; }
void kMyMoneyCalendar::dateChangedSlot(const QDate& date) { kDebug() << "kMyMoneyCalendar::dateChangedSlot: date changed (" << date.year() << "/" << date.month() << "/" << date.day() << ")."; line->setText(KGlobal::locale()->formatDate(date, KLocale::ShortDate)); d->selectWeek->setText(i18n("Week %1", weekOfYear(date))); selectMonth->setText(MONTH_NAME(date.month(), date.year(), KCalendarSystem::ShortName)); selectYear->setText(date.toString("yyyy")); emit(dateChanged(date)); }
bool kMyMoneyCalendar::setDate(const QDate& date) { if (!table) return true; // hack if (date.isValid()) { QString temp; // ----- table->setDate(date); d->selectWeek->setText(i18n("Week %1", weekOfYear(date))); selectMonth->setText(MONTH_NAME(date.month(), date.year(), KCalendarSystem::LongName)); temp.setNum(date.year()); selectYear->setText(temp); line->setText(KGlobal::locale()->formatDate(date, KLocale::ShortDate)); return true; } else { kDebug() << "kMyMoneyCalendar::setDate: refusing to set invalid date."; return false; } }
std::string Timestamp::format(const std::string& format) const { char buf[20]; uint16_t year, month, day, hour, minute, second, millisecond; calcFromJulDay(_time, year, month, day, hour, minute, second, millisecond); FormatContext context(format); std::stringstream ss; while(!context.ready()) { char c = context.nextFormatSpecifier(); switch(c) { case '%': { char n = context.nextFormatSpecifier(); switch(n) { case 'Y': ss << std::setw(4) << std::setfill('0') << itoa(year, buf); break; case 'm': ss << std::setw(2) << std::setfill('0') << itoa(month, buf); break; case 'd': ss << std::setw(2) << std::setfill('0') << itoa(day, buf); break; case 'F': { if(year > 1000) { itoa(year, buf); } else if(year > 100) { buf[0] = '0'; itoa(year, &buf[1]); } else if(year > 10) { buf[0] = '0'; buf[1] = '0'; itoa(year, &buf[2]); } else { buf[0] = '0'; buf[1] = '0'; buf[2] = '0'; itoa(year, &buf[3]); } buf[4] = '-'; if(month > 9) { itoa(month, &buf[5]); } else { buf[5] = '0'; itoa(month, &buf[6]); } buf[7] = '-'; if(day > 9) { itoa(day, &buf[8]); } else { buf[8] = '0'; itoa(day, &buf[9]); } buf[10] = 'T'; if(hour > 9) { itoa(hour, &buf[11]); } else { buf[11] = '0'; itoa(hour, &buf[12]); } buf[13] = ':'; if(minute > 9) { itoa(minute, &buf[14]); } else { buf[14] = '0'; itoa(minute, &buf[15]); } buf[16] = ':'; if(second > 9) { itoa(second, &buf[17]); } else { buf[17] = '0'; itoa(second, &buf[18]); } ss << buf; break; } case 'j': ss << std::setw(3) << std::setfill('0') << itoa(dayOfYear(), buf); break; case 'U': ss << std::setw(2) << std::setfill('0') << itoa(weekOfYear(), buf); break; case 'w': ss << itoa(dayOfWeek(), buf); break; case 'H': ss << std::setw(2) << std::setfill('0') << itoa(hour, buf); break; case 'M': ss << std::setw(2) << std::setfill('0') << itoa(minute, buf); break; case 'S': ss << std::setw(2) << std::setfill('0') << itoa(second, buf); break; case 's': ss << std::setw(3) << std::setfill('0') << itoa(millisecond, buf); break; } break; } default: ss << c; } } return ss.str(); }
CString CDateTime::toString(const CString& format) const { if (isNull()) { return CString(); } CString str; bool special = false; const unsigned int len = format.getSize(); for (unsigned int i = 0 ; i < len ; ++i) { if (format[i] == CChar('%')) { special = true; } else if (special) { switch (format[i].toLatin1()) { default: str += format[i]; break; case 'a': str += (m_hour < 12 ? "am" : "pm"); break; case 'A': str += (m_hour < 12 ? "AM" : "PM"); break; case 'd': if (m_day < 10) str += '0'; str += CString::fromNumber(m_day); break; case 'D': str += shortDayName(dayOfWeek(m_year, m_month, m_day)); break; case 'F': str += longMonthName(m_month); break; case 'g': if (m_hour == 0) { str += "12"; } else if (m_hour < 13) { str += CString::fromNumber(m_hour); } else { str += CString::fromNumber(m_hour - 12); } break; case 'G': str += CString::fromNumber(m_hour); break; case 'h': if (m_hour == 0) { str += "12"; } else if (m_hour < 13) { if (m_hour < 10) { str += '0'; } str += CString::fromNumber(m_hour); } else { if (m_hour < 22) { str += '0'; } str += CString::fromNumber(m_hour - 12); } break; case 'H': if (m_hour < 10) { str += '0'; } str += CString::fromNumber(m_hour); break; case 'i': if (m_minute < 10) { str += '0'; } str += CString::fromNumber(m_minute); break; case 'j': str += CString::fromNumber(m_day); break; case 'l': str += longDayName(dayOfWeek(m_year, m_month, m_day)); break; case 'L': str += (isLeapYear(m_year) ? '1' : '0'); break; case 'm': if (m_month < 10) { str += '0'; } str += CString::fromNumber(m_month); break; case 'M': str += shortMonthName(m_month); break; case 'n': str += CString::fromNumber(m_month); break; case 'N': str += dayOfWeek(m_year, m_month, m_day); break; case 's': if (m_second < 10) { str += '0'; } str += CString::fromNumber(m_second); break; case 't': str += CString::fromNumber(daysInMonth(m_month, m_year)); break; case 'U': str += CString::fromNumber(timestamp(m_year, m_month, m_day, m_hour, m_minute, m_second)); break; case 'W': str += CString::fromNumber(weekOfYear(m_year, m_month, m_day)); break; case 'y': if (m_year < 0) { str += CString::fromNumber(m_year); } else { unsigned int tmp = m_year % 100; if (tmp < 10) { str += "0"; } str += CString::fromNumber(tmp); } break; case 'Y': if (m_year < 0) { str += CString::fromNumber(m_year); } else { unsigned int tmp = m_year % 10000; if (tmp < 10) str += "000"; if (tmp < 100) str += "00"; if (tmp < 1000) str += "000"; str += CString::fromNumber(tmp); } break; case 'z': str += dayOfYear(m_year, m_month, m_day); break; } special = false; } else { str += format[i]; } } return str; }
void CalendarDatePrivate::initDayView() { ComputeDays(); GtkWidget* swipebox; gint rowCount = 7; gint offsetCols = (m_isShowWeekNum ? 1 : 0); gint colCount = 7 + offsetCols; int row = 0; int col = 0; char buffer[32]; int selectedYear = m_selectedYear; int selectedMonth = m_selectedMonth; GDateTime* nowDate = g_date_time_new_now_local(); int cellWidth = MAIN_BOX_DEFAULT_WIDTH / colCount; int cellHeight = MAIN_BOX_DEFAULT_WIDTH / rowCount; m_swipeBox[VIEWTYPE_DAY] = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); swipebox = m_swipeBox[VIEWTYPE_DAY]; gtk_fixed_put(GTK_FIXED(m_mainBox), GTK_WIDGET(m_swipeBox[VIEWTYPE_DAY]), 0, 0); gtk_widget_set_name(GTK_WIDGET(swipebox), "calendar-table"); GDateTime* selectedDate = g_date_time_new_local(m_selectedYear, m_selectedMonth, m_selectedDayOfMonth, 1, 1, 1); gchar* dateLabelText = g_date_time_format(selectedDate, C_("calendar heading", "%Y, %b")); gtk_button_set_label(GTK_BUTTON(m_viewTypeSwitch), dateLabelText); g_date_time_unref(selectedDate); g_free(dateLabelText); for (col = 0; col < colCount; ++col) { GtkWidget* daylayout = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); for (row = 0; row < rowCount; ++row) { if (m_isShowWeekNum && (0 == col)) { if (0 == row) { GtkWidget* weeknumbtn = gtk_button_new_with_label(""); gtk_widget_set_size_request(GTK_WIDGET(weeknumbtn), cellWidth, cellHeight); gtk_box_pack_start(GTK_BOX(daylayout), GTK_WIDGET(weeknumbtn), TRUE, TRUE, 0); } else { int day = m_day[row - 1][6]; int month = selectedMonth; int year = selectedYear; switch (m_dayMonth[row - 1][6]) { case MONTH_PREV: --month; if (1 > month) { month = 12; --year; } break; case MONTH_CURRENT: break; case MONTH_NEXT: ++month; if (12 < month) { month = 1; ++year; } break; default: g_assert(FALSE); break; } int week = weekOfYear(year, month, day); g_snprintf(buffer, sizeof(buffer), "%02d", week); GtkWidget* weeknumbtn = gtk_button_new_with_label(buffer); gtk_widget_set_size_request(GTK_WIDGET(weeknumbtn), cellWidth, cellHeight); gtk_box_pack_start(GTK_BOX(daylayout), GTK_WIDGET(weeknumbtn), TRUE, TRUE, 0); } // end: if (0 == row) } else { if (0 == row) { GtkWidget* weekTitleItem = gtk_button_new_with_label(m_defaultAbbreviatedDayname[(col - offsetCols + m_weekStart) % 7]); gtk_widget_set_size_request(GTK_WIDGET(weekTitleItem), cellWidth, cellHeight); gtk_box_pack_start(GTK_BOX(daylayout), GTK_WIDGET(weekTitleItem), TRUE, TRUE, 0); } else { m_dayItem[row - 1][col - offsetCols] = new CalendarItem(ITEMTYPE_DAY_OF_MONTH, m_day[row - 1][col - offsetCols]); m_dayItem[row - 1][col - offsetCols]->setSize(cellWidth, cellHeight); m_dayItem[row - 1][col - offsetCols]->setParent(GTK_WIDGET(daylayout)); } } // end: if (priv->show_week_num && (0 == col)) } // end: for row gtk_box_pack_start(GTK_BOX(swipebox), GTK_WIDGET(daylayout), TRUE, TRUE, 0); } // end: for col g_date_time_unref(nowDate); }