home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / ZSI / TCtimes.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  10.4 KB  |  294 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from ZSI import _copyright, _floattypes, _inttypes, _get_idstr, EvaluateException
  5. from ZSI.TC import TypeCode, SimpleType
  6. from ZSI.wstools.Namespaces import SCHEMA
  7. import operator
  8. import re
  9. import time as _time
  10. from time import mktime as _mktime, localtime as _localtime, gmtime as _gmtime
  11. from datetime import tzinfo as _tzinfo, timedelta as _timedelta, datetime as _datetime
  12. from math import modf as _modf
  13. _niltime = [
  14.     0,
  15.     0,
  16.     0,
  17.     0,
  18.     0,
  19.     0,
  20.     0,
  21.     0,
  22.     0]
  23. _zero = _timedelta(0)
  24. _dstoffset = _stdoffset = _timedelta(seconds = -(_time.timezone))
  25. if _time.daylight:
  26.     _dstoffset = _timedelta(seconds = -(_time.altzone))
  27.  
  28. _dstdiff = _dstoffset - _stdoffset
  29.  
  30. class _localtimezone(_tzinfo):
  31.     
  32.     def dst(self, dt):
  33.         tt = _localtime(_mktime((dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1)))
  34.         if tt.tm_isdst > 0:
  35.             return _dstdiff
  36.         return _zero
  37.  
  38.     
  39.     def tzname(self, dt):
  40.         tt = _localtime(_mktime((dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1)))
  41.         return _time.tzname[tt.tm_isdst > 0]
  42.  
  43.     
  44.     def utcoffset(self, dt):
  45.         tt = _localtime(_mktime((dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1)))
  46.         if tt.tm_isdst > 0:
  47.             return _dstoffset
  48.         return _stdoffset
  49.  
  50.  
  51.  
  52. class _fixedoffset(_tzinfo):
  53.     
  54.     def __init__(self, offset):
  55.         self._fixedoffset__offset = _timedelta(minutes = offset)
  56.  
  57.     
  58.     def dst(self, dt):
  59.         return _zero
  60.  
  61.     
  62.     def tzname(self, dt):
  63.         return 'server'
  64.  
  65.     
  66.     def utcoffset(self, dt):
  67.         return self._fixedoffset__offset
  68.  
  69.  
  70.  
  71. def _dict_to_tuple(d):
  72.     retval = _niltime[:]
  73.     for k, i in (('Y', 0), ('M', 1), ('D', 2), ('h', 3), ('m', 4)):
  74.         v = d.get(k)
  75.         if v:
  76.             retval[i] = int(v)
  77.             continue
  78.     
  79.     v = d.get('s')
  80.     if v:
  81.         (msec, sec) = _modf(float(v))
  82.         retval[6] = int(round(msec * 1000))
  83.         retval[5] = int(sec)
  84.     
  85.     v = d.get('tz')
  86.     if v and v != 'Z':
  87.         (h, m) = map(int, v.split(':'))
  88.         offset = _localtimezone().utcoffset(_datetime.now())
  89.         local_offset_hour = offset.seconds / 3600
  90.         local_offset_min = offset.seconds % 3600 % 60
  91.         if local_offset_hour > 12:
  92.             local_offset_hour -= 24
  93.         
  94.         if local_offset_hour != h or local_offset_min != m:
  95.             if h < 0:
  96.                 foff = _fixedoffset(-(abs(h) * 60 + m))
  97.             else:
  98.                 foff = _fixedoffset(abs(h) * 60 + m)
  99.             dt = _datetime(retval[0], retval[1], retval[2], retval[3], retval[4], retval[5], 0, foff)
  100.             localdt = dt.astimezone(_localtimezone())
  101.             retval[0] = localdt.year
  102.             retval[1] = localdt.month
  103.             retval[2] = localdt.day
  104.             retval[3] = localdt.hour
  105.             retval[4] = localdt.minute
  106.             retval[5] = localdt.second
  107.         
  108.     
  109.     if d.get('neg', 0):
  110.         retval[0:5] = map(operator.__neg__, retval[0:5])
  111.     
  112.     return tuple(retval)
  113.  
  114.  
  115. class Duration(SimpleType):
  116.     parselist = [
  117.         (None, 'duration')]
  118.     lex_pattern = re.compile('^(?P<neg>-?)P((?P<Y>\\d+)Y)?((?P<M>\\d+)M)?((?P<D>\\d+)D)?(?P<T>T?)((?P<h>\\d+)H)?((?P<m>\\d+)M)?((?P<s>\\d*(\\.\\d+)?)S)?$')
  119.     type = (SCHEMA.XSD3, 'duration')
  120.     
  121.     def text_to_data(self, text, elt, ps):
  122.         if text is None:
  123.             return None
  124.         m = Duration.lex_pattern.match(text)
  125.         if m is None:
  126.             raise EvaluateException('Illegal duration', ps.Backtrace(elt))
  127.         m is None
  128.         d = m.groupdict()
  129.         if d['T'] and d['h'] is None and d['m'] is None and d['s'] is None:
  130.             raise EvaluateException('Duration has T without time')
  131.         d['s'] is None
  132.         
  133.         try:
  134.             retval = _dict_to_tuple(d)
  135.         except ValueError:
  136.             text is None
  137.             e = text is None
  138.             raise EvaluateException(str(e))
  139.         except:
  140.             text is None
  141.  
  142.         if self.pyclass is not None:
  143.             return self.pyclass(retval)
  144.         return retval
  145.  
  146.     
  147.     def get_formatted_content(self, pyobj):
  148.         if type(pyobj) in _floattypes or type(pyobj) in _inttypes:
  149.             pyobj = _gmtime(pyobj)
  150.         
  151.         d = { }
  152.         pyobj = tuple(pyobj)
  153.         if 1 in map((lambda x: x < 0), pyobj[0:6]):
  154.             pyobj = map(abs, pyobj)
  155.             neg = '-'
  156.         else:
  157.             neg = ''
  158.         val = '%sP%dY%dM%dDT%dH%dM%dS' % (neg, pyobj[0], pyobj[1], pyobj[2], pyobj[3], pyobj[4], pyobj[5])
  159.         return val
  160.  
  161.  
  162.  
  163. class Gregorian(SimpleType):
  164.     lex_pattern = None
  165.     tag = None
  166.     format = None
  167.     
  168.     def text_to_data(self, text, elt, ps):
  169.         if text is None:
  170.             return None
  171.         m = self.lex_pattern.match(text)
  172.         if not m:
  173.             raise EvaluateException('Bad Gregorian: %s' % text, ps.Backtrace(elt))
  174.         m
  175.         
  176.         try:
  177.             retval = _dict_to_tuple(m.groupdict())
  178.         except ValueError:
  179.             text is None
  180.             e = text is None
  181.             raise 
  182.         except:
  183.             text is None
  184.  
  185.         if self.pyclass is not None:
  186.             return self.pyclass(retval)
  187.         return retval
  188.  
  189.     
  190.     def get_formatted_content(self, pyobj):
  191.         if type(pyobj) in _floattypes or type(pyobj) in _inttypes:
  192.             pyobj = _gmtime(pyobj)
  193.         
  194.         d = { }
  195.         pyobj = tuple(pyobj)
  196.         if 1 in map((lambda x: x < 0), pyobj[0:6]):
  197.             pyobj = map(abs, pyobj)
  198.             d['neg'] = '-'
  199.         else:
  200.             d['neg'] = ''
  201.         ms = pyobj[6]
  202.         if not ms or not hasattr(self, 'format_ms'):
  203.             d = {
  204.                 'Y': pyobj[0],
  205.                 'M': pyobj[1],
  206.                 'D': pyobj[2],
  207.                 'h': pyobj[3],
  208.                 'm': pyobj[4],
  209.                 's': pyobj[5] }
  210.             return self.format % d
  211.         if ms > 999:
  212.             raise ValueError, 'milliseconds must be a integer between 0 and 999'
  213.         ms > 999
  214.         d = {
  215.             'Y': pyobj[0],
  216.             'M': pyobj[1],
  217.             'D': pyobj[2],
  218.             'h': pyobj[3],
  219.             'm': pyobj[4],
  220.             's': pyobj[5],
  221.             'ms': ms }
  222.         return self.format_ms % d
  223.  
  224.  
  225.  
  226. class gDateTime(Gregorian):
  227.     parselist = [
  228.         (None, 'dateTime')]
  229.     lex_pattern = re.compile('^(?P<neg>-?)(?P<Y>\\d{4,})-(?P<M>\\d\\d)-(?P<D>\\d\\d)T(?P<h>\\d\\d):(?P<m>\\d\\d):(?P<s>\\d*(\\.\\d+)?)(?P<tz>(Z|([-+]\\d\\d:\\d\\d))?)$')
  230.     (tag, format) = ('dateTime', '%(Y)04d-%(M)02d-%(D)02dT%(h)02d:%(m)02d:%(s)02dZ')
  231.     format_ms = format[:-1] + '.%(ms)03dZ'
  232.     type = (SCHEMA.XSD3, 'dateTime')
  233.  
  234.  
  235. class gDate(Gregorian):
  236.     parselist = [
  237.         (None, 'date')]
  238.     lex_pattern = re.compile('^(?P<neg>-?)(?P<Y>\\d{4,})-(?P<M>\\d\\d)-(?P<D>\\d\\d)(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  239.     (tag, format) = ('date', '%(Y)04d-%(M)02d-%(D)02dZ')
  240.     type = (SCHEMA.XSD3, 'date')
  241.  
  242.  
  243. class gYearMonth(Gregorian):
  244.     parselist = [
  245.         (None, 'gYearMonth')]
  246.     lex_pattern = re.compile('^(?P<neg>-?)(?P<Y>\\d{4,})-(?P<M>\\d\\d)(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  247.     (tag, format) = ('gYearMonth', '%(Y)04d-%(M)02dZ')
  248.     type = (SCHEMA.XSD3, 'gYearMonth')
  249.  
  250.  
  251. class gYear(Gregorian):
  252.     parselist = [
  253.         (None, 'gYear')]
  254.     lex_pattern = re.compile('^(?P<neg>-?)(?P<Y>\\d{4,})(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  255.     (tag, format) = ('gYear', '%(Y)04dZ')
  256.     type = (SCHEMA.XSD3, 'gYear')
  257.  
  258.  
  259. class gMonthDay(Gregorian):
  260.     parselist = [
  261.         (None, 'gMonthDay')]
  262.     lex_pattern = re.compile('^(?P<neg>-?)--(?P<M>\\d\\d)-(?P<D>\\d\\d)(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  263.     (tag, format) = ('gMonthDay', '---%(M)02d-%(D)02dZ')
  264.     type = (SCHEMA.XSD3, 'gMonthDay')
  265.  
  266.  
  267. class gDay(Gregorian):
  268.     parselist = [
  269.         (None, 'gDay')]
  270.     lex_pattern = re.compile('^(?P<neg>-?)---(?P<D>\\d\\d)(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  271.     (tag, format) = ('gDay', '---%(D)02dZ')
  272.     type = (SCHEMA.XSD3, 'gDay')
  273.  
  274.  
  275. class gMonth(Gregorian):
  276.     parselist = [
  277.         (None, 'gMonth')]
  278.     lex_pattern = re.compile('^(?P<neg>-?)---(?P<M>\\d\\d)(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  279.     (tag, format) = ('gMonth', '---%(M)02dZ')
  280.     type = (SCHEMA.XSD3, 'gMonth')
  281.  
  282.  
  283. class gTime(Gregorian):
  284.     parselist = [
  285.         (None, 'time')]
  286.     lex_pattern = re.compile('^(?P<neg>-?)(?P<h>\\d\\d):(?P<m>\\d\\d):(?P<s>\\d*(\\.\\d+)?)(?P<tz>Z|([-+]\\d\\d:\\d\\d))?$')
  287.     (tag, format) = ('time', '%(h)02d:%(m)02d:%(s)02dZ')
  288.     format_ms = format[:-1] + '.%(ms)03dZ'
  289.     type = (SCHEMA.XSD3, 'time')
  290.  
  291. if __name__ == '__main__':
  292.     print _copyright
  293.  
  294.