Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- digital_clock_v1.c
- https://www.quora.com/How-do-I-print-a-digital-clock-in-C
- https://stackoverflow.com/questions/9253250/need-help-understanding-how-n-b-and-r-will-render-printf-output
- https://www.tutorialspoint.com/c-program-to-print-digital-clock-with-current-time
- size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
- Formats the time represented in the structure timeptr
- according to the formatting rules defined in format and stored into str.
- https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm
- struct tm *localtime(const time_t *timer)
- Uses the time pointed by timer to fill a tm structure
- with the values that represent the corresponding local time.
- The value of timer is broken up into the structure tm
- and expressed in the local time zone.
- https://www.tutorialspoint.com/c_standard_library/c_function_localtime.htm
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <time.h>
- #include <unistd.h>
- #define LEN 100
- #define ESC 27
- // wait (do nothing) for time in milliseconds
- void wait( int milliseconds )
- {
- clock_t start_time = clock(); // get start time
- // looping (do nothing) till required time is not acheived
- while ( clock() < start_time + milliseconds )
- ; // do nothing
- }
- int main (void)
- {
- int ch; // for pressed key
- char buf[LEN]; // string buffer
- time_t curtime; // current local time from system
- struct tm *loc_time; // tm structure for local time
- printf("\n"); // new line
- while(1) // an infinite loop that exits with choice Ecsape, ESC (27)
- {
- while( !_kbhit() ) // rotates in that loop until some key is pressed
- {
- curtime = time (NULL); // reset
- loc_time = localtime (&curtime); // get current local time from system
- // fill buf with local date and time %I hour in 12h format (01-12)
- //strftime( buf, LEN, "\r Date is %d.%m.%Y. Time is %I:%M:%S %p", loc_time );
- // or
- // fill buf with local date and time %H hour in 24h format (00-23)
- strftime( buf, LEN, "\r Date is %d.%m.%Y. Time is %H:%M:%S ", loc_time );
- fputs (buf, stdout); // print string buf without '\n'
- wait(1000); // wait 1 second = 1000 ms
- // rewrites the same line from the beginning, \r Carriage Return
- printf("\r ");
- } // while( !_kbhit() )
- ch = _getch(); // now key is pressed, read the keyboard
- if( ch == 0 || ch == 224 ) // if is pressed function key with leading 0 or 224
- ch = _getch(); // let's empty that 0 or 224
- if( ch == ESC ) // if is pressed ESC
- return(0); // end of work, have to return something
- } // while(1)
- //return 0; // this line is never executed and is therefore commented
- }
Advertisement
RAW Paste Data
Copied
Advertisement