def set_timezone():
"""Ajax call to set the timezone information for the session."""
tz_name = request.vars.name
# Validates the name.
from pytz import all_timezones_set
if tz_name in all_timezones_set:
session.user_timezone = tz_name
# If the user is logged in, sets also the timezone for the user.
# Otherwise, it can happen that a user expires a cookie, then click on edit.
# When the user is presented the edit page, the translation is done according to UTC,
# but when the user is done editing, due to autodetection, the user is then in
# it's own time zone, and the dates of an assignment change.
# This really happened.
if auth.user is not None:
db.auth_user[auth.user.id] = dict(user_timezone = tz_name)
logger.info("Set timezone to: %r" % tz_name)
else:
logger.warning("Invalid timezone received: %r" % tz_name)
python类all_timezones_set()的实例源码
def set_timezone():
"""Ajax call to set the timezone information for the session."""
tz_name = request.vars.name
# Validates the name.
from pytz import all_timezones_set
if tz_name in all_timezones_set:
session.user_timezone = tz_name
# If the user is logged in, sets also the timezone for the user.
# Otherwise, it can happen that a user expires a cookie, then click on edit.
# When the user is presented the edit page, the translation is done according to UTC,
# but when the user is done editing, due to autodetection, the user is then in
# it's own time zone, and the dates of an assignment change.
# This really happened.
if auth.user is not None:
db.auth_user[auth.user.id] = dict(user_timezone = tz_name)
logger.info("Set timezone to: %r" % tz_name)
else:
logger.warning("Invalid timezone received: %r" % tz_name)
# TODO: use vue.js
def cmd_time(expr, chatid, replyid, msg):
'''/time - Get time for various timezones'''
tzs = list(filter(lambda x: x in pytz.all_timezones_set, expr.split()))
if not tzs:
if chatid > 0:
tzs = [USER_CACHE[msg['from']['id']]['timezone']]
else:
tzs = [row[0] for row in CONN.execute(
'SELECT users.timezone FROM users'
' INNER JOIN user_chats ON users.id = user_chats.user'
' WHERE user_chats.chat = ? GROUP BY users.timezone'
' ORDER BY count(users.timezone) DESC, users.timezone ASC',
(msg['chat']['id'],))]
if tzs:
text = [_('The time is:')]
for tz in tzs:
usertime = datetime.datetime.now(pytz.timezone(tz))
text.append(' '.join((
'??' if tz_is_day(usertime, tz) else '??',
usertime.strftime('%H:%M'), tz
)))
sendmsg('\n'.join(text), chatid, replyid)
else:
sendmsg(_("No timezone specified."), chatid, replyid)
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def find_tz(self,place):
for x in pytz.all_timezones_set:
if place in x:
return x
return None
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def _get_localzone():
pipe = subprocess.Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def _get_localzone():
pipe = subprocess.Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)
def _get_localzone():
pipe = subprocess.Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)
def _get_localzone():
pipe = subprocess.Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def _get_localzone(_root='/'):
with Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
) as pipe:
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink(os.path.join(_root, "etc/localtime"))
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def cmd_settz(expr, chatid, replyid, msg):
'''/settz - Set your timezone'''
if expr and expr in pytz.all_timezones_set:
update_user(msg['from'], timezone=expr)
sendmsg(_("Your timezone is %s now.") % expr, chatid, replyid)
else:
try:
current = USER_CACHE[msg['from']['id']]['timezone']
except KeyError:
current = CFG['defaulttz']
sendmsg(_("Invalid timezone. Your current timezone is %s.") % current, chatid, replyid)
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def test_belfast(self):
# Belfast uses London time.
self.assertTrue('Europe/Belfast' in pytz.all_timezones_set)
self.assertFalse('Europe/Belfast' in pytz.common_timezones)
self.assertFalse('Europe/Belfast' in pytz.common_timezones_set)
def _get_localzone():
pipe = subprocess.Popen(
"systemsetup -gettimezone",
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE
)
tzname = pipe.stdout.read().replace(b'Time Zone: ', b'').strip()
if not tzname or tzname not in pytz.all_timezones_set:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9:]
return pytz.timezone(tzname)