Esempio n. 1
0
  public void testToDate2() {
    String[] dateStrings = {"Jan 11, 2002", "Jan 11, 2002", "Feb 12, 1981 6:17 AM"};
    String[] timeZoneIDs = {"GMT+00:00", "GMT-02:00", "GMT+03:00"};
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
    Date[] resultDates = new Date[3];
    calendar.clear();
    calendar.set(2002, 0, 11, 0, 0, 0);
    resultDates[0] = calendar.getTime();
    calendar.clear();
    calendar.set(2002, 0, 11, 2, 0, 0);
    resultDates[1] = calendar.getTime();
    calendar.clear();
    calendar.set(1981, 1, 12, 3, 17, 0);
    resultDates[2] = calendar.getTime();

    for (int i = 0; i < dateStrings.length; i++) {
      try {
        Date dateResult =
            DataTypeUtil.toDate(dateStrings[i], ULocale.US, TimeZone.getTimeZone(timeZoneIDs[i]));
        assertEquals(dateResult, resultDates[i]);
      } catch (BirtException e) {
        fail("Should not throw Exception.");
      }
    }
  }
Esempio n. 2
0
  /**
   * Executes the scalar function toDate() on the given values. The list is expected to be of length
   * 1, and the value in it should be of an appropiate type. The method does not validate the
   * parameters, the user must check the parameters before calling this method.
   *
   * @param values A list with the values that the scalar function is performed on them.
   * @return A date value with the appropriate date.
   */
  public Value evaluate(List<Value> values) {
    Value value = values.get(0);
    Date date;
    GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

    // If the given value is null, return a null date value.
    if (value.isNull()) {
      return DateValue.getNullValue();
    }
    DateValue dateValue;
    switch (value.getType()) {
      case DATE:
        dateValue = (DateValue) value;
        break;
      case DATETIME:
        dateValue =
            new DateValue((GregorianCalendar) (((DateTimeValue) value).getObjectToFormat()));
        break;
      case NUMBER:
        date = new Date((long) ((NumberValue) value).getValue());
        gc.setTime(date);
        dateValue = new DateValue(gc);
        break;
      default: // Should never get here.
        throw new RuntimeException("Value type was not found: " + value.getType());
    }
    return dateValue;
  }
