-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data-classes): Add appsync scalar_types_utils (#339)
Changes: - Move existing scalar util functions in to scalar_types_utils.py - Add support for timezone offset - Add docstrings for scalar utils - Update tests Closes #324
- Loading branch information
Michael Brewer
authored
Mar 15, 2021
1 parent
5daa45a
commit 542eb5b
Showing
3 changed files
with
111 additions
and
33 deletions.
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
aws_lambda_powertools/utilities/data_classes/appsync/resolver_utils.py
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
89 changes: 89 additions & 0 deletions
89
aws_lambda_powertools/utilities/data_classes/appsync/scalar_types_utils.py
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,89 @@ | ||
import datetime | ||
import time | ||
import uuid | ||
|
||
|
||
def _formatted_time(now: datetime.date, fmt: str, timezone_offset: int) -> str: | ||
"""String formatted time with optional timezone offset | ||
Parameters | ||
---------- | ||
now : datetime.date | ||
Current datetime with zero timezone offset | ||
fmt : str | ||
Data format before adding timezone offset | ||
timezone_offset : int | ||
Timezone offset in hours, defaults to 0 | ||
Returns | ||
------- | ||
str | ||
Returns string formatted time with optional timezone offset | ||
""" | ||
if timezone_offset == 0: | ||
return now.strftime(fmt + "Z") | ||
|
||
now = now + datetime.timedelta(hours=timezone_offset) | ||
fmt += "+" if timezone_offset > 0 else "-" | ||
fmt += str(abs(timezone_offset)).zfill(2) | ||
fmt += ":00:00" | ||
|
||
return now.strftime(fmt) | ||
|
||
|
||
def make_id() -> str: | ||
"""ID - A unique identifier for an object. This scalar is serialized like a String but isn't meant to be | ||
human-readable.""" | ||
return str(uuid.uuid4()) | ||
|
||
|
||
def aws_date(timezone_offset: int = 0) -> str: | ||
"""AWSDate - An extended ISO 8601 date string in the format YYYY-MM-DD. | ||
Parameters | ||
---------- | ||
timezone_offset : int | ||
Timezone offset, defaults to 0 | ||
Returns | ||
------- | ||
str | ||
Returns current time as AWSDate scalar string with optional timezone offset | ||
""" | ||
return _formatted_time(datetime.datetime.utcnow(), "%Y-%m-%d", timezone_offset) | ||
|
||
|
||
def aws_time(timezone_offset: int = 0) -> str: | ||
"""AWSTime - An extended ISO 8601 time string in the format hh:mm:ss.sss. | ||
Parameters | ||
---------- | ||
timezone_offset : int | ||
Timezone offset, defaults to 0 | ||
Returns | ||
------- | ||
str | ||
Returns current time as AWSTime scalar string with optional timezone offset | ||
""" | ||
return _formatted_time(datetime.datetime.utcnow(), "%H:%M:%S", timezone_offset) | ||
|
||
|
||
def aws_datetime(timezone_offset: int = 0) -> str: | ||
"""AWSDateTime - An extended ISO 8601 date and time string in the format YYYY-MM-DDThh:mm:ss.sssZ. | ||
Parameters | ||
---------- | ||
timezone_offset : int | ||
Timezone offset, defaults to 0 | ||
Returns | ||
------- | ||
str | ||
Returns current time as AWSDateTime scalar string with optional timezone offset | ||
""" | ||
return _formatted_time(datetime.datetime.utcnow(), "%Y-%m-%dT%H:%M:%S", timezone_offset) | ||
|
||
|
||
def aws_timestamp() -> int: | ||
"""AWSTimestamp - An integer value representing the number of seconds before or after 1970-01-01-T00:00Z.""" | ||
return int(time.time()) |
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