|
9 | 9 | import os
|
10 | 10 | import sys
|
11 | 11 |
|
| 12 | +from datetime import datetime |
| 13 | + |
12 | 14 | from coverage import env
|
13 | 15 |
|
14 | 16 |
|
@@ -217,6 +219,30 @@ def __eq__(self, other):
|
217 | 219 | return self.__dict__ == other.__dict__
|
218 | 220 |
|
219 | 221 |
|
| 222 | +def format_local_datetime(dt): |
| 223 | + """Return a string with local timezone representing the date.""" |
| 224 | + try: |
| 225 | + return dt.astimezone().strftime('%Y-%m-%d %H:%M %z') |
| 226 | + except (TypeError, ValueError): |
| 227 | + # Class datetime.timezone introduced in Python 3.2 |
| 228 | + # Datetime.astimezone in Python 3.5 can not handle naive datetime |
| 229 | + import time |
| 230 | + def get_timezone_offset(): |
| 231 | + timestamp = time.time() |
| 232 | + delta = datetime.fromtimestamp(timestamp) - datetime.utcfromtimestamp(timestamp) |
| 233 | + if delta.seconds >= 0: |
| 234 | + sign = '+' |
| 235 | + seconds = delta.seconds |
| 236 | + else: |
| 237 | + sign = '-' |
| 238 | + seconds = - delta.seconds |
| 239 | + hours, rest = divmod(seconds, 60 * 60) |
| 240 | + minutes, _ = divmod(rest, 60) |
| 241 | + return '%s%02d%02d' % (sign, hours, minutes) |
| 242 | + offset = get_timezone_offset() |
| 243 | + return '%s %s' % (dt.strftime('%Y-%m-%d %H:%M'), offset) |
| 244 | + |
| 245 | + |
220 | 246 | def invalidate_import_caches():
|
221 | 247 | """Invalidate any import caches that may or may not exist."""
|
222 | 248 | if importlib and hasattr(importlib, "invalidate_caches"):
|
|
0 commit comments