long Date::getHours(long y,long m,long d, long hh) const { long g = getDays(y,m,d); int hfd = hh/24; hh = hh%24; return (g+hfd)*24 + hh; }
Period *Period::normalizedStandard(const PeriodType *type) { type = DateTimeUtils::getPeriodType(type); int64_t millis = getMillis(); // no overflow can happen, even with Integer.MAX_VALUEs millis += (((int64_t) getSeconds()) * ((int64_t) DateTimeConstants::MILLIS_PER_SECOND)); millis += (((int64_t) getMinutes()) * ((int64_t) DateTimeConstants::MILLIS_PER_MINUTE)); millis += (((int64_t) getHours()) * ((int64_t) DateTimeConstants::MILLIS_PER_HOUR)); millis += (((int64_t) getDays()) * ((int64_t) DateTimeConstants::MILLIS_PER_DAY)); millis += (((int64_t) getWeeks()) * ((int64_t) DateTimeConstants::MILLIS_PER_WEEK)); Period *result = new Period(millis, type, ISOChronology::getInstanceUTC()); int years = getYears(); int months = getMonths(); if (years != 0 || months != 0) { int64_t totalMonths = years * 12L + months; if (type->isSupported(DurationFieldType::YEARS_TYPE)) { int normalizedYears = FieldUtils::safeToInt(totalMonths / 12); result = result->withYears(normalizedYears); totalMonths = totalMonths - (normalizedYears * 12); } if (type->isSupported(DurationFieldType::MONTHS_TYPE)) { int normalizedMonths = FieldUtils::safeToInt(totalMonths); result = result->withMonths(normalizedMonths); totalMonths = totalMonths - normalizedMonths; } if (totalMonths != 0) { string err("Unable to normalize as PeriodType is missing either years or months but period has a month/year amount: "); err.append(toString()); throw UnsupportedOperationException(err); } } return result; }
int Date::test() { unsigned long long gg = getDays(2008,12,31); getDateFromDays(gg); gg = getHours(2008,12,31,12); getDateFromHours(gg); gg = getMinutes(2008,12,31,12,56); getDateFromMinutes(gg); gg = getSeconds(2008,12,31,12,56,56); getDateFromSeconds(gg); Date d; //logger << d.toString() << endl; Date d1 = addYearsGet(1); //logger << d1.toString() << endl; Date d2 = addMonthsGet(23); //logger << d2.toString() << endl; Date d3 = addDaysGet(17); //logger << d3.toString() << endl; Date d4 = addHoursGet(25); //logger << d4.toString() << endl; Date d5 = addMinutesGet(61); //logger << d5.toString() << endl; Date d6 = addSecondsGet(61); //logger << d6.toString() << endl; return 0; }
int main(){ // save the frequency that the 13th of each month lands on // Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday int frequency[7] = { 0, 0, 0, 0, 0, 0, 0 }; int days = 13; int years = 0; // N int i = 0; int j = 0; FILE *fin = fopen( "friday.in", "r" ); FILE *fout = fopen( "friday.out", "w" ); assert( fin != NULL && fout != NULL ); // get N fscanf( fin, "%d", &years ); // start: 1900,01,01, Monday for( i = 0; i < years; i++ ){ for( j = 1; j <= 12; j++){ frequency[ days % 7 ]++; days += getDays( i, j ); } } // result fprintf( fout, "%d", frequency[6] ); for( i = 0; i < 6; i++ ){ fprintf( fout, " %d", frequency[i] ); } fprintf( fout, "\n"); exit(0); }
Date Date::addYearsGet(const long& years) { long g = getDays(year+years,month,day); Date d = getDateFromDays(g); d.epochTime = getSecondsI(d.year, d.month, d.day, d.hours, d.minutes, d.seconds); return d; }
/*------------------------------------------------------------------------*/ static char *ASCIItime(const struct tm *timeptr) { char **wday_name = getDays(); char **mon_name = getMonths(); char *result = NULL; if ((wday_name) && (mon_name)) { #define FORMAT_TIME "%s %s%3d %.2d:%.2d:%.2d %d" int len_result = (int)strlen(wday_name[timeptr->tm_wday]) + (int)strlen(mon_name[timeptr->tm_mon]) + (int)strlen(FORMAT_TIME); result = (char *)MALLOC(sizeof(char) * (len_result + 1)); if (result) { sprintf(result, FORMAT_TIME, wday_name[timeptr->tm_wday], mon_name[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, 1900 + timeptr->tm_year); } } else { strcpy(result, ""); } /* free pointers */ freeArrayOfString(wday_name, MAX_wday); freeArrayOfString(mon_name, MAX_mon); return result; }
std::string CCopasiTimeVariable::isoFormat() const { std::stringstream Iso; bool first = true; if (mTime < LLONG_CONST(0)) { CCopasiTimeVariable Tmp(-mTime); Iso << "-"; Iso << Tmp.isoFormat(); return Iso.str(); } if (mTime >= LLONG_CONST(86400000000)) { Iso << LL2String(getDays()) << ":"; first = false; } if (mTime >= LLONG_CONST(3600000000)) Iso << LL2String(getHours(true), first ? 0 : 2) << ":"; if (mTime >= LLONG_CONST(60000000)) Iso << LL2String(getMinutes(true), first ? 0 : 2) << ":"; if (mTime >= LLONG_CONST(1000000)) Iso << LL2String(getSeconds(true), first ? 0 : 2) << "."; else Iso << "0."; Iso << LL2String(getMilliSeconds(true), 3) << LL2String(getMicroSeconds(true), 3); return Iso.str(); }
Date Date::addMonthsGet(long months) { months = getMonth() + months; long year = getYear() + months/12; months = months%12; long g = getDays(year,months,getDay()); return getDateFromDays(g); }
Date Date::addDaysGet(long days) { long months = getMonth(); long year = getYear() + months/12; months = months%12; long g = getDays(year,months,getDay()+days); return getDateFromDays(g); }
Date::Date(int yyyy,int mm,int dd) { if(!validateDate(dd,mm,yyyy))throw "Invalid date"; long g = getDays(yyyy,mm,dd); *this = getDateFromDays(g); this->nanoseconds = 0; this->timeZoneOffset = 0; }
void Date::updateYears(long years) { long g = getDays(getYear()+years,getMonth(),getDay()); Date d = getDateFromDays(g); setDay(d.getDay()); setMonth(d.getMm()); setYear(d.getYear()); setDayw(d.getDayw()); }
Date::Date(const int& yyyy, const int& mm, const int& dd) { if(!validateDate(dd,mm,yyyy))throw std::runtime_error("Invalid date"); long g = getDays(yyyy,mm,dd); *this = getDateFromDays(g); populateDay(); populateMonth(); populateEpochAndTimeZone(false); }
void Date::updateYears(const long& years) { long g = getDays(year+years,month,day); Date d = getDateFromDays(g); this->day = d.day; this->month = d.month; this->year = d.year; populateDay(); epochTime = getSecondsI(year, month, day, hours, minutes, seconds); }
Date Date::addMonthsGet(long months) { months = month + months; long year = this->year + months/12; months = months%12; long g = getDays(year,months,day); Date d = getDateFromDays(g); d.epochTime = getSecondsI(d.year, d.month, d.day, d.hours, d.minutes, d.seconds); return d; }
Date Date::addDaysGet(const long& days) { long months = month; long year = this->year + months/12; months = months%12; long g = getDays(year,months,day+days); Date d = getDateFromDays(g); d.epochTime += days*24*3600; return d; }
Date::Date(int yyyy,string mmm,int dd) { string mm = getMon(mmm); if(mm=="-1")throw "Invalid month"; if(!validateDate(dd,CastUtil::lexical_cast<int>(mm),yyyy))throw "Invalid date"; long g = getDays(yyyy,CastUtil::lexical_cast<long>(mm),dd); *this = getDateFromDays(g); this->nanoseconds = 0; this->timeZoneOffset = 0; }
Duration *Period::toStandardDuration() { checkYearsAndMonths("Duration"); int64_t millis = getMillis(); // no overflow can happen, even with Integer.MAX_VALUEs millis += (((int64_t) getSeconds()) * ((int64_t) DateTimeConstants::MILLIS_PER_SECOND)); millis += (((int64_t) getMinutes()) * ((int64_t) DateTimeConstants::MILLIS_PER_MINUTE)); millis += (((int64_t) getHours()) * ((int64_t) DateTimeConstants::MILLIS_PER_HOUR)); millis += (((int64_t) getDays()) * ((int64_t) DateTimeConstants::MILLIS_PER_DAY)); millis += (((int64_t) getWeeks()) * ((int64_t) DateTimeConstants::MILLIS_PER_WEEK)); return new Duration(millis); }
Weeks *Period::toStandardWeeks() { checkYearsAndMonths("Weeks"); int64_t millis = getMillis(); // assign to a int64_t millis += ((int64_t) getSeconds()) * DateTimeConstants::MILLIS_PER_SECOND; millis += ((int64_t) getMinutes()) * DateTimeConstants::MILLIS_PER_MINUTE; millis += ((int64_t) getHours()) * DateTimeConstants::MILLIS_PER_HOUR; millis += ((int64_t) getDays()) * DateTimeConstants::MILLIS_PER_DAY; int64_t weeks = ((int64_t) getWeeks()) + millis / DateTimeConstants::MILLIS_PER_WEEK; return Weeks::weeks(FieldUtils::safeToInt(weeks)); }
Seconds *Period::toStandardSeconds() { checkYearsAndMonths("Seconds"); int64_t seconds = getMillis() / DateTimeConstants::MILLIS_PER_SECOND; seconds = FieldUtils::safeAdd(seconds, (int64_t) getSeconds()); seconds = FieldUtils::safeAdd(seconds, ((int64_t) getMinutes()) * ((int64_t) DateTimeConstants::SECONDS_PER_MINUTE)); seconds = FieldUtils::safeAdd(seconds, ((int64_t) getHours()) * ((int64_t) DateTimeConstants::SECONDS_PER_HOUR)); seconds = FieldUtils::safeAdd(seconds, ((int64_t) getDays()) * ((int64_t) DateTimeConstants::SECONDS_PER_DAY)); seconds = FieldUtils::safeAdd(seconds, ((int64_t) getWeeks()) * ((int64_t) DateTimeConstants::SECONDS_PER_WEEK)); return Seconds::seconds(FieldUtils::safeToInt(seconds)); }
Date::Date(int yy,int mm,int dd,bool te) { Date d; string syyyy = d.year.substr(0,2) + CastUtil::lexical_cast<string>(yy); int yyyy = CastUtil::lexical_cast<int>(syyyy); if(!validateDate(dd,mm,yyyy))throw "Invalid date"; long g = getDays(yyyy,mm,dd); *this = getDateFromDays(g); this->nanoseconds = 0; this->timeZoneOffset = 0; }
Date::Date(const int& yyyy, const std::string& mmm, const int& dd) { std::string mm = getMon(mmm); if(mm=="-1")throw std::runtime_error("Invalid month"); if(!validateDate(dd,CastUtil::lexical_cast<int>(mm),yyyy))throw std::runtime_error("Invalid date"); long g = getDays(yyyy,CastUtil::lexical_cast<long>(mm),dd); *this = getDateFromDays(g); populateDay(); populateMonth(); populateEpochAndTimeZone(false); }
Minutes *Period::toStandardMinutes() { checkYearsAndMonths("Minutes"); int64_t millis = getMillis(); // assign to a int64_t millis += ((int64_t) getSeconds()) * DateTimeConstants::MILLIS_PER_SECOND; int64_t minutes = millis / DateTimeConstants::MILLIS_PER_MINUTE; minutes = FieldUtils::safeAdd(minutes, (int64_t) getMinutes()); minutes = FieldUtils::safeAdd(minutes, ((int64_t) getHours()) * ((int64_t) DateTimeConstants::MINUTES_PER_HOUR)); minutes = FieldUtils::safeAdd(minutes, ((int64_t) getDays()) * ((int64_t) DateTimeConstants::MINUTES_PER_DAY)); minutes = FieldUtils::safeAdd(minutes, ((int64_t) getWeeks()) * ((int64_t) DateTimeConstants::MINUTES_PER_WEEK)); return Minutes::minutes(FieldUtils::safeToInt(minutes)); }
Days *Period::toStandardDays() { checkYearsAndMonths("Days"); int64_t millis = getMillis(); // assign to a int64_t millis += ((int64_t) getSeconds()) * DateTimeConstants::MILLIS_PER_SECOND; millis += ((int64_t) getMinutes()) * DateTimeConstants::MILLIS_PER_MINUTE; millis += ((int64_t) getHours()) * DateTimeConstants::MILLIS_PER_HOUR; int64_t days = millis / DateTimeConstants::MILLIS_PER_DAY; days = FieldUtils::safeAdd(days, (int64_t) getDays()); days = FieldUtils::safeAdd(days, ((int64_t) getWeeks()) * ((int64_t) DateTimeConstants::DAYS_PER_WEEK)); return Days::days(FieldUtils::safeToInt(days)); }
Date::Date(const int& yy, const int& mm, const int& dd, const bool& te) { Date d; std::string syyyy = CastUtil::lexical_cast<std::string>(d.year).substr(0,2) + CastUtil::lexical_cast<std::string>(yy); int yyyy = CastUtil::lexical_cast<int>(syyyy); if(!validateDate(dd,mm,yyyy))throw std::runtime_error("Invalid date"); long g = getDays(yyyy,mm,dd); *this = getDateFromDays(g); populateDay(); populateMonth(); populateEpochAndTimeZone(false); }
void Date::updateMonths(long months) { months = getMonth() + months; long year = getYear() + months/12; months = months%12; long g = getDays(year,months,getDay()); Date d = getDateFromDays(g); setDay(d.getDay()); setMonth(d.getMm()); setYear(d.getYear()); setDayw(d.getDayw()); }
void Date::updateDays(const long& days) { long months = month; long year = this->year + months/12; months = months%12; long g = getDays(year,months,day+days); Date d = getDateFromDays(g); this->day = d.day; this->month = d.month; this->year = d.year; populateDay(); epochTime += days*24*60*60; }
void Date::updateMonths(long months) { months = month + months; long year = this->year + months/12; months = months%12; long g = getDays(year,months,day); Date d = getDateFromDays(g); this->day = d.day; this->month = d.month; this->year = d.year; populateDay(); epochTime = getSecondsI(year, month, day, hours, minutes, seconds); }
Date::Date(int yy,string mmm,int dd,bool te) { Date d; string syyyy = d.year.substr(0,2) + CastUtil::lexical_cast<string>(yy); int yyyy = CastUtil::lexical_cast<int>(syyyy); string mm = getMon(mmm); if(mm=="-1")throw "Invalid month"; if(!validateDate(dd,CastUtil::lexical_cast<int>(mm),yyyy))throw "Invalid date"; long g = getDays(yyyy,CastUtil::lexical_cast<long>(mm),dd); *this = getDateFromDays(g); this->nanoseconds = 0; this->timeZoneOffset = 0; setDayName(); }
Date Date::addYearsGet(long years) { long g = getDays(getYear()+years,getMonth(),getDay()); return getDateFromDays(g); }
String DateInterval::format(CStrRef format_spec) { StringBuffer s; for(int i = 0; i < format_spec.length(); i++) { const int MAXLEN = 22; // 64bit signed int string length, plus terminating \0 char buf[MAXLEN]; int l; char c = format_spec.charAt(i); if (c != '%') { s.append(c); continue; } i++; if (i == format_spec.length()) { // End of format, use literal % and finish s.append(c); break; } c = format_spec.charAt(i); switch(c) { case 'Y': l = snprintf(buf, MAXLEN, "%02lld", getYears()); break; case 'y': l = snprintf(buf, MAXLEN, "%lld", getYears()); break; case 'M': l = snprintf(buf, MAXLEN, "%02lld", getMonths()); break; case 'm': l = snprintf(buf, MAXLEN, "%lld", getMonths()); break; case 'D': l = snprintf(buf, MAXLEN, "%02lld", getDays()); break; case 'd': l = snprintf(buf, MAXLEN, "%lld", getDays()); break; case 'H': l = snprintf(buf, MAXLEN, "%02lld", getHours()); break; case 'h': l = snprintf(buf, MAXLEN, "%lld", getHours()); break; case 'I': l = snprintf(buf, MAXLEN, "%02lld", getMinutes()); break; case 'i': l = snprintf(buf, MAXLEN, "%lld", getMinutes()); break; case 'S': l = snprintf(buf, MAXLEN, "%02lld", getSeconds()); break; case 's': l = snprintf(buf, MAXLEN, "%lld", getSeconds()); break; case 'a': if (haveTotalDays()) { l = snprintf(buf, MAXLEN, "%lld", getTotalDays()); } else { l = snprintf(buf, MAXLEN, "(unknown)"); } break; case 'R': l = snprintf(buf, MAXLEN, "%c", isInverted() ? '-' : '+'); break; case 'r': l = snprintf(buf, MAXLEN, "%s", isInverted() ? "-" : ""); break; case '%': default: l = 0; s.append('%'); break; } if (l > 0) { s.append(buf, l); } } return s.detach(); }