Esempio n. 1
1
  public void parseLogToThingWorx(File file) {
    DaiCollectionParser parser = new DaiCollectionParser();
    Map<String, String> fileMeta = new HashMap<String, String>();
    List<DaiMeterCollection> collections = null;
    DateTime time = new DateTime();
    String timezone = time.getZone().toString();
    fileMeta.put("olson_timezone_id", timezone);
    fileMeta.put("current_system_time", String.valueOf(System.currentTimeMillis()));
    String fileCreated = "";
    BasicFileAttributes attributes = null;

    try {
      attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
      fileCreated = String.valueOf(attributes.creationTime().toMillis());
    } catch (IOException e) {
      logger.warn("Unable to get File Create Time: ", e.getMessage());
    }
    fileMeta.put("file_create_time", fileCreated);
    try {
      collections = parser.parse(file, fileMeta);
    } catch (Exception e) {
      logger.warn("Failed to parse file: ", e.getMessage());
    }
    sendToThingWorx(collections);
  }
Esempio n. 2
0
 private void addDateTimeInUTC(String name, DateTime time) {
   long utctime = time.getZone().convertLocalToUTC(time.getMillis(), true);
   Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
   cal.setTimeInMillis(utctime);
   DateFormat format = new SimpleDateFormat(UTC_TIMEFORMAT);
   inputDocument.addField(name, format.format(cal.getTime()));
 }
  @Override
  protected Object[] toConvertedColumns(DateTime value) {

    final DateTime myValue;
    if (databaseZone == null) {
      myValue = value;
    } else {
      myValue = value.withZone(databaseZone);
    }
    return new Object[] {
      myValue.toLocalDateTime(),
      new DateTimeZoneWithOffset(
          value.getZone(),
          value.getZone().isFixed()
              ? null
              : DateTimeZone.forOffsetMillis(value.getZone().getOffset(value)))
    };
  }
Esempio n. 4
0
  /**
   * Builds and returns text for the "exact" time an alarm occurs as opposed to the period left for
   * it to occur.<br>
   * In English, 12:00 today would become "Today 12:00", tomorrow would be come "Tomorrow 12:00",
   * and on Monday it would become
   *
   * @param formats the formats to use, e.g: [Today %1$s, Tomorrow %1$s, %2$s %1$s].
   * @param noActive if no alarm was active, this is used.
   * @param now current time in Unix epoch timestamp.
   * @param ats an AlarmTimestamp: the information about the alarm & its timestamp.
   * @param locale the locale to use for weekdays.
   * @return the built time-to string.
   */
  public static final String getTime(
      String[] formats, String noActive, long now, AlarmTimestamp ats, Locale locale) {

    if (ats == AlarmTimestamp.INVALID) {
      return noActive;
    } else {
      if (ats.getMillis() < now) {
        throw new RuntimeException("Time given is before now.");
      }

      // Prepare replacements.
      Alarm alarm = ats.getAlarm();
      String timeReplacement = StringUtils.joinTime(alarm.getHour(), alarm.getMinute());

      // Calculate start of tomorrow.
      DateTime nowTime = new DateTime(now);
      DateTime time = new DateTime(ats.getMillis());
      LocalDate tomorrow = new LocalDate(nowTime).plusDays(1);
      DateTime startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(nowTime.getZone());

      if (time.isBefore(startOfTomorrow)) {
        // Alarm is today.
        Log.d(TAG, "today");
        return String.format(formats[0], timeReplacement);
      }

      // Calculate start of the day after tomorrow.
      LocalDate afterTomorrow = tomorrow.plusDays(1);
      DateTime startOfAfterTomorrow = afterTomorrow.toDateTimeAtStartOfDay(nowTime.getZone());

      if (time.isBefore(startOfAfterTomorrow)) {
        Log.d(TAG, "tomorrow");
        // Alarm is tomorrow.
        return String.format(formats[1], timeReplacement);
      }

      // Alarm is after tomorrow.
      Log.d(TAG, "after tomorrow");
      String weekday = new DateTime(ats.getMillis()).dayOfWeek().getAsText(locale);
      return String.format(formats[2], timeReplacement, weekday);
    }
  }
