#!/usr/bin/python3
import gi, time
from datetime import datetime, timezone
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, Pango

class App(Gtk.Window):
    def __init__(self):
        super().__init__(title='Timestamp Tool')
        self.set_default_size(420, 320)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        vbox.set_margin_top(16); vbox.set_margin_bottom(16)
        vbox.set_margin_start(16); vbox.set_margin_end(16)
        self.add(vbox)
        # live timestamp
        frame = Gtk.Frame(label='Current Unix Timestamp')
        fb = Gtk.Box(spacing=6)
        fb.set_margin_top(8); fb.set_margin_bottom(8)
        fb.set_margin_start(8); fb.set_margin_end(8)
        self.live_lbl = Gtk.Label()
        self.live_lbl.override_font(Pango.FontDescription('Monospace Bold 20'))
        fb.pack_start(self.live_lbl, True, True, 0)
        frame.add(fb)
        vbox.pack_start(frame, False, False, 0)
        # unix → human
        f2 = Gtk.Frame(label='Unix → Human')
        f2b = Gtk.Grid(column_spacing=8, row_spacing=6)
        f2b.set_margin_top(8); f2b.set_margin_bottom(8)
        f2b.set_margin_start(8); f2b.set_margin_end(8)
        f2b.attach(Gtk.Label(label='Unix timestamp:', xalign=0), 0, 0, 1, 1)
        self.ts_entry = Gtk.Entry()
        f2b.attach(self.ts_entry, 1, 0, 1, 1)
        btn1 = Gtk.Button(label='Convert →')
        btn1.connect('clicked', self.ts_to_human)
        f2b.attach(btn1, 2, 0, 1, 1)
        self.human_out = Gtk.Label(label='', xalign=0, selectable=True)
        f2b.attach(self.human_out, 0, 1, 3, 1)
        f2.add(f2b)
        vbox.pack_start(f2, False, False, 0)
        # human → unix
        f3 = Gtk.Frame(label='Human → Unix')
        f3b = Gtk.Grid(column_spacing=8, row_spacing=6)
        f3b.set_margin_top(8); f3b.set_margin_bottom(8)
        f3b.set_margin_start(8); f3b.set_margin_end(8)
        f3b.attach(Gtk.Label(label='Date (YYYY-MM-DD HH:MM:SS):', xalign=0), 0, 0, 1, 1)
        self.dt_entry = Gtk.Entry()
        self.dt_entry.set_text(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        f3b.attach(self.dt_entry, 1, 0, 1, 1)
        btn2 = Gtk.Button(label='Convert →')
        btn2.connect('clicked', self.human_to_ts)
        f3b.attach(btn2, 2, 0, 1, 1)
        self.ts_out = Gtk.Label(label='', xalign=0, selectable=True)
        f3b.attach(self.ts_out, 0, 1, 3, 1)
        f3.add(f3b)
        vbox.pack_start(f3, False, False, 0)
        GLib.timeout_add(1000, self.update_live)
        self.update_live()
    def update_live(self):
        ts = int(time.time())
        self.live_lbl.set_text(str(ts))
        return True
    def ts_to_human(self, *_):
        try:
            ts = int(self.ts_entry.get_text().strip())
            dt = datetime.fromtimestamp(ts)
            utc = datetime.fromtimestamp(ts, tz=timezone.utc)
            self.human_out.set_text(f'Local: {dt.strftime("%Y-%m-%d %H:%M:%S")}  UTC: {utc.strftime("%Y-%m-%d %H:%M:%S")}')
        except Exception as e:
            self.human_out.set_text(f'Error: {e}')
    def human_to_ts(self, *_):
        try:
            dt = datetime.strptime(self.dt_entry.get_text().strip(), '%Y-%m-%d %H:%M:%S')
            self.ts_out.set_text(str(int(dt.timestamp())))
        except Exception as e:
            self.ts_out.set_text(f'Error: {e}')

win = App()
win.connect('destroy', Gtk.main_quit)
win.show_all()
Gtk.main()
