# 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


import curses
import sys
import time
from curses import wrapper
from datetime import datetime
from alertaclient.models.alert import Alert
from alertaclient.utils import DateTime

        # draw header
        self._addstr(0, 0, self.client.endpoint, curses.A_BOLD)
        self._addstr(0, 'C', f'alerta {version}', curses.A_BOLD)
        self._addstr(0, 'R', '{}'.format(now.strftime('%H:%M:%S %d/%m/%y')), curses.A_BOLD)

        # TODO - draw bars

        # draw alerts
        text_width = self.cols - 95 if self.cols >= 95 else 0
        self._addstr(2, 1, 'Sev. Time     Dupl. Customer Env.         Service      Resource     Group Event'
                     + '        Value Text' + ' ' * (text_width - 4), curses.A_UNDERLINE)

        def short_sev(severity):
            return self.SEVERITY_MAP.get(severity, self.SEVERITY_MAP['unknown'])[0]

        def color(severity):
            return self.SEVERITY_MAP.get(severity, self.SEVERITY_MAP['unknown'])[1]

        r = self.client.http.get('/alerts')
        alerts = [Alert.parse(a) for a in r['alerts']]
        last_time = DateTime.parse(r['lastTime'])

        for i, alert in enumerate(alerts):
            row = i + 3
            if row >= self.lines - 2:  # leave room for footer
                break

            text = '{:<4} {} {:5d} {:8.8} {:<12} {:<12} {:<12.12} {:5.5} {:<12.12} {:<5.5} {:.{width}}'.format(
                short_sev(alert.severity),

# 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


# Path: alertaclient/top.py
import curses
import sys
import time
from curses import wrapper
from datetime import datetime
from alertaclient.models.alert import Alert
from alertaclient.utils import DateTime


        # draw header
        self._addstr(0, 0, self.client.endpoint, curses.A_BOLD)
        self._addstr(0, 'C', f'alerta {version}', curses.A_BOLD)
        self._addstr(0, 'R', '{}'.format(now.strftime('%H:%M:%S %d/%m/%y')), curses.A_BOLD)

        # TODO - draw bars

        # draw alerts
        text_width = self.cols - 95 if self.cols >= 95 else 0
        self._addstr(2, 1, 'Sev. Time     Dupl. Customer Env.         Service      Resource     Group Event'
                     + '        Value Text' + ' ' * (text_width - 4), curses.A_UNDERLINE)

        def short_sev(severity):
            return self.SEVERITY_MAP.get(severity, self.SEVERITY_MAP['unknown'])[0]

        def color(severity):
            return self.SEVERITY_MAP.get(severity, self.SEVERITY_MAP['unknown'])[1]

        r = self.client.http.get('/alerts')
        alerts = [Alert.parse(a) for a in r['alerts']]
        last_time = DateTime.parse(r['lastTime'])

        for i, alert in enumerate(alerts):
            row = i + 3
            if row >= self.lines - 2:  # leave room for footer
                break

            text = '{:<4} {} {:5d} {:8.8} {:<12} {:<12} {:<12.12} {:5.5} {:<12.12} {:<5.5} {:.{width}}'.format(
                short_sev(alert.severity),

                DateTime.localtime(alert.last_receive_time, self.timezone, fmt='%H:%M:%S'),