Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * MAIN.CPP - 25.07.2021 @ 09:59
- * Marcin Gomulak aka Maki
- * makipol@gmail.com
- * PUBLIC DOMAIN - whatever
- *
- *
- * This tiny software just displays time in 24-hour mode updating every second
- */
- // ReSharper disable CommentTypo
- #include <iostream>
- #include <ctime>
- #include <chrono>
- using chrono = std::chrono::system_clock;
- #define NEWLINE << std::endl; // NOLINT(cppcoreguidelines-macro-usage) ||||| No, I don't agree
- #define __GLORYTEXT "glory to twenty-four time system!"
- #define WIDE_CHARS 1 //CHANGE TO SWITCH BETWEEN ANSI AND WIDE CHAR
- #if WIDE_CHARS
- #define _CHAR wchar_t //this is extremely dangerous- don't do that
- #define _COUT std::wcout
- #define SPRINTFS swprintf_s
- constexpr _CHAR gloryText[] = L" " __GLORYTEXT;
- constexpr _CHAR TIMEFORMAT[] = L"%02d:%02d:%02d - %ls";
- #else
- #define SPRINTFS sprintf_s
- #define _COUT std::cout
- #define _CHAR char
- constexpr _CHAR gloryText[] = u8" " __GLORYTEXT;
- constexpr _CHAR TIMEFORMAT[] = u8"%02d:%02d:%02d - %s";
- #endif
- constexpr uint16_t DEFAULT_BUFFER_SIZE = 1<<8; //8bit maximum + 1
- char buffer[DEFAULT_BUFFER_SIZE];
- _forceinline time_t GetChronoTime() { return chrono::to_time_t(chrono::now()); }
- static time_t newTime, oldTime = GetChronoTime();
- static _Notnull_ _CHAR * staticBuffer = nullptr;
- _CHAR* GetTimeFormatted()
- {
- tm tm; // NOLINT(cppcoreguidelines-pro-type-member-init |||| should we init the struct with {} or just
- //hold the return address to stack local space?
- localtime_s(&tm, &newTime); //struct tm holds parsed data
- if(staticBuffer == nullptr)
- staticBuffer = static_cast<_CHAR*>(malloc(DEFAULT_BUFFER_SIZE * sizeof(_CHAR)));
- #pragma warning (push)
- #pragma warning (disable: 6387) //I'm aware of it and it's not a case in here
- SPRINTFS(staticBuffer, DEFAULT_BUFFER_SIZE, TIMEFORMAT,
- tm.tm_hour, tm.tm_min, tm.tm_sec,
- gloryText);
- #pragma warning (pop)
- return staticBuffer;
- }
- int main()
- {
- while(true) { //main loop
- newTime = GetChronoTime(); //grab actual value
- if(oldTime != newTime) { //this is stack so whatever
- oldTime = newTime; //reupdate the value for last/old time
- _COUT << GetTimeFormatted() NEWLINE }
- } //end of endless loop
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement