Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zoned time when parse string #341

Closed

Conversation

IlyaTsoi
Copy link
Contributor

I suggest to use ZonedDateTime instead of LocalDateTime. So we can parse a time_string with an offset marker e.g. +03:00, -0130, Z. Also we can handle an exception when a dateTime string has invalid year e.g. in MSSQL "0000-00-01 00:00:00". Zero year value is invalid in java.

IlyaTsoi and others added 2 commits October 24, 2023 23:49
It would be better if it didn't depend on the hard-coded <server>.<database>.<table> template
…rse a time_string with an offset marker e.g. +03:00, -0130, Z.

Also we can handle an exception when a dateTime string has invalid year e.g. in MSSQL "0000-00-01 00:00:00". Zero year value is invalid in java.
@IlyaTsoi
Copy link
Contributor Author

Maybe it can solve #215?

if(parsingSuccesful == false) {

if (parsingSuccesful) {
if (setMinDate) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they are both true, why is there a need to have 2 variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we get invalid year exception we can handle it by setting the setMinDate flag. So we don't get a date by parsing, but use the setMinDate flag to set it to minimum date. If this error occurs then we can safely assume that the date was not parsed due to zero year value

parsingSuccesful = true;
break;
} catch(Exception e) {
if (e.getMessage().contains("Invalid value for YearOfEra")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like a good idea to check for exception using the message(Message could change in the future), isnt there a specific exception if parsing fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, i will check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have rewritten a bit this part. It's possible to use getCause() method that inherited from Throwable to define if it's either a DateTimeParseException error or a DateTimeException. If getCause() returns null - it's a parsing error, otherwise not.

@subkanthi
Copy link
Collaborator

Please add to this test case like this

    @Test
    public void testZonedTimestampConverter() {

        String formattedTimestamp = DebeziumConverter.ZonedTimestampConverter.convert("2021-12-31T19:01:00Z");
        Assert.assertTrue(formattedTimestamp.equalsIgnoreCase("2021-12-31 19:01:00.000000"));

        String formattedTimestampWMicroSeconds = DebeziumConverter.ZonedTimestampConverter.convert("2038-01-19T03:14:07.999999Z");
        Assert.assertTrue(formattedTimestampWMicroSeconds.equalsIgnoreCase("2038-01-19 03:14:07.999999"));

        String formattedTimestamp3 = DebeziumConverter.ZonedTimestampConverter.convert("2038-01-19T03:14:07.99Z");
        Assert.assertTrue(formattedTimestamp3.equalsIgnoreCase("2038-01-19 03:14:07.990000"));

        String formattedTimestamp4 = DebeziumConverter.ZonedTimestampConverter.convert("0000-00-01 00:00:00");
        System.out.println(formattedTimestamp4);
    }

if(splitName.length == 3) {
tableName = splitName[2];
}
tableName = splitName[splitName.length - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove these changes for topic.prefix

LocalDateTime zd = LocalDateTime.parse((String) value, formatter);
result = zd.format(destFormatter);
//result = removeTrailingZeros(result);
parsedDT = ZonedDateTime.parse((String) value, formatter.withZone(ZoneId.of("UTC")));
parsingSuccesful = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, parsingSuccessful

@subkanthi
Copy link
Collaborator

This was implemented as part of other PR's , closing this for now.

@subkanthi subkanthi closed this Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants