Java Code Examples for org.joda.time.DateTimeZone#forID()

The following examples show how to use org.joda.time.DateTimeZone#forID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDateTimeZone.java    From joda-time-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsLocalDateTimeGap_Berlin() {
    DateTimeZone zone = DateTimeZone.forID("Europe/Berlin");
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 1, 0)));
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 1, 59, 59, 99)));
    assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 2, 0)));
    assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 2, 30)));
    assertEquals(true, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 2, 59, 59, 99)));
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 3, 0)));
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 3, 25, 4, 0)));
    
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 10, 28, 1, 30)));  // before overlap
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 10, 28, 2, 30)));  // overlap
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 10, 28, 3, 30)));  // after overlap
    assertEquals(false, zone.isLocalDateTimeGap(new LocalDateTime(2007, 12, 24, 12, 34)));
}
 
Example 2
Source File: DateTimeUtilTest.java    From cloudhopper-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void floorToDay() throws Exception {
    // create a reference datetime
    DateTime dt0 = new DateTime(2009,6,24,23,30,30,789,DateTimeZone.forID("America/Los_Angeles"));

    //
    // floor to nearest day
    //
    DateTime dt1 = DateTimeUtil.floorToDay(dt0);

    Assert.assertEquals(2009, dt1.getYear());
    Assert.assertEquals(6, dt1.getMonthOfYear());
    Assert.assertEquals(24, dt1.getDayOfMonth());
    Assert.assertEquals(0, dt1.getHourOfDay());
    Assert.assertEquals(0, dt1.getMinuteOfHour());
    Assert.assertEquals(0, dt1.getSecondOfMinute());
    Assert.assertEquals(0, dt1.getMillisOfSecond());
    Assert.assertEquals(DateTimeZone.forID("America/Los_Angeles"), dt1.getZone());

    //
    // floor null
    //
    DateTime dt2 = DateTimeUtil.floorToDay(null);
    Assert.assertNull(dt2);
}
 
Example 3
Source File: DateTimeUtilTest.java    From cloudhopper-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void floorToMinute() throws Exception {
    // create a reference datetime
    DateTime dt0 = new DateTime(2009,6,24,23,30,30,789,DateTimeZone.forID("America/Los_Angeles"));

    //
    // floor to nearest minute
    //
    DateTime dt1 = DateTimeUtil.floorToMinute(dt0);

    Assert.assertEquals(2009, dt1.getYear());
    Assert.assertEquals(6, dt1.getMonthOfYear());
    Assert.assertEquals(24, dt1.getDayOfMonth());
    Assert.assertEquals(23, dt1.getHourOfDay());
    Assert.assertEquals(30, dt1.getMinuteOfHour());
    Assert.assertEquals(0, dt1.getSecondOfMinute());
    Assert.assertEquals(0, dt1.getMillisOfSecond());
    Assert.assertEquals(DateTimeZone.forID("America/Los_Angeles"), dt1.getZone());

    //
    // floor null
    //
    DateTime dt2 = DateTimeUtil.floorToMinute(null);
    Assert.assertNull(dt2);
}
 
Example 4
Source File: DateTimeUtilTest.java    From cloudhopper-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void floorToMonth() throws Exception {
    // create a reference datetime
    DateTime dt0 = new DateTime(2009,6,24,23,30,30,789,DateTimeZone.forID("America/Los_Angeles"));

    //
    // floor to nearest month
    //
    DateTime dt1 = DateTimeUtil.floorToMonth(dt0);

    Assert.assertEquals(2009, dt1.getYear());
    Assert.assertEquals(6, dt1.getMonthOfYear());
    Assert.assertEquals(1, dt1.getDayOfMonth());
    Assert.assertEquals(0, dt1.getHourOfDay());
    Assert.assertEquals(0, dt1.getMinuteOfHour());
    Assert.assertEquals(0, dt1.getSecondOfMinute());
    Assert.assertEquals(0, dt1.getMillisOfSecond());
    Assert.assertEquals(DateTimeZone.forID("America/Los_Angeles"), dt1.getZone());

    //
    // floor null
    //
    DateTime dt2 = DateTimeUtil.floorToMonth(null);
    Assert.assertNull(dt2);
}
 
