Как отобразить только текущее время - PullRequest
0 голосов
/ 08 ноября 2019

Я вызываю API, который возвращает текущее время для определенного часового пояса (в данном случае estern). Однако он возвращает мне много данных, которые мне не нужны. Как сузить его до currentDateTime "2019-11-07T20: 38-05: 00"? вот мой код;

import requests
import json
import jsonpath
import dateutil
from flask import Flask, render_template, request
from flask import jsonify, make_response

# declaring app
app = Flask(__name__, template_folder="templates")

# app route defined
@app.route('/get_time', methods=['GET'])
def get_time():
    # error handling
    try:
        time_zone = request.args.get('time_zone')
        url = "http://worldclockapi.com/api/json/" + time_zone + "/now"
        r = requests.get(url)
    except Exception:
        return make_response(jsonify({"Error": "Some error message"}), 400)
    return r.json()
    # I could not figure out how to display only Current Date Time for this error handling scenario
    # If I do narrow it down the invalid time zone error is not displayed.

    # this will return error if condition not met
    if response.status_code != 200:
        print("Error on response")
        return response.status_code, response.text

# main app
if __name__ == '__main__':
    app.run(debug=True)

вот что я получаю взамен:

{
  "$id": "1", 
  "currentDateTime": "2019-11-07T20:38-05:00", 
  "currentFileTime": 132176327192699737, 
  "dayOfTheWeek": "Thursday", 
  "isDayLightSavingsTime": false, 
  "ordinalDate": "2019-311", 
  "serviceResponse": null, 
  "timeZoneName": "Eastern Standard Time", 
  "utcOffset": "-05:00:00"
}

Ответы [ 2 ]

1 голос
/ 08 ноября 2019

Попробуйте что-то вроде

return r.json()["currentDateTime"]
0 голосов
/ 08 ноября 2019

Измените reutrn, дайте мне знать, работает ли он или нет.

return r.json()["currentDateTime"]

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...