-
Notifications
You must be signed in to change notification settings - Fork 202
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,6 @@ extension TemporalSpec { | |
""" | ||
) | ||
} | ||
return Self.init(date) | ||
return Self.init(date, timeZone: timeZone) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
Amplify/Categories/DataStore/Model/Temporal/TimeZone+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
extension TimeZone { | ||
private static let iso8601TimeZoneHHColonMMColonSSRegex = try? NSRegularExpression(pattern: "^[+-]\\d{2}:\\d{2}:\\d{2}$") | ||
private static let iso8601TimeZoneHHColonMMRegex = try? NSRegularExpression(pattern: "^[+-]\\d{2}:\\d{2}$") | ||
private static let iso8601TimeZoneHHMMRegex = try? NSRegularExpression(pattern: "^[+-]\\d{2}\\d{2}$") | ||
private static let iso8601TimeZoneHHRegex = try? NSRegularExpression(pattern: "^[+-]\\d{2}$") | ||
lawmicha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// ±hh:mm:ss is not a standard of ISO8601 date format, but it's supported by AWSDateTime. | ||
/// https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html | ||
private enum ISO8601TimeZonePart { | ||
case utc | ||
case hhmmss(hours: Int, minutes: Int, seconds: Int) | ||
case hhmm(hours: Int, minutes: Int) | ||
case hh(hours: Int) | ||
|
||
init?(iso8601DateString: String) { | ||
func hasMatch(regex: NSRegularExpression?, str: String) -> Bool { | ||
return regex.flatMap { | ||
$0.firstMatch(in: str, range: NSRange(location: 0, length: str.count)) | ||
} != nil | ||
} | ||
|
||
// <time>±hh:mm:ss | ||
func suffixHHColonMMColonSS() -> String? { | ||
if iso8601DateString.count > 9 { | ||
let tz = String(iso8601DateString.dropFirst(iso8601DateString.count - 9)) | ||
if hasMatch(regex: TimeZone.iso8601TimeZoneHHColonMMColonSSRegex, str: tz) { | ||
return tz | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// <time>±hh:mm | ||
func suffixHHColonMM() -> String? { | ||
if iso8601DateString.count > 6 { | ||
let tz = String(iso8601DateString.dropFirst(iso8601DateString.count - 6)) | ||
if hasMatch(regex: TimeZone.iso8601TimeZoneHHColonMMRegex, str: tz) { | ||
return tz | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// <time>±hhmm | ||
func suffixHHMM() -> String? { | ||
if iso8601DateString.count > 5 { | ||
let tz = String(iso8601DateString.dropFirst(iso8601DateString.count - 5)) | ||
if hasMatch(regex: TimeZone.iso8601TimeZoneHHMMRegex, str: tz) { | ||
return tz | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// <time>±hh | ||
func suffixHH() -> String? { | ||
if iso8601DateString.count > 3 { | ||
let tz = String(iso8601DateString.dropFirst(iso8601DateString.count - 3)) | ||
if hasMatch(regex: TimeZone.iso8601TimeZoneHHRegex, str: tz) { | ||
return tz | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
if iso8601DateString.hasPrefix("Z") { // <time>Z | ||
self = .utc | ||
return | ||
} | ||
|
||
if let tz = suffixHHColonMM(), | ||
let hours = Int(tz.dropLast(3)), | ||
let minutes = Int(tz.dropFirst(4)) | ||
{ | ||
self = .hhmm(hours: hours, minutes: minutes) | ||
return | ||
} | ||
|
||
if let tz = suffixHHMM(), | ||
let hours = Int(tz.dropLast(2)), | ||
let minutes = Int(tz.dropFirst(3)) | ||
{ | ||
self = .hhmm(hours: hours, minutes: minutes) | ||
return | ||
} | ||
|
||
if let tz = suffixHH(), | ||
let hours = Int(tz) | ||
{ | ||
self = .hh(hours: hours) | ||
return | ||
} | ||
|
||
if let tz = suffixHHColonMMColonSS(), | ||
let hours = Int(tz.dropLast(6)), | ||
let minutes = Int(tz.dropFirst(4).dropLast(3)), | ||
let seconds = Int(tz.dropFirst(7)) | ||
{ | ||
self = .hhmmss(hours: hours, minutes: minutes, seconds: seconds) | ||
return | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
/// https://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators | ||
@usableFromInline | ||
internal init?(iso8601DateString: String) { | ||
switch ISO8601TimeZonePart(iso8601DateString: iso8601DateString) { | ||
case .some(.utc): | ||
self.init(abbreviation: "UTC") | ||
case let .some(.hh(hours: hours)): | ||
self.init(secondsFromGMT: hours * 60 * 60) | ||
case let .some(.hhmm(hours: hours, minutes: minutes)): | ||
self.init(secondsFromGMT: hours * 60 * 60 + | ||
(hours > 0 ? 1 : -1) * minutes * 60) | ||
lawmicha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case let .some(.hhmmss(hours: hours, minutes: minutes, seconds: seconds)): | ||
self.init(secondsFromGMT: hours * 60 * 60 + | ||
(hours > 0 ? 1 : -1) * minutes * 60 + | ||
(hours > 0 ? 1 : -1) * seconds) | ||
case .none: | ||
return nil | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
HHMM does not work
So expectedly, HHMMSS also does not work..
HH:MM:SS works as expected
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
orHH:MM:SS
. We always send date to AppSync with string formatHH:MM
code.The format
HHMM
orHH: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 inHH:MM
format.