diff --git a/coverage/backward.py b/coverage/backward.py index 37b4916761..7217363f69 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -217,6 +217,40 @@ def __eq__(self, other): return self.__dict__ == other.__dict__ +try: + # Class datetime.timezone introduced in Python 3.2 + from datetime import timezone + del timezone + + from datetime import datetime + + def format_local_datetime(dt): + """Return a string with local timezone representing the date.""" + return dt.astimezone().strftime('%Y-%m-%d %H:%M %z') +except ImportError: + # Python 2 do not have builtin timezone + import time + from datetime import datetime + + def format_local_datetime(dt): + """Return a string with local timezone representing the date.""" + def get_timezone_offset(): + timestamp = time.time() + delta = datetime.fromtimestamp(timestamp) - datetime.utcfromtimestamp(timestamp) + if delta.seconds >= 0: + sign = '+' + seconds = delta.seconds + else: + sign = '-' + seconds = - delta.seconds + hours, rest = divmod(seconds, 60 * 60) + minutes, _ = divmod(rest, 60) + return '%s%02d%02d' % (sign, hours, minutes) + + offset = get_timezone_offset() + return '%s %s' % (dt.strftime('%Y-%m-%d %H:%M'), offset) + + def invalidate_import_caches(): """Invalidate any import caches that may or may not exist.""" if importlib and hasattr(importlib, "invalidate_caches"): diff --git a/coverage/html.py b/coverage/html.py index a8aa3f6549..3596bbe1d9 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -11,7 +11,7 @@ import coverage from coverage import env -from coverage.backward import iitems, SimpleNamespace +from coverage.backward import iitems, SimpleNamespace, format_local_datetime from coverage.data import add_data_to_hash from coverage.files import flat_rootname from coverage.misc import CoverageException, ensure_dir, file_be_gone, Hasher, isolate_module @@ -200,7 +200,7 @@ def __init__(self, cov): '__url__': coverage.__url__, '__version__': coverage.__version__, 'title': title, - 'time_stamp': datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M %z'), + 'time_stamp': format_local_datetime(datetime.datetime.now()), 'extra_css': self.extra_css, 'has_arcs': self.has_arcs, 'show_contexts': self.config.show_contexts,