Esempio n. 1
0
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;
}
Esempio n. 2
0
void Period::checkYearsAndMonths(string destintionType) {
    if (getMonths() != 0) {
        string err("Cannot convert to ");
        err.append(destintionType);
        err.append(" as this period contains months and months vary in.size()");
        throw UnsupportedOperationException(err);
    }
    if (getYears() != 0) {
        string err("Cannot convert to ");
        err.append(destintionType);
        err.append(" as this period contains years and years vary in.size()");
        throw UnsupportedOperationException(err);
    }
}
Esempio n. 3
0
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();
}
Esempio n. 4
0
int getCurrentYear(int days){
	int year =  getYears(convertDaysToSeconds(days)) + 1970;
	return year;
}