def set_timezone(self): try: with open('/etc/timezone', "r") as f: current_timezone = f.readline().strip() except: logger.exception("Can't get current timezone!") current_timezone = None else: logger.info("Current timezone: {}".format(repr(current_timezone))) lc = [] with LoadingBar(self.i, self.o, message="Getting timezones"): for k in ZoneInfoFile(getzoneinfofile_stream()).zones.keys(): lc.append([k]) lc = sorted(lc) choice = Listbox(lc, self.i, self.o, "Timezone selection listbox", selected=current_timezone).activate() if choice: # Setting timezone using timedatectl try: check_output(["timedatectl", "set-timezone", choice]) except CalledProcessError as e: logger.exception( "Can't set timezone using timedatectl! Return code: {}, output: {}" .format(e.returncode, repr(e.output))) return False else: logger.info("Set timezone successfully") return True else: return None
def reload(self): self.loadFinished = False GeoIPLookup(self._onGeoIpData) self.timezones = [] self._lut = {} self._regions = OrderedDict() zones = ZoneInfoFile(getzoneinfofile_stream()).zones keys = sorted(zones.keys()) for key in keys: zinfo = zones[key] timezone = Timezone(key, zinfo) region = self._regions.get(timezone.region, []) region.append(timezone) self._regions[timezone.region] = region self._lut[key] = timezone self.timezones.append([timezone.name, timezone.key])
async def set_timezone(cmd, value): tz_obj = tz.gettz(value) if not tz_obj: err = True await cmd.send('The timezone `{:s}` is not valid'.format(value)) all_zones = list(ZoneInfoFile(getzoneinfofile_stream()).zones.keys()) closest_tz = difflib.get_close_matches(value, all_zones) if closest_tz: await cmd.send('Did you mean `{:s}`?'.format( '`, `'.join(closest_tz))) # only save correct timezones if tz_obj: Connector.set_timezone(cmd.guild.id, value) await cmd.send('Timezone is now set to `{:s}`'.format(value))
def get_all_timezones(): if len(zoneinfo._CLASS_ZONE_INSTANCE) == 0: zoneinfo._CLASS_ZONE_INSTANCE.append(zoneinfo.ZoneInfoFile(zoneinfo.getzoneinfofile_stream())) keys = zoneinfo._CLASS_ZONE_INSTANCE[0].zones.keys() tz_dict = {} for i in keys: try: continent = i.split("/")[0].strip() city = i.split("/")[1].strip() if not continent in tz_dict: tz_dict[continent] = set() tz_dict[continent].add(city) except: pass # Sort the cities for key, val in tz_dict.items(): tz_dict[key] = sorted(val) # Sort the continents tz_dict = OrderedDict(sorted(tz_dict.items(), key=lambda t: t[0])) return tz_dict
def test_can_coerce_pytz_StaticTzInfo(): tzcol = TimezoneType(backend='pytz') tz = pytz.timezone('Pacific/Truk') assert isinstance(tz, pytz.tzfile.StaticTzInfo) assert tzcol._coerce(tz) is tz @pytest.mark.parametrize('zone', pytz.all_timezones) def test_can_coerce_string_for_pytz_zone(zone): tzcol = TimezoneType(backend='pytz') assert tzcol._coerce(zone).zone == zone @pytest.mark.parametrize( 'zone', ZoneInfoFile(getzoneinfofile_stream()).zones.keys()) def test_can_coerce_string_for_dateutil_zone(zone): tzcol = TimezoneType(backend='dateutil') assert isinstance(tzcol._coerce(zone), tzfile) @pytest.mark.parametrize('backend', TIMEZONE_BACKENDS) def test_can_coerce_and_raise_UnknownTimeZoneError_or_ValueError(backend): tzcol = TimezoneType(backend=backend) with pytest.raises((ValueError, pytz.exceptions.UnknownTimeZoneError)): tzcol._coerce('SolarSystem/Mars') with pytest.raises((ValueError, pytz.exceptions.UnknownTimeZoneError)): tzcol._coerce('') @pytest.mark.parametrize('backend', TIMEZONE_BACKENDS)
def test_can_coerce_pytz_StaticTzInfo(): tzcol = TimezoneType(backend='pytz') tz = pytz.timezone('Pacific/Truk') assert isinstance(tz, pytz.tzfile.StaticTzInfo) assert tzcol._coerce(tz) is tz @pytest.mark.parametrize('zone', pytz.all_timezones) def test_can_coerce_string_for_pytz_zone(zone): tzcol = TimezoneType(backend='pytz') assert tzcol._coerce(zone).zone == zone @pytest.mark.parametrize('zone', ZoneInfoFile(getzoneinfofile_stream()).zones.keys()) def test_can_coerce_string_for_dateutil_zone(zone): tzcol = TimezoneType(backend='dateutil') assert isinstance(tzcol._coerce(zone), tzfile) @pytest.mark.parametrize('backend', TIMEZONE_BACKENDS) def test_can_coerce_and_raise_UnknownTimeZoneError_or_ValueError(backend): tzcol = TimezoneType(backend=backend) with pytest.raises((ValueError, pytz.exceptions.UnknownTimeZoneError)): tzcol._coerce('SolarSystem/Mars') with pytest.raises((ValueError, pytz.exceptions.UnknownTimeZoneError)): tzcol._coerce('') @pytest.mark.parametrize('backend', TIMEZONE_BACKENDS)
def get_timezone_by_name(tzname): zone_info_file = ZoneInfoFile(getzoneinfofile_stream()) return zone_info_file.zones.get(tzname)