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

Timestamptz should use toUtc().toIso8601String() #364

Closed
orestesgaolin opened this issue Oct 3, 2024 · 4 comments
Closed

Timestamptz should use toUtc().toIso8601String() #364

orestesgaolin opened this issue Oct 3, 2024 · 4 comments

Comments

@orestesgaolin
Copy link

There's a type timestamptz in PostgreSQL database that includes a tz information (timezone offset). When using it with default DateTime as type in build runner configuration, it may lead to errors where timezone gets lost.

build.yaml:

targets:
  $default:
    builders:
      graphql_codegen:
        options:
          scalars:
            timestamptz:
              type: DateTime

In Dart if DateTime is in the local timezone of the device the call to toIso8601String() results with a string without Z in the local timezone of the device. When sent via graphql mutation to a PostgreSQL database it will be interpreted as if this was in the UTC timezone.

  // in UTC+1 timezone
  final date = DateTime(2024,01,01,01);

  print(date.toUtc()); // 2024-01-01 00:00:00.000Z
  print(date.toIso8601String()); // 2024-01-01T01:00:00.000
  print(date.toUtc().toIso8601String()); // 2024-01-01T00:00:00.000Z

I suggest that graphql_codegen should include default timestamptz converter, e.g. something like:

DateTime timestamptzFromJson(String json) {
  return DateTime.parse(json);
}

String dateToJson(DateTime object) {
  return object.toUtc().toIso8601String();
}
targets:
  $default:
    builders:
      graphql_codegen:
        options:
          scalars:
            ISODateTime:
              type: DateTime
            timestamp:
              type: DateTime
            timestamptz:
              type: DateTime
              fromJsonFunctionName: timestamptzFromJson
              toJsonFunctionName: dateToJson
              import: package:my_package/my_package.dart
Copy link

github-actions bot commented Oct 3, 2024

👋 @orestesgaolin
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

@budde377
Copy link
Contributor

budde377 commented Oct 4, 2024

Thank you for creating this issue. Date-times are complex and very hard to generalise. I would advice anyone to review the DateTime provided and determine if it meets the requirements of their setup and implement appropriate parsers. Changing the underlying implementation might cause a lot of issues for existing users, and as I said are probably not going to work for everyone, so I am reluctant to change it.

@budde377 budde377 closed this as completed Oct 4, 2024
@orestesgaolin
Copy link
Author

Thanks for clarification, I understand your concern. By having the issue here I hope some other folks stumble upon it and make their converters aware of timezone.

Do you mind if I update the readme with one more example of converter?

@budde377
Copy link
Contributor

budde377 commented Oct 4, 2024

That would be very useful - please feel free!

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

No branches or pull requests

2 participants