Exemple #1
0
  /**
   * Converts a Date in default time zone to client's time zone.
   *
   * @param inputDate a date in the default time zone
   * @return a date in the client time zone
   */
  private Date convertDateToClientTimeZone(Date inputDate) {
    Calendar c = Calendar.getInstance(locale);
    c.setTime(inputDate);

    TimeZone clientZone = timeZoneTracker.getClientTimeZone();
    TimeZone defaultZone = TimeZone.getDefault();
    long now = System.currentTimeMillis();
    int offset = defaultZone.getOffset(now) - clientZone.getOffset(now);

    c.add(Calendar.MILLISECOND, offset);

    return c.getTime();
  }
Exemple #2
0
 public static String date2UTC(Date dateTime, TimeZone timeZone) throws UtilitiesException {
   try {
     DecimalFormat twoDigits;
     String utc;
     String sign;
     int hours;
     int minutes;
     SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
     dateFormatter.setLenient(false);
     dateFormatter.setTimeZone(timeZone);
     twoDigits = new DecimalFormat("00");
     utc = dateFormatter.format(dateTime);
     int tzOffset = timeZone.getOffset(dateTime.getTime());
     sign = "+";
     if (tzOffset < 0) {
       sign = "-";
       tzOffset = -tzOffset;
     }
     hours = tzOffset / 0x36ee80;
     minutes = (tzOffset % 0x36ee80) / 60000;
     return (new StringBuffer(utc.length() + 7))
         .append(utc)
         .append(sign)
         .append(twoDigits.format(hours))
         .append(":")
         .append(twoDigits.format(minutes))
         .toString();
   } catch (Exception ex) {
     throw new hk.hku.cecid.piazza.commons.util.UtilitiesException(ex);
   }
 }
    DataContextImpl(OptiqConnectionImpl connection, List<Object> parameterValues) {
      this.queryProvider = connection;
      this.typeFactory = connection.getTypeFactory();
      this.rootSchema = connection.rootSchema;

      // Store the time at which the query started executing. The SQL
      // standard says that functions such as CURRENT_TIMESTAMP return the
      // same value throughout the query.
      final long time = System.currentTimeMillis();
      final TimeZone timeZone = connection.getTimeZone();
      final long localOffset = timeZone.getOffset(time);
      final long currentOffset = localOffset;

      ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
      builder
          .put("utcTimestamp", time)
          .put("currentTimestamp", time + currentOffset)
          .put("localTimestamp", time + localOffset)
          .put("timeZone", timeZone);
      for (Ord<Object> value : Ord.zip(parameterValues)) {
        Object e = value.e;
        if (e == null) {
          e = AvaticaParameter.DUMMY_VALUE;
        }
        builder.put("?" + value.i, e);
      }
      map = builder.build();
    }
 public long getPreviousFireTimeBefore(long beforeTime) {
   long returnTime = Long.MIN_VALUE;
   if (beforeTime > getStart().getTime()) {
     if (beforeTime > getEnd().getTime()) beforeTime = getEnd().getTime();
     long lstart = getStart().getTime();
     CompanyCalendar cal = new CompanyCalendar();
     cal.setTimeInMillis(lstart);
     TimeZone timeZone = cal.getTimeZone();
     int ofset =
         (timeZone.getOffset(lstart)
             - timeZone.getOffset(
                 beforeTime)); // questo server per calcolare i millisecondi effettivi in caso di
                               // ora legale
     long distInMillisec = beforeTime - lstart - ofset;
     long distInDays = distInMillisec / CompanyCalendar.MILLIS_IN_DAY;
     long distInYear =
         distInDays / 365; // non sbaglia il conto fino a che non sono passati 1460 anni
     if (distInYear > 0) {
       cal.add(Calendar.YEAR, (int) distInYear);
       distInDays -= (distInYear * 365);
     }
     int distInMonth =
         (int) distInDays
             / 29; // in un anno dividendo per 29 i giorni ottengo sempre il numero corretto di
                   // mesi
     int freq = (this.getFreq() > 0 ? this.getFreq() : 1);
     int rep = distInMonth / freq;
     if (this.getRepeat() == 0 || rep <= (this.getRepeat() - 1)) {
       cal.add(Calendar.MONTH, rep * freq);
       long before;
       if (weekOfMonth > 0 && dayOfWeek > 0) {
         before = getDayInWeekInMonth(cal);
         if (before < beforeTime) returnTime = before;
       } else {
         before = getDayInMonthBefore(cal, beforeTime);
         if (before < beforeTime) returnTime = before;
       }
     }
   }
   return returnTime;
 }
