List of usage examples for org.joda.time DateTime withYear
public DateTime withYear(int year)
From source file:org.apache.flume.ext.source.SyslogParser.java
License:Apache License
/** * Parse the RFC3164 date format. This is trickier than it sounds because this * format does not specify a year so we get weird edge cases at year * boundaries. This implementation tries to "do what I mean". * @param ts RFC3164-compatible timestamp to be parsed * @return Typical (for Java) milliseconds since the UNIX epoch */// w ww . j a v a 2s .co m protected long parseRfc3164Time(String ts) { DateTime now = DateTime.now(); int year = now.getYear(); ts = TWO_SPACES.matcher(ts).replaceFirst(" "); DateTime date; try { date = rfc3164Format.parseDateTime(ts); } catch (IllegalArgumentException e) { logger.debug("rfc3164 date parse failed on (" + ts + "): invalid format", e); return 0; } // try to deal with boundary cases, i.e. new year's eve. // rfc3164 dates are really dumb. // NB: cannot handle replaying of old logs or going back to the future if (date != null) { DateTime fixed = date.withYear(year); // flume clock is ahead or there is some latency, and the year rolled if (fixed.isAfter(now) && fixed.minusMonths(1).isAfter(now)) { fixed = date.withYear(year - 1); // flume clock is behind and the year rolled } else if (fixed.isBefore(now) && fixed.plusMonths(1).isBefore(now)) { fixed = date.withYear(year + 1); } date = fixed; } if (date == null) { return 0; } return date.getMillis(); }
From source file:org.apache.flume.serialization.SyslogAvroEventSerializer.java
License:Apache License
/** * Returns epoch time in millis, or 0 if the string cannot be parsed. * We use two date formats because the date spec in rfc3164 is kind of weird. * <br/>/*from w ww.j av a 2 s. c o m*/ * <b>Warning:</b> logic is used here to determine the year even though it's * not part of the timestamp format, and we assume that the machine running * Flume has a clock that is at least close to the same day as the machine * that generated the event. We also assume that the event was generated * recently. */ private static long parseRfc3164Date(String in) { DateTime date = null; try { date = dateFmt1.parseDateTime(in); } catch (IllegalArgumentException e) { // ignore the exception, we act based on nullity of date object logger.debug("Date parse failed on ({}), trying single-digit date", in); } if (date == null) { try { date = dateFmt2.parseDateTime(in); } catch (IllegalArgumentException e) { // ignore the exception, we act based on nullity of date object logger.debug("2nd date parse failed on ({}), unknown date format", in); } } // hacky stuff to try and deal with boundary cases, i.e. new year's eve. // rfc3164 dates are really dumb. // NB: cannot handle replaying of old logs or going back to the future if (date != null) { DateTime now = new DateTime(); int year = now.getYear(); DateTime corrected = date.withYear(year); // flume clock is ahead or there is some latency, and the year rolled if (corrected.isAfter(now) && corrected.minusMonths(1).isAfter(now)) { corrected = date.withYear(year - 1); // flume clock is behind and the year rolled } else if (corrected.isBefore(now) && corrected.plusMonths(1).isBefore(now)) { corrected = date.withYear(year + 1); } date = corrected; } if (date == null) { return 0; } return date.getMillis(); }
From source file:org.jgrasstools.gears.io.las.core.LasReader_1_0.java
License:Open Source License
@SuppressWarnings("nls") protected void parseHeader() throws Exception { try {//from ww w.j a v a 2 s . c o m StringBuilder sb = new StringBuilder(); // file signature (LASF) String signature = getString(4); sb.append("File signature: ").append(signature).append("\n"); // file source ID short fileSourceId = getShort2Bytes(); sb.append("File source ID: ").append(fileSourceId).append("\n"); // reserved (optional) getShort2Bytes(); // Project ID - data 1 (optional) long projectIdGuidData1 = getLong4Bytes(); sb.append("Project ID - data 1: ").append(projectIdGuidData1).append("\n"); // Project ID - data 2 (optional) short projectIdGuidData2 = getShort2Bytes(); sb.append("Project ID - data 2: ").append(projectIdGuidData2).append("\n"); // Project ID - data 3 (optional) short projectIdGuidData3 = getShort2Bytes(); sb.append("Project ID - data 3: ").append(projectIdGuidData3).append("\n"); // Project ID - data 4 (optional) String projectIdGuidData4 = getString(8); sb.append("Project ID - data 4: ").append(projectIdGuidData4).append("\n"); // Version Major byte versionMajor = get(); // Version Minor byte versionMinor = get(); version = versionMajor + "." + versionMinor; sb.append("Version: ").append(versionMajor).append(".").append(versionMinor).append("\n"); // System identifier String systemIdentifier = getString(32); sb.append("System identifier: ").append(systemIdentifier).append("\n"); // generating software String generatingSoftware = getString(32); sb.append("Generating software: ").append(generatingSoftware).append("\n"); // File creation Day of Year (optional) short dayOfYear = getShort2Bytes(); // File creation Year (optional) short year = getShort2Bytes(); if (dayOfYear != 0 && year != 0) { DateTime dateTime = new DateTime(); dateTime = dateTime.withYear(year).withDayOfYear(dayOfYear); String dtString = dateTime.toString(dateTimeFormatterYYYYMMDD); sb.append("File creation date: ").append(dtString).append("\n"); } else { sb.append("File creation Day of Year: ").append(dayOfYear).append("\n"); sb.append("File creation Year: ").append(year).append("\n"); } // header size short headerSize = getShort2Bytes(); sb.append("Header size: ").append(headerSize).append("\n"); // offset to point data offset = getLong4Bytes(); // Number of variable length records long variableLengthRecordNum = getLong4Bytes(); sb.append("Variable length records: ").append(variableLengthRecordNum).append("\n"); // point data format ID (0-99 for spec) byte pointDataFormat = get(); sb.append("Point data format ID (0-99 for spec): ").append(pointDataFormat).append("\n"); recordLength = getShort2Bytes(); // Number of point records records = getLong4Bytes(); sb.append("Number of point records: ").append(records).append("\n"); fc.position(fc.position() + 20); // skip xScale = getDouble8Bytes(); yScale = getDouble8Bytes(); zScale = getDouble8Bytes(); xOffset = getDouble8Bytes(); yOffset = getDouble8Bytes(); zOffset = getDouble8Bytes(); xMax = getDouble8Bytes(); xMin = getDouble8Bytes(); yMax = getDouble8Bytes(); yMin = getDouble8Bytes(); zMax = getDouble8Bytes(); zMin = getDouble8Bytes(); sb.append("X Range: [").append(xMin).append(", ").append(xMax).append("]\n"); sb.append("Y Range: [").append(yMin).append(", ").append(yMax).append("]\n"); sb.append("Z Range: [").append(zMin).append(", ").append(zMax).append("]\n"); header = sb.toString(); if (crs != null) { crs = DefaultGeographicCRS.WGS84; } dataEnvelope = new ReferencedEnvelope(xMin, xMax, yMin, yMax, crs); /* * move to the data position */ fc.position(offset); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.jgrasstools.gears.io.las.core.liblas.LiblasHeader.java
License:Open Source License
public String toString() { StringBuilder sb = new StringBuilder(); sb.append("File signature: ").append(signature).append("\n"); sb.append("File source ID: ").append(fileSourceId).append("\n"); sb.append("Project ID - data 1: ").append(projectIdGuidData1).append("\n"); sb.append("Project ID - data 2: ").append(projectIdGuidData2).append("\n"); sb.append("Project ID - data 3: ").append(projectIdGuidData3).append("\n"); sb.append("Project ID - data 4: ").append(projectIdGuidData4).append("\n"); sb.append("Version: ").append(versionMajor).append(".").append(versionMinor).append("\n"); sb.append("System identifier: ").append(systemIdentifier).append("\n"); sb.append("Generating software: ").append(generatingSoftware).append("\n"); if (dayOfYear != 0 && year != 0) { DateTime dateTime = new DateTime(); dateTime = dateTime.withYear(year).withDayOfYear(dayOfYear); String dtString = dateTime.toString(LasUtils.dateTimeFormatterYYYYMMDD); sb.append("File creation date: ").append(dtString).append("\n"); } else {//from w ww.j a va 2 s . co m sb.append("File creation Day of Year: ").append(dayOfYear).append("\n"); sb.append("File creation Year: ").append(year).append("\n"); } sb.append("Header size: ").append(headerSize).append("\n"); sb.append("Offset to data: ").append(offset).append("\n"); sb.append("Variable length records: ").append(variableLengthRecordNum).append("\n"); sb.append("Point data format ID (0-99 for spec): ").append(pointDataFormat).append("\n"); sb.append("Number of point records: ").append(pointRecordsCount).append("\n"); sb.append("Record length: ").append(recordLength).append("\n"); sb.append("Scale: [").append(xScale).append(", ").append(yScale).append(", ").append(zScale).append("]\n"); sb.append("Offset: [").append(xOffset).append(", ").append(yOffset).append(", ").append(zOffset) .append("]\n"); sb.append("X Range: [").append(xMin).append(", ").append(xMax).append("]\n"); sb.append("Y Range: [").append(yMin).append(", ").append(yMax).append("]\n"); sb.append("Z Range: [").append(zMin).append(", ").append(zMax).append("]\n"); sb.append("Has gps time info: ").append(hasGpsTime()).append("\n"); sb.append("Has color info: ").append(hasRGB()).append("\n"); // sb.append("Gps time type: ").append(gpsTimeType).append("\n"); return sb.toString(); }
From source file:org.jgrasstools.gears.io.las.core.v_1_0.LasHeader.java
License:Open Source License
public String toString() { StringBuilder sb = new StringBuilder(); sb.append("File signature: ").append(signature).append("\n"); sb.append("File source ID: ").append(fileSourceId).append("\n"); sb.append("Project ID - data 1: ").append(projectIdGuidData1).append("\n"); sb.append("Project ID - data 2: ").append(projectIdGuidData2).append("\n"); sb.append("Project ID - data 3: ").append(projectIdGuidData3).append("\n"); sb.append("Project ID - data 4: ").append(projectIdGuidData4).append("\n"); sb.append("Version: ").append(versionMajor).append(".").append(versionMinor).append("\n"); sb.append("System identifier: ").append(systemIdentifier).append("\n"); sb.append("Generating software: ").append(generatingSoftware).append("\n"); if (dayOfYear != 0 && year != 0) { DateTime dateTime = new DateTime(); dateTime = dateTime.withYear(year).withDayOfYear(dayOfYear); String dtString = dateTime.toString(LasUtils.dateTimeFormatterYYYYMMDD); sb.append("File creation date: ").append(dtString).append("\n"); } else {//from w w w. j a v a 2 s . co m sb.append("File creation Day of Year: ").append(dayOfYear).append("\n"); sb.append("File creation Year: ").append(year).append("\n"); } sb.append("Header size: ").append(headerSize).append("\n"); sb.append("Offset to data: ").append(offset).append("\n"); sb.append("Variable length records: ").append(variableLengthRecordNum).append("\n"); sb.append("Point data format ID (0-99 for spec): ").append(pointDataFormat).append("\n"); sb.append("Number of point records: ").append(records).append("\n"); sb.append("Record length: ").append(recordLength).append("\n"); sb.append("Scale: [").append(xScale).append(", ").append(yScale).append(", ").append(zScale).append("]\n"); sb.append("Offset: [").append(xOffset).append(", ").append(yOffset).append(", ").append(zOffset) .append("]\n"); sb.append("X Range: [").append(xMin).append(", ").append(xMax).append("]\n"); sb.append("Y Range: [").append(yMin).append(", ").append(yMax).append("]\n"); sb.append("Z Range: [").append(zMin).append(", ").append(zMax).append("]\n"); sb.append("Has gps time info: ").append(hasGpsTime()).append("\n"); sb.append("Has color info: ").append(hasRGB()).append("\n"); sb.append("Gps time type: ").append(gpsTimeType).append("\n"); return sb.toString(); }
From source file:org.jgrasstools.gears.io.las.core.v_1_0.LasHeader_1_0.java
License:Open Source License
public String toString() { StringBuilder sb = new StringBuilder(); sb.append("File signature: ").append(signature).append("\n"); sb.append("File source ID: ").append(fileSourceId).append("\n"); sb.append("Project ID - data 1: ").append(projectIdGuidData1).append("\n"); sb.append("Project ID - data 2: ").append(projectIdGuidData2).append("\n"); sb.append("Project ID - data 3: ").append(projectIdGuidData3).append("\n"); sb.append("Project ID - data 4: ").append(projectIdGuidData4).append("\n"); sb.append("Version: ").append(versionMajor).append(".").append(versionMinor).append("\n"); sb.append("System identifier: ").append(systemIdentifier).append("\n"); sb.append("Generating software: ").append(generatingSoftware).append("\n"); if (dayOfYear != 0 && year != 0) { DateTime dateTime = new DateTime(); dateTime = dateTime.withYear(year).withDayOfYear(dayOfYear); String dtString = dateTime.toString(AbstractLasReader.dateTimeFormatterYYYYMMDD); sb.append("File creation date: ").append(dtString).append("\n"); } else {/*from w w w.j a va2s. c o m*/ sb.append("File creation Day of Year: ").append(dayOfYear).append("\n"); sb.append("File creation Year: ").append(year).append("\n"); } sb.append("Header size: ").append(headerSize).append("\n"); sb.append("Offset to data: ").append(offset).append("\n"); sb.append("Variable length records: ").append(variableLengthRecordNum).append("\n"); sb.append("Point data format ID (0-99 for spec): ").append(pointDataFormat).append("\n"); sb.append("Number of point records: ").append(records).append("\n"); sb.append("Scale: [").append(xScale).append(", ").append(yScale).append(", ").append(zScale).append("]\n"); sb.append("Offset: [").append(xOffset).append(", ").append(yOffset).append(", ").append(zOffset) .append("]\n"); sb.append("X Range: [").append(xMin).append(", ").append(xMax).append("]\n"); sb.append("Y Range: [").append(yMin).append(", ").append(yMax).append("]\n"); sb.append("Z Range: [").append(zMin).append(", ").append(zMax).append("]\n"); sb.append("Has gps time info: ").append(hasGpsTime()).append("\n"); sb.append("Has color info: ").append(hasRGB()).append("\n"); sb.append("Gps time type: ").append(gpsTimeType).append("\n"); return sb.toString(); }
From source file:org.jruby.CompatVersion.java
License:LGPL
protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) { Ruby runtime = recv.getRuntime(); DateTime dt = new DateTime(DateTimeZone.UTC); byte[] fromAsBytes = null; fromAsBytes = from.convertToString().getBytes(); if (fromAsBytes.length != 8) { throw runtime.newTypeError("marshaled time format differ"); }/*from w w w .ja v a 2 s . com*/ int p = 0; int s = 0; for (int i = 0; i < 4; i++) { p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i); } for (int i = 4; i < 8; i++) { s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4)); } if ((p & (1 << 31)) == 0) { dt = dt.withMillis(p * 1000L + s); } else { p &= ~(1 << 31); dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900); dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1); dt = dt.withDayOfMonth(((p >>> 5) & 0x1F)); dt = dt.withHourOfDay((p & 0x1F)); dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F)); dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F)); // marsaling dumps usec, not msec dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000); dt = dt.withZone(getLocalTimeZone(runtime)); time.setUSec((s & 0xFFFFF) % 1000); } time.setDateTime(dt); return time; }
From source file:org.jruby.RubyTime.java
License:LGPL
protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) { Ruby runtime = recv.getRuntime();/* w w w . jav a 2 s. com*/ DateTime dt = new DateTime(DateTimeZone.UTC); byte[] fromAsBytes; fromAsBytes = from.convertToString().getBytes(); if (fromAsBytes.length != 8) { throw runtime.newTypeError("marshaled time format differ"); } int p = 0; int s = 0; for (int i = 0; i < 4; i++) { p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i); } for (int i = 4; i < 8; i++) { s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4)); } boolean utc = false; if ((p & (1 << 31)) == 0) { dt = dt.withMillis(p * 1000L); time.setUSec((s & 0xFFFFF) % 1000); } else { p &= ~(1 << 31); utc = ((p >>> 30 & 0x1) == 0x1); dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900); dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1); dt = dt.withDayOfMonth(((p >>> 5) & 0x1F)); dt = dt.withHourOfDay((p & 0x1F)); dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F)); dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F)); // marsaling dumps usec, not msec dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000); time.setUSec((s & 0xFFFFF) % 1000); } time.setDateTime(dt); if (!utc) time.localtime(); from.getInstanceVariables().copyInstanceVariablesInto(time); // pull out nanos, offset, zone IRubyObject nano_num = (IRubyObject) from.getInternalVariables().getInternalVariable("nano_num"); IRubyObject nano_den = (IRubyObject) from.getInternalVariables().getInternalVariable("nano_den"); IRubyObject offsetVar = (IRubyObject) from.getInternalVariables().getInternalVariable("offset"); IRubyObject zoneVar = (IRubyObject) from.getInternalVariables().getInternalVariable("zone"); if (nano_num != null && nano_den != null) { long nanos = nano_num.convertToInteger().getLongValue() / nano_den.convertToInteger().getLongValue(); time.nsec += nanos; } int offset = 0; if (offsetVar != null && offsetVar.respondsTo("to_int")) { IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $! try { offset = offsetVar.convertToInteger().getIntValue() * 1000; } catch (RaiseException typeError) { runtime.getGlobalVariables().set("$!", oldExc); // Restore $! } } String zone = ""; if (zoneVar != null && zoneVar.respondsTo("to_str")) { IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $! try { zone = zoneVar.convertToString().toString(); } catch (RaiseException typeError) { runtime.getGlobalVariables().set("$!", oldExc); // Restore $! } } time.dt = dt.withZone(getTimeZoneWithOffset(runtime, zone, offset)); return time; }
From source file:org.jvyamlb.SafeConstructorImpl.java
public static Object constructYamlTimestamp(final Constructor ctor, final Node node) { Matcher match = YMD_REGEXP.matcher(node.getValue().toString()); if (match.matches()) { final String year_s = match.group(1); final String month_s = match.group(2); final String day_s = match.group(3); DateTime dt = new DateTime(0, 1, 1, 0, 0, 0, 0); if (year_s != null) { dt = dt.withYear(Integer.parseInt(year_s)); }/*ww w .j a v a2 s. c o m*/ if (month_s != null) { dt = dt.withMonthOfYear(Integer.parseInt(month_s)); } if (day_s != null) { dt = dt.withDayOfMonth(Integer.parseInt(day_s)); } return new Object[] { dt }; } match = TIMESTAMP_REGEXP.matcher(node.getValue().toString()); if (!match.matches()) { return new Object[] { ctor.constructPrivateType(node) }; } final String year_s = match.group(1); final String month_s = match.group(2); final String day_s = match.group(3); final String hour_s = match.group(4); final String min_s = match.group(5); final String sec_s = match.group(6); final String fract_s = match.group(7); final String utc = match.group(8); final String timezoneh_s = match.group(9); final String timezonem_s = match.group(10); int usec = 0; if (fract_s != null) { usec = Integer.parseInt(fract_s); if (usec != 0) { while (10 * usec < 1000) { usec *= 10; } } } DateTime dt = new DateTime(0, 1, 1, 0, 0, 0, 0); if ("Z".equalsIgnoreCase(utc)) { dt = dt.withZone(DateTimeZone.forID("Etc/GMT")); } else { if (timezoneh_s != null || timezonem_s != null) { int zone = 0; int sign = +1; if (timezoneh_s != null) { if (timezoneh_s.startsWith("-")) { sign = -1; } zone += Integer.parseInt(timezoneh_s.substring(1)) * 3600000; } if (timezonem_s != null) { zone += Integer.parseInt(timezonem_s) * 60000; } dt = dt.withZone(DateTimeZone.forOffsetMillis(sign * zone)); } } if (year_s != null) { dt = dt.withYear(Integer.parseInt(year_s)); } if (month_s != null) { dt = dt.withMonthOfYear(Integer.parseInt(month_s)); } if (day_s != null) { dt = dt.withDayOfMonth(Integer.parseInt(day_s)); } if (hour_s != null) { dt = dt.withHourOfDay(Integer.parseInt(hour_s)); } if (min_s != null) { dt = dt.withMinuteOfHour(Integer.parseInt(min_s)); } if (sec_s != null) { dt = dt.withSecondOfMinute(Integer.parseInt(sec_s)); } dt = dt.withMillisOfSecond(usec / 1000); return new Object[] { dt, new Integer(usec % 1000) }; }
From source file:org.logstash.filters.parser.JodaParser.java
License:Apache License
private Instant parseAndGuessYear(DateTimeFormatter parser, String value) { // if we get here, we need to do some special handling at the time each event is handled // because things like the current year could be different, etc. DateTime dateTime; if (hasZone) { dateTime = parser.parseDateTime(value); } else {/* www . j av a 2s . c om*/ dateTime = parser.withZoneUTC().parseLocalDateTime(value).toDateTime(parser.getZone()); } // The time format we have has no year listed, so we'll have to guess the year. int month = dateTime.getMonthOfYear(); DateTime now = clock.read(); int eventYear; if (month == 12 && now.getMonthOfYear() == 1) { // Now is January, event is December. Assume it's from last year. eventYear = now.getYear() - 1; } else if (month == 1 && now.getMonthOfYear() == 12) { // Now is December, event is January. Assume it's from next year. eventYear = now.getYear() + 1; } else { // Otherwise, assume it's from this year. eventYear = now.getYear(); } return dateTime.withYear(eventYear).toInstant(); }