Skip to content

Commit

Permalink
added function utc_to_local (#38)
Browse files Browse the repository at this point in the history
* added function utc_to_local
* corrected utc_to_local, corrected version, added test for utc_to_local
* deleted extra libraries from requirements-dev.txt, added version to tzlocal

Co-authored-by: yaroslav.kudrin <Iaroslav_Kudrin@epam.com>
  • Loading branch information
kudrinyaroslav and yaroslav.kudrin authored May 4, 2021
1 parent 8cccf72 commit 7e42508
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Reformat with Black
8cccf72646d604a342991af64762b0026c673ec2
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pyOpenSSL>=18.0.0
cryptography==3.3.2; python_version == '2.7'
beautifulsoup4>=4.6.0
lxml>=4.3.0
pytz==2021.1
tzlocal==2.1
14 changes: 14 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
super,
zip,
)
from datetime import datetime
from os import path
import sys

Expand Down Expand Up @@ -244,6 +245,19 @@ def test_collect_integer(monkeypatch, value, expected):
assert helpers.collect_integer(10) == expected


def test_utc_to_local():
"""Check if passed utc datestamp becomes local one."""
import pytz
from tokendito import helpers
from tzlocal import get_localzone

utc = datetime.now(pytz.utc)
local_time = utc.replace(tzinfo=pytz.utc).astimezone(tz=get_localzone())
local_time = local_time.strftime("%Y-%m-%d %H:%M:%S %Z")

assert helpers.utc_to_local(utc) == local_time


def test_prepare_payload():
"""Check if values passed return in a dictionary."""
from tokendito import helpers
Expand Down
2 changes: 1 addition & 1 deletion tokendito/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# vim: set filetype=python ts=4 sw=4
# -*- coding: utf-8 -*-
"""tokendito version."""
__version__ = "1.1.0"
__version__ = "1.1.1"
__title__ = "tokendito"
__description__ = "Get AWS STS tokens from Okta SSO"
__long_description_content_type__ = "text/x-rst"
Expand Down
19 changes: 18 additions & 1 deletion tokendito/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
from bs4 import __version__ as __bs4_version__
from bs4 import BeautifulSoup
from future import standard_library
import pytz
import requests
from requests import __version__ as __requests_version__
from tokendito import settings
from tokendito.__version__ import __version__
from tzlocal import get_localzone


standard_library.install_aliases()

Expand Down Expand Up @@ -147,6 +150,18 @@ def setup(args):
return parsed_args


def utc_to_local(utc_dt):
"""Convert UTC time into local time.
:param:utc_str:datetime
:return:local_time:string
"""
local_time = utc_dt.replace(tzinfo=pytz.utc).astimezone(tz=get_localzone())
local_time = local_time.strftime("%Y-%m-%d %H:%M:%S %Z")

return local_time


def to_unicode(bytestring):
"""Convert a string into a Unicode compliant object.
Expand Down Expand Up @@ -342,19 +357,21 @@ def print_selected_role(profile_name, expiration_time):
:return:
"""
expiration_time_local = utc_to_local(expiration_time)
msg = (
"\nGenerated profile '{}' in {}.\n"
"\nUse profile to authenticate to AWS:\n\t"
"aws --profile '{}' sts get-caller-identity"
"\nOR\n\t"
"export AWS_PROFILE='{}'\n\n"
"Credentials are valid until {}."
"Credentials are valid until {} ({})."
).format(
profile_name,
settings.aws_shared_credentials_file,
profile_name,
profile_name,
expiration_time,
expiration_time_local,
)

return print(msg)
Expand Down

0 comments on commit 7e42508

Please sign in to comment.