Advertisement
MaKiPL

C++ time displayer - testing stuff

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