Esempio n. 5
0
  private IRubyObject inspectCommon(DateTimeFormatter formatter, DateTimeFormatter utcFormatter) {
    DateTimeFormatter simpleDateFormat;
    if (dt.getZone() == DateTimeZone.UTC) {
      simpleDateFormat = utcFormatter;
    } else {
      simpleDateFormat = formatter;
    }

    String result = simpleDateFormat.print(dt);

    return getRuntime().newString(result);
  }
Esempio n. 6
0
  @JRubyMethod(name = "zone")
  public RubyString zone() {
    Ruby runtime = getRuntime();
    String envTZ = getEnvTimeZone(runtime).toString();
    // see declaration of SHORT_TZNAME
    if (SHORT_STD_TZNAME.containsKey(envTZ)
        && !dt.getZone().toTimeZone().inDaylightTime(dt.toDate())) {
      return runtime.newString(SHORT_STD_TZNAME.get(envTZ));
    }

    if (SHORT_DL_TZNAME.containsKey(envTZ)
        && dt.getZone().toTimeZone().inDaylightTime(dt.toDate())) {
      return runtime.newString(SHORT_DL_TZNAME.get(envTZ));
    }

    String zone = dt.getZone().getShortName(dt.getMillis());

    Matcher offsetMatcher = TIME_OFFSET_PATTERN.matcher(zone);

    if (offsetMatcher.matches()) {
      boolean minus_p = offsetMatcher.group(1).toString().equals("-");
      int hourOffset = Integer.valueOf(offsetMatcher.group(2));

      if (zone.equals("+00:00")) {
        zone = "GMT";
      } else {
        // try non-localized time zone name
        zone = dt.getZone().getNameKey(dt.getMillis());
        if (zone == null) {
          char sign = minus_p ? '+' : '-';
          zone = "GMT" + sign + hourOffset;
        }
      }
    }

    return runtime.newString(zone);
  }
 @Override
 public void nullSafeSet(
     PreparedStatement st,
     Object value,
     int index,
     SharedSessionContractImplementor sharedSessionContractImplementor)
     throws HibernateException, SQLException {
   DateTime dt = (DateTime) value;
   if (dt == null) {
     StandardBasicTypes.TIMESTAMP.nullSafeSet(st, null, index, sharedSessionContractImplementor);
     StandardBasicTypes.STRING.nullSafeSet(st, null, index + 1, sharedSessionContractImplementor);
   } else {
     StandardBasicTypes.TIMESTAMP.nullSafeSet(
         st, dt.toDate(), index, sharedSessionContractImplementor);
     StandardBasicTypes.STRING.nullSafeSet(
         st, dt.getZone().getID(), index + 1, sharedSessionContractImplementor);
   }
 }
Esempio n. 8
0
  private IRubyObject opPlusCommon(long adjustment) {
    long micro = adjustment % 1000;
    adjustment = adjustment / 1000;

    long time = getTimeInMillis();
    time += adjustment;

    if ((getUSec() + micro) >= 1000) {
      time++;
      micro = (getUSec() + micro) - 1000;
    } else {
      micro = getUSec() + micro;
    }

    RubyTime newTime = new RubyTime(getRuntime(), getMetaClass());
    newTime.dt = new DateTime(time).withZone(dt.getZone());
    newTime.setUSec(micro);

    return newTime;
  }
Esempio n. 9
0
  private IRubyObject opMinusCommon(IRubyObject other) {
    long time = getTimeInMillis();
    long adjustment = Math.round(RubyNumeric.num2dbl(other) * 1000000);
    long micro = adjustment % 1000;
    adjustment = adjustment / 1000;

    time -= adjustment;

    if (getUSec() < micro) {
      time--;
      micro = 1000 - (micro - getUSec());
    } else {
      micro = getUSec() - micro;
    }

    RubyTime newTime = new RubyTime(getRuntime(), getMetaClass());
    newTime.dt = new DateTime(time).withZone(dt.getZone());
    newTime.setUSec(micro);

    return newTime;
  }
