Source code for cup.timeplus
#!/usr/bin/python
# -*- coding: utf-8 -*
# Copyright: [CUP] - See LICENSE for details.
# Authors: Guannan Ma (@mythmgn),
"""
:desc:
time related module. looking forward to accepting new patches
"""
from __future__ import print_function
import time
import datetime
import pytz
__all__ = ['get_str_now', 'TimePlus']
[docs]def get_str_now(fmt='%Y-%m-%d-%H-%M-%S'):
"""
return string of 'now'
:param fmt:
print-format, '%Y-%m-%d-%H-%M-%S' by default
"""
return str(time.strftime(fmt, time.localtime()))
[docs]class TimePlus(object):
"""
TimePlus can convert printable time from 1 timezone 2 another including
utc.
Code Example:
::
from __future__ import print_function
from cup import timeplus
import pytz
tz_shanghai = pytz.timezone('Aisa/Shanghai')
tplus = timeplus.TimePlus(tz_shanghai)
dtobj = datetime.datetime.now()
print(tplus.utc2local(dtobj))
"""
def __init__(self, timezone):
"""
initialize with timezone setup
"""
if not isinstance(timezone, pytz.BaseTzInfo):
raise ValueError('not a object of pytz.timezone("xxx/xxx")')
self._timezone = timezone
self._utc_tz = pytz.timezone('UTC')
[docs] def get_timezone(self):
"""
return current pytz timezone object
"""
return self._timezone
[docs] def set_newtimezone(self, pytz_timezone):
"""
refresh timezone
:return:
True if refreshing is done. False otherwise
"""
self._timezone = pytz_timezone
[docs] @classmethod
def utc_now(cls):
"""return utc_now"""
return datetime.datetime.now(pytz.UTC)
[docs] def local2utc(self, dateobj):
"""
local timezone to utc conversion
:return:
a datetime.datetime object with utc timezone enabled
:raise:
ValueError if dateobj is not a datetime.datetime object
"""
if not isinstance(dateobj, datetime.datetime):
raise ValueError('dateobj is not a datetime.datetime')
return dateobj.astimezone(self._utc_tz)
[docs] def utc2local(self, dateobj):
"""
utc datetime to local timezone datetime.datetime
"""
if not isinstance(dateobj, datetime.datetime):
raise ValueError('dateobj is not a datetime.datetime')
return dateobj.astimezone(self._timezone)
def sleep(seconds):
"""sleep xx seconds; more accurate than time.sleep"""
start = time.time()
left = seconds
while True:
time.sleep(left)
left = seconds - (time.time() - start)
if left <= 0:
break
if __name__ == '__main__':
print(get_str_now())