Skip to content
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

Correct expiration epoch time #18026

Merged
merged 8 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Leoaqr marked this conversation as resolved.
Show resolved Hide resolved
from msrest.serialization import TZ_UTC
from azure.core.credentials import AccessToken


def _convert_datetime_to_utc_int(expires_on):
Leoaqr marked this conversation as resolved.
Show resolved Hide resolved
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
Leoaqr marked this conversation as resolved.
Show resolved Hide resolved
Leoaqr marked this conversation as resolved.
Show resolved Hide resolved
from datetime import datetime
import dateutil.tz
Leoaqr marked this conversation as resolved.
Show resolved Hide resolved

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,18 +6,17 @@

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,18 +6,17 @@

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