/* Performs a addition of dates, times and durations. */ public static SchemaDateTime datetimeAdd(SchemaDateTime datetime, SchemaDuration duration) { int months = duration.getYearMonthValue(); SchemaDateTime result = new SchemaDateTime(datetime); result.setMonth(datetime.getMonth() + months); int yearsOverflow; if (result.getMonth() >= 1) yearsOverflow = (result.getMonth() - 1) / 12; else yearsOverflow = (result.getMonth() - 12) / 12; result.setMonth(result.getMonth() - yearsOverflow * 12); if (yearsOverflow > 0 && result.getYear() < 0 && yearsOverflow >= -result.getYear()) result.setYear(result.getYear() + yearsOverflow + 1); else if (yearsOverflow < 0 && result.getYear() > 0 && -yearsOverflow >= result.getYear()) result.setYear(result.getYear() + yearsOverflow - 1); else result.setYear(result.getYear() + yearsOverflow); boolean leapYear = (result.getYear() % 4 == 0) && ((result.getYear() % 100 != 0) || (result.getYear() % 400 == 0)); int daysInMonth = result.getMonth() == 2 ? leapYear ? 29 : 28 : (30 + ((result.getMonth() & 1) ^ ((result.getMonth() >> 3) & 1))); if (result.getDay() > daysInMonth) result.setDay(daysInMonth); long tv = result.getTimeValue() + duration.getDayTimeValue() + result.getTimezoneOffset() * 60000; result.setTimeFromTimeValue(tv); result.setDateFromTimeValue(tv); return result; }
/* Result is the datetime value of subtracting datetime2 from datetime1. */ public static SchemaDuration datetimeDiff(SchemaDateTime datetime1, SchemaDateTime datetime2) { long resultticks = datetime1.getTimeValue() - datetime2.getTimeValue(); SchemaDuration dur = new SchemaDuration("PT0S"); if (resultticks < 0) { dur.setNegative(true); resultticks = -resultticks; } dur.setTimeFromTimeValue(resultticks); dur.setDay((int) (resultticks / (86400 * 1000))); return dur; }
/* Returns the addition of two durations. */ public static SchemaDuration durationAdd(SchemaDuration duration1, SchemaDuration duration2) { SchemaDuration dur = new SchemaDuration(); dur.setNegative(false); // sets isempty to false int ym = duration1.getYearMonthValue() + duration2.getYearMonthValue(); long dt = duration1.getDayTimeValue() + duration2.getDayTimeValue(); if ((ym < 0 && dt > 0) || (ym > 0 && dt < 0)) throw new NotANumberException( "Invalid duration result, yearmonth part differs in sign from daytime part."); if (ym != 0) dur.setYearMonthValue(ym); if (dt != 0) dur.setDayTimeValue(dt); return dur; }
/* The result is the datetime value consisting of the parts given by year, month, day, hour, minute and second. */ public static SchemaDuration durationFromParts( SchemaTypeNumber year, SchemaTypeNumber month, SchemaTypeNumber day, SchemaTypeNumber hour /* optional */, SchemaTypeNumber minute /* optional */, SchemaTypeNumber second /* optional */, SchemaTypeNumber partsecond /* optional */, SchemaBoolean negative /* optional */) { SchemaDuration result = new SchemaDuration( year.intValue(), month.intValue(), day.intValue(), hour.intValue(), minute.intValue(), second.intValue(), 0, negative.booleanValue()); result.setMillisecond(partsecond.intValue()); return result; }
/* Returns the year of the given datetime value. */ public static SchemaTypeNumber yearFromDuration(SchemaDuration value) { return new SchemaInt(value.isNegative() ? -value.getYear() : value.getYear()); }
/* Returns the seconds of the given datetime value. */ public static SchemaTypeNumber secondFromDuration(SchemaDuration value) { return new SchemaInt(value.isNegative() ? -value.getSecond() : value.getSecond()); }
/* Returns the month of the given datetime value. */ public static SchemaTypeNumber monthFromDuration(SchemaDuration value) { return new SchemaInt(value.isNegative() ? -value.getMonth() : value.getMonth()); }