# Path: alertaclient/utils.py
# class DateTime:
#     @staticmethod
#     def parse(date_str):
#         if not isinstance(date_str, str):
#             return
#         try:
#             return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%fZ')
#         except Exception:
#             raise ValueError('dates must be ISO 8601 date format YYYY-MM-DDThh:mm:ss.sssZ')
# 
#     @staticmethod
#     def iso8601(dt):
#         return dt.replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') + '.%03dZ' % (dt.microsecond // 1000)
# 
#     @staticmethod
#     def localtime(dt, timezone=None, fmt='%Y/%m/%d %H:%M:%S'):
#         tz = pytz.timezone(timezone)
#         try:
#             return dt.replace(tzinfo=pytz.UTC).astimezone(tz).strftime(fmt)
#         except AttributeError:
#             return
# 
# def build_query(filters):
#     return [tuple(f.split('=', 1)) for f in filters if '=' in f]


import json
import click
from tabulate import tabulate
from alertaclient.models.alert import Alert
from alertaclient.utils import DateTime, build_query
    else:
        query = build_query(filters)
    if from_date:
        query.append(('from-date', from_date))

    r = client.http.get('/alerts', query, page=1, page_size=1000)

    if obj['output'] == 'json':
        click.echo(json.dumps(r['alerts'], sort_keys=True, indent=4, ensure_ascii=False))
    elif obj['output'] in ['json_lines', 'jsonl', 'ndjson']:
        for alert in r['alerts']:
            click.echo(json.dumps(alert, ensure_ascii=False))
    else:
        alerts = [Alert.parse(a) for a in r['alerts']]
        last_time = r['lastTime']
        auto_refresh = r['autoRefresh']

        if display == 'oneline':
            headers = {'id': 'ID', 'lastReceiveTime': 'LAST RECEIVED', 'severity': 'SEVERITY', 'status': 'STATUS',
                       'duplicateCount': 'DUPL', 'customer': 'CUSTOMER', 'environment': 'ENVIRONMENT', 'service': 'SERVICE',
                       'resource': 'RESOURCE', 'group': 'GROUP', 'event': 'EVENT', 'value': 'VALUE', 'text': 'DESCRIPTION'}

            data = [{k: v for k, v in a.tabular(timezone).items() if k in headers.keys()} for a in alerts]
            click.echo(tabulate(data, headers=headers, tablefmt=obj['output']))

        else:
            for alert in reversed(alerts):
                color = COLOR_MAP.get(alert.severity, {'fg': 'white'})
                click.secho('{}|{}|{}|{:5d}|{}|{:<5s}|{:<10s}|{:<18s}|{:12s}|{:16s}|{:12s}'.format(
                    alert.id[0:8],

# Path: alertaclient/utils.py
# class DateTime:
#     @staticmethod
#     def parse(date_str):
#         if not isinstance(date_str, str):
#             return
#         try:
#             return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%fZ')
#         except Exception:
#             raise ValueError('dates must be ISO 8601 date format YYYY-MM-DDThh:mm:ss.sssZ')
# 
#     @staticmethod
#     def iso8601(dt):
#         return dt.replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') + '.%03dZ' % (dt.microsecond // 1000)
# 
#     @staticmethod
#     def localtime(dt, timezone=None, fmt='%Y/%m/%d %H:%M:%S'):
#         tz = pytz.timezone(timezone)
#         try:
#             return dt.replace(tzinfo=pytz.UTC).astimezone(tz).strftime(fmt)
#         except AttributeError:
#             return
# 
# def build_query(filters):
#     return [tuple(f.split('=', 1)) for f in filters if '=' in f]


# Path: alertaclient/commands/cmd_query.py
import json
import click
from tabulate import tabulate
from alertaclient.models.alert import Alert
from alertaclient.utils import DateTime, build_query

    else:
        query = build_query(filters)
    if from_date:
        query.append(('from-date', from_date))

    r = client.http.get('/alerts', query, page=1, page_size=1000)

    if obj['output'] == 'json':
        click.echo(json.dumps(r['alerts'], sort_keys=True, indent=4, ensure_ascii=False))
    elif obj['output'] in ['json_lines', 'jsonl', 'ndjson']:
        for alert in r['alerts']:
            click.echo(json.dumps(alert, ensure_ascii=False))
    else:
        alerts = [Alert.parse(a) for a in r['alerts']]
        last_time = r['lastTime']
        auto_refresh = r['autoRefresh']

        if display == 'oneline':
            headers = {'id': 'ID', 'lastReceiveTime': 'LAST RECEIVED', 'severity': 'SEVERITY', 'status': 'STATUS',
                       'duplicateCount': 'DUPL', 'customer': 'CUSTOMER', 'environment': 'ENVIRONMENT', 'service': 'SERVICE',
                       'resource': 'RESOURCE', 'group': 'GROUP', 'event': 'EVENT', 'value': 'VALUE', 'text': 'DESCRIPTION'}

            data = [{k: v for k, v in a.tabular(timezone).items() if k in headers.keys()} for a in alerts]
            click.echo(tabulate(data, headers=headers, tablefmt=obj['output']))

        else:
            for alert in reversed(alerts):
                color = COLOR_MAP.get(alert.severity, {'fg': 'white'})
                click.secho('{}|{}|{}|{:5d}|{}|{:<5s}|{:<10s}|{:<18s}|{:12s}|{:16s}|{:12s}'.format(
                    alert.id[0:8],

                    DateTime.localtime(alert.last_receive_time, timezone),