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

fix(datastore): store time zone info in Temporal.DateTime #3393

Merged
merged 5 commits into from
Dec 11, 2023
Merged

Conversation

5d
Copy link
Contributor

@5d 5d commented Nov 30, 2023

Issue #

#1175

Description

  • add timezone information while encoding/decoding Temporal.DateTime to ISO8601 date string.

General Checklist

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
  • Security oriented best practices and standards are followed (e.g. using input sanitization, principle of least privilege, etc)
  • Documentation update for the change if required
  • PR title conforms to conventional commit style
  • New or updated tests include Given When Then inline code documentation and are named accordingly testThing_condition_expectation()
  • If breaking change, documentation/changelog update with migration instructions

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov-commenter
Copy link

codecov-commenter commented Nov 30, 2023

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (b9b5918) 68.17% compared to head (c792c45) 68.07%.
Report is 22 commits behind head on main.

Files Patch % Lines
...Categories/DataStore/Model/Temporal/Temporal.swift 60.00% 2 Missing ⚠️
...ataStore/Model/Internal/Model+DateFormatting.swift 0.00% 1 Missing ⚠️
...ify/Categories/DataStore/Model/Temporal/Date.swift 66.66% 1 Missing ⚠️
...Categories/DataStore/Model/Temporal/DateTime.swift 75.00% 1 Missing ⚠️
...ify/Categories/DataStore/Model/Temporal/Time.swift 66.66% 1 Missing ⚠️
.../DataStore/Model/Temporal/TimeZone+Extension.swift 98.85% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3393      +/-   ##
==========================================
- Coverage   68.17%   68.07%   -0.10%     
==========================================
  Files        1077     1078       +1     
  Lines       35973    36066      +93     
==========================================
+ Hits        24523    24551      +28     
- Misses      11450    11515      +65     
Flag Coverage Δ
API_plugin_unit_test 67.07% <ø> (ø)
AWSPluginsCore 64.59% <ø> (ø)
Amplify 48.58% <93.69%> (+0.77%) ⬆️
Analytics_plugin_unit_test 81.16% <ø> (ø)
Auth_plugin_unit_test 79.51% <ø> (+0.04%) ⬆️
CoreMLPredictions_plugin_unit_test 59.44% <ø> (ø)
DataStore_plugin_unit_test 79.91% <ø> (-1.06%) ⬇️
Geo_plugin_unit_test 70.75% <ø> (ø)
Logging_plugin_unit_test 63.09% <ø> (ø)
Predictions_plugin_unit_test 37.29% <ø> (ø)
PushNotifications_plugin_unit_test 87.03% <ø> (ø)
Storage_plugin_unit_test 78.00% <ø> (ø)
unit_tests 68.07% <93.69%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@5d 5d marked this pull request as ready for review December 1, 2023 22:00
@5d 5d requested a review from a team as a code owner December 1, 2023 22:00
import Foundation

extension TimeZone {
private static let iso8601TimeZoneHHColonMMColonSSRegex = try? NSRegularExpression(pattern: "^[+-]\\d{2}:\\d{2}:\\d{2}$")
Copy link
Contributor

Choose a reason for hiding this comment

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

In addition to HH:MM:SS, does AWSDateTime also support HHMMSS? HH:MMSS? HHMM:SS ? I think we should verify this and try to support it fully if it does not impact the performance of the app during parsing the input string

I suspect that HHMMSS is supported since the docs say "can optionally include a time zone offset" linking to the ISO 8601 wiki, while only providing "hh:mm:ss" as an example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From current amplify-js implementation, it only supports HH:MM:SS format.

Copy link
Contributor

Choose a reason for hiding this comment

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

that's surprising, I ran a few live tests

HH:MM:SS works as expected

mutation MyMutationHHColonMMColonSS {
  createTest(input: {userID: "123", dt: "2023-11-30T11:04:03+08:00:30"}) {
    dt
  }
}
{
  "data": {
    "createTest": {
      "dt": "2023-11-30T11:04:03+08:00:30"
    }
  }
}

HHMM does not work

mutation MyMutationHHMM {
  createTest(input: {userID: "123", dt: "2023-11-30T11:04:03+0800"}) {
    dt
  }
}

{
  "data": null,
  "errors": [
    {
      "path": null,
      "locations": [
        {
          "line": 2,
          "column": 14,
          "sourceName": null
        }
      ],
      "message": "Validation error of type WrongType: argument 'input.dt' with value 'StringValue{value='2023-11-30T11:04:03+0800'}' is not a valid 'AWSDateTime' @ 'createTest'"
    }
  ]
}

So expectedly, HHMMSS also does not work..

mutation MyMutationHHMMSS {
  createTest(input: {userID: "123", dt: "2023-11-30T11:04:03+080030"}) {
    dt
  }
}
{
  "data": null,
  "errors": [
    {
      "path": null,
      "locations": [
        {
          "line": 2,
          "column": 14,
          "sourceName": null
        }
      ],
      "message": "Validation error of type WrongType: argument 'input.dt' with value 'StringValue{value='2023-11-30T11:04:03+080030'}' is not a valid 'AWSDateTime' @ 'createTest'"
    }
  ]
}

HH:MM:SS works as expected

mutation MyMutationHHColonMMColonSS {
  createTest(input: {userID: "123", dt: "2023-11-30T11:04:03+08:00:30"}) {
    dt
  }
}

{
  "data": {
    "createTest": {
      "dt": "2023-11-30T11:04:03+08:00:30"
    }
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

So what we have now looks good to me.

  • Data that is AWSDateTime with HH:MM:SS will be decoded properly.

  • Data would never be stored in the format HHMM since AppSync doesn't allow it, so even though we support decoding HHMM, that code path won't ever be used. This means it may pose the inverse problem: AppSync type is more restrictive than the Amplify Swift type. We allow storing date times with offset containing HHMM but the mutation with AppSync will fail. I believe this is still the correct thing to do since it conforms to the iSO 8601 spec and we can have varying types of data sources, ie. SQLite / local-only use cases.

  • HHMMSS is not supported by AppSync AWSDateTime and not part of the ISO8601 spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for verifying this with AppSync.
We won't send date with string format HHMM or HH:MM:SS. We always send date to AppSync with string formatHH:MM code.

The format HHMM or HH:MM:SS are only the acceptable inputs. The code here is ensure we are able to decode these ISO8601 date formats to correct Tempral.DateTime instance. When encoding them back to string and submit to AppSync. They will always be in HH:MM format.

@5d 5d merged commit ad342ba into main Dec 11, 2023
76 checks passed
@5d 5d deleted the 5d/date-tz branch December 11, 2023 22:21
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.

3 participants