// Timeux
// Written by Nils Liaaen Corneliusen. Revised 2026.
// Info here: https://www.ignorantus.com/news/2026/#23-January-2026
// License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

static uint64_t fonty[11] = {
    0x00001e333333331e, 0x00001e0c0c0c0e0c, 0x00003f03031e301f, 0x00001f30301c301f, // 0123
    0x0000183f191a1c18, 0x00001f30301f033f, 0x00001e33331f031e, 0x00000c0c0c18303f, // 4567
    0x00001e33331e331e, 0x00001e303e33331e, 0x0000060600060600,                     // 89:
};

int main( int argc, char *argv[] )
{
    time_t prevSec = 0;
    puts( "\n\n\n\n\n" );

    while( 1 ) {
        struct timeval tv;
        gettimeofday( &tv, NULL );
        if( tv.tv_sec == prevSec ) {
            usleep( 1000000 - tv.tv_usec );
            continue;
        }
        prevSec = tv.tv_sec;

        char *tim = ctime( &tv.tv_sec );
        tim[19] = 0; tim += 11;

        char row[80] = "\x1b[6A";
        for( int y = 0; y < 6; y++ ) {
            char *rp = row + (y==0)*4;
            for( int s = 0; tim[s]; s++ ) {
                uint64_t ch = fonty[tim[s]-0x30] >> 8*y;
                for( int x = (tim[s]==':')*2; x < 7; x++ ) {
                    *rp++ = 0x20+(ch&1)*3;
                    ch >>= 1;
                }
            }
            *rp = 0;
            puts( row );
        }
    }
    return 0;
}
