MaKiPL

C++ time displayer - testing stuff

Jul 25th, 2021
916
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * MAIN.CPP - 25.07.2021 @ 09:59
  3.  * Marcin Gomulak aka Maki
  4.  * makipol@gmail.com
  5.  * PUBLIC DOMAIN - whatever
  6.  *
  7.  *
  8.  * This tiny software just displays time in 24-hour mode updating every second
  9.  */
  10.  
  11.  
  12. // ReSharper disable CommentTypo
  13. #include <iostream>
  14. #include <ctime>
  15. #include <chrono>
  16.  
  17. using chrono = std::chrono::system_clock;
  18. #define NEWLINE << std::endl;  // NOLINT(cppcoreguidelines-macro-usage) ||||| No, I don't agree
  19.  
  20. #define __GLORYTEXT "glory to twenty-four time system!"
  21.  
  22. #define WIDE_CHARS 1 //CHANGE TO SWITCH BETWEEN ANSI AND WIDE CHAR
  23. #if WIDE_CHARS
  24. #define _CHAR wchar_t //this is extremely dangerous- don't do that
  25. #define _COUT std::wcout
  26. #define SPRINTFS swprintf_s
  27. constexpr _CHAR gloryText[] = L" " __GLORYTEXT;
  28. constexpr _CHAR TIMEFORMAT[] = L"%02d:%02d:%02d - %ls";
  29. #else
  30. #define SPRINTFS sprintf_s
  31. #define _COUT std::cout
  32. #define _CHAR char
  33. constexpr _CHAR gloryText[] = u8" " __GLORYTEXT;
  34. constexpr _CHAR TIMEFORMAT[] = u8"%02d:%02d:%02d - %s";
  35. #endif
  36.  
  37. constexpr uint16_t DEFAULT_BUFFER_SIZE = 1<<8; //8bit maximum + 1
  38. char buffer[DEFAULT_BUFFER_SIZE];
  39. _forceinline time_t GetChronoTime() { return chrono::to_time_t(chrono::now()); }
  40. static time_t newTime, oldTime = GetChronoTime();
  41. static _Notnull_ _CHAR * staticBuffer = nullptr;
  42.  _CHAR* GetTimeFormatted()
  43. {
  44.     tm tm; // NOLINT(cppcoreguidelines-pro-type-member-init |||| should we init the struct with {} or just
  45.                                                                 //hold the return address to stack local space?
  46.     localtime_s(&tm, &newTime); //struct tm holds parsed data
  47.     if(staticBuffer == nullptr)
  48.         staticBuffer = static_cast<_CHAR*>(malloc(DEFAULT_BUFFER_SIZE * sizeof(_CHAR)));
  49. #pragma warning (push)
  50. #pragma warning (disable: 6387) //I'm aware of it and it's not a case in here
  51.     SPRINTFS(staticBuffer, DEFAULT_BUFFER_SIZE, TIMEFORMAT,
  52.         tm.tm_hour, tm.tm_min, tm.tm_sec,
  53.         gloryText);
  54. #pragma warning (pop)
  55.     return staticBuffer;
  56. }
  57.  
  58. int main()
  59. {
  60.     while(true) { //main loop
  61.         newTime = GetChronoTime(); //grab actual value
  62.         if(oldTime != newTime) {  //this is stack so whatever
  63.             oldTime = newTime; //reupdate the value for last/old time
  64.             _COUT << GetTimeFormatted() NEWLINE }
  65.                 } //end of endless loop
  66.     return 0;
  67. }
RAW Paste Data