Hi here.

I have a question about fromLogical method in
connect/api/src/main/java/org/apache/kafka/connect/data/Date.java

below code does these step.
1. calendar with UTC timezone
2. set value
3. check hour, minute, second, millisecond are 0. if not throw DataException.

```
public static int fromLogical(Schema schema, java.util.Date value) {
    if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME)))
        throw new DataException("Requested conversion of Date object
but the schema does not match.");
    Calendar calendar = Calendar.getInstance(UTC);
    calendar.setTime(value);
    if (calendar.get(Calendar.HOUR_OF_DAY) != 0 ||
calendar.get(Calendar.MINUTE) != 0 ||
            calendar.get(Calendar.SECOND) != 0 ||
calendar.get(Calendar.MILLISECOND) != 0) {
        throw new DataException("Kafka Connect Date type should not
have any time fields set to non-zero values.");
    }
    long unixMillis = calendar.getTimeInMillis();
    return (int) (unixMillis / MILLIS_PER_DAY);
}
```

but, if value is not made by UTC, for if we read value from other
Timezone, the Date can be represented other value, because timezone
will make difference.

so I think this code cause failure when different timezone.

I think revising with timezone info is a way to solve this.
```
public static int fromLogical(Schema schema, java.util.Date value) {
    if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME)))
        throw new DataException("Requested conversion of Date object
but the schema does not match.");
    //Using DefaultTimeZone
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    calendar.setTime(value);
    if (calendar.get(Calendar.HOUR_OF_DAY) != 0 ||
calendar.get(Calendar.MINUTE) != 0 ||
            calendar.get(Calendar.SECOND) != 0 ||
calendar.get(Calendar.MILLISECOND) != 0) {
        throw new DataException("Kafka Connect Date type should not
have any time fields set to non-zero values.");
    }

    //Calculate timezone difference as reviseMillis
   long reviseMillis = calendar.get(Calendar.ZONE_OFFSET) +
calendar.get(Calendar.DST_OFFSET) / 60;

    //revising with reviseMillis
    long unixMillis = calendar.getTimeInMillis() + reviseMillis;
    return (int) (unixMillis / MILLIS_PER_DAY);
}

public static java.util.Date toLogical(Schema schema, int value) {
    if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME)))
        throw new DataException("Requested conversion of Date object
but the schema does not match.");

    //Calculate timezone difference as reviseMillis
   Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    long reviseMillis = calendar.get(Calendar.ZONE_OFFSET) +
calendar.get(Calendar.DST_OFFSET) / 60;
    //revising with reviseMillis
   return new java.util.Date(value * MILLIS_PER_DAY - reviseMillis);
}
```

But I wonder, is there some reason to force this work?
to check not to have any time fields set to non-zero values?

What do you think guys?

Reply via email to