Esempio n. 10
0
 @JRubyMethod(name = {"isdst", "dst?"})
 public RubyBoolean isdst() {
   return getRuntime().newBoolean(!dt.getZone().isStandardOffset(dt.getMillis()));
 }
Esempio n. 11
0
  @JRubyMethod(name = {"gmt_offset", "gmtoff", "utc_offset"})
  public RubyInteger gmt_offset() {
    int offset = dt.getZone().getOffset(dt.getMillis());

    return getRuntime().newFixnum((int) (offset / 1000));
  }
Esempio n. 12
0
 @JRubyMethod(name = {"gmt?", "utc?", "gmtime?"})
 public RubyBoolean gmt() {
   return getRuntime().newBoolean(dt.getZone().getID().equals("UTC"));
 }
Esempio n. 13
0
 public static String formatarComTimezone(Date date) {
   DateTime d = new DateTime(date);
   DateTimeFormatter dtf = ISODateTimeFormat.dateTimeNoMillis().withZone(d.getZone());
   return d.toString(dtf);
 }
Esempio n. 14
0
  /**
   * If date is not match, will return previous closest match. If date is match, will return this
   * date.
   *
   * @param date - reference DateTime instance - never null;
   * @return DateTime instance, never null. Value obeys logic specified above.
   * @throws NoSuchValueException
   */
  DateTime previousClosestMatch(DateTime date) throws NoSuchValueException {
    List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear());
    TimeNode days = generateDays(cronDefinition, date);
    int highestMonth = months.getValues().get(months.getValues().size() - 1);
    int highestDay = days.getValues().get(days.getValues().size() - 1);
    int highestHour = hours.getValues().get(hours.getValues().size() - 1);
    int highestMinute = minutes.getValues().get(minutes.getValues().size() - 1);
    int highestSecond = seconds.getValues().get(seconds.getValues().size() - 1);

    NearestValue nearestValue;
    DateTime newDate;
    if (year.isEmpty()) {
      int previousYear = yearsValueGenerator.generatePreviousValue(date.getYear());
      if (highestDay > 28) {
        int highestDayOfMonth =
            new DateTime(previousYear, highestMonth, 1, 0, 0).dayOfMonth().getMaximumValue();
        if (highestDay > highestDayOfMonth) {
          nearestValue = days.getPreviousValue(highestDay, 1);
          if (nearestValue.getShifts() > 0) {
            newDate =
                new DateTime(previousYear, highestMonth, 1, 23, 59, 59, date.getZone())
                    .minusMonths(nearestValue.getShifts())
                    .dayOfMonth()
                    .withMaximumValue();
            return previousClosestMatch(newDate);
          } else {
            highestDay = nearestValue.getValue();
          }
        }
      }
      return initDateTime(
          previousYear,
          highestMonth,
          highestDay,
          highestHour,
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!months.getValues().contains(date.getMonthOfYear())) {
      nearestValue = months.getPreviousValue(date.getMonthOfYear(), 0);
      int previousMonths = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), 12, 31, 23, 59, 59, date.getZone())
                .minusYears(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          previousMonths,
          highestDay,
          highestHour,
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!days.getValues().contains(date.getDayOfMonth())) {
      nearestValue = days.getPreviousValue(date.getDayOfMonth(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), date.getMonthOfYear(), 1, 23, 59, 59, date.getZone())
                .minusMonths(nearestValue.getShifts())
                .dayOfMonth()
                .withMaximumValue();
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          nearestValue.getValue(),
          highestHour,
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!hours.getValues().contains(date.getHourOfDay())) {
      nearestValue = hours.getPreviousValue(date.getHourOfDay(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    23,
                    59,
                    59,
                    date.getZone())
                .minusDays(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          nearestValue.getValue(),
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!minutes.getValues().contains(date.getMinuteOfHour())) {
      nearestValue = minutes.getPreviousValue(date.getMinuteOfHour(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    59,
                    59,
                    date.getZone())
                .minusHours(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          nearestValue.getValue(),
          highestSecond,
          date.getZone());
    }
    if (!seconds.getValues().contains(date.getSecondOfMinute())) {
      nearestValue = seconds.getPreviousValue(date.getSecondOfMinute(), 0);
      int previousSeconds = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    date.getMinuteOfHour(),
                    59,
                    date.getZone())
                .minusMinutes(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          date.getMinuteOfHour(),
          previousSeconds,
          date.getZone());
    }
    return date;
  }
Esempio n. 15
0
  /**
   * If date is not match, will return next closest match. If date is match, will return this date.
   *
   * @param date - reference DateTime instance - never null;
   * @return DateTime instance, never null. Value obeys logic specified above.
   * @throws NoSuchValueException
   */
  DateTime nextClosestMatch(DateTime date) throws NoSuchValueException {
    List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear());
    TimeNode days = null;
    int lowestMonth = months.getValues().get(0);
    int lowestHour = hours.getValues().get(0);
    int lowestMinute = minutes.getValues().get(0);
    int lowestSecond = seconds.getValues().get(0);

    NearestValue nearestValue;
    DateTime newDate;
    if (year.isEmpty()) {
      int newYear = yearsValueGenerator.generateNextValue(date.getYear());
      days = generateDays(cronDefinition, new DateTime(newYear, lowestMonth, 1, 0, 0));
      return initDateTime(
          yearsValueGenerator.generateNextValue(date.getYear()),
          lowestMonth,
          days.getValues().get(0),
          lowestHour,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    if (!months.getValues().contains(date.getMonthOfYear())) {
      nearestValue = months.getNextValue(date.getMonthOfYear(), 0);
      int nextMonths = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), 1, 1, 0, 0, 0, date.getZone())
                .plusYears(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getMonthOfYear()) {
        date = date.plusYears(1);
      }
      days = generateDays(cronDefinition, new DateTime(date.getYear(), nextMonths, 1, 0, 0));
      return initDateTime(
          date.getYear(),
          nextMonths,
          days.getValues().get(0),
          lowestHour,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    days = generateDays(cronDefinition, date);
    if (!days.getValues().contains(date.getDayOfMonth())) {
      nearestValue = days.getNextValue(date.getDayOfMonth(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), date.getMonthOfYear(), 1, 0, 0, 0, date.getZone())
                .plusMonths(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getDayOfMonth()) {
        date = date.plusMonths(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          nearestValue.getValue(),
          lowestHour,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    if (!hours.getValues().contains(date.getHourOfDay())) {
      nearestValue = hours.getNextValue(date.getHourOfDay(), 0);
      int nextHours = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    0,
                    0,
                    0,
                    date.getZone())
                .plusDays(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getHourOfDay()) {
        date = date.plusDays(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          nextHours,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    if (!minutes.getValues().contains(date.getMinuteOfHour())) {
      nearestValue = minutes.getNextValue(date.getMinuteOfHour(), 0);
      int nextMinutes = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    0,
                    0,
                    date.getZone())
                .plusHours(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getMinuteOfHour()) {
        date = date.plusHours(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          nextMinutes,
          lowestSecond,
          date.getZone());
    }
    if (!seconds.getValues().contains(date.getSecondOfMinute())) {
      nearestValue = seconds.getNextValue(date.getSecondOfMinute(), 0);
      int nextSeconds = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    date.getMinuteOfHour(),
                    0,
                    date.getZone())
                .plusMinutes(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getSecondOfMinute()) {
        date = date.plusMinutes(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          date.getMinuteOfHour(),
          nextSeconds,
          date.getZone());
    }
    return date;
  }