dan-s1 commented on code in PR #9196:
URL: https://github.com/apache/nifi/pull/9196#discussion_r1759006593
##########
nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/field/ObjectTimestampFieldConverter.java:
##########
@@ -39,7 +57,80 @@ class ObjectTimestampFieldConverter implements
FieldConverter<Object, Timestamp>
*/
@Override
public Timestamp convertField(final Object field, final Optional<String>
pattern, final String name) {
- final LocalDateTime localDateTime = CONVERTER.convertField(field,
pattern, name);
- return localDateTime == null ? null : Timestamp.valueOf(localDateTime);
+ Instant instant = null;
+ switch (field) {
+ case null -> {
+ return null;
+ }
+ case Timestamp timestamp -> {
+ return timestamp;
+ }
+ case ZonedDateTime zonedDateTime -> {
+ instant = zonedDateTime.toInstant();
+ }
+ case Time time -> {
+ // Convert to an Instant object preserving millisecond
precision
+ final long epochMilli = time.getTime();
+ instant = Instant.ofEpochMilli(epochMilli);
+ }
+ case Date date -> {
+ final long epochMilli = date.getTime();
+ instant = Instant.ofEpochMilli(epochMilli);
+ }
+ case Number number -> {
+ switch (field) {
+ case Double d -> instant =
FractionalSecondsUtils.toInstant(d);
+ case Float f -> instant =
FractionalSecondsUtils.toInstant(f.doubleValue());
+ default -> instant =
FractionalSecondsUtils.toInstant(number.longValue());
+ }
+ }
+ case String string -> {
+ final String stringTrimmed = string.trim();
+ if (stringTrimmed.isEmpty()) {
+ return null;
+ }
+
+ if (pattern.isPresent()) {
+ final String patternString = pattern.get();
+ final DateTimeFormatter formatter =
DateTimeFormatterRegistry.getDateTimeFormatter(patternString);
+ try {
+ // NOTE: In order to calculate any possible timezone
offsets, the string must be parsed as a ZoneDateTime.
+ // It is not possible to always parse as a
ZoneDateTime as it will fail if the pattern has
+ // no timezone information. Hence, a regular
expression is used to determine whether it is necessary
+ // to parse with ZoneDateTime or not.
+ final Matcher matcher =
TIMEZONE_PATTERN.matcher(patternString);
+ final ZonedDateTime zonedDateTime;
+
+ if (matcher.find()) {
+ zonedDateTime = ZonedDateTime.parse(stringTrimmed,
formatter);
+ } else {
+ final LocalDateTime localDateTime =
LocalDateTime.parse(stringTrimmed, formatter);
+ zonedDateTime = ZonedDateTime.of(localDateTime,
ZoneId.systemDefault());
Review Comment:
I thought I needed this step in order to keep track of the right timezone. I
added similar logic in `ObjectStringFieldConverter` although I did not use an
`Instant` as there is only formatting to a string there. I then have a single
return at lines 122-124 where the `instant` variable is converted to a
`Timestamp` .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]