Exemple #5
0
 private long normalizeTimeOfDayPart(long t, TimeZone tz) {
   long millis = t;
   long low = -tz.getOffset(millis);
   long high = low + DAY;
   if (millis < low) {
     do {
       millis += DAY;
     } while (millis < low);
   } else if (millis >= high) {
     do {
       millis -= DAY;
     } while (millis > high);
   }
   return millis;
 }
 public long getNextFireTimeAfter(long afterTime) {
   long returnTime = Long.MAX_VALUE;
   if (afterTime <= getEnd().getTime()) {
     if (afterTime > getStart().getTime()) {
       long lstart = getStart().getTime();
       CompanyCalendar cal = new CompanyCalendar();
       cal.setTimeInMillis(lstart);
       TimeZone timeZone = cal.getTimeZone();
       int ofset =
           (timeZone.getOffset(lstart)
               - timeZone.getOffset(
                   afterTime)); // questo server per calcolare i millisecondi effettivi in caso di
                                // ora legale
       long distInMillisec = afterTime - lstart - ofset;
       long distInDays = distInMillisec / CompanyCalendar.MILLIS_IN_DAY;
       long distInYear =
           distInDays / 365; // non sbaglia il conto fino a che non sono passati 1460 anni
       if (distInYear > 0) {
         cal.add(Calendar.YEAR, (int) distInYear);
         distInDays -= (distInYear * 365);
       }
       double distInMonth =
           (int) distInDays
               / 29; // in un anno dividendo per 29 i giorni ottengo sempre il numero corretto di
                     // mesi
       int rep = 1;
       int freq = (this.getFreq() > 0 ? this.getFreq() : 1);
       if (distInMonth > 0) {
         rep = (int) distInMonth / freq;
         if ((distInMonth % freq) != 0) rep++;
       }
       if (this.getRepeat() > 0 && rep > (this.getRepeat() - 1)) rep = this.getRepeat() - 1;
       cal.add(Calendar.MONTH, rep * freq);
       long next;
       if (weekOfMonth > 0 && dayOfWeek > 0) {
         next = getDayInWeekInMonth(cal);
         cal.setTimeInMillis(next);
         int watchDog = 1; // not more than 10 occurrences for event
         while (next < afterTime && watchDog < 10) {
           cal.add(Calendar.MONTH, freq);
           next = getDayInWeekInMonth(cal);
           cal.setTimeInMillis(next);
           watchDog++;
         }
         if (watchDog >= 10) {
           Tracer.platformLogger.warn("watchDog barking on ScheduleMonthly.getNextFireTimeAfter");
         }
         returnTime = next;
       } else {
         next = getDayInMonthAfter(cal, afterTime);
         int watchDog = 1; // not more than 10 occurrences for event
         while (next < afterTime && watchDog < 10) {
           cal.add(Calendar.MONTH, freq);
           next = getDayInMonthAfter(cal, afterTime);
           watchDog++;
         }
         if (watchDog >= 10)
           Tracer.platformLogger.warn("watchDog barking on ScheduleMonthly.getNextFireTimeAfter");
         returnTime = next;
       }
     } else {
       returnTime = getStart().getTime();
     }
   }
   return returnTime;
 }
  /**
   * Converts the milliseconds since the epoch UTC (<code>time</code>) to time fields (<code>fields
   * </code>).
   */
  protected synchronized void computeFields() {
    boolean gregorian = (time >= gregorianCutover);

    TimeZone zone = getTimeZone();
    fields[ZONE_OFFSET] = zone.getRawOffset();
    long localTime = time + fields[ZONE_OFFSET];

    long day = localTime / (24 * 60 * 60 * 1000L);
    int millisInDay = (int) (localTime % (24 * 60 * 60 * 1000L));

    if (millisInDay < 0) {
      millisInDay += (24 * 60 * 60 * 1000);
      day--;
    }

    calculateDay(fields, day, gregorian);
    fields[DST_OFFSET] =
        zone.getOffset(
                fields[ERA], fields[YEAR],
                fields[MONTH], fields[DAY_OF_MONTH],
                fields[DAY_OF_WEEK], millisInDay)
            - fields[ZONE_OFFSET];

    millisInDay += fields[DST_OFFSET];
    if (millisInDay >= 24 * 60 * 60 * 1000) {
      millisInDay -= 24 * 60 * 60 * 1000;
      calculateDay(fields, ++day, gregorian);
    }

    fields[DAY_OF_WEEK_IN_MONTH] = (fields[DAY_OF_MONTH] + 6) / 7;

    // which day of the week are we (0..6), relative to getFirstDayOfWeek
    int relativeWeekday = (7 + fields[DAY_OF_WEEK] - getFirstDayOfWeek()) % 7;

    fields[WEEK_OF_MONTH] = (fields[DAY_OF_MONTH] - relativeWeekday + 12) / 7;

    int weekOfYear = (fields[DAY_OF_YEAR] - relativeWeekday + 6) / 7;

    // Do the Correction: getMinimalDaysInFirstWeek() is always in the
    // first week.
    int minDays = getMinimalDaysInFirstWeek();
    int firstWeekday = (7 + getWeekDay(fields[YEAR], minDays) - getFirstDayOfWeek()) % 7;
    if (minDays - firstWeekday < 1) weekOfYear++;
    fields[WEEK_OF_YEAR] = weekOfYear;

    int hourOfDay = millisInDay / (60 * 60 * 1000);
    fields[AM_PM] = (hourOfDay < 12) ? AM : PM;
    int hour = hourOfDay % 12;
    fields[HOUR] = hour;
    fields[HOUR_OF_DAY] = hourOfDay;
    millisInDay %= (60 * 60 * 1000);
    fields[MINUTE] = millisInDay / (60 * 1000);
    millisInDay %= (60 * 1000);
    fields[SECOND] = millisInDay / (1000);
    fields[MILLISECOND] = millisInDay % 1000;

    areFieldsSet =
        isSet[ERA] =
            isSet[YEAR] =
                isSet[MONTH] =
                    isSet[WEEK_OF_YEAR] =
                        isSet[WEEK_OF_MONTH] =
                            isSet[DAY_OF_MONTH] =
                                isSet[DAY_OF_YEAR] =
                                    isSet[DAY_OF_WEEK] =
                                        isSet[DAY_OF_WEEK_IN_MONTH] =
                                            isSet[AM_PM] =
                                                isSet[HOUR] =
                                                    isSet[HOUR_OF_DAY] =
                                                        isSet[MINUTE] =
                                                            isSet[SECOND] =
                                                                isSet[MILLISECOND] =
                                                                    isSet[ZONE_OFFSET] =
                                                                        isSet[DST_OFFSET] = true;
  }
  /**
   * Converts the time field values (<code>fields</code>) to milliseconds since the epoch UTC (
   * <code>time</code>).
   *
   * @throws IllegalArgumentException if any calendar fields are invalid.
   */
  protected synchronized void computeTime() {
    int millisInDay = 0;
    int era = fields[ERA];
    int year = fields[YEAR];
    int month = fields[MONTH];
    int day = fields[DAY_OF_MONTH];

    int minute = fields[MINUTE];
    int second = fields[SECOND];
    int millis = fields[MILLISECOND];
    int[] month_days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int[] dayCount = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    int hour = 0;

    if (!isLenient()) nonLeniencyCheck();

    if (!isSet[MONTH] && (!isSet[DAY_OF_WEEK] || isSet[WEEK_OF_YEAR])) {
      // 5: YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
      if (isSet[WEEK_OF_YEAR]) {
        int first = getFirstDayOfMonth(year, 0);
        int offs = 1;
        int daysInFirstWeek = getFirstDayOfWeek() - first;
        if (daysInFirstWeek <= 0) daysInFirstWeek += 7;

        if (daysInFirstWeek < getMinimalDaysInFirstWeek()) offs += daysInFirstWeek;
        else offs -= 7 - daysInFirstWeek;
        month = 0;
        day = offs + 7 * (fields[WEEK_OF_YEAR] - 1);
        offs = fields[DAY_OF_WEEK] - getFirstDayOfWeek();

        if (offs < 0) offs += 7;
        day += offs;
      } else {
        // 4:  YEAR + DAY_OF_YEAR
        month = 0;
        day = fields[DAY_OF_YEAR];
      }
    } else {
      if (isSet[DAY_OF_WEEK]) {
        int first = getFirstDayOfMonth(year, month);

        // 3: YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
        if (isSet[DAY_OF_WEEK_IN_MONTH]) {
          if (fields[DAY_OF_WEEK_IN_MONTH] < 0) {
            month++;
            first = getFirstDayOfMonth(year, month);
            day = 1 + 7 * (fields[DAY_OF_WEEK_IN_MONTH]);
          } else day = 1 + 7 * (fields[DAY_OF_WEEK_IN_MONTH] - 1);

          int offs = fields[DAY_OF_WEEK] - first;
          if (offs < 0) offs += 7;
          day += offs;
        } else { // 2: YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
          int offs = 1;
          int daysInFirstWeek = getFirstDayOfWeek() - first;
          if (daysInFirstWeek <= 0) daysInFirstWeek += 7;

          if (daysInFirstWeek < getMinimalDaysInFirstWeek()) offs += daysInFirstWeek;
          else offs -= 7 - daysInFirstWeek;

          day = offs + 7 * (fields[WEEK_OF_MONTH] - 1);
          offs = fields[DAY_OF_WEEK] - getFirstDayOfWeek();
          if (offs <= 0) offs += 7;
          day += offs;
        }
      }

      // 1:  YEAR + MONTH + DAY_OF_MONTH
    }
    if (era == BC && year > 0) year = 1 - year;

    // rest of code assumes day/month/year set
    // should negative BC years be AD?
    // get the hour (but no check for validity)
    if (isSet[HOUR]) {
      hour = fields[HOUR];
      if (fields[AM_PM] == PM) hour += 12;
    } else hour = fields[HOUR_OF_DAY];

    // Read the era,year,month,day fields and convert as appropriate.
    // Calculate number of milliseconds into the day
    // This takes care of both h, m, s, ms over/underflows.
    long allMillis = (((hour * 60L) + minute) * 60L + second) * 1000L + millis;
    day += allMillis / (24 * 60 * 60 * 1000L);
    millisInDay = (int) (allMillis % (24 * 60 * 60 * 1000L));

    if (month < 0) {
      year += (int) month / 12;
      month = month % 12;
      if (month < 0) {
        month += 12;
        year--;
      }
    }
    if (month > 11) {
      year += (month / 12);
      month = month % 12;
    }

    month_days[1] = isLeapYear(year) ? 29 : 28;

    while (day <= 0) {
      if (month == 0) {
        year--;
        month_days[1] = isLeapYear(year) ? 29 : 28;
      }
      month = (month + 11) % 12;
      day += month_days[month];
    }
    while (day > month_days[month]) {
      day -= (month_days[month]);
      month = (month + 1) % 12;
      if (month == 0) {
        year++;
        month_days[1] = isLeapYear(year) ? 29 : 28;
      }
    }

    // ok, by here we have valid day,month,year,era and millisinday
    int dayOfYear = dayCount[month] + day - 1; // (day starts on 1)
    if (isLeapYear(year) && month > 1) dayOfYear++;

    int relativeDay =
        (year - 1) * 365
            + ((year - 1) >> 2)
            + dayOfYear
            - EPOCH_DAYS; // gregorian days from 1 to epoch.
    int gregFactor =
        (int) Math.floor((double) (year - 1) / 400.) - (int) Math.floor((double) (year - 1) / 100.);

    if ((relativeDay + gregFactor) * 60L * 60L * 24L * 1000L >= gregorianCutover)
      relativeDay += gregFactor;
    else relativeDay -= 2;

    time = relativeDay * (24 * 60 * 60 * 1000L) + millisInDay;

    // the epoch was a Thursday.
    int weekday = (int) (relativeDay + THURSDAY) % 7;
    if (weekday <= 0) weekday += 7;
    fields[DAY_OF_WEEK] = weekday;

    // Time zone corrections.
    TimeZone zone = getTimeZone();
    int rawOffset = isSet[ZONE_OFFSET] ? fields[ZONE_OFFSET] : zone.getRawOffset();

    int dstOffset =
        isSet[DST_OFFSET]
            ? fields[DST_OFFSET]
            : (zone.getOffset(
                    (year < 0) ? BC : AD,
                    (year < 0) ? 1 - year : year,
                    month,
                    day,
                    weekday,
                    millisInDay)
                - zone.getRawOffset());

    time -= rawOffset + dstOffset;

    isTimeSet = true;
  }
Exemple #9
0
 /**
  * Gets the value of this datetime as a milliseconds value for {@link java.sql.Time}.
  *
  * @param zone time zone in which to generate a time value for
  */
 public long getJdbcTime(TimeZone zone) {
   long timeValue = getTimeValue();
   return timeValue - zone.getOffset(timeValue);
 }
Exemple #10
0
 // implement BasicDatetime
 public void setZonedTime(long value, TimeZone zone) {
   this.internalTime = value + zone.getOffset(value);
 }