0

I wrote a program that uses the Windows Command Prompt to display a digital clock. I have been using the Chrono library to build this clock. It is designed to keep track of every minute of system time that passes since the program has opened, and then it will increment the clock. It does this all of this manually, and the program works the way I want it to (so far). Now since I have manually kept track of time - I am also curious as to if it is possible to pull the system time directly so that way when the program is opened it does not start at a manually initialized time, but rather what time it currently is.

So the general question is... using Chrono how do I pull the system time's hours and minutes? Thanks.

Code for reference...

#include <iostream>
#include <chrono>
using namespace std;

#include <Windows.h>

int nScreenWidth = 240;
int nScreenHeight = 80;

int nMapWidth = 8;
int nMapHeight = 5;

int main()
{
    // Create Screen Buffer
    wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
    HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(hConsole);
    DWORD dwBytesWritten = 0;

    // Current Time
    int hourDoubleDigit = 1;
    int hourSingleDigit = 2;
    int minuteDoubleDigit = 0;
    int minuteSingleDigit = 0;

    // Character Mapping
    wstring numberSystem[10] = {
    };

    numberSystem[0] += L"########"; numberSystem[1] += L"      ##"; numberSystem[2] += L"########";
    numberSystem[0] += L"##   ###"; numberSystem[1] += L"      ##"; numberSystem[2] += L"      ##";
    numberSystem[0] += L"## ## ##"; numberSystem[1] += L"      ##"; numberSystem[2] += L"########";
    numberSystem[0] += L"###   ##"; numberSystem[1] += L"      ##"; numberSystem[2] += L"##      ";
    numberSystem[0] += L"########"; numberSystem[1] += L"      ##"; numberSystem[2] += L"########";

    numberSystem[3] += L"########"; numberSystem[4] += L"##    ##"; numberSystem[5] += L"########";
    numberSystem[3] += L"      ##"; numberSystem[4] += L"##    ##"; numberSystem[5] += L"##      ";
    numberSystem[3] += L"########"; numberSystem[4] += L"########"; numberSystem[5] += L"########";
    numberSystem[3] += L"      ##"; numberSystem[4] += L"      ##"; numberSystem[5] += L"      ##";
    numberSystem[3] += L"########"; numberSystem[4] += L"      ##"; numberSystem[5] += L"########";

    numberSystem[6] += L"########"; numberSystem[7] += L"########"; numberSystem[8] += L"########"; numberSystem[9] += L"########";
    numberSystem[6] += L"##      "; numberSystem[7] += L"      ##"; numberSystem[8] += L"##    ##"; numberSystem[9] += L"##    ##";
    numberSystem[6] += L"########"; numberSystem[7] += L"      ##"; numberSystem[8] += L"########"; numberSystem[9] += L"########";
    numberSystem[6] += L"##    ##"; numberSystem[7] += L"      ##"; numberSystem[8] += L"##    ##"; numberSystem[9] += L"      ##";
    numberSystem[6] += L"########"; numberSystem[7] += L"      ##"; numberSystem[8] += L"########"; numberSystem[9] += L"########";

    // Keeping Track of System Time
    auto tp1 = chrono::system_clock::now();
    auto tp2 = chrono::system_clock::now();

    auto TickInterval = 60s;

    while (1)
    {
        for (int xy = 0; xy <= nScreenWidth * nScreenHeight; xy++) screen[xy] = ' ';//To prevent buggy character spawning...
        // You don't always have to write this above line if you're already making use of writing characters to every space in the screen, but I'm not!


        // Let the program know a minute has passed... (Time passes too much too often)
        tp2 = chrono::system_clock::now();
        chrono::duration<long double> delta__time = tp2 - tp1;
        bool TICKhasOCCURED = false;

        if (delta__time >= TickInterval)
        {
            tp1 += TickInterval;
            TICKhasOCCURED = true;
        }

        if (TICKhasOCCURED)
        {
            if (minuteSingleDigit >= 9)
            {
                if (minuteDoubleDigit >= 5)
                {

                    if (hourSingleDigit >= 9 && hourDoubleDigit == 0)
                    {
                        hourDoubleDigit++;
                        hourSingleDigit = 0;
                        minuteDoubleDigit = 0;
                        minuteSingleDigit = 0;
                    }
                    else if (hourSingleDigit >= 2 && hourDoubleDigit == 1)
                    {
                        hourDoubleDigit = 0;
                        hourSingleDigit = 1;
                        minuteDoubleDigit = 0;
                        minuteSingleDigit = 0;

                    }
                    else {
                        hourSingleDigit++;
                        minuteDoubleDigit = 0;
                        minuteSingleDigit = 0;
                    }//After 59 minutes check the hour and change it.
                }
                else {
                    minuteDoubleDigit++;
                    minuteSingleDigit = 0;
                }
            }
            else {
                minuteSingleDigit++;
            }
        }


        // The Current Time is then drawn out using the mapped characters.
        for(int ix = 0; ix < nMapWidth; ix++)
            for (int iy = 0; iy < nMapHeight; iy++)
            {
                screen[00 + ix + (nScreenWidth * iy)] = numberSystem[hourDoubleDigit][ix + (nMapWidth * iy)];
                screen[10 + ix + (nScreenWidth * iy)] = numberSystem[hourSingleDigit][ix + (nMapWidth * iy)];
                screen[20 + ix + (nScreenWidth * iy)] = numberSystem[minuteDoubleDigit][ix + (nMapWidth * iy)];
                screen[30 + ix + (nScreenWidth * iy)] = numberSystem[minuteSingleDigit][ix + (nMapWidth * iy)];
            }


        //Debug
        //cout << hourDoubleDigit << "\t" << hourSingleDigit << "\t" << minuteDoubleDigit << "\t" << minuteSingleDigit << endl;
        TICKhasOCCURED = false;

        screen[nScreenWidth * nScreenHeight-1] = '\0';
        WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
    }

    return 0;
}

