diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 23:56:48 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 23:56:48 +0300 |
commit | 48b19f2c9fec8a7e2f26ddd058c794d4f5a33894 (patch) | |
tree | 23f397fdad7a38ea2f7d3890593fddedd0572867 /vk/tracking/db/timestamp.py | |
parent | README update (diff) | |
download | vk-scripts-48b19f2c9fec8a7e2f26ddd058c794d4f5a33894.tar.gz vk-scripts-48b19f2c9fec8a7e2f26ddd058c794d4f5a33894.zip |
vk.utils.tracking -> vk.tracking
Diffstat (limited to 'vk/tracking/db/timestamp.py')
-rw-r--r-- | vk/tracking/db/timestamp.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/vk/tracking/db/timestamp.py b/vk/tracking/db/timestamp.py new file mode 100644 index 0000000..35cf5b3 --- /dev/null +++ b/vk/tracking/db/timestamp.py @@ -0,0 +1,34 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from datetime import datetime, timezone + +class Timestamp: + @staticmethod + def _new(): + return datetime.utcnow() + + @staticmethod + def _is_timezone_aware(dt): + return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None + + @staticmethod + def _lose_timezone(dt): + if Timestamp._is_timezone_aware(dt): + return dt.astimezone(timezone.utc).replace(tzinfo=None) + return dt + + def __init__(self, dt=None): + if dt is None: + dt = self._new() + dt = dt.replace(microsecond=0) + dt = self._lose_timezone(dt) + self.dt = dt + + @staticmethod + def from_string(s): + return Timestamp(datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')) + + def __str__(self): + return self.dt.isoformat() + 'Z' |