Example 5
Source File: WorkspaceManagerImpl.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void updateFileTime(FileInfo fileInfo, Path p, LinkOption linkOption) throws IOException {
    BasicFileAttributes attr;

    if (linkOption == null) {
        attr = Files.readAttributes(p, BasicFileAttributes.class);
    } else {
        attr = Files.readAttributes(p, BasicFileAttributes.class, linkOption);
    }

    // file size, lastModiled and lastAccessed

    fileInfo.setSize(attr.size());
    DateTimeZone timeZone = DateTimeZone.forID("UTC");
    DateTime lm = new DateTime(attr.lastModifiedTime().toMillis(), timeZone);
    fileInfo.setLastModified(lm.withZone(DateTimeZone.getDefault()));
    DateTime la = new DateTime(attr.lastAccessTime().toMillis(), timeZone);
    fileInfo.setLastAccessed(la.withZone(DateTimeZone.getDefault()));
}
 
Example 6
Source File: AnomalyDetectorWrapper.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
long getLastTimeStamp() {
  long end = this.endTime;
  if (this.dataset != null) {
    MetricSlice metricSlice = MetricSlice.from(this.metricEntity.getId(),
        this.startTime,
        this.endTime,
        this.metricEntity.getFilters());
    DoubleSeries timestamps = this.provider.fetchTimeseries(Collections.singleton(metricSlice)).get(metricSlice).getDoubles(COL_TIME);
    if (timestamps.size() == 0) {
      // no data available, don't update time stamp
      return -1;
    }
    Period period = dataset.bucketTimeGranularity().toPeriod();
    DateTimeZone timezone = DateTimeZone.forID(dataset.getTimezone());
    long lastTimestamp = timestamps.getLong(timestamps.size() - 1);

    end = new DateTime(lastTimestamp, timezone).plus(period).getMillis();
  }

  // truncate at analysis end time
  return Math.min(end, this.endTime);
}
 
Example 7
Source File: MDateAndTime.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Convert {@code millis} to a DateTime and {@link #encodeTime(long, long, long, TExecutionContext)}. */
public static int encodeTime(long millis, String tz) {
    DateTime dt = new DateTime(millis, DateTimeZone.forID(tz));
    return encodeTime(dt.getHourOfDay(),
                      dt.getMinuteOfHour(),
                      dt.getSecondOfMinute(),
                      null);
}
 
Example 8
Source File: TestTransforms.java    From DataVec with Apache License 2.0 5 votes vote down vote up
private void testStringToDateTime(String timeFormat) throws Exception {
    Schema schema = getSchema(ColumnType.String);

    //http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
    Transform transform = new StringToTimeTransform("column", timeFormat, DateTimeZone.forID("UTC"));
    transform.setInputSchema(schema);

    Schema out = transform.transform(schema);

    assertEquals(1, out.getColumnMetaData().size());
    TestCase.assertEquals(ColumnType.Time, out.getMetaData(0).getColumnType());

    String in1 = "2016-01-01 12:30:45";
    long out1 = 1451651445000L;

    String in2 = "2015-06-30 23:59:59";
    long out2 = 1435708799000L;

    assertEquals(Collections.singletonList((Writable) new LongWritable(out1)),
            transform.map(Collections.singletonList((Writable) new Text(in1))));
    assertEquals(Collections.singletonList((Writable) new LongWritable(out2)),
            transform.map(Collections.singletonList((Writable) new Text(in2))));

    //Check serialization: things like DateTimeFormatter etc aren't serializable, hence we need custom serialization :/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(transform);

    byte[] bytes = baos.toByteArray();

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);

    Transform deserialized = (Transform) ois.readObject();
    assertEquals(Collections.singletonList((Writable) new LongWritable(out1)),
            deserialized.map(Collections.singletonList((Writable) new Text(in1))));
    assertEquals(Collections.singletonList((Writable) new LongWritable(out2)),
            deserialized.map(Collections.singletonList((Writable) new Text(in2))));
}
 
Example 9
Source File: TimeRangeCheckerTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeRangeChecker() {
  // January 1st, 2015 (a Thursday)
  DateTime dateTime = new DateTime(2015, 1, 1, 0, 0, 0, DateTimeZone.forID(ConfigurationKeys.PST_TIMEZONE_NAME));

  // Positive Tests

  // Base hour
  Assert.assertTrue(TimeRangeChecker.isTimeInRange(Lists.newArrayList("THURSDAY"), "00-00", "06-00", dateTime));

  // Valid minute
  Assert.assertTrue(TimeRangeChecker.isTimeInRange(Lists.newArrayList("THURSDAY"), "00-00", "00-01", dateTime));

  // Multiple days
  Assert.assertTrue(TimeRangeChecker.isTimeInRange(Lists.newArrayList("MONDAY", "THURSDAY"), "00-00", "06-00", dateTime));

  // Negative Tests

  // Invalid day
  Assert.assertFalse(TimeRangeChecker.isTimeInRange(Lists.newArrayList("MONDAY"), "00-00", "06-00", dateTime));

  // Invalid minute
  Assert.assertFalse(TimeRangeChecker.isTimeInRange(Lists.newArrayList("THURSDAY"), "00-01", "06-00", dateTime));

  // Invalid hour
  Assert.assertFalse(TimeRangeChecker.isTimeInRange(Lists.newArrayList("THURSDAY"), "01-00", "06-00", dateTime));
}
 
Example 10
Source File: BaselineRuleFilterWrapper.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public BaselineRuleFilterWrapper(DataProvider provider, DetectionConfigDTO config, long startTime, long endTime) {
  super(provider, config, startTime, endTime);
  int weeks = MapUtils.getIntValue(config.getProperties(), PROP_WEEKS, PROP_WEEKS_DEFAULT);
  DateTimeZone timezone =
      DateTimeZone.forID(MapUtils.getString(this.config.getProperties(), PROP_TIMEZONE, PROP_TIMEZONE_DEFAULT));
  this.baseline = BaselineAggregate.fromWeekOverWeek(BaselineAggregateType.MEDIAN, weeks, 1, timezone);
  // percentage change
  this.change = MapUtils.getDoubleValue(config.getProperties(), PROP_CHANGE, PROP_CHANGE_DEFAULT);
  // absolute change
  this.difference = MapUtils.getDoubleValue(config.getProperties(), PROP_DIFFERENCE, PROP_DIFFERENCE_DEFAULT);
  // site wide impact
  this.siteWideImpactThreshold = MapUtils.getDoubleValue(config.getProperties(), PROP_SITEWIDE_THRESHOLD, PROP_SITEWIDE_THRESHOLD_DEFAULT);
  this.siteWideMetricUrn = MapUtils.getString(config.getProperties(), PROP_SITEWIDE_METRIC);
}
 
Example 11
Source File: AnomalyDetectorWrapperTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Test
public void testMovingMonitoringWindowBoundary() {
  this.properties.put(PROP_MOVING_WINDOW_DETECTION, true);
  this.properties.put(PROP_TIMEZONE, TimeSpec.DEFAULT_TIMEZONE);
  AnomalyDetectorWrapper detectionPipeline =
      new AnomalyDetectorWrapper(this.provider, this.config, 1540080000000L, 1540425600000L);
  List<Interval> monitoringWindows = detectionPipeline.getMonitoringWindows();
  DateTimeZone timeZone = DateTimeZone.forID(TimeSpec.DEFAULT_TIMEZONE);
  Assert.assertEquals(monitoringWindows,
      Arrays.asList(new Interval(1540080000000L, 1540166400000L, timeZone), new Interval(1540166400000L, 1540252800000L, timeZone),
          new Interval(1540252800000L, 1540339200000L, timeZone), new Interval(1540339200000L, 1540425600000L, timeZone)));
}
 
Example 12
Source File: MockThirdEyeDataSource.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
static MockDataset fromMap(String name, Map<String, Object> map) {
  return new MockDataset(
      name,
      DateTimeZone.forID(MapUtils.getString(map, "timezone", "America/Los_Angeles")),
      ConfigUtils.<String>getList(map.get("dimensions")),
      ConfigUtils.<String, Map<String, Object>>getMap(map.get("metrics")),
      ConfigUtils.parsePeriod(MapUtils.getString(map, "granularity", "1hour")));
}
 
Example 13
Source File: CoreFormattersTest.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
protected static String getDateTestJson(long timestamp, String tzId) {
  DateTimeZone timezone = DateTimeZone.forID(tzId);
  ObjectNode node = JsonUtils.createObjectNode();
  node.put("time", timestamp);
  ObjectNode website = node.putObject("website");
  website.put("timeZoneOffset", timezone.getOffset(timestamp));
  website.put("timeZone", timezone.getID());
  return node.toString();
}
 
Example 14
Source File: TestDateTimeZone.java    From joda-time-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatchedNameKeysSydney() throws Exception {
    // the tz database does not have unique name keys [1716305]
    DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
    
    DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
    String str1 = zone.getName(now.getMillis());
    String str2 = zone.getName(now.plusMonths(6).getMillis());
    assertEquals(false, str1.equals(str2));
}
 
Example 15
Source File: TimestampColumnReader.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void startStripe(ZoneId fileTimeZone, ZoneId storageTimeZone, InputStreamSources dictionaryStreamSources, ColumnMetadata<ColumnEncoding> encoding)
{
    baseTimestampInSeconds = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, fileTimeZone).toEpochSecond();

    /*
     * In legacy semantics, timestamp represents a point in time. ORC effectively stores millis and zone (like ZonedDateTime).
     * Hive interprets the ORC value as local date/time in file time zone.
     * We need to calculate point in time corresponding to (local date/time read from ORC) at Hive warehouse time zone.
     *
     * TODO support new timestamp semantics
     */
    TimeZoneKey fileTimeZoneKey = TimeZoneKey.getTimeZoneKey(fileTimeZone.getId()); // normalize and detect UTC-equivalent zones
    TimeZoneKey storageTimeZoneKey = TimeZoneKey.getTimeZoneKey(storageTimeZone.getId()); // normalize and detect UTC-equivalent zones
    if (fileTimeZoneKey.equals(storageTimeZoneKey)) {
        storageDateTimeZone = null;
        fileDateTimeZone = null;
    }
    else {
        storageDateTimeZone = DateTimeZone.forID(storageTimeZoneKey.getId());
        fileDateTimeZone = DateTimeZone.forID(fileTimeZoneKey.getId());
    }

    presentStreamSource = missingStreamSource(BooleanInputStream.class);
    secondsStreamSource = missingStreamSource(LongInputStream.class);
    nanosStreamSource = missingStreamSource(LongInputStream.class);

    readOffset = 0;
    nextBatchSize = 0;

    presentStream = null;
    secondsStream = null;
    nanosStream = null;

    rowGroupOpen = false;
}
 
Example 16
Source File: TestGJChronology.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDurationFields() {
    final GJChronology gj = GJChronology.getInstance();
    assertEquals("eras", gj.eras().getName());
    assertEquals("centuries", gj.centuries().getName());
    assertEquals("years", gj.years().getName());
    assertEquals("weekyears", gj.weekyears().getName());
    assertEquals("months", gj.months().getName());
    assertEquals("weeks", gj.weeks().getName());
    assertEquals("halfdays", gj.halfdays().getName());
    assertEquals("days", gj.days().getName());
    assertEquals("hours", gj.hours().getName());
    assertEquals("minutes", gj.minutes().getName());
    assertEquals("seconds", gj.seconds().getName());
    assertEquals("millis", gj.millis().getName());
    
    assertEquals(false, gj.eras().isSupported());
    assertEquals(true, gj.centuries().isSupported());
    assertEquals(true, gj.years().isSupported());
    assertEquals(true, gj.weekyears().isSupported());
    assertEquals(true, gj.months().isSupported());
    assertEquals(true, gj.weeks().isSupported());
    assertEquals(true, gj.days().isSupported());
    assertEquals(true, gj.halfdays().isSupported());
    assertEquals(true, gj.hours().isSupported());
    assertEquals(true, gj.minutes().isSupported());
    assertEquals(true, gj.seconds().isSupported());
    assertEquals(true, gj.millis().isSupported());
    
    assertEquals(false, gj.centuries().isPrecise());
    assertEquals(false, gj.years().isPrecise());
    assertEquals(false, gj.weekyears().isPrecise());
    assertEquals(false, gj.months().isPrecise());
    assertEquals(false, gj.weeks().isPrecise());
    assertEquals(false, gj.days().isPrecise());
    assertEquals(false, gj.halfdays().isPrecise());
    assertEquals(true, gj.hours().isPrecise());
    assertEquals(true, gj.minutes().isPrecise());
    assertEquals(true, gj.seconds().isPrecise());
    assertEquals(true, gj.millis().isPrecise());
    
    final GJChronology gjUTC = GJChronology.getInstanceUTC();
    assertEquals(false, gjUTC.centuries().isPrecise());
    assertEquals(false, gjUTC.years().isPrecise());
    assertEquals(false, gjUTC.weekyears().isPrecise());
    assertEquals(false, gjUTC.months().isPrecise());
    assertEquals(true, gjUTC.weeks().isPrecise());
    assertEquals(true, gjUTC.days().isPrecise());
    assertEquals(true, gjUTC.halfdays().isPrecise());
    assertEquals(true, gjUTC.hours().isPrecise());
    assertEquals(true, gjUTC.minutes().isPrecise());
    assertEquals(true, gjUTC.seconds().isPrecise());
    assertEquals(true, gjUTC.millis().isPrecise());
    
    final DateTimeZone gmt = DateTimeZone.forID("Etc/GMT");
    final GJChronology gjGMT = GJChronology.getInstance(gmt);
    assertEquals(false, gjGMT.centuries().isPrecise());
    assertEquals(false, gjGMT.years().isPrecise());
    assertEquals(false, gjGMT.weekyears().isPrecise());
    assertEquals(false, gjGMT.months().isPrecise());
    assertEquals(true, gjGMT.weeks().isPrecise());
    assertEquals(true, gjGMT.days().isPrecise());
    assertEquals(true, gjGMT.halfdays().isPrecise());
    assertEquals(true, gjGMT.hours().isPrecise());
    assertEquals(true, gjGMT.minutes().isPrecise());
    assertEquals(true, gjGMT.seconds().isPrecise());
    assertEquals(true, gjGMT.millis().isPrecise());
}
 
Example 17
Source File: RestApiSession.java    From sdk-rest with MIT License 4 votes vote down vote up
private DateTime getNow() {
	return new DateTime(DateTimeZone.forID("EST5EDT"));
}
 
Example 18
Source File: ADDYEARS.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

  Object top = stack.pop();
  
  if (!(top instanceof Long)) {
    throw new WarpScriptException(getName() + " expects a number of years on top of the stack.");
  }
  
  int years = ((Number) top).intValue();
  
  top = stack.pop();
  
  String tz = null;
  
  if (top instanceof String) {
    tz = top.toString();
    top = stack.pop();
    if (!(top instanceof Long)) {
      throw new WarpScriptException(getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
    }
  } else if (!(top instanceof List) && !(top instanceof Long)) {
    throw new WarpScriptException(getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
  }
  
  if (top instanceof Long) {
    long instant = ((Number) top).longValue();
    
    if (null == tz) {
      tz = "UTC";
    }
    
    DateTimeZone dtz = DateTimeZone.forID(tz);
    
    DateTime dt = new DateTime(instant / Constants.TIME_UNITS_PER_MS, dtz);
    
    dt = dt.plusYears(years);
    
    long ts = dt.getMillis() * Constants.TIME_UNITS_PER_MS + (instant % Constants.TIME_UNITS_PER_MS);
    
    stack.push(ts);      
  } else {
    List<Object> elts = new ArrayList<Object>((List<Object>) top);
    
    int year = ((Number) elts.get(0)).intValue();
    year += years;
    
    elts.set(0, (long) year);
    
    // Now check if we are in february and if this is coherent with
    // a possibly non leap year
    
    if (elts.size() > 2) {
      int month = ((Number) elts.get(1)).intValue();
      int day = ((Number) elts.get(2)).intValue();
      
      if (2 == month && day > 28) {
        if ((0 != year % 4) || (0 == year % 100)) {
          elts.set(2, (long) 28);
        }
      }
    }
   
    stack.push(elts);
  }
      
  return stack;
}
 
Example 19
Source File: DateFormatter.java    From hangout with MIT License 4 votes vote down vote up
public DateFormatter(String format, String timezone) {
    this.format = format;
    this.tz = DateTimeZone.forID(timezone);
}
 
Example 20
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2004() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2004, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2004, 3, 26, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}