Last active
September 20, 2021 23:17
-
-
Save declon/0749f2d8173f61a21e918b76364267ed to your computer and use it in GitHub Desktop.
AWS - Python Lambda Function - Set Local Time Zone
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
#!/usr/bin/env python3 | |
from pytz import timezone | |
from datetime import datetime | |
# Get timezone name from a config source ( For example an environment variable LOCAL_TZ='Australia/Sydney' ) | |
def get_local_date_string(time_zone: str) -> str: | |
"""Get Local Date String | |
Args: | |
time_zone (str): Unix timezone name (e.g 'Australia/Sydney') | |
Returns: | |
str: Localised and formatted datetime string | |
""" | |
# set time zone | |
local_zone = timezone(time_zone) | |
# localise current UTC time to timezone | |
utc_dt = datetime.utcnow() | |
local_time = utc_dt.astimezone(local_zone) | |
# format the local time to the desired format (update this as required) | |
fmt = "%Y-%m-%d-%H-%M" | |
local_time_fmt = local_time.strftime(fmt) | |
return local_time_fmt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment