#include "project.h"

#include <time.h>

TCHAR* int_build_date()
{
    // return a text string containing the current date and time based on your system e.g. UTC ,or say UTC+2,...

    // I specifically do NOT use the  USA / MicroSoft default format since it is not sortable as text
    // without rearrangement, or a custom sort function!
    //
    // The format generated here IS sortable!
    // Format:   yyyy:mm:dd  hh:mm:ss
    // e.g.      2025-06-29  12:47:20

    static TCHAR    myDATE[14]; //  =  { __DATE__[7], __DATE__[8], __DATE__[9], __DATE__[10], ' ', __DATE__[0], __DATE__[1], __DATE__[2], ' ', __DATE__[4], __DATE__[5], '\0' } ;

    int i;
    WORD month;
    TCHAR tmptime[32];
    struct tm    newTime;
    time_t      szClock;

    // Get time in seconds
    time(&szClock);

    // Convert time to struct tm form
    localtime_s(&newTime, &szClock);

    myDATE[0] = _TEXT(__DATE__[7]);
    myDATE[1] = _TEXT(__DATE__[8]);
    myDATE[2] = _TEXT(__DATE__[9]);
    myDATE[3] = _TEXT(__DATE__[10]);
    myDATE[4] = _TEXT('-');
    myDATE[5] = _TEXT(__DATE__[0]);
    myDATE[6] = _TEXT(__DATE__[1]);
    myDATE[7] = _TEXT(__DATE__[2]);
    myDATE[8] = _TEXT('-');
    myDATE[9] = _TEXT(__DATE__[4]);
    myDATE[10] = _TEXT(__DATE__[5]);
    myDATE[11] = _TEXT(' ');
    myDATE[12] = _TEXT('\0');

    i = -1;
    for (month = 0;month < 12;month++)
    {
        newTime.tm_mon = month;
        newTime.tm_mday = 1;    // Careful, if have date >27 and set month to feb can crash!

        // tmptime = asctime ( newTime ) ;
        _tasctime_s(tmptime, 32, &newTime);

        if ((tmptime[4] == __DATE__[0]) LAND(tmptime[5] == __DATE__[1]) LAND(tmptime[6] == __DATE__[2]))
        i = month + 1;
    }

    if (!(i == -1))
    {
        myDATE[5] = _TEXT('0');
        if (i > 9)
        {
            myDATE[5] = _TEXT('1');
            i = i - 10;
        }

        myDATE[6] = '0' + i;

        myDATE[7] = _TEXT('-');
        myDATE[8] = _TEXT(__DATE__[4]);
        myDATE[9] = _TEXT(__DATE__[5]);
        myDATE[10] = _TEXT(' ');
        myDATE[11] = _TEXT('\0');
    }

    if (!((myDATE[8] EQU _TEXT('0')) LOR(myDATE[8] EQU _TEXT('1')) LOR(myDATE[8] EQU _TEXT('2')) LOR(myDATE[8] EQU _TEXT('3'))))
    myDATE[8] = '0';

    return  myDATE;
}
