import json from datetime import datetime import pytz def lambda_handler(event, context): # Define the European capitals with their respective time zones capitals = { "Lisbon" : "Europe/Lisbon", "London": "Europe/London", "Paris": "Europe/Paris", "Berlin": "Europe/Berlin", "Madrid": "Europe/Madrid", "Rome": "Europe/Rome" } dict_params_received = event.get('queryStringParameters', {}) city_asked = dict_params_received.get('city', None) if city_asked and city_asked in capitals: # If a specific city is requested, return its current time timezone = pytz.timezone(capitals[city_asked]) current_time = datetime.now(timezone).strftime('%Y-%m-%d %H:%M:%S') body = {city_asked: current_time} else: # If no specific city is requested, return times for all cities times_in_capitals = {} for capital, timezone in capitals.items(): tz = pytz.timezone(timezone) current_time = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S') times_in_capitals[capital] = current_time # for body = times_in_capitals # if-else # Return the result as a JSON object return { 'statusCode': 200, 'body': json.dumps(body) } # def lambda_handler