home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / pyxmpp / utils.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  1.8 KB  |  64 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: utils.py 714 2010-04-05 10:20:10Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import sys
  7. if sys.hexversion < 33751040:
  8.     raise ImportError, 'Python 2.3 or newer is required'
  9. sys.hexversion < 33751040
  10. import time
  11. import datetime
  12.  
  13. def to_utf8(s):
  14.     if s is None:
  15.         return None
  16.     if type(s) is unicode:
  17.         return s.encode('utf-8')
  18.     if type(s) is str:
  19.         return s
  20.     return unicode(s).encode('utf-8')
  21.  
  22.  
  23. def from_utf8(s):
  24.     if s is None:
  25.         return None
  26.     if type(s) is unicode:
  27.         return s
  28.     if type(s) is str:
  29.         return unicode(s, 'utf-8')
  30.     return unicode(s)
  31.  
  32. minute = datetime.timedelta(minutes = 1)
  33. nulldelta = datetime.timedelta()
  34.  
  35. def datetime_utc_to_local(utc):
  36.     ts = time.time()
  37.     cur = datetime.datetime.fromtimestamp(ts)
  38.     cur_utc = datetime.datetime.utcfromtimestamp(ts)
  39.     offset = cur - cur_utc
  40.     t = utc
  41.     d = datetime.timedelta(hours = 2)
  42.     while d > minute:
  43.         local = t + offset
  44.         tm = local.timetuple()
  45.         tm = tm[0:8] + (0,)
  46.         ts = time.mktime(tm)
  47.         u = datetime.datetime.utcfromtimestamp(ts)
  48.         diff = u - utc
  49.         if diff < minute and diff > -minute:
  50.             break
  51.         
  52.         if diff > nulldelta:
  53.             offset -= d
  54.         else:
  55.             offset += d
  56.         d /= 2
  57.     return local
  58.  
  59.  
  60. def datetime_local_to_utc(local):
  61.     ts = time.mktime(local.timetuple())
  62.     return datetime.datetime.utcfromtimestamp(ts)
  63.  
  64.