void Date::add_month (int n) { // Negative if(n < 0) { int tmp = month(); this->m = tmp - abs(n) > 0 ? tmp - abs(n) : 12 - abs(n + tmp) % 12; if(n + tmp <= 0) { this->y -= (1 + floor(abs(n + tmp)/12)); } } else { int months = abs(n) + month(); if(months % 12 != 0) this->m = months % 12; else this->m = 12; if(months > 12 && months % 12 != 0) { this->y += months / 12; } else if (months > 12) { this->y += months / 12 - 1; } } if(!valid_date(year(), month(), day())) { this->d = days_this_month(); } this->julian_day_number = calc_julian_day_number(year(), month(), day()); }
void Date::add_year (int n) { this->y += n; if(!valid_date(year(), month(), day())) { this->d = days_this_month(); } this->julian_day_number = calc_julian_day_number(year(), month(), day()); }
Middle& Middle::add_year(int n) { if(n == 0) return *this; int sign = -1; if( n > 0) { sign = 1; } if(isLeapYear()) { if(std::abs(n) > 3 && isLeapYear(year() + 4*sign)) { _numeric += (365*4 + 1 )*sign; n -= sign * 4; return add_year(n); }else if((sign > 0 && (month() < 3 && !(month() == 2 && day() == 29 ))) || (sign < 0 && (month() > 2 || (month() == 2 && day() == 29 )))) { _numeric += 366 * sign; n -= sign; return add_year(n); } } else if(isLeapYear(year() + sign) && ((( month() > 2 || (month() == 2 && day() == 29 ))&& sign > 0) || (sign < 0 && (month() < 3 && !(month() == 2 && day() == 29 ))))) { _numeric += 366 * sign; n -= sign; return add_year(n); } _numeric += 365 * sign; n -= sign; return add_year(n); }
// This method MUST be re-implemented in any new Calendar System bool KCalendarSystem::isValid( int y, int month, int day ) const { // Default to true Gregorian if ( y < year( earliestValidDate() ) || y > year( latestValidDate() ) ) { return false; } if ( month < 1 || month > 12 ) { return false; } if ( month == 2 ) { if ( isLeapYear( y ) ) { return ( day >= 1 && day <= 29 ); } else { return ( day >= 1 && day <= 28 ); } } if ( month == 4 || month == 6 || month == 9 || month == 11 ) { return ( day >= 1 && day <= 30 ); } return ( day >= 1 && day <= 31 ); }
/* * This redisplays the 3 month panels 2 show the correct 3-month * window, centred around the month containing the "current" date. */ static void update_quarter(Calendar *c) { Day *d = (Day *)c->view->day_info; int year_num, month_num; /* previous month */ year_num = year(previousmonth(c->view->date)); month_num = month(previousmonth(c->view->date)); XtVaSetValues(d->month_panels[0], XmNyear, year_num, XmNmonth, month_num, NULL); /* current month */ year_num = year(c->view->date); month_num = month(c->view->date); XtVaSetValues(d->month_panels[1], XmNyear, year_num, XmNmonth, month_num, NULL); /* next month */ year_num = year(nextmonth(c->view->date)); month_num = month(nextmonth(c->view->date)); XtVaSetValues(d->month_panels[2], XmNyear, year_num, XmNmonth, month_num, NULL); }
//日期/时间字符串 //length对于年份,=2则为短格式(如2004年则返回"04"),其余 //数值则为4位字符串 //对于其他日期/时间,=2则为固定两位字符串,(如1返回"01", //29返回"29"),其余数值则根据具体日期/时间返回1或2位字符串 //因此只有length=2的情况才会有相应操作,其余均采用默认操作。 LPCTSTR ringDateTime::year2str(int length/*=0*/) { if(length == 2) return formatStr(m_str,year()%100,length); else return formatStr(m_str,year(),0); }
// Ok int KCalendarSystemHebrew::daysInYear(const TQDate & date) const { TQDate first, last; setYMD(first, year(date), 1, 1); // 1 Tishrey setYMD(last, year(date) + 1, 1, 1); // 1 Tishrey the year later return first.daysTo(last); }
BOOST_FORCEINLINE Date operator >(month_day md, Date d) { Date res; if (res.set_if_valid_date(year(d),month(md),day(md)) && res > d ) return res; if (res.set_if_valid_date(year(year(d)+1),month(md),day(md)) && res > d ) return res; res=Date(year(year(d)+2),month(md),day(md)); return res; }
/*----------------------------------------------------------------------* * Determine whether the given Local time_t is within the DST interval * * or the Standard time interval. * *----------------------------------------------------------------------*/ boolean Timezone::locIsDST(time_t local) { //recalculate the time change points if needed if (year(local) != year(_dstLoc)) calcTimeChanges(year(local)); if (_stdLoc > _dstLoc) //northern hemisphere return (local >= _dstLoc && local < _stdLoc); else //southern hemisphere return !(local >= _stdLoc && local < _dstLoc); }
/*----------------------------------------------------------------------* * Convert the given UTC time to local time, standard or * * daylight time, as appropriate. * *----------------------------------------------------------------------*/ time_t Timezone::toLocal(time_t utc) { //recalculate the time change points if needed if (year(utc) != year(_dstUTC)) calcTimeChanges(year(utc)); if (utcIsDST(utc)) return utc + _dst.offset * SECS_PER_MIN; else return utc + _std.offset * SECS_PER_MIN; }
/*----------------------------------------------------------------------* * Convert the given local time to UTC time. * * * * WARNING: * * This function is provided for completeness, but should seldom be * * needed and should be used sparingly and carefully. * * * * Ambiguous situations occur after the Standard-to-DST and the * * DST-to-Standard time transitions. When changing to DST, there is * * one hour of local time that does not exist, since the clock moves * * forward one hour. Similarly, when changing to standard time, there * * is one hour of local times that occur twice since the clock moves * * back one hour. * * * * This function does not test whether it is passed an erroneous time * * value during the Local -> DST transition that does not exist. * * If passed such a time, an incorrect UTC time value will be returned. * * * * If passed a local time value during the DST -> Local transition * * that occurs twice, it will be treated as the earlier time, i.e. * * the time that occurs before the transistion. * * * * Calling this function with local times during a transition interval * * should be avoided! * *----------------------------------------------------------------------*/ time_t Timezone::toUTC(time_t local) { //recalculate the time change points if needed if (year(local) != year(_dstLoc)) calcTimeChanges(year(local)); if (locIsDST(local)) return local - _dst.offset * SECS_PER_MIN; else return local - _std.offset * SECS_PER_MIN; }
/*----------------------------------------------------------------------* * Determine whether the given UTC time_t is within the DST interval * * or the Standard time interval. * *----------------------------------------------------------------------*/ boolean Timezone::utcIsDST(time_t utc) { //recalculate the time change points if needed if (year(utc) != year(_dstUTC)) calcTimeChanges(year(utc)); if (_stdUTC > _dstUTC) //northern hemisphere return (utc >= _dstUTC && utc < _stdUTC); else //southern hemisphere return !(utc >= _stdUTC && utc < _dstUTC); }
void ContentStructuredDocument::parseDate(const string& dateStr, QDate& dateBegin, QDate& dateEnd) { dateBegin=QDate(); dateEnd=QDate(); char sep('/'); //uint64_t firstSep,secondSep; portage 32 64 std::string::size_type firstSep,secondSep; try { if ( (firstSep=dateStr.find(sep)) != string::npos ) { if ( (secondSep=dateStr.find(sep,firstSep+1)) != string::npos ) { // suppose day/month/year // from_uk_string only defined in recent versions of boost // dateBegin=boost::gregorian::from_uk_string(dateStr); // dateEnd=boost::gregorian::from_uk_string(dateStr); // current version knows only year/month/day string day(dateStr,0,firstSep); string month(dateStr,firstSep+1,secondSep-firstSep); string year(dateStr,secondSep+1); string newDateStr=year+'/'+month+'/'+day; dateBegin=QDate::fromString(newDateStr.c_str()); dateEnd=QDate::fromString(newDateStr.c_str()); } else { // one separator : suppose month/year // find end of month string month(dateStr,0,firstSep); string year(dateStr,firstSep+1); unsigned short monthNum=atoi(month.c_str()); unsigned short yearNum=atoi(year.c_str()); dateBegin=QDate(yearNum,monthNum,1); dateEnd=dateBegin.addMonths(1).addDays(-1); } } else if (! dateStr.empty()) { // no separator : suppose year unsigned short yearNum=atoi(dateStr.c_str()); dateBegin=QDate(yearNum,01,01); dateEnd=QDate(yearNum,12,31); } } //catch (boost::bad_lexical_cast& e) { catch (std::exception& e) { DRLOGINIT; LWARN << "Warning: " << e.what(); LWARN << "Failed parsing of date [" << dateStr << "]"; // do nothing, keep not_a_date_time as default value } }
QString PartialDate::range(const PartialDate& d) const { if (year() != d.year()) return asShortString() + "-" + d.asShortString(); QString result = numberToString(year()); if (month() != d.month()) return QString("%1.%2-%3").arg(year()).arg(asShortString(Month | Day)) .arg(d.asShortString(Month | Day)); else if (day() != d.day()) return QString("%1.%2.%3-%4").arg(year()).arg(month()) .arg(asShortString(Day)).arg(d.asShortString(Day)); else return asShortString(); }
Datetime Datetime::preMonth() const { Datetime result; if (*this == Null<Datetime>()) return result; try { int m = month(); result = (m == 1) ? Datetime(year()-1, 12, 1) : Datetime(year(), m-1, 1); } catch(...) { result = Datetime::min(); } return result; }
Datetime Datetime::preHalfyear() const { Datetime result; if (*this == Null<Datetime>()) return result; try { int m = startOfHalfyear().month(); result = (m <= 6) ? Datetime(year()-1, 7, 1) : Datetime(year(), 1, 1); } catch(...) { result = Datetime::min(); } return result; }
Datetime Datetime::preQuarter() const { Datetime result; if (*this == Null<Datetime>()) return result; try { int m = startOfQuarter().month(); result = (m == 1) ? Datetime(year()-1, 10, 1) : Datetime(year(), m-3, 1); } catch(...) { result = Datetime::min(); } return result; }
/*----------------------------------------------------------------------* * Convert the given UTC time to local time, standard or * * daylight time, as appropriate, and return a pointer to the time * * change rule used to do the conversion. The caller must take care * * not to alter this rule. * *----------------------------------------------------------------------*/ time_t Timezone::toLocal(time_t utc, TimeChangeRule **tcr) { //recalculate the time change points if needed if (year(utc) != year(_dstUTC)) calcTimeChanges(year(utc)); if (utcIsDST(utc)) { *tcr = &_dst; return utc + _dst.offset * SECS_PER_MIN; } else { *tcr = &_std; return utc + _std.offset * SECS_PER_MIN; } }
/* Negativa! */ Date & Julian::add_year(int y){ if(day()==29&&month()==2){ //börjar på skottdag == 29 if((year()+y)%4==0){ // landar på en skottdag == 29 offset = double_julian_day(year()+y,2,29); return *this; }else{ offset = double_julian_day(year()+y,month(),28); // landar på ej skottår return *this; } }else{ // inte börjat på skottdag offset = double_julian_day(year()+y,month(),day()); return *this; } }
QString Tasty::parseDate(const QString& d, const bool bigLetter) { auto datetime = QDateTime::fromString(d.left(19), "yyyy-MM-ddTHH:mm:ss"); auto date = datetime.date(); auto today = QDate::currentDate(); if (today == date) return datetime.toString(QString("%1егодня в H:mm").arg(bigLetter ? "С" : "с")); if (today == date.addDays(1)) return datetime.toString(QString("%1чера в H:mm").arg(bigLetter ? "В" : "в")); bool showYear = date.year() != today.year(); QString format = showYear ? "d MMM yyyy" : "d MMM в H:mm"; return datetime.toString(format); }
int KCalendarSystem::weeksInYear( const QDate &date ) const { if ( isValid( date ) ) { return weeksInYear( year( date ) ); } return -1; }
// Ok int KCalendarSystemHebrew::dayOfYear(const TQDate & date) const { TQDate first; setYMD(first, year(date), 1, 1); return first.daysTo(date) + 1; }
// Ok int KCalendarSystemHebrew::monthsInYear( const TQDate & date ) const { if ( is_leap_year( year(date) ) ) return 13; else return 12; }
/* Still UNTESTED: create Unix like long representing the date/time */ uint32_t RTCCValue::getTimestamp() { int i; uint32_t tseconds; uint32_t yr = year() + 2000 - 1970; // seconds from 1970 till 1 jan 00:00:00 of the given year tseconds= yr*(SECS_PER_DAY * 365); for (i = 0; i < yr; i++) { if (LEAP_YEAR(i)) { tseconds += SECS_PER_DAY; // add extra days for leap years } } // add days for this year, months start from 1 for (i = 1; i < month(); i++) { if ( (i == 2) && LEAP_YEAR(yr)) { tseconds += SECS_PER_DAY * 29; } else { tseconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0 } } tseconds+= (day()-1) * SECS_PER_DAY; tseconds+= hours() * SECS_PER_HOUR; tseconds+= minutes() * SECS_PER_MIN; tseconds+= seconds(); return tseconds; }
int KCalendarSystem::daysInMonth( const QDate &date ) const { // Days In Month = jd of first day of next month minus jd of first day of this month // Use setAnyDate() to allow correct calculation in last valid year if ( isValid( date ) ) { QDate firstDayOfThisMonth, firstDayOfNextMonth; int thisYear = year( date ); int thisMonth = month( date ); setDate( firstDayOfThisMonth, thisYear, thisMonth, 1 ); //check if next month falls in next year if ( thisMonth < monthsInYear( date ) ) { setDate( firstDayOfNextMonth, thisYear, thisMonth + 1, 1 ); } else { d->setAnyDate( firstDayOfNextMonth, thisYear + 1, 1, 1 ); } return ( firstDayOfNextMonth.toJulianDay() - firstDayOfThisMonth.toJulianDay() ); } return -1; }
void prepared_statement_t::_bind_date_parameter(size_t index, const ::sqlpp::chrono::day_point* value, bool is_null) { if(_handle->debug) { std::cerr << "ODBC debug: binding date parameter" " at index: " << index << ", being " << (is_null ? std::string() : "not") << " null" << std::endl; } SQLLEN indPtr(is_null ? SQL_NULL_DATA : 0); SQL_DATE_STRUCT ymd_value = {0}; if(!is_null) { const auto ymd = ::date::year_month_day{*value}; ymd_value.year = static_cast<int>(ymd.year()); ymd_value.month = static_cast<unsigned>(ymd.month()); ymd_value.day = static_cast<unsigned>(ymd.day()); } auto rc = SQLBindParameter(_handle->stmt, index, SQL_PARAM_INPUT, SQL_C_TYPE_DATE, SQL_TYPE_DATE, 10, 0, (SQLPOINTER)&ymd_value, sizeof(SQL_DATE_STRUCT), &indPtr); if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { throw sqlpp::exception("ODBC error: couldn't bind date parameter: "+detail::odbc_error(_handle->stmt, SQL_HANDLE_STMT)); } }
void loop() { char str[256]; snprintf(str, 256, "%04u-%02u-%02uT%02u:%02u:%02u", year(), month(), day(), hour(), minute(), second()); Serial.println(str); delay(5000); }
int main() { int a; year(a); system("pause"); return 0; }
void prepared_statement_t::_bind_date_time_parameter(size_t index, const ::sqlpp::chrono::microsecond_point* value, bool is_null) { if(_handle->debug) { std::cerr << "ODBC debug: binding date_time parameter" " at index: " << index << ", being " << (is_null ? std::string() : "not") << " null" << std::endl; } SQLLEN indPtr(is_null ? SQL_NULL_DATA : 0); SQL_TIMESTAMP_STRUCT ts_value = {0}; if(!is_null) { const auto dp = ::date::floor<::date::days>(*value); const auto time = date::make_time(*value - dp); const auto ymd = ::date::year_month_day{dp}; ts_value.year = static_cast<int>(ymd.year()); ts_value.month = static_cast<unsigned>(ymd.month()); ts_value.day = static_cast<unsigned>(ymd.day()); ts_value.hour = time.hours().count(); ts_value.minute = time.minutes().count(); ts_value.second = time.seconds().count(); ts_value.fraction = time.subseconds().count(); } auto rc = SQLBindParameter(_handle->stmt, index, SQL_PARAM_INPUT, SQL_C_TYPE_TIMESTAMP, SQL_TYPE_TIMESTAMP, sizeof(SQL_TIMESTAMP_STRUCT), 0, (SQLPOINTER)&ts_value, sizeof(SQL_DATE_STRUCT), &indPtr); if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { throw sqlpp::exception("ODBC error: couldn't bind date parameter: "+detail::odbc_error(_handle->stmt, SQL_HANDLE_STMT)); } }
int KCalendarSystem::daysInYear( const QDate &date ) const { // Days in year = jd of first day of next year minus jd of first day of this year // Use setAnyDate() to allow correct calculation in last valid year if ( isValid( date ) ) { QDate firstDayOfThisYear, firstDayOfNextYear; setDate( firstDayOfThisYear, year( date ), 1, 1 ); d->setAnyDate( firstDayOfNextYear, year( date ) + 1, 1, 1 ); return ( firstDayOfNextYear.toJulianDay() - firstDayOfThisYear.toJulianDay() ); } return -1; }