From b2148f2df4da806146d0e038eb28123ec2801f22 Mon Sep 17 00:00:00 2001 From: Silent YANG Date: Sun, 27 Oct 2024 16:12:54 +0800 Subject: [PATCH] update support local time --- app.py | 11 +++++++---- head.py | 16 ++++++++-------- req.txt | 4 +++- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 273fa81..6ac64dd 100644 --- a/app.py +++ b/app.py @@ -5,14 +5,15 @@ from flask_cors import cross_origin import ephem import redis import uuid +import dateutil app = Flask(__name__) r = redis.Redis(host='127.0.0.1', port=6379, db=0) -def local2utc(local_dtm): +def local2utc(local_dtm, tz="Asia/Shanghai"): # 本地时间转 UTC 时间( -8:00 ) - return datetime.utcfromtimestamp(local_dtm.timestamp()) + return datetime.utcfromtimestamp(local_dtm.replace(tzinfo=dateutil.tz.gettz(tz)).timestamp()) @app.route("/lol", methods=['POST']) @cross_origin() @@ -36,11 +37,12 @@ def satpass(): lat = request.json.get('lat', 0) lng = request.json.get('lng', 0) alt = request.json.get('alt', 0) + tz = request.json.get('tz', "Asia/Shanghai") target_satellite, find_flag = head.FIND_SATE(sat_line_1, sat_line_2, sat) pass_times, departure_times = head.CAL_PASS_TIME(target_satellite, float(lat), float(lng), - float(alt)) + float(alt), tz) return jsonify({ 'code': 200, @@ -60,6 +62,7 @@ def doppler(): alt = request.json.get('alt', 0) tx = request.json.get('tx', 0) rx = request.json.get('rx', 0) + tz = request.json.get('tz', "Asia/Shanghai") format = "%Y-%m-%d %H:%M:%S" pass_time = datetime.strptime(request.json.get('pass_time', ''), format) departure_time = datetime.strptime(request.json.get('departure_time', ''), @@ -70,7 +73,7 @@ def doppler(): while pass_time < departure_time + timedelta(seconds=1): AZ, EI, SHITF_UP, SHIFT_DOWN, DIS = head.CAL_DATA( satellite, sat_line_1, sat_line_2, float(lng), float(lat), - float(alt), local2utc(pass_time), + float(alt), local2utc(pass_time, tz), float(tx) * 1000000, float(rx) * 1000000) shift_array.append([SHITF_UP, SHIFT_DOWN]) diff --git a/head.py b/head.py index c2ecef2..0247ff9 100644 --- a/head.py +++ b/head.py @@ -26,7 +26,7 @@ def find_lines_after_string(filename, search_string): line2 = lines[i+2].strip() break return line1, line2 -def CAL_PASS_TIME(target_satellite, observer_lat, observer_lon, observer_elevation): +def CAL_PASS_TIME(target_satellite, observer_lat, observer_lon, observer_elevation, tz="Asia/Shanghai"): topos = Topos(latitude_degrees=observer_lat, longitude_degrees=observer_lon, elevation_m=observer_elevation) # 获取当前时间并创建Skyfield的时间对象 @@ -35,21 +35,21 @@ def CAL_PASS_TIME(target_satellite, observer_lat, observer_lon, observer_elevati t0 = ts.utc(now) t1 = t0 + timedelta(days=2) # 查找2天内过境时间 passes = target_satellite.find_events(topos, t0, t1) - # 获取北京时区 - beijing_tz = pytz.timezone('Asia/Shanghai') + # 获取当地时区 + local_tz = pytz.timezone(tz) pass_times = [] departure_times = [] if not passes[0]: return None, None - # 打印下一次过境的北京时间和离境时间 + # 打印下一次过境的当地时间和离境时间 for time, event in zip(passes[0], passes[1]): if event == 0: # 0表示升,1表示降 - # 将UTC时间转换为北京时间 - beijing_time = time.utc_datetime().replace(tzinfo=pytz.utc).astimezone(beijing_tz) - pass_times.append(beijing_time.strftime('%Y-%m-%d %H:%M:%S')) + # 将UTC时间转换为当地时间 + local_time = time.utc_datetime().replace(tzinfo=pytz.utc).astimezone(local_tz) + pass_times.append(local_time.strftime('%Y-%m-%d %H:%M:%S')) elif event == 2: - departure_time_utc = time.utc_datetime().replace(tzinfo=pytz.utc).astimezone(beijing_tz) + departure_time_utc = time.utc_datetime().replace(tzinfo=pytz.utc).astimezone(local_tz) departure_times.append(departure_time_utc.strftime('%Y-%m-%d %H:%M:%S')) return pass_times, departure_times diff --git a/req.txt b/req.txt index 40ea567..0831451 100644 --- a/req.txt +++ b/req.txt @@ -3,4 +3,6 @@ pytz==2024.1 skyfield==1.47 Flask==3.0.2 Flask-Cors==4.0.0 -redis==5.0.1 \ No newline at end of file +redis==5.0.1 +py2wasm==2.6.2 +python-dateutil==2.9.0.post0 \ No newline at end of file