Beispiel #1
0
def GetTimeZone(name):
    """Returns a datetime.tzinfo object for name.

  Args:
    name: A timezone name string, None for the local timezone.

  Returns:
    A datetime.tzinfo object for name, local timezone if name is unknown.
  """
    if name in ('UTC', 'Z'):
        return UTC
    name = times_data.ABBREVIATION_TO_IANA.get(name, name)
    tzinfo = tz.gettz(name)
    if not tzinfo and tzwin:
        name = times_data.IANA_TO_WINDOWS.get(name, name)
        try:
            tzinfo = tzwin.tzwin(name)
        except WindowsError:  # pylint: disable=undefined-variable
            pass
    return tzinfo
Beispiel #2
0
def GetTimeZone(name):
  """Returns a datetime.tzinfo object for name.

  Args:
    name: A timezone name string, None for the local timezone.

  Returns:
    A datetime.tzinfo object for name, local timezone if name is unknown.
  """
  if name in ('UTC', 'Z'):
    return UTC
  name = times_data.ABBREVIATION_TO_IANA.get(name, name)
  tzinfo = tz.gettz(name)
  if not tzinfo and tzwin:
    name = times_data.IANA_TO_WINDOWS.get(name, name)
    try:
      tzinfo = tzwin.tzwin(name)
    except WindowsError:  # pylint: disable=undefined-variable
      pass
  return tzinfo
Beispiel #3
0
def gettz(name=None):
    tz = None
    if not name:
        try:
            name = os.environ["TZ"]
        except KeyError:
            pass
    if name is None or name == ":":
        for filepath in TZFILES:
            if not os.path.isabs(filepath):
                filename = filepath
                for path in TZPATHS:
                    filepath = os.path.join(path, filename)
                    if os.path.isfile(filepath):
                        break
                else:
                    continue
            if os.path.isfile(filepath):
                try:
                    tz = tzfile(filepath)
                    break
                except (IOError, OSError, ValueError):
                    pass
    else:
        if name.startswith(":"):
            name = name[:-1]
        if os.path.isabs(name):
            if os.path.isfile(name):
                tz = tzfile(name)
            else:
                tz = None
        else:
            for path in TZPATHS:
                filepath = os.path.join(path, name)
                if not os.path.isfile(filepath):
                    filepath = filepath.replace(' ','_')
                    if not os.path.isfile(filepath):
                        continue
                try:
                    tz = tzfile(filepath)
                    break
                except (IOError, OSError, ValueError):
                    pass
            else:
                tz = None
                if tzwin:
                    try:
                        tz = tzwin(name)
                    except OSError:
                        pass
                if not tz:
                    from dateutil.zoneinfo import gettz
                    tz = gettz(name)
                if not tz:
                    for c in name:
                        # name must have at least one offset to be a tzstr
                        if c in "0123456789":
                            try:
                                tz = tzstr(name)
                            except ValueError:
                                pass
                            break
                    else:
                        if name in ("GMT", "UTC"):
                            tz = tzutc()
                        elif name in time.tzname:
                            tz = tzlocal()
    return tz
Beispiel #4
0
 def testOffset(self):
     tz = tzwin.tzwin("Cape Verde Standard Time")
     self.assertEqual(tz.utcoffset(datetime(1995, 5, 21, 12, 9, 13)),
                      timedelta(-1, 82800))
Beispiel #5
0
 def testIsdstZoneWithNoDaylightSaving(self):
     tz = tzwin.tzwin("UTC")
     dt = parse("2013-03-06 19:08:15")
     self.assertFalse(tz._isdst(dt))
Beispiel #6
0
 def testOffset(self):
     tz = tzwin.tzwin("Cape Verde Standard Time")
     self.assertEqual(tz.utcoffset(datetime(1995, 5, 21, 12, 9, 13)), timedelta(-1, 82800))
Beispiel #7
0
 def testIsdstZoneWithNoDaylightSaving(self):
     tz = tzwin.tzwin("UTC")
     dt = parse("2013-03-06 19:08:15")
     self.assertFalse(tz._isdst(dt))
