import json
import datetime
from json import JSONEncoder

eve_date_format = "%Y-%m-%d %H:%M:%S"
keys = []

class DateTimeEncoder(JSONEncoder):
        ''' encode datetime objects to json '''
        #Override the default method
        def default(self, obj):
            if isinstance(obj, (datetime.date, datetime.datetime, datetime.timezone)):
                return obj.strftime(eve_date_format)

# custom Decoder
def decodedatetime(dict):
    for key in keys:
        if key in dict:
            dict[key] = datetime.datetime.strptime(dict[key], eve_date_format).strftime(eve_date_format)
    return dict

def encode_json(dict):
    ''' Encode DateTime Object into JSON using custom JSONEncoder '''
    try:
        encoded_json = json.dumps(dict, indent=4, cls=DateTimeEncoder)
        return encoded_json
    except Exception as decodingException:
        response = {'status':False, 'err_code':301, 'msg': decodingException}
        return response 