def ParseISO8601(datetimeStr):
   """
   Parse ISO 8601 date time from string.
   Returns datetime if ok, None otherwise
   Note: Allows YYYY / YYYY-MM, but truncate YYYY -> YYYY-01-01,
                                             YYYY-MM -> YYYY-MM-01
         Truncate microsecond to most significant 6 digits
   """
   datetimeVal = None
   match = _dtExpr.match(datetimeStr)
   if match:
      try:
         dt = {}
         for key, defaultVal in iteritems(_dtExprKeyDefValMap):
            val = match.group(key)
            if val:
               if key == 'microsecond':
                  val = val[:6] + '0' * (6 - len(val))
               dt[key] = int(val)
            elif defaultVal:
               dt[key] = defaultVal

         # Orig. XMLSchema don't allow all zeros year. But newer draft is ok
         #if dt['year'] == 0:
         #   # Year cannot be all zeros
         #   raise Exception('Year cannot be all zeros')

         # 24 is a special case. It is actually represented as next day 00:00
         delta = None
         if dt.get('hour', 0) == 24:
            # Must be 24:00:00.0
            if dt.get('minute', 0) == 0 and dt.get('second', 0) == 0 and \
               dt.get('microsecond', 0) == 0:
               dt['hour'] = 23
               delta = timedelta(hours=1)
            else:
               return None

         # Set tzinfo
         # TODO: dst
         tzInfo = None
         val = match.group('tzutc')
         if val:
            tzInfo = TZManager.GetTZInfo()
         else:
            val = match.group('tzhr')
            if val:
               # tz hours offset
               tzhr = int(val)
               utcsign = val[0]

               # tz minutes offset
               tzmin = 0
               val = match.group('tzmin')
               if val:
                  tzmin = tzhr >= 0 and int(val) or -int(val)

               # Better tzname (map UTC +-00:00 to UTC)
               tzname = 'UTC'
               if tzhr != 0 or tzmin != 0:
                  tzname += ' %s%02d:%02d' % (utcsign, abs(tzhr), abs(tzmin))

               tzInfo = TZManager.GetTZInfo(tzname=tzname,
                                            utcOffset=timedelta(hours=tzhr,
                                                                minutes=tzmin))
         if tzInfo:
            dt['tzinfo'] = tzInfo

         datetimeVal = datetime(**dt)
         if delta:
            datetimeVal += delta
      except Exception as e:
         pass
   return datetimeVal
dataset/ETHPy150Open vmware/pyvmomi/pyVmomi/Iso8601.py/ParseISO8601/original