Beispiel #8
0
def gettz(name=None):
    tz = None
    if not name:
        try:
            name = os.environ["TZ"]
        except KeyError:
            pass
    if name is None or name == ":":
        for filepath in TZFILES:
            if not os.path.isabs(filepath):
                filename = filepath
                for path in TZPATHS:
                    filepath = os.path.join(path, filename)
                    if os.path.isfile(filepath):
                        break
                else:
                    continue
            if os.path.isfile(filepath):
                try:
                    tz = tzfile(filepath)
                    break
                except (IOError, OSError, ValueError):
                    pass
        else:
            tz = tzlocal()
    else:
        if name.startswith(":"):
            name = name[:-1]
        if os.path.isabs(name):
            if os.path.isfile(name):
                tz = tzfile(name)
            else:
                tz = None
        else:
            for path in TZPATHS:
                filepath = os.path.join(path, name)
                if not os.path.isfile(filepath):
                    filepath = filepath.replace(' ', '_')
                    if not os.path.isfile(filepath):
                        continue
                try:
                    tz = tzfile(filepath)
                    break
                except (IOError, OSError, ValueError):
                    pass
            else:
                tz = None
                if tzwin is not None:
                    try:
                        tz = tzwin(name)
                    except WindowsError:
                        tz = None
                if not tz:
                    try:  # GOOGLE
                        from dateutil.zoneinfo import gettz
                        tz = gettz(name)
                    except ImportError:  # GOOGLE
                        logging.info('dateutil.zoneinfo module not available;'
                                     ' using system TZ database.')
                        logging.info(
                            'Add "//third_party/py/dateutil:zoneinfo"'
                            ' to your deps if you need your own copy.')
                if not tz:
                    for c in name:
                        # name must have at least one offset to be a tzstr
                        if c in "0123456789":
                            try:
                                tz = tzstr(name)
                            except ValueError:
                                pass
                            break
                    else:
                        if name in ("GMT", "UTC"):
                            tz = tzutc()
                        elif name in time.tzname:
                            tz = tzlocal()
    return tz
Beispiel #9
0
def gettz(name=None):
    tz = None
    if not name:
        try:
            name = os.environ["TZ"]
        except KeyError:
            pass
    if name is None or name == ":":
        for filepath in TZFILES:
            if not os.path.isabs(filepath):
                filename = filepath
                for path in TZPATHS:
                    filepath = os.path.join(path, filename)
                    if os.path.isfile(filepath):
                        break
                else:
                    continue
            if os.path.isfile(filepath):
                try:
                    tz = tzfile(filepath)
                    break
                except (IOError, OSError, ValueError):
                    pass
        else:
            tz = tzlocal()
    else:
        if name.startswith(":"):
            name = name[:-1]
        if os.path.isabs(name):
            if os.path.isfile(name):
                tz = tzfile(name)
            else:
                tz = None
        else:
            for path in TZPATHS:
                filepath = os.path.join(path, name)
                if not os.path.isfile(filepath):
                    filepath = filepath.replace(' ', '_')
                    if not os.path.isfile(filepath):
                        continue
                try:
                    tz = tzfile(filepath)
                    break
                except (IOError, OSError, ValueError):
                    pass
            else:
                tz = None
                if tzwin is not None:
                    try:
                        tz = tzwin(name)
                    except WindowsError:
                        tz = None
                if not tz:
                    try:  # GOOGLE
                        from dateutil.zoneinfo import gettz
                        tz = gettz(name)
                    except ImportError:  # GOOGLE
                        logging.info('dateutil.zoneinfo module not available;'
                                     ' using system TZ database.')
                        logging.info('Add "//third_party/py/dateutil:zoneinfo"'
                                     ' to your deps if you need your own copy.')
                if not tz:
                    for c in name:
                        # name must have at least one offset to be a tzstr
                        if c in "0123456789":
                            try:
                                tz = tzstr(name)
                            except ValueError:
                                pass
                            break
                    else:
                        if name in ("GMT", "UTC"):
                            tz = tzutc()
                        elif name in time.tzname:
                            tz = tzlocal()
    return tz