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

Correct parsing of DateTime in non-utc client timezone #83

Merged
merged 8 commits into from
May 5, 2021
Merged

Conversation

adamlofts
Copy link
Owner

@adamlofts adamlofts commented May 5, 2021

  • Fix parsing of timestamp
  • Still assume server is in UTC
  • Add test in non-utc timezone

Fix: #39
Fix: #71
Fix: #61

@adamlofts adamlofts changed the title Timezone Correct parsing of DateTime value May 5, 2021
@adamlofts adamlofts changed the title Correct parsing of DateTime value Correct parsing of DateTime in non-utc client timezone May 5, 2021
/// See: https://github.com/adamlofts/mysql1_dart/issues/39
static DateTime parseDateTimeInUtc(String s) {
var lt = DateTime.parse(s); // lt is local time.
return DateTime.utc(
Copy link

Choose a reason for hiding this comment

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

why not just return lt.add(lt.timeZoneOffset).toUtc();?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I find this expression a bit cryptic. Its not obviously correct to me at a glance. For example its not obvious to me that we should add the offset and not subtract it. Its not clearly more performant so I went for the other version.

/// Dart does not provide a simple way to do this.
/// See: https://github.com/adamlofts/mysql1_dart/issues/39
static DateTime parseDateTimeInUtc(String s) {
var lt = DateTime.parse(s); // lt is local time.
Copy link

Choose a reason for hiding this comment

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

instead of creating a variable called lt then adding a comment explaining what it means, why not just create a variable called localDateTime?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fair

@@ -63,7 +81,7 @@ class StandardDataPacket extends ResultRow {
case FIELD_TYPE_DATETIME: // datetime
case FIELD_TYPE_TIMESTAMP: // timestamp
var s = utf8.decode(list);
return DateTime.parse(s).toUtc();
return parseDateTimeInUtc(s);
Copy link

Choose a reason for hiding this comment

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

[Opinion] Why is this functioned off? I don't personally think the implementation is that complicated (especially if you simplify the function), the function isn't re-used and its name doesn't really add value to what's going on under the hood. I think you should probably just inline the code.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Because you can understand what is happening from just the function name. Otherwise we would need a comment to explain it.

@adamlofts adamlofts merged commit 7795932 into master May 5, 2021
@adamlofts adamlofts deleted the timezone branch May 5, 2021 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants