Beispiel #1
0
  public static String toHumanTimePeriod(Context ctx, DateTime start, DateTime end) {
    String result;
    Resources res = ctx.getResources();
    Period p = new Period(start, end);

    if (p.getYears() == 0
        && p.getMonths() == 0
        && p.getWeeks() == 0
        && p.getDays() == 0
        && p.getHours() == 0
        && p.getMinutes() == 0) {
      result = res.getQuantityString(R.plurals.seconds_ago, p.getSeconds(), p.getSeconds());
    } else if (p.getYears() == 0
        && p.getMonths() == 0
        && p.getWeeks() == 0
        && p.getDays() == 0
        && p.getHours() == 0) {
      result = res.getQuantityString(R.plurals.minutes_ago, p.getMinutes(), p.getMinutes());
    } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0) {
      result = res.getQuantityString(R.plurals.hours_ago, p.getHours(), p.getHours());
    } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0) {
      result = res.getQuantityString(R.plurals.days_ago, p.getDays(), p.getDays());
    } else {
      result =
          start
              .toLocalDateTime()
              .toString(DateTimeFormat.patternForStyle("M-", res.getConfiguration().locale));
    }
    return result;
  }
 private String epocToSimpleDate(long epoc) {
   Period diff = new Period(epoc, System.currentTimeMillis(), PeriodType.standard());
   PeriodType type;
   if (diff.getMonths() > 0) {
     type = PeriodType.yearMonthDay();
   } else if (diff.getWeeks() > 0) {
     type = PeriodType.yearWeekDay();
   } else if (diff.getDays() > 0) {
     type = PeriodType.dayTime().withSecondsRemoved().withMillisRemoved().withMinutesRemoved();
   } else if (diff.getMinutes() > 0) {
     type = PeriodType.time().withMillisRemoved().withSecondsRemoved();
   } else {
     type = PeriodType.time().withMillisRemoved();
   }
   return PeriodFormat.getDefault().print(new Period(epoc, System.currentTimeMillis(), type));
 }
  /**
   * Adds adjustment to the shortest set time range in period. E.g. period("5 days 3 hours", 1) ->
   * "5 days 4 hours". This will fall back to adjusting years if no field in the period is set.
   *
   * @param period The period to be adjusted
   * @param adjustment The adjustment. Note that positive values will result in larger periods and
   *     an earlier time
   * @return The adjusted period
   */
  private Period adjust(final Period period, int adjustment) {
    if (adjustment == 0) return period;

    // Order is VERY important here
    LinkedHashMap<Integer, DurationFieldType> map = new LinkedHashMap<>();
    map.put(period.getSeconds(), DurationFieldType.seconds());
    map.put(period.getMinutes(), DurationFieldType.minutes());
    map.put(period.getHours(), DurationFieldType.hours());
    map.put(period.getDays(), DurationFieldType.days());
    map.put(period.getWeeks(), DurationFieldType.weeks());
    map.put(period.getMonths(), DurationFieldType.months());
    map.put(period.getYears(), DurationFieldType.years());

    for (Map.Entry<Integer, DurationFieldType> entry : map.entrySet()) {
      if (entry.getKey() > 0) {
        return period.withFieldAdded(entry.getValue(), adjustment);
      }
    }
    // Fall back to modifying years
    return period.withFieldAdded(DurationFieldType.years(), adjustment);
  }
