Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- time_and_date_in_c_v1.c
- C date and time:
- https://en.wikipedia.org/wiki/C_date_and_time_functions
- https://en.wikibooks.org/wiki/C_Programming/time.h/time_t
- https://www.thegeekdiary.com/c-library/
- http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.dinkum_en_c99%2Ftime.html
- https://en.cppreference.com/w/c/chrono/time_t
- http://zetcode.com/articles/cdatetime/
- http://zetcode.com/gui/winapi/datetime/
- https://c-for-dummies.com/blog/?p=3120
- C function difftime()
- https://www.tutorialspoint.com/c_standard_library/c_function_difftime.htm
- Description
- The C library function double difftime(time_t time1, time_t time2)
- returns the difference of seconds between time1 and time2 i.e. (time1 - time2).
- The two times are specified in calendar time, which represents the time elapsed
- since the Epoch (00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)).
- Declaration
- Following is the declaration for difftime() function.
- double difftime(time_t time1, time_t time2)
- Parameters
- time1 − This is the time_t object for end time.
- time2 − This is the time_t object for start time.
- Return Value
- This function returns the difference of two times (time1 - time2) as a double value.
- C function time()
- https://www.tutorialspoint.com/c_standard_library/c_function_time.htm
- Description
- The C library function time_t time(time_t *seconds) returns the time
- since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
- If seconds is not NULL, the return value is also stored in variable seconds.
- Declaration
- Following is the declaration for time() function.
- time_t time(time_t *t)
- Parameters
- seconds − This is the pointer to an object of type time_t,
- where the seconds value will be stored.
- Return Value
- The current calendar time as a time_t object.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <time.h> // for time(), difftime()
- #include <stdlib.h> // for exit()
- int main(void)
- {
- int i, j;
- time_t seconds;
- time_t start_t, end_t;
- time_t now;
- time_t current_time;
- double diff_t;
- struct tm *ts;
- char buf[80];
- char* c_time_string;
- printf("Starting of the program...\n");
- time(&start_t); // get start time
- printf("\n Work something ... \n");
- for(i=0;i<100;i++)
- for(j=0;j<100;j++)
- printf(" %6d ", i*j);
- time(&end_t); // get end time
- diff_t = difftime(end_t, start_t);
- printf("\n Execution time = %f \n", diff_t);
- seconds = time(NULL);
- printf("\n Hours since January 1, 1970 = %ld \n", seconds/3600);
- now = time(NULL); // Get the current time
- // Format and print the time, "\n ddd yyyy-mm-dd hh:mm:ss zzz \n"
- ts = localtime(&now);
- strftime(buf, sizeof(buf), "\n %a %Y-%m-%d %H:%M:%S %Z \n", ts);
- puts(buf);
- current_time = time(NULL); // Obtain current time.
- if (current_time == ((time_t)-1))
- {
- (void) fprintf(stderr, "Failure to obtain the current time.\n");
- exit(EXIT_FAILURE);
- }
- c_time_string = ctime(¤t_time); // Convert to local time format.
- if (c_time_string == NULL)
- {
- (void) fprintf(stderr, "Failure to convert the current time.\n");
- exit(EXIT_FAILURE);
- }
- // Print to stdout. ctime() has already added a terminating newline character.
- (void) printf(" Current time is %s \n", c_time_string);
- exit(EXIT_SUCCESS);
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment