@Test public void testRoundingWithTimeZone() { MutableDateTime time = new MutableDateTime(DateTimeZone.UTC); time.setZone(DateTimeZone.forOffsetHours(-2)); time.setRounding(time.getChronology().dayOfMonth(), MutableDateTime.ROUND_FLOOR); MutableDateTime utcTime = new MutableDateTime(DateTimeZone.UTC); utcTime.setRounding(utcTime.getChronology().dayOfMonth(), MutableDateTime.ROUND_FLOOR); time.setMillis(utcTimeInMillis("2009-02-03T01:01:01")); utcTime.setMillis(utcTimeInMillis("2009-02-03T01:01:01")); assertThat(time.toString(), equalTo("2009-02-02T00:00:00.000-02:00")); assertThat(utcTime.toString(), equalTo("2009-02-03T00:00:00.000Z")); // the time is on the 2nd, and utcTime is on the 3rd, but, because time already encapsulates // time zone, the millis diff is not 24, but 22 hours assertThat( time.getMillis(), equalTo(utcTime.getMillis() - TimeValue.timeValueHours(22).millis())); time.setMillis(utcTimeInMillis("2009-02-04T01:01:01")); utcTime.setMillis(utcTimeInMillis("2009-02-04T01:01:01")); assertThat(time.toString(), equalTo("2009-02-03T00:00:00.000-02:00")); assertThat(utcTime.toString(), equalTo("2009-02-04T00:00:00.000Z")); assertThat( time.getMillis(), equalTo(utcTime.getMillis() - TimeValue.timeValueHours(22).millis())); }
public void testUTCIntervalRounding() { Rounding tzRounding = Rounding.builder(TimeValue.timeValueHours(12)).build(); DateTimeZone tz = DateTimeZone.UTC; assertThat( tzRounding.round(time("2009-02-03T01:01:01")), isDate(time("2009-02-03T00:00:00.000Z"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-03T00:00:00.000Z")), isDate(time("2009-02-03T12:00:00.000Z"), tz)); assertThat( tzRounding.round(time("2009-02-03T13:01:01")), isDate(time("2009-02-03T12:00:00.000Z"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-03T12:00:00.000Z")), isDate(time("2009-02-04T00:00:00.000Z"), tz)); tzRounding = Rounding.builder(TimeValue.timeValueHours(48)).build(); assertThat( tzRounding.round(time("2009-02-03T01:01:01")), isDate(time("2009-02-03T00:00:00.000Z"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-03T00:00:00.000Z")), isDate(time("2009-02-05T00:00:00.000Z"), tz)); assertThat( tzRounding.round(time("2009-02-05T13:01:01")), isDate(time("2009-02-05T00:00:00.000Z"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-05T00:00:00.000Z")), isDate(time("2009-02-07T00:00:00.000Z"), tz)); }
private AbstractDistanceScoreFunction parseDateVariable( XContentParser parser, QueryShardContext context, DateFieldMapper.DateFieldType dateFieldType, MultiValueMode mode) throws IOException { XContentParser.Token token; String parameterName = null; String scaleString = null; String originString = null; String offsetString = "0d"; double decay = 0.5; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { parameterName = parser.currentName(); } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) { scaleString = parser.text(); } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) { originString = parser.text(); } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) { decay = parser.doubleValue(); } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) { offsetString = parser.text(); } else { throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName); } } long origin; if (originString == null) { origin = context.nowInMillis(); } else { origin = dateFieldType.parseToMilliseconds(originString, false, null, null); } if (scaleString == null) { throw new ElasticsearchParseException( "[{}] must be set for date fields.", DecayFunctionBuilder.SCALE); } TimeValue val = TimeValue.parseTimeValue( scaleString, TimeValue.timeValueHours(24), DecayFunctionParser.class.getSimpleName() + ".scale"); double scale = val.getMillis(); val = TimeValue.parseTimeValue( offsetString, TimeValue.timeValueHours(24), DecayFunctionParser.class.getSimpleName() + ".offset"); double offset = val.getMillis(); IndexNumericFieldData numericFieldData = context.getForField(dateFieldType); return new NumericFieldDataScoreFunction( origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode); }
@Test public void testDefaultRecoverAfterTime() throws IOException { // check that the default is not set GatewayService service = createService(Settings.builder()); assertNull(service.recoverAfterTime()); // ensure default is set when setting expected_nodes service = createService(Settings.builder().put("gateway.expected_nodes", 1)); assertThat( service.recoverAfterTime(), Matchers.equalTo(GatewayService.DEFAULT_RECOVER_AFTER_TIME_IF_EXPECTED_NODES_IS_SET)); // ensure default is set when setting expected_data_nodes service = createService(Settings.builder().put("gateway.expected_data_nodes", 1)); assertThat( service.recoverAfterTime(), Matchers.equalTo(GatewayService.DEFAULT_RECOVER_AFTER_TIME_IF_EXPECTED_NODES_IS_SET)); // ensure default is set when setting expected_master_nodes service = createService(Settings.builder().put("gateway.expected_master_nodes", 1)); assertThat( service.recoverAfterTime(), Matchers.equalTo(GatewayService.DEFAULT_RECOVER_AFTER_TIME_IF_EXPECTED_NODES_IS_SET)); // ensure settings override default TimeValue timeValue = TimeValue.timeValueHours(3); // ensure default is set when setting expected_nodes service = createService( Settings.builder() .put("gateway.expected_nodes", 1) .put("gateway.recover_after_time", timeValue.toString())); assertThat(service.recoverAfterTime().millis(), Matchers.equalTo(timeValue.millis())); }
@Test public void testIsoDateFormatDateOptionalTimeUTC() { DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC); long millis = formatter.parseMillis("1970-01-01T00:00:00Z"); assertThat(millis, equalTo(0l)); millis = formatter.parseMillis("1970-01-01T00:00:00.001Z"); assertThat(millis, equalTo(1l)); millis = formatter.parseMillis("1970-01-01T00:00:00.1Z"); assertThat(millis, equalTo(100l)); millis = formatter.parseMillis("1970-01-01T00:00:00.1"); assertThat(millis, equalTo(100l)); millis = formatter.parseMillis("1970-01-01T00:00:00"); assertThat(millis, equalTo(0l)); millis = formatter.parseMillis("1970-01-01"); assertThat(millis, equalTo(0l)); millis = formatter.parseMillis("1970"); assertThat(millis, equalTo(0l)); try { formatter.parseMillis("1970 kuku"); assert false : "formatting should fail"; } catch (IllegalArgumentException e) { // all is well } // test offset in format millis = formatter.parseMillis("1970-01-01T00:00:00-02:00"); assertThat(millis, equalTo(TimeValue.timeValueHours(2).millis())); }
@Inject public LocalGatewayMetaState( Settings settings, ThreadPool threadPool, NodeEnvironment nodeEnv, TransportNodesListGatewayMetaState nodesListGatewayMetaState, LocalAllocateDangledIndices allocateDangledIndices, NodeIndexDeletedAction nodeIndexDeletedAction) throws Exception { super(settings); this.nodeEnv = nodeEnv; this.threadPool = threadPool; this.format = XContentType.fromRestContentType(settings.get("format", "smile")); this.allocateDangledIndices = allocateDangledIndices; this.nodeIndexDeletedAction = nodeIndexDeletedAction; nodesListGatewayMetaState.init(this); if (this.format == XContentType.SMILE) { Map<String, String> params = Maps.newHashMap(); params.put("binary", "true"); formatParams = new ToXContent.MapParams(params); Map<String, String> globalOnlyParams = Maps.newHashMap(); globalOnlyParams.put("binary", "true"); globalOnlyParams.put(MetaData.PERSISTENT_ONLY_PARAM, "true"); globalOnlyParams.put(MetaData.GLOBAL_ONLY_PARAM, "true"); globalOnlyFormatParams = new ToXContent.MapParams(globalOnlyParams); } else { formatParams = ToXContent.EMPTY_PARAMS; Map<String, String> globalOnlyParams = Maps.newHashMap(); globalOnlyParams.put(MetaData.PERSISTENT_ONLY_PARAM, "true"); globalOnlyParams.put(MetaData.GLOBAL_ONLY_PARAM, "true"); globalOnlyFormatParams = new ToXContent.MapParams(globalOnlyParams); } this.autoImportDangled = AutoImportDangledState.fromString( settings.get( "gateway.local.auto_import_dangled", AutoImportDangledState.YES.toString())); this.danglingTimeout = settings.getAsTime("gateway.local.dangling_timeout", TimeValue.timeValueHours(2)); logger.debug( "using gateway.local.auto_import_dangled [{}], with gateway.local.dangling_timeout [{}]", this.autoImportDangled, this.danglingTimeout); if (DiscoveryNode.masterNode(settings)) { try { pre019Upgrade(); long start = System.currentTimeMillis(); loadState(); logger.debug( "took {} to load state", TimeValue.timeValueMillis(System.currentTimeMillis() - start)); } catch (Exception e) { logger.error("failed to read local state, exiting...", e); throw e; } } }
public void testTimeRounding() { // hour unit DateTimeZone tz = DateTimeZone.forOffsetHours(-2); Rounding tzRounding = Rounding.builder(DateTimeUnit.HOUR_OF_DAY).timeZone(tz).build(); assertThat(tzRounding.round(0), equalTo(0L)); assertThat(tzRounding.nextRoundingValue(0L), equalTo(TimeValue.timeValueHours(1L).getMillis())); assertThat( tzRounding.round(time("2009-02-03T01:01:01")), isDate(time("2009-02-03T01:00:00"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-03T01:00:00")), isDate(time("2009-02-03T02:00:00"), tz)); }
public void testDayRounding() { int timezoneOffset = -2; Rounding tzRounding = Rounding.builder(DateTimeUnit.DAY_OF_MONTH) .timeZone(DateTimeZone.forOffsetHours(timezoneOffset)) .build(); assertThat( tzRounding.round(0), equalTo(0L - TimeValue.timeValueHours(24 + timezoneOffset).millis())); assertThat( tzRounding.nextRoundingValue(0L - TimeValue.timeValueHours(24 + timezoneOffset).millis()), equalTo(0L - TimeValue.timeValueHours(timezoneOffset).millis())); DateTimeZone tz = DateTimeZone.forID("-08:00"); tzRounding = Rounding.builder(DateTimeUnit.DAY_OF_MONTH).timeZone(tz).build(); assertThat( tzRounding.round(time("2012-04-01T04:15:30Z")), isDate(time("2012-03-31T08:00:00Z"), tz)); tzRounding = Rounding.builder(DateTimeUnit.MONTH_OF_YEAR).timeZone(tz).build(); assertThat( tzRounding.round(time("2012-04-01T04:15:30Z")), equalTo(time("2012-03-01T08:00:00Z"))); // date in Feb-3rd, but still in Feb-2nd in -02:00 timezone tz = DateTimeZone.forID("-02:00"); tzRounding = Rounding.builder(DateTimeUnit.DAY_OF_MONTH).timeZone(tz).build(); assertThat( tzRounding.round(time("2009-02-03T01:01:01")), isDate(time("2009-02-02T02:00:00"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-02T02:00:00")), isDate(time("2009-02-03T02:00:00"), tz)); // date in Feb-3rd, also in -02:00 timezone tzRounding = Rounding.builder(DateTimeUnit.DAY_OF_MONTH).timeZone(tz).build(); assertThat( tzRounding.round(time("2009-02-03T02:01:01")), isDate(time("2009-02-03T02:00:00"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-03T02:00:00")), isDate(time("2009-02-04T02:00:00"), tz)); }
/** test TimeIntervalRounding, (interval < 12h) with time zone shift */ public void testTimeIntervalRounding() { DateTimeZone tz = DateTimeZone.forOffsetHours(-1); Rounding tzRounding = Rounding.builder(TimeValue.timeValueHours(6)).timeZone(tz).build(); assertThat( tzRounding.round(time("2009-02-03T00:01:01")), isDate(time("2009-02-02T19:00:00.000Z"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-02T19:00:00.000Z")), isDate(time("2009-02-03T01:00:00.000Z"), tz)); assertThat( tzRounding.round(time("2009-02-03T13:01:01")), isDate(time("2009-02-03T13:00:00.000Z"), tz)); assertThat( tzRounding.nextRoundingValue(time("2009-02-03T13:00:00.000Z")), isDate(time("2009-02-03T19:00:00.000Z"), tz)); }
public static long parseHumanDTToMills(String humanDateTime) { long datetime = 0; if (humanDateTime.indexOf("ms") != -1) { datetime = TimeValue.timeValueMillis(NumberUtils.toLong(humanDateTime.replaceAll("ms", ""))) .getMillis(); } else if (humanDateTime.indexOf("h") != -1) { datetime = TimeValue.timeValueHours(NumberUtils.toLong(humanDateTime.replaceAll("h", ""))) .getMillis(); } else if (humanDateTime.indexOf("m") != -1) { datetime = TimeValue.timeValueMinutes(NumberUtils.toLong(humanDateTime.replaceAll("m", ""))) .getMillis(); } else if (humanDateTime.indexOf("s") != -1) { datetime = TimeValue.timeValueSeconds(NumberUtils.toLong(humanDateTime.replaceAll("s", ""))) .getMillis(); } return datetime; }