Skip to content

Commit

Permalink
Support timezone short IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
cavemandaveman committed Jul 10, 2018
1 parent 5543e39 commit c262fd2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions nifi-standardize-date-nar/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
<parent>
<groupId>com.nineteen04labs</groupId>
<artifactId>nifi-standardize-date-bundle</artifactId>
<version>18.07.1</version>
<version>18.07.2</version>
</parent>

<artifactId>nifi-standardize-date-nar</artifactId>
<version>18.07.1</version>
<version>18.07.2</version>
<packaging>nar</packaging>
<properties>
<maven.javadoc.skip>true</maven.javadoc.skip>
Expand All @@ -34,7 +34,7 @@
<dependency>
<groupId>com.nineteen04labs</groupId>
<artifactId>nifi-standardize-date-processors</artifactId>
<version>18.07.1</version>
<version>18.07.2</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion nifi-standardize-date-processors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>com.nineteen04labs</groupId>
<artifactId>nifi-standardize-date-bundle</artifactId>
<version>18.07.1</version>
<version>18.07.2</version>
</parent>

<artifactId>nifi-standardize-date-processors</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ public void process(InputStream in, OutputStream out) throws IOException {

String invalidDate = jsonParser.getText();
String invalidDateFormat = invalidDates.get(tokenString);
jsonGen.writeString(ManipulateDate.standardize(invalidDate, invalidDateFormat, timezone));
String standardizedDate;
try {
standardizedDate = ManipulateDate.standardize(invalidDate, invalidDateFormat, timezone);
} catch (Exception e) {
throw new ProcessException("Couldn't convert '" + invalidDate + "' with format '" + invalidDateFormat + "' with timezone '" + timezone + "'");
}
jsonGen.writeString(standardizedDate);

if (flowFormat == "AVRO")
newSchemaFields.add(new Schema.Field(newFieldName, Schema.create(Type.STRING), null, "null"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class StandardizeDateProperties {
public static final PropertyDescriptor TIMEZONE = new PropertyDescriptor
.Builder().name("TIMEZONE")
.displayName("Timezone")
.description("The originating timezone of the date fields in the FlowFile")
.description("The originating timezone of the date fields in the FlowFile. Short or standard IDs accepted (i.e. 'CST' or 'America/Chicago')")
.required(false)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@
public class ManipulateDate {

public static String standardize(String dateTime, String format, String timezone) {
DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern(format);
DateTimeFormatter dtFormat = null;
try {
dtFormat = DateTimeFormatter.ofPattern(format);
} catch (DateTimeParseException e) {
e.printStackTrace();
}

LocalDateTime localDT = null;
try {
localDT = LocalDateTime.parse(dateTime, dtFormat);
} catch (DateTimeParseException e) {
localDT = LocalDate.parse(dateTime, dtFormat).atStartOfDay();
} catch (Exception e) {
e.printStackTrace();
}

ZoneId localZone = ZoneId.of(timezone);
ZoneId localZone = null;
try {
localZone = ZoneId.of(ZoneId.SHORT_IDS.get(timezone));
} catch (NullPointerException e) {
localZone = ZoneId.of(timezone);
}

ZonedDateTime localZonedDT = ZonedDateTime.of(localDT, localZone);
ZonedDateTime standardizedDT = localZonedDT.withZoneSameInstant(ZoneOffset.UTC);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,22 @@ public void testStandardization() throws IOException {
outFile.assertContentEquals(processedFile);
}

@Test
public void testShortZoneId() throws IOException {
final Path processedFile = Paths.get("src/test/resources/processed.json");

runner.setProperty(StandardizeDateProperties.FLOW_FORMAT, "JSON");
runner.setProperty(StandardizeDateProperties.INVALID_DATES, "{\"bad_date\":\"MM/dd/yy\"}");
runner.setProperty(StandardizeDateProperties.TIMEZONE, "CST");

runner.enqueue(unprocessedFile);

runner.run();
runner.assertQueueEmpty();
runner.assertAllFlowFilesTransferred(StandardizeDateRelationships.REL_SUCCESS, 1);

final MockFlowFile outFile = runner.getFlowFilesForRelationship(StandardizeDateRelationships.REL_SUCCESS).get(0);

outFile.assertContentEquals(processedFile);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<groupId>com.nineteen04labs</groupId>
<artifactId>nifi-standardize-date-bundle</artifactId>
<version>18.07.1</version>
<version>18.07.2</version>
<packaging>pom</packaging>

<modules>
Expand Down

0 comments on commit c262fd2

Please sign in to comment.