Skip to content

Commit 94c4bb7

Browse files
committed
Update WebexTeamsDateTime
Attempting to apply a timezone to a naive datetime (no tzinfo) raises a Value error in Python v2.7 and v3.5. ValueError: astimezone() cannot be applied to a naive datetime Check to see if the datetime has time zone information (tzinfo). If it has timezone info, use the `astimezone()` method to adjust it to ZuluTime. If it does not have timezone info, assume the time is UTC time and add the ZuluTimeZone (raise a warning when this happens).
1 parent b63a6db commit 94c4bb7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

webexteamssdk/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
)
3232

3333
from future import standard_library
34+
3435
standard_library.install_aliases()
3536
native_str = str
3637

@@ -39,8 +40,9 @@
3940
import os
4041
import sys
4142
import urllib.parse
43+
import warnings
4244
from builtins import *
43-
from collections import OrderedDict, namedtuple
45+
from collections import namedtuple, OrderedDict
4446
from datetime import datetime, timedelta, tzinfo
4547

4648
from past.builtins import basestring
@@ -309,7 +311,15 @@ def strftime(self, fmt=WEBEX_TEAMS_DATETIME_FORMAT):
309311

310312
def __str__(self):
311313
"""Human readable string representation of this WebexTeamsDateTime."""
312-
dt = self.astimezone(ZuluTimeZone())
314+
if self.tzinfo:
315+
dt = self.astimezone(ZuluTimeZone())
316+
else:
317+
warnings.warn(UserWarning(
318+
"Datetime {} does not have an associated timezone; assuming it"
319+
"should be UTC/Zulu.".format(repr(self))
320+
))
321+
dt = self.replace(tzinfo=ZuluTimeZone())
322+
313323
return dt.strftime("%Y-%m-%dT%H:%M:%S.{:0=3}%Z").format(
314324
self.microsecond // 1000
315325
)

0 commit comments

Comments
 (0)