#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name,redefined-outer-name
"""
Convert datetime from unix timestamp in log files
"""
from __future__ import print_function
import fileinput
import re
import sys
from datetime import datetime

UNIX_TS = re.compile(r"\[(\d{10})\]")
DATE_FMT = "%Y-%m-%d %H:%M:%S"


def convert_line(line):
    """Convert unix time to local time"""

    def replace(match):
        """Return the hex string for a decimal number"""
        return "[%s]" % datetime.fromtimestamp(float(match.group(1))).strftime(DATE_FMT)

    print(UNIX_TS.sub(replace, line), end="")


if __name__ == "__main__":
    if len(sys.argv) > 1:
        fname = sys.argv[1]
        with open(fname, "rt") as fptr:
            for line in fptr.readlines():
                convert_line(line)
    else:
        for line in fileinput.input():
            convert_line(line)