Beispiel #4
0
  @Override
  protected void reduce(Text key, Iterable<Text> values, Context context)
      throws IOException, InterruptedException {
    Text temp = new Text();
    DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    WeekSpec ws = new WeekSpec();
    int numberWeek = 0;

    Date[] firstDayPeriod = new Date[48];
    Date[] lastDayPeriod = new Date[48];

    Date firstPeriod, lastPeriod;

    // iniciar daqui
    for (Text val : values) {
      // String[] tokens = StringUtils.split(text.replaceAll("\"", ""), SEPERATOR);
      // Deslocando alguns arquivos
      String[] tokens = val.toString().split(",");

      // Alterar para joda
      DateTime st_time = dtf.parseDateTime(tokens[0]);
      DateTime en_time = dtf.parseDateTime(tokens[1]);

      // DateTime dateTime = new DateTime(date);
      // Data de referencia da semana

      // PROCESSO DE TRANSBORDO
      // Transhipment process
      ws.setReferenceDate(st_time.toDate());
      // comparação se a data de término esta dentro da data da partição(Ultimo dia da semana)
      // cria um novos registros

      // quantas semanas devo duplicar
      // A classe periodo do Joda esta co problema, temos que verificar se será utilizada
      Period period = new Period(st_time, en_time);
      numberWeek = period.getWeeks();
      // System.out.println(numberWeek);

      if (en_time.toDate().after(ws.getLastDate())) {

        ws.setReferenceDate(st_time.toDate());
        firstPeriod = st_time.toDate();
        lastPeriod = ws.getLastDate();

        for (int i = 0; i <= numberWeek; ++i) {

          if (numberWeek != i) {

            if (firstDayPeriod[i] == null) {
              firstDayPeriod[i] = firstPeriod;
            }
            if (firstDayPeriod[i].after(firstPeriod)) {
              firstDayPeriod[i] = firstPeriod;
            }

            if (lastDayPeriod[i] == null) {
              lastDayPeriod[i] = lastPeriod;
            }
            if (lastDayPeriod[i].before(lastPeriod)) {
              lastDayPeriod[i] = lastPeriod;
            }

          } else {

            // tratamento o ultimo loop
            if (firstDayPeriod[i] == null) {
              firstDayPeriod[i] = firstPeriod;
            }
            if (firstDayPeriod[i].after(firstPeriod)) {
              firstDayPeriod[i] = firstPeriod;
            }

            if (lastDayPeriod[i] == null) {
              lastDayPeriod[i] = en_time.toDate();
            }
            if (lastDayPeriod[i].before(en_time.toDate())) {
              lastDayPeriod[i] = en_time.toDate();
            }
          }

          // Popula variaveis para o próximo loop
          ws.setReferenceDate(ws.getFirstDayofNextWeek());
          firstPeriod = ws.getFirstDate();
          lastPeriod = ws.getLastDate();
        }
      } else {

        // Logica do numero maior
        // se a data do looping for menor que a do array
        // atualiza o array
        if (firstDayPeriod[0] == null) {
          firstDayPeriod[0] = st_time.toDate();
        }
        if (firstDayPeriod[0].after(st_time.toDate())) {
          firstDayPeriod[0] = st_time.toDate();
        }

        // se a data do looping for maior que a do array
        // atualiza o array
        if (lastDayPeriod[0] == null) {
          lastDayPeriod[0] = en_time.toDate();
        }
        if (lastDayPeriod[0].before(en_time.toDate())) {
          lastDayPeriod[0] = en_time.toDate();
        }
      }
      // fim do processo de transbordo

    }

    String[] rawTokens = key.toString().split(",");

    for (int i = 0; i <= firstDayPeriod.length; i++) {
      if (firstDayPeriod[i] == null) {
        break;
      }
      temp.set(
          sdf1.format(firstDayPeriod[i])
              + ";"
              + sdf1.format(lastDayPeriod[i])
              + ";"
              + rawTokens[0]
              + ";"
              + rawTokens[1]
              + ";"
              + rawTokens[2]);
      context.write(NullWritable.get(), temp);
    }
  }
    @Override
    public long truncate(long t) {
      if (isCompound) {
        try {
          return truncateMillisPeriod(t);
        } catch (UnsupportedOperationException e) {
          return truncateCompoundPeriod(t);
        }
      }

      final int years = period.getYears();
      if (years > 0) {
        if (years > 1 || hasOrigin) {
          int y = chronology.years().getDifference(t, origin);
          y -= y % years;
          long tt = chronology.years().add(origin, y);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.years().add(tt, -years);
          else t = tt;
          return t;
        } else {
          return chronology.year().roundFloor(t);
        }
      }

      final int months = period.getMonths();
      if (months > 0) {
        if (months > 1 || hasOrigin) {
          int m = chronology.months().getDifference(t, origin);
          m -= m % months;
          long tt = chronology.months().add(origin, m);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.months().add(tt, -months);
          else t = tt;
          return t;
        } else {
          return chronology.monthOfYear().roundFloor(t);
        }
      }

      final int weeks = period.getWeeks();
      if (weeks > 0) {
        if (weeks > 1 || hasOrigin) {
          // align on multiples from origin
          int w = chronology.weeks().getDifference(t, origin);
          w -= w % weeks;
          long tt = chronology.weeks().add(origin, w);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.weeks().add(tt, -weeks);
          else t = tt;
          return t;
        } else {
          t = chronology.dayOfWeek().roundFloor(t);
          // default to Monday as beginning of the week
          return chronology.dayOfWeek().set(t, 1);
        }
      }

      final int days = period.getDays();
      if (days > 0) {
        if (days > 1 || hasOrigin) {
          // align on multiples from origin
          int d = chronology.days().getDifference(t, origin);
          d -= d % days;
          long tt = chronology.days().add(origin, d);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.days().add(tt, -days);
          else t = tt;
          return t;
        } else {
          t = chronology.hourOfDay().roundFloor(t);
          return chronology.hourOfDay().set(t, 0);
        }
      }

      final int hours = period.getHours();
      if (hours > 0) {
        if (hours > 1 || hasOrigin) {
          // align on multiples from origin
          long h = chronology.hours().getDifferenceAsLong(t, origin);
          h -= h % hours;
          long tt = chronology.hours().add(origin, h);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.hours().add(tt, -hours);
          else t = tt;
          return t;
        } else {
          t = chronology.minuteOfHour().roundFloor(t);
          return chronology.minuteOfHour().set(t, 0);
        }
      }

      final int minutes = period.getMinutes();
      if (minutes > 0) {
        // align on multiples from origin
        if (minutes > 1 || hasOrigin) {
          long m = chronology.minutes().getDifferenceAsLong(t, origin);
          m -= m % minutes;
          long tt = chronology.minutes().add(origin, m);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.minutes().add(tt, -minutes);
          else t = tt;
          return t;
        } else {
          t = chronology.secondOfMinute().roundFloor(t);
          return chronology.secondOfMinute().set(t, 0);
        }
      }

      final int seconds = period.getSeconds();
      if (seconds > 0) {
        // align on multiples from origin
        if (seconds > 1 || hasOrigin) {
          long s = chronology.seconds().getDifferenceAsLong(t, origin);
          s -= s % seconds;
          long tt = chronology.seconds().add(origin, s);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.seconds().add(tt, -seconds);
          else t = tt;
          return t;
        } else {
          return chronology.millisOfSecond().set(t, 0);
        }
      }

      final int millis = period.getMillis();
      if (millis > 0) {
        if (millis > 1) {
          long ms = chronology.millis().getDifferenceAsLong(t, origin);
          ms -= ms % millis;
          long tt = chronology.millis().add(origin, ms);
          // always round down to the previous period (for timestamps prior to origin)
          if (t < tt) t = chronology.millis().add(tt, -millis);
          else t = tt;
          return t;
        } else {
          return t;
        }
      }

      return t;
    }