如何解决TypeError: _get_unique_zone()缺少必填参数'shortcut_id_y'问题
问题修复方案
核心错误原因
你没有正确实例化TimezoneFinder类,直接将类本身当做实例使用,导致方法参数匹配逻辑异常。你之前遇到的需要手动填充self参数的报错,本质也是这个问题导致的。
具体修改点
- 实例化
TimezoneFinder类,将tf = TimezoneFinder改为tf = TimezoneFinder() - 去掉
offset函数中多余的self参数,直接通过实例调用certain_timezone_at方法,不需要手动传self - 修正最后打印语句的变量名错误,前面定义的变量是
Correction,不要误用未定义的GMTcor
修复后完整代码
from geopy.geocoders import Nominatim from datetime import timedelta from pytz import timezone import pytz import datetime from timezonefinder import TimezoneFinder address = input("Insert place: ") geolocator = Nominatim(user_agent="Time Correction Calculator") location = geolocator.geocode(address) print(location.address) t = input("Insert time (hh:mm): ") (h, m) = t.split(':') time_in = int(h) * 60 + int(m) date_input = input("Insert date (dd.mm.yyyy): ") utc = pytz.utc # 正确实例化TimezoneFinder类 tf = TimezoneFinder() def offset(target): today = datetime.datetime.strptime(date_input, '%d.%m.%Y') # 直接通过实例调用方法,自动填充self tz_target = timezone(tf.certain_timezone_at(lat=target['lat'], lng = target['lng'])) today_target = tz_target.localize(today) today_utc = utc.localize(today) return (today_utc - today_target).total_seconds() / 3600 City = dict({'lat' : location.latitude, 'lng' : location.longitude}) realTime : float = location.longitude * 4 realTime = round(realTime) Correction = time_in - offset(City) * 60 + realTime # 修正变量名与参数写法 print(str(timedelta(minutes = Correction))[:-3])
内容的提问来源于stack exchange,提问作者Marvin W
相关产品推荐
相关产品推荐

