Example #1
0
  /* The result is the datetime value built of datevalue and timevalue.
   */
  public static SchemaDateTime datetimeFromDateAndTime(
      SchemaDate datevalue, SchemaTime timevalue /* optional */) {
    if (timevalue.hasTimezone() == SchemaCalendarBase.TZ_OFFSET)
      return new SchemaDateTime(
          datevalue.getYear(),
          datevalue.getMonth(),
          datevalue.getDay(),
          timevalue.getHour(),
          timevalue.getMinute(),
          timevalue.getSecond(),
          timevalue.getPartSecond(),
          timevalue.getTimezoneOffset());

    SchemaDateTime result =
        new SchemaDateTime(
            datevalue.getYear(),
            datevalue.getMonth(),
            datevalue.getDay(),
            timevalue.getHour(),
            timevalue.getMinute(),
            timevalue.getSecond(),
            timevalue.getPartSecond());
    result.setTimezone(timevalue.hasTimezone(), 0);

    return result;
  }
Example #2
0
  /* Returns the day of week of the given datetime value. Sunday=1, Monday=2,...
   */
  public static SchemaTypeNumber weekday(SchemaDateTime value) {
    long a = (14 - value.getMonth()) / 12;
    long m = value.getMonth() + 12 * a - 3;
    long y = value.getYear() + 4800 - a;

    long JD = value.getDay() + (153 * m + 2) / 5 + y * 365 + y / 4 - y / 100 + y / 400 - 32045;

    return new SchemaInt((int) (JD % 7 + 1));
  }
Example #3
0
  /* Returns the number of the week within the year of the given datetime value.
   */
  public static SchemaTypeNumber weeknumber(SchemaDateTime value) {
    long a = (14 - value.getMonth()) / 12;
    long m = value.getMonth() + 12 * a - 3;
    long y = value.getYear() + 4800 - a;

    long JD = value.getDay() + (153 * m + 2) / 5 + y * 365 + y / 4 - y / 100 + y / 400 - 32045;

    long d4 = (JD + 31741 - (JD % 7)) % 146097 % 36524 % 1461;
    long L = d4 / 1460;
    long d1 = ((d4 - L) % 365) + L;

    return new SchemaInt((int) (d1 / 7 + 1));
  }
Example #4
0
  /* 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;
  }
Example #5
0
 /* The result is the datetime value consisting of the parts given by year, month, day, hour, minute and second.
  */
 public static SchemaDateTime datetimeFromParts(
     SchemaTypeNumber year,
     SchemaTypeNumber month,
     SchemaTypeNumber day,
     SchemaTypeNumber hour /* optional */,
     SchemaTypeNumber minute /* optional */,
     SchemaTypeNumber second /* optional */,
     SchemaTypeNumber millisecond /* optional */,
     SchemaTypeNumber timezone /* optional */) {
   SchemaDateTime result =
       new SchemaDateTime(
           year.intValue(),
           month.intValue(),
           day.intValue(),
           hour.intValue(),
           minute.intValue(),
           second.intValue(),
           0);
   result.setMillisecond(millisecond.intValue());
   long tv = result.getTimeValue();
   result.setTimeFromTimeValue(tv);
   result.setDateFromTimeValue(tv);
   if (timezone.intValue() >= -1440 && timezone.intValue() <= 1440)
     result.setTimezone(SchemaCalendarBase.TZ_OFFSET, timezone.intValue());
   return result;
 }
Example #6
0
 /* Returns the year of the given datetime value.
  */
 public static SchemaTypeNumber yearFromDatetime(SchemaDateTime value) {
   return new SchemaInt(value.getYear());
 }
Example #7
0
 /* Returns the timezone of the given datetime value.
  */
 public static SchemaTypeNumber timezone(SchemaDateTime value) {
   return new SchemaInt(value.getTimezoneOffset());
 }
Example #8
0
 /* Returns the seconds of the given datetime value.
  */
 public static SchemaTypeNumber secondFromDatetime(SchemaDateTime value) {
   return new SchemaInt(value.getSecond());
 }
Example #9
0
 /* Returns the month of the given datetime value.
  */
 public static SchemaTypeNumber monthFromDatetime(SchemaDateTime value) {
   return new SchemaInt(value.getMonth());
 }
Example #10
0
 /* Returns the minutes of the given datetime value.
  */
 public static SchemaTypeNumber minuteFromDatetime(SchemaDateTime value) {
   return new SchemaInt(value.getMinute());
 }
Example #11
0
 /* Returns true if the given year of the date is a leapyear.
  */
 public static SchemaBoolean leapyear(SchemaDateTime value) {
   return new SchemaBoolean(new GregorianCalendar().isLeapYear(value.getYear()));
 }
Example #12
0
 /* Returns the actual date and time.
  */
 public static SchemaDateTime now() {
   return SchemaDateTime.now();
 }
Example #13
0
 /* Returns the day of the given datetime value.
  */
 public static SchemaTypeNumber dayFromDatetime(SchemaDateTime value) {
   return new SchemaInt(value.getDay());
 }
Example #14
0
 /* Returns the time-part of the given datetime value.
  */
 public static SchemaTime timeFromDatetime(SchemaDateTime value) {
   return value.timeValue();
 }
Example #15
0
  /* 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;
  }