Picture for reference...

Joshua
  • 1
  • you are already using `chrono::system_clock::now();` to get the time when your program started. Then you are using it again to get the current time and subtract the two. To only get the current time you have to do less, not more... – 463035818_is_not_an_ai Apr 12 '20 at 20:26
  • I see. Using chrono::system_clock::now(); is there already a built-in way to display the time as a 00:00 rather than by the amount of duration elapsed? If so, what would that be? – Joshua Apr 13 '20 at 02:38
  • sorry but I have to ask, did you write the code you posted yourself? – 463035818_is_not_an_ai Apr 13 '20 at 06:41
  • Yes from scratch. Truthfully. (Except the screen buffer) – Joshua Apr 13 '20 at 16:20
  • I learned everything I wrote from the following two videos: https://www.youtube.com/watch?v=xW8skO7MFYw https://www.youtube.com/watch?v=P32hvk8b13M – Joshua Apr 13 '20 at 16:23
  • ok, I had to ask to know what is your level and to be honest I had the tiny suspicion that you copied it from somewhere without proper attribution, sorry for that. Now I feel like I owe you an answer ;) I'll give it a try – 463035818_is_not_an_ai Apr 13 '20 at 16:23
  • Haha thank you. It's okay to ask I completely understand. – Joshua Apr 13 '20 at 16:24
  • actually I could not explain it better than the author himself. In [this answer](https://stackoverflow.com/a/15958113/4117728) he explains in great detail how to get hours and minutes (also year, month, etc) from `std::chrono::system_clock::now()`. I would suggest you to use functions to better seperate display and getting the time in your code. Perhaps a `my_timer` class with `get_hour()` and `get_minute()` methods. Then it will be easy to change what time you want to display, because you will only have to modify `my_timer` nothing else. – 463035818_is_not_an_ai Apr 13 '20 at 16:36

0 Answers0