Advertisement
dmilicev

digital_clock_v1.c

Nov 13th, 2019
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None
  1. /*
  2.  
  3.     digital_clock_v1.c
  4.  
  5.  
  6.     https://www.quora.com/How-do-I-print-a-digital-clock-in-C
  7.  
  8.     https://stackoverflow.com/questions/9253250/need-help-understanding-how-n-b-and-r-will-render-printf-output
  9.  
  10.     https://www.tutorialspoint.com/c-program-to-print-digital-clock-with-current-time
  11.  
  12.  
  13. size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
  14. Formats the time represented in the structure timeptr
  15. according to the formatting rules defined in format and stored into str.
  16. https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm
  17.  
  18. struct tm *localtime(const time_t *timer)
  19. Uses the time pointed by timer to fill a tm structure
  20. with the values that represent the corresponding local time.
  21. The value of timer is broken up into the structure tm
  22. and expressed in the local time zone.
  23. https://www.tutorialspoint.com/c_standard_library/c_function_localtime.htm
  24.  
  25.  
  26.  
  27.     You can find all my C programs at Dragan Milicev's pastebin:
  28.  
  29.     https://pastebin.com/u/dmilicev
  30.  
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <time.h>
  35. #include <unistd.h>
  36.  
  37. #define LEN 100
  38. #define ESC  27
  39.  
  40.  
  41. // wait (do nothing) for time in milliseconds
  42. void wait( int milliseconds )
  43. {
  44.     clock_t start_time = clock();       // get start time
  45.  
  46.     // looping (do nothing) till required time is not acheived
  47.     while ( clock() < start_time + milliseconds )
  48.         ;                               // do nothing
  49. }
  50.  
  51.  
  52. int main (void)
  53. {
  54.     int ch;                             // for pressed key
  55.     char buf[LEN];                      // string buffer
  56.     time_t curtime;                     // current local time from system
  57.     struct tm *loc_time;                // tm structure for local time
  58.  
  59.     printf("\n");                       // new line
  60.  
  61.     while(1)                // an infinite loop that exits with choice Ecsape, ESC (27)
  62.     {
  63.         while( !_kbhit() )  // rotates in that loop until some key is pressed
  64.         {
  65.             curtime = time (NULL);              // reset
  66.             loc_time = localtime (&curtime);    // get current local time from system
  67.  
  68.             // fill buf with local date and time              %I hour in 12h format (01-12)
  69.             //strftime( buf, LEN, "\r Date is %d.%m.%Y. Time is %I:%M:%S %p", loc_time );
  70.  
  71.             // or
  72.  
  73.             // fill buf with local date and time              %H hour in 24h format (00-23)
  74.             strftime( buf, LEN, "\r Date is %d.%m.%Y. Time is %H:%M:%S ", loc_time );
  75.  
  76.             fputs (buf, stdout);        // print string buf without '\n'
  77.  
  78.             wait(1000);                 // wait 1 second = 1000 ms
  79.  
  80.             // rewrites the same line from the beginning, \r Carriage Return
  81.             printf("\r                                        ");
  82.  
  83.         } // while( !_kbhit() )
  84.  
  85.         ch = _getch();                  // now key is pressed, read the keyboard
  86.  
  87.         if( ch == 0 || ch == 224 )      // if is pressed function key with leading 0 or 224
  88.             ch = _getch();              // let's empty that 0 or 224
  89.  
  90.         if( ch == ESC )                 // if is pressed ESC
  91.             return(0);                  // end of work, have to return something
  92.  
  93.     } // while(1)
  94.  
  95.    //return 0;                  // this line is never executed and is therefore commented
  96. }
Advertisement
RAW Paste Data Copied
Advertisement