Skip to content

Commit

Permalink
Correct expiration epoch time (#18026)
Browse files Browse the repository at this point in the history
* Correct expiration epoch time

* Add pydoc and address comments

* Add UTs

* Update in shared lib

* Update in shared lib

* Fix lint issue

* Update utils.py

* Update dev_requirements.txt

Co-authored-by: Leo Li <jixli@microsoft.com>
  • Loading branch information
Leoaqr and Leo Li authored Apr 17, 2021
1 parent a5d0e97 commit 66f11d7
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@

import base64
import json
import time
from typing import ( # pylint: disable=unused-import
cast,
Tuple,
)
from datetime import datetime
import calendar
from msrest.serialization import TZ_UTC
from azure.core.credentials import AccessToken


def _convert_datetime_to_utc_int(expires_on):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
return epoch-time.mktime(expires_on.timetuple())
"""
Converts DateTime in local time to the Epoch in UTC in second.
:param input_datetime: Input datetime
:type input_datetime: datetime
:return: Integer
:rtype: int
"""
return int(calendar.timegm(expires_on.utctimetuple()))

def parse_connection_str(conn_str):
# type: (str) -> Tuple[str, str, str, str]
Expand Down Expand Up @@ -54,7 +60,7 @@ def get_current_utc_time():

def get_current_utc_as_int():
# type: () -> int
current_utc_datetime = datetime.utcnow().replace(tzinfo=TZ_UTC)
current_utc_datetime = datetime.utcnow()
return _convert_datetime_to_utc_int(current_utc_datetime)

def create_access_token(token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
../azure-communication-nspkg
-e ../azure-communication-identity
aiohttp>=3.0; python_version >= '3.5'
-e ../../../tools/azure-devtools
-e ../../../tools/azure-devtools
python-dateutil>=2.8.1; python_version >= '3.5'
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from datetime import datetime
import dateutil.tz

from azure.communication.chat._shared.utils import(
_convert_datetime_to_utc_int
)

class UtilsTest(unittest.TestCase):

def test_convert_datetime_to_utc_int(self):
# UTC
utc_time_in_sec = _convert_datetime_to_utc_int(datetime(1970, 1, 1, 0, 0, 0, 0, tzinfo=dateutil.tz.tzutc()))
assert utc_time_in_sec == 0
# PST is UTC-8
pst_time_in_sec = _convert_datetime_to_utc_int(datetime(1970, 1, 1, 0, 0, 0, 0, tzinfo=dateutil.tz.gettz('America/Vancouver')))
assert pst_time_in_sec == 8 * 3600
# EST is UTC-5
est_time_in_sec = _convert_datetime_to_utc_int(datetime(1970, 1, 1, 0, 0, 0, 0, tzinfo=dateutil.tz.gettz('America/New_York')))
assert est_time_in_sec == 5 * 3600
# CST is UTC+8
cst_time_in_sec = _convert_datetime_to_utc_int(datetime(1970, 1, 1, 0, 0, 0, 0, tzinfo=dateutil.tz.gettz('Asia/Shanghai')))
assert cst_time_in_sec == -8 * 3600

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@

from unittest_helpers import mock_response
from datetime import datetime
import calendar

try:
from unittest.mock import Mock, patch
except ImportError: # python < 3.3
from mock import Mock, patch # type: ignore

def _convert_datetime_to_utc_int(input):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
input_datetime_as_int = epoch - time.mktime(input.timetuple())
return input_datetime_as_int
return int(calendar.timegm(input.utctimetuple()))

class TestChatClient(unittest.TestCase):
@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@

import pytest
import time

import calendar

def _convert_datetime_to_utc_int(input):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
input_datetime_as_int = epoch - time.mktime(input.timetuple())
return input_datetime_as_int
return int(calendar.timegm(input.utctimetuple()))


async def mock_get_token():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unittest
import time

import calendar
from datetime import datetime
from msrest.serialization import TZ_UTC
from azure.core.credentials import AccessToken
Expand All @@ -26,9 +27,7 @@
from mock import Mock, patch # type: ignore

def _convert_datetime_to_utc_int(input):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
input_datetime_as_int = epoch - time.mktime(input.timetuple())
return input_datetime_as_int
return int(calendar.timegm(input.utctimetuple()))

class TestChatThreadClient(unittest.TestCase):
@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@

import pytest
import time

import calendar

def _convert_datetime_to_utc_int(input):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
input_datetime_as_int = epoch - time.mktime(input.timetuple())
return input_datetime_as_int
return int(calendar.timegm(input.utctimetuple()))


async def mock_get_token():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
Tuple,
)
from datetime import datetime
import calendar
from msrest.serialization import TZ_UTC
from azure.core.credentials import AccessToken

def _convert_datetime_to_utc_int(expires_on):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
return epoch-time.mktime(expires_on.timetuple())
return int(calendar.timegm(expires_on.utctimetuple()))

def parse_connection_str(conn_str):
# type: (str) -> Tuple[str, str, str, str]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@

import base64
import json
import time
from typing import ( # pylint: disable=unused-import
cast,
Tuple,
)
from datetime import datetime
import calendar
from msrest.serialization import TZ_UTC
from azure.core.credentials import AccessToken


def _convert_datetime_to_utc_int(expires_on):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
return epoch-time.mktime(expires_on.timetuple())
return int(calendar.timegm(expires_on.utctimetuple()))

def parse_connection_str(conn_str):
# type: (str) -> Tuple[str, str, str, str]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@

import base64
import json
import time
from typing import ( # pylint: disable=unused-import
cast,
Tuple,
)
from datetime import datetime
import calendar
from msrest.serialization import TZ_UTC
from azure.core.credentials import AccessToken


def _convert_datetime_to_utc_int(expires_on):
epoch = time.mktime(datetime(1970, 1, 1).timetuple())
return epoch-time.mktime(expires_on.timetuple())
return int(calendar.timegm(expires_on.utctimetuple()))

def parse_connection_str(conn_str):
# type: (str) -> Tuple[str, str, str, str]
Expand Down

0 comments on commit 66f11d7

Please sign in to comment.