/usr/share/pyshared/pyxmpp/utils.py is in python-pyxmpp 1.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #
# (C) Copyright 2003-2010 Jacek Konieczny <jajcus@jajcus.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License Version
# 2.1 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
"""Utility functions for the pyxmpp package."""
__docformat__="restructuredtext en"
import sys
if sys.hexversion<0x02030000:
raise ImportError,"Python 2.3 or newer is required"
import time
import datetime
def to_utf8(s):
"""
Convevert `s` to UTF-8 if it is Unicode, leave unchanged
if it is string or None and convert to string overwise
"""
if s is None:
return None
elif type(s) is unicode:
return s.encode("utf-8")
elif type(s) is str:
return s
else:
return unicode(s).encode("utf-8")
def from_utf8(s):
"""
Convert `s` to Unicode or leave unchanged if it is None.
Regular strings are assumed to be UTF-8 encoded
"""
if s is None:
return None
elif type(s) is unicode:
return s
elif type(s) is str:
return unicode(s,"utf-8")
else:
return unicode(s)
minute=datetime.timedelta(minutes=1)
nulldelta=datetime.timedelta()
def datetime_utc_to_local(utc):
"""
An ugly hack to convert naive `datetime.datetime` object containing
UTC time to a naive `datetime.datetime` object with local time.
It seems standard Python 2.3 library doesn't provide any better way to
do that.
"""
ts=time.time()
cur=datetime.datetime.fromtimestamp(ts)
cur_utc=datetime.datetime.utcfromtimestamp(ts)
offset=cur-cur_utc
t=utc
d=datetime.timedelta(hours=2)
while d>minute:
local=t+offset
tm=local.timetuple()
tm=tm[0:8]+(0,)
ts=time.mktime(tm)
u=datetime.datetime.utcfromtimestamp(ts)
diff=u-utc
if diff<minute and diff>-minute:
break
if diff>nulldelta:
offset-=d
else:
offset+=d
d/=2
return local
def datetime_local_to_utc(local):
"""
Simple function to convert naive `datetime.datetime` object containing
local time to a naive `datetime.datetime` object with UTC time.
"""
ts=time.mktime(local.timetuple())
return datetime.datetime.utcfromtimestamp(ts)
# vi: sts=4 et sw=4
|