From 9c40c32e95c3f4fe4b592bc2269f3ec80475c269 Mon Sep 17 00:00:00 2001 From: c727 Date: Sat, 17 Feb 2018 16:13:23 +0100 Subject: [PATCH] Add timezone to snapshot timestamp (#360) * Add timezone to snapshot timestamp ``` old: 2018-02-14T15:13:46.391829 new: 2018-02-14T15:13:46.391829+00:00 ``` * Update __init__.py * Move code to dt util * Lint * Lint 2 * Update dt.py * Update __init__.py --- hassio/snapshots/__init__.py | 4 ++-- hassio/utils/dt.py | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hassio/snapshots/__init__.py b/hassio/snapshots/__init__.py index 1911c0444..dd8808b7c 100644 --- a/hassio/snapshots/__init__.py +++ b/hassio/snapshots/__init__.py @@ -1,6 +1,5 @@ """Snapshot system control.""" import asyncio -from datetime import datetime import logging from pathlib import Path @@ -9,6 +8,7 @@ from .utils import create_slug from ..const import ( FOLDER_HOMEASSISTANT, SNAPSHOT_FULL, SNAPSHOT_PARTIAL) from ..coresys import CoreSysAttributes +from ..utils.dt import utcnow _LOGGER = logging.getLogger(__name__) @@ -33,7 +33,7 @@ class SnapshotManager(CoreSysAttributes): def _create_snapshot(self, name, sys_type, password): """Initialize a new snapshot object from name.""" - date_str = datetime.utcnow().isoformat() + date_str = utcnow().isoformat() slug = create_slug(name, date_str) tar_file = Path(self._config.path_backup, f"{slug}.tar") diff --git a/hassio/utils/dt.py b/hassio/utils/dt.py index 849fc1a3f..fb5033a88 100644 --- a/hassio/utils/dt.py +++ b/hassio/utils/dt.py @@ -8,6 +8,8 @@ import aiohttp import async_timeout import pytz +UTC = pytz.utc + _LOGGER = logging.getLogger(__name__) FREEGEOIP_URL = "https://freegeoip.io/json/" @@ -61,7 +63,7 @@ def parse_datetime(dt_str): tzinfo = None # type: Optional[dt.tzinfo] if tzinfo_str == 'Z': - tzinfo = pytz.utc + tzinfo = UTC elif tzinfo_str is not None: offset_mins = int(tzinfo_str[-2:]) if len(tzinfo_str) > 3 else 0 offset_hours = int(tzinfo_str[1:3]) @@ -74,3 +76,8 @@ def parse_datetime(dt_str): kws = {k: int(v) for k, v in kws.items() if v is not None} kws['tzinfo'] = tzinfo return datetime(**kws) + + +def utcnow(): + """Returns current timestamp including timezone.""" + return datetime.now(UTC)