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;
}