Esempio n. 3
0
  public void testToDate1() {
    String[] testStrings = {
      "1997",
      "1997-07",
      "1997-07-16",
      "1997-07-16T19:20+02",
      "1997-07-16T19:20:30GMT+01:00",
      "1997-07-16T19:20:30.045+01:00",
      "1997-07-16 19:20+01:00",
      "1997-07-16 19:20:30+01:00",
      "1997-07-16 19:20:30.045+01:00",
      "1997-07-16 19:20:30.045 GMT+01:00",
      "1997-07-16T19:20:30.045-01:00"
    };
    Calendar calendar = Calendar.getInstance();

    Date[] resultDates = new Date[11];

    calendar.clear();
    calendar.set(1997, 0, 1);
    resultDates[0] = calendar.getTime();
    calendar.set(1997, 6, 1);
    resultDates[1] = calendar.getTime();
    calendar.set(1997, 6, 16);
    resultDates[2] = calendar.getTime();

    calendar.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
    calendar.set(1997, 6, 16, 17, 20, 0);
    resultDates[3] = calendar.getTime();
    calendar.set(1997, 6, 16, 18, 20, 30);
    resultDates[4] = calendar.getTime();
    calendar.set(1997, 6, 16, 18, 20, 30);
    calendar.set(Calendar.MILLISECOND, 45);
    resultDates[5] = calendar.getTime();
    calendar.set(1997, 6, 16, 18, 20, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    resultDates[6] = calendar.getTime();
    calendar.set(1997, 6, 16, 18, 20, 30);
    resultDates[7] = calendar.getTime();
    calendar.set(1997, 6, 16, 18, 20, 30);
    calendar.set(Calendar.MILLISECOND, 45);
    resultDates[8] = calendar.getTime();
    resultDates[9] = calendar.getTime();
    // "1997-07-16T19:20:30.45-01:00"
    calendar.set(1997, 6, 16, 20, 20, 30);
    calendar.set(Calendar.MILLISECOND, 45);
    resultDates[10] = calendar.getTime();

    for (int i = 0; i < testStrings.length; i++) {
      try {
        Date dateResult = DataTypeUtil.toDate(testStrings[i]);
        assertEquals(dateResult, resultDates[i]);
      } catch (BirtException e) {
        fail("Should not throw Exception.");
      }
    }
  }
 @LayoutlibDelegate
 /*package*/ static long createDateIntervalFormat(
     String skeleton, String localeName, String tzName) {
   TimeZone prevDefaultTz = TimeZone.getDefault();
   TimeZone.setDefault(TimeZone.getTimeZone(tzName));
   DateIntervalFormat_Delegate newDelegate = new DateIntervalFormat_Delegate();
   newDelegate.mFormat = DateIntervalFormat.getInstance(skeleton, new ULocale(localeName));
   TimeZone.setDefault(prevDefaultTz);
   return sManager.addNewDelegate(newDelegate);
 }
Esempio n. 5
0
  private String generateDataTableJson(TimeChartData chartData) throws DataSourceException {
    DataTable data = new DataTable();
    ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
    cd.add(new ColumnDescription("date", ValueType.DATETIME, "Date"));

    for (String serie : chartData.getSeries()) {
      cd.add(new ColumnDescription(serie, ValueType.NUMBER, serie));
    }
    data.addColumns(cd);
    try {

      for (TimeChartDataValue timeSerie : chartData.getValues()) {

        TableRow row = new TableRow();
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.setTimeInMillis(
            TimeZone.getDefault().getOffset(timeSerie.getTimestamp().getTime())
                + timeSerie.getTimestamp().getTime());
        row.addCell(new TableCell(new DateTimeValue(calendar)));
        for (Double value : timeSerie.getValues()) {
          if (value != null) {
            row.addCell(value);
          } else {
            row.addCell(Value.getNullValueFromValueType(ValueType.NUMBER));
          }
        }
        data.addRow(row);
      }

    } catch (TypeMismatchException e) {
      System.out.println("Invalid type!");
    }
    Query query = new Query();
    DataSourceParameters parameters = new DataSourceParameters("");
    DataSourceRequest request = new DataSourceRequest(query, parameters, ULocale.UK);
    String generateResponse = DataSourceHelper.generateResponse(data, request);

    return generateResponse;
  }
  public static void replaceFieldTypesExample() {
    // Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv'
    System.out.println("========================================================================");
    System.out.println(" replaceFieldTypeExample()");
    System.out.println();
    System.out.println(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv");
    System.out.println("========================================================================");
    // ---replaceFieldTypesExample
    Date date = new GregorianCalendar(1999, 9, 13, 23, 58, 59).getTime();
    TimeZone zone = TimeZone.getTimeZone("Europe/Paris");
    ULocale locale = ULocale.FRANCE;
    DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
    SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz", locale);
    format.setTimeZone(zone);
    String pattern = format.toPattern();
    System.out.println("Pattern before replacement:");
    System.out.println(pattern);
    System.out.println("Date/Time format in fr_FR:");
    System.out.println(format.format(date));
    // Replace zone "zzzz" in the pattern with "vvvv"
    String newPattern = gen.replaceFieldTypes(pattern, "vvvv");
    // Apply the new pattern
    format.applyPattern(newPattern);
    System.out.println("Pattern after replacement:");
    System.out.println(newPattern);
    System.out.println("Date/Time format in fr_FR:");
    System.out.println(format.format(date));

    /**
     * output of the sample code:
     * **************************************************************************************************
     * Pattern before replacement: EEEE d MMMM y HH:mm:ss zzzz Date/Time format in fr_FR: jeudi 14
     * octobre 1999 05:58:59 heure avancée d’Europe centrale Pattern after replacement: EEEE d MMMM
     * y HH:mm:ss vvvv Date/Time format in fr_FR: jeudi 14 octobre 1999 05:58:59 heure de l’Europe
     * centrale
     *
     * <p>************************************************************************************************
     */
    // ---replaceFieldTypesExample
  }
Esempio n. 7
0
  @Before
  public void columnBindingSetUp() throws Exception {

    TimeZone.setDefault(TimeZone.getTimeZone("GMT+0"));
  }
@SourceMonitored
public class TimeDurationTests extends GenericTestCaseBase {
  private static final Calendar zero = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

  static {
    zero.clear();
    zero.setTimeInMillis(0);
  }

  public TimeDurationTests(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    super.setUp();
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }

  private static <T extends Comparable<T>> int doCompare(T comparable, T other) {
    return comparable.compareTo(other);
  }

  private static void assertDurationFields(
      String label,
      int years,
      int months,
      int days,
      int hours,
      int minutes,
      int seconds,
      int milliseconds,
      String string,
      TimeDuration duration,
      boolean isNegative,
      boolean isZero) {
    assertEquals(label + ".years()", years, duration.years());
    assertEquals(label + ".months()", months, duration.months());
    assertEquals(label + ".days()", days, duration.days());
    assertEquals(label + ".hours()", hours, duration.hours());
    assertEquals(label + ".minutes()", minutes, duration.minutes());
    assertEquals(label + ".seconds()", seconds, duration.seconds());
    assertEquals(label + ".milliseconds()", milliseconds, duration.milliseconds());
    assertEquals(label + ".isNegative()", isNegative, duration.isNegative());
    assertEquals(label + ".toString()", string, duration.toString());
    assertEquals(
        label + ".equals(from/to long)",
        duration,
        TimeDuration.fromLong(TimeDuration.toLong(duration)));
    assertEquals(
        label + ".equals(from/to number)",
        duration,
        TimeDuration.fromNumber(TimeDuration.toLong(duration)));
    assertEquals(label + ".isZero", isZero, duration.isZero());
    if (isZero) {
      assertEquals(
          label + ".compareTo(zero) == 0", 0, doCompare(TimeDuration.ZeroTimeDuration, duration));
      assertEquals(
          label + ".compareTo(zero) == 0", 0, doCompare(duration, TimeDuration.ZeroTimeDuration));
    } else {
      assertNotSame(
          label + ".compareTo(zero) != 0", 0, doCompare(TimeDuration.ZeroTimeDuration, duration));
      assertNotSame(
          label + ".compareTo(zero) != 0", 0, doCompare(duration, TimeDuration.ZeroTimeDuration));
    }
  }

  private static TimeDuration assertDurationLoop(
      String label,
      Calendar right,
      int years,
      int months,
      int days,
      int hours,
      int minutes,
      int seconds,
      int milliseconds,
      TimeDuration lastString,
      boolean isNegative) {
    StringBuilder sb = new StringBuilder();
    sb.append(years != 0 ? years : "");
    sb.append(':').append(months != 0 ? months : "");
    sb.append(':').append(days != 0 ? days : "");
    sb.append(':').append(hours != 0 ? hours : "");
    sb.append(':').append(minutes != 0 ? minutes : "");
    sb.append(':').append(seconds != 0 ? seconds : "");
    sb.append(':').append(milliseconds != 0 ? milliseconds : "");
    String durationString =
        years
            + ":"
            + months
            + ":"
            + days
            + ":"
            + hours
            + ":"
            + minutes
            + ":"
            + seconds
            + ":"
            + milliseconds;
    TimeDuration stringDuration = TimeDuration.parseDuration(sb.toString());
    right.setTimeInMillis(0);
    if (years != 0) {
      right.set(Calendar.YEAR, 1970 + Math.abs(years));
    }
    if (months != 0) {
      right.set(Calendar.MONTH, Math.abs(months));
    }
    right.set(Calendar.DAY_OF_MONTH, Math.abs(days) + 1);
    if (hours != 0) {
      right.set(Calendar.HOUR, Math.abs(hours));
    }
    if (minutes != 0) {
      right.set(Calendar.MINUTE, Math.abs(minutes));
    }
    if (seconds != 0) {
      right.set(Calendar.SECOND, Math.abs(seconds));
    }
    if (milliseconds != 0) {
      right.set(Calendar.MILLISECOND, Math.abs(milliseconds));
    }
    TimeDuration calDuration =
        isNegative ? new TimeDuration(right, zero) : new TimeDuration(zero, right);
    assertDurationFields(
        label + "(parseString[0])",
        years,
        months,
        days,
        hours,
        minutes,
        seconds,
        milliseconds,
        durationString,
        TimeDuration.parseDuration(durationString),
        isNegative,
        false);
    assertDurationFields(
        label + "(parseString)",
        years,
        months,
        days,
        hours,
        minutes,
        seconds,
        milliseconds,
        durationString,
        stringDuration,
        isNegative,
        false);
    assertDurationFields(
        label + "(cal)",
        years,
        months,
        days,
        hours,
        minutes,
        seconds,
        milliseconds,
        durationString,
        calDuration,
        isNegative,
        false);
    Calendar added = calDuration.addToCalendar((Calendar) zero.clone());
    TimeDuration addDuration = new TimeDuration(zero, added);
    assertDurationFields(
        label + "(cal[add])",
        years,
        months,
        days,
        hours,
        minutes,
        seconds,
        milliseconds,
        durationString,
        addDuration,
        isNegative,
        false);
    assertEquals(label + ".compareTo(string, cal)", 0, doCompare(stringDuration, calDuration));
    assertEquals(
        label + ".compareTo(string, string)", 0, doCompare(stringDuration, stringDuration));
    assertEquals(label + ".compareTo(cal, cal)", 0, doCompare(calDuration, calDuration));
    assertEquals(label + ".compareTo(cal, string)", 0, doCompare(calDuration, stringDuration));
    assertEquals(label + ".equals(cal, cal)", calDuration, calDuration);
    assertEquals(label + ".equals(cal, string)", calDuration, stringDuration);
    assertEquals(label + ".equals(string, cal)", stringDuration, calDuration);
    assertEquals(label + ".equals(string, string)", stringDuration, stringDuration);
    assertFalse(
        label + ".not-equals(string, this)", stringDuration.equals(TimeDurationTests.class));
    if (lastString != null) {
      assertFalse(label + ".not-equals(string, lastString)", stringDuration.equals(lastString));
    }
    return stringDuration;
  }

  public static void assertDuration(
      String label,
      int years,
      int months,
      int days,
      int hours,
      int minutes,
      int seconds,
      int milliseconds) {
    TimeDuration lastString = null;
    Calendar right = (Calendar) zero.clone();
    for (int i = 1; i < 12; i++) {
      lastString =
          assertDurationLoop(
              i + " " + label,
              right,
              i * years,
              i * months,
              i * days,
              i * hours,
              i * minutes,
              i * seconds,
              i * milliseconds,
              lastString,
              false);
    }
    lastString = null;
    for (int i = -2; i > -12; i--) {
      lastString =
          assertDurationLoop(
              i + " " + label,
              right,
              i * years,
              i * months,
              i * days,
              i * hours,
              i * minutes,
              i * seconds,
              i * milliseconds,
              lastString,
              true);
    }
  }

  public void testDuration() throws Exception {
    Calendar now = Calendar.getInstance();
    TimeDuration zeroDuration = TimeDuration.ZeroTimeDuration;
    assertFalse("zero equals null", zeroDuration.equals(null));
    Calendar newTime = (Calendar) now.clone();
    zeroDuration.addToCalendar(newTime);
    assertEquals("zero same calendar", now, newTime);
    assertDurationFields(
        "zero(same zero calendar)",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        "0:0:0:0:0:0:0",
        new TimeDuration(zero, zero),
        false,
        true);
    assertDurationFields(
        "zero(same now calendar)",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        "0:0:0:0:0:0:0",
        new TimeDuration(now, now),
        false,
        true);
    assertDurationFields(
        "zero(empty parse)",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        "0:0:0:0:0:0:0",
        TimeDuration.parseDuration(""),
        false,
        true);
    assertDurationFields(
        "zero(zero parse)",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        "0:0:0:0:0:0:0",
        TimeDuration.parseDuration("0:0:0:0:0:0:0"),
        false,
        true);
    assertDurationFields(
        "zero(from null number)",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        "0:0:0:0:0:0:0",
        TimeDuration.fromNumber(null),
        false,
        true);
    assertDurationFields(
        "zero(from null number)",
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        "0:0:0:0:0:0:0",
        TimeDuration.fromNumber(null),
        false,
        true);
    assertDuration("millisecond", 0, 0, 0, 0, 0, 0, 1);
    assertDuration("second", 0, 0, 0, 0, 0, 1, 0);
    assertDuration("minute", 0, 0, 0, 0, 1, 0, 0);
    assertDuration("hour", 0, 0, 0, 1, 0, 0, 0);
    assertDuration("day", 0, 0, 1, 0, 0, 0, 0);
    assertDuration("month", 0, 1, 0, 0, 0, 0, 0);
    assertDuration("year", 1, 0, 0, 0, 0, 0, 0);
    Calendar start = new com.ibm.icu.util.GregorianCalendar(1967, 1, 1, 0, 0, 0);
    start.set(Calendar.MILLISECOND, 0);
    Calendar end = (Calendar) start.clone();
    end.add(Calendar.MILLISECOND, 1);
    end.add(Calendar.SECOND, 1);
    end.add(Calendar.MINUTE, 1);
    end.add(Calendar.HOUR_OF_DAY, 1);
    end.add(Calendar.DAY_OF_MONTH, 1);
    end.add(Calendar.MONTH, 1);
    end.add(Calendar.YEAR, 1);
    assertDurationFields(
        "pre-epoch elapsed time",
        1,
        1,
        1,
        1,
        1,
        1,
        1,
        "1:1:1:1:1:1:1",
        new TimeDuration(start, end),
        false,
        false);
  }
}