dmilicev

time_and_date_in_c_v1.c

Sep 7th, 2020
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     time_and_date_in_c_v1.c
  4.  
  5. C date and time:
  6. https://en.wikipedia.org/wiki/C_date_and_time_functions
  7. https://en.wikibooks.org/wiki/C_Programming/time.h/time_t
  8. https://www.thegeekdiary.com/c-library/
  9. http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.dinkum_en_c99%2Ftime.html
  10. https://en.cppreference.com/w/c/chrono/time_t
  11. http://zetcode.com/articles/cdatetime/
  12. http://zetcode.com/gui/winapi/datetime/
  13. https://c-for-dummies.com/blog/?p=3120
  14.  
  15. C function difftime()
  16. https://www.tutorialspoint.com/c_standard_library/c_function_difftime.htm
  17.  
  18. Description
  19. The C library function double difftime(time_t time1, time_t time2)
  20. returns the difference of seconds between time1 and time2 i.e. (time1 - time2).
  21. The two times are specified in calendar time, which represents the time elapsed
  22. since the Epoch (00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)).
  23.  
  24. Declaration
  25. Following is the declaration for difftime() function.
  26. double difftime(time_t time1, time_t time2)
  27.  
  28. Parameters
  29. time1 − This is the time_t object for end time.
  30. time2 − This is the time_t object for start time.
  31.  
  32. Return Value
  33. This function returns the difference of two times (time1 - time2) as a double value.
  34.  
  35.  
  36. C function time()
  37. https://www.tutorialspoint.com/c_standard_library/c_function_time.htm
  38.  
  39. Description
  40. The C library function time_t time(time_t *seconds) returns the time
  41. since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
  42. If seconds is not NULL, the return value is also stored in variable seconds.
  43.  
  44. Declaration
  45. Following is the declaration for time() function.
  46. time_t time(time_t *t)
  47.  
  48. Parameters
  49. seconds − This is the pointer to an object of type time_t,
  50. where the seconds value will be stored.
  51.  
  52. Return Value
  53. The current calendar time as a time_t object.
  54.  
  55.  
  56.     You can find all my C programs at Dragan Milicev's pastebin:
  57.  
  58.     https://pastebin.com/u/dmilicev
  59.  
  60. */
  61.  
  62. #include <stdio.h>
  63. #include <time.h>       // for time(), difftime()
  64. #include <stdlib.h>     // for exit()
  65.  
  66. int main(void)
  67. {
  68.     int i, j;
  69.     time_t seconds;
  70.     time_t start_t, end_t;
  71.     time_t now;
  72.     time_t current_time;
  73.     double diff_t;
  74.     struct tm *ts;
  75.     char       buf[80];
  76.     char* c_time_string;
  77.  
  78.     printf("Starting of the program...\n");
  79.     time(&start_t);     // get start time
  80.  
  81.     printf("\n Work something ... \n");
  82.     for(i=0;i<100;i++)
  83.         for(j=0;j<100;j++)
  84.             printf(" %6d ", i*j);
  85.  
  86.     time(&end_t);       // get end time
  87.     diff_t = difftime(end_t, start_t);
  88.  
  89.     printf("\n Execution time = %f \n", diff_t);
  90.  
  91.  
  92.     seconds = time(NULL);
  93.     printf("\n Hours since January 1, 1970 = %ld \n", seconds/3600);
  94.  
  95.  
  96.     now = time(NULL);   // Get the current time
  97.  
  98.     // Format and print the time, "\n ddd yyyy-mm-dd hh:mm:ss zzz \n"
  99.     ts = localtime(&now);
  100.     strftime(buf, sizeof(buf), "\n %a %Y-%m-%d %H:%M:%S %Z \n", ts);
  101.     puts(buf);
  102.  
  103.  
  104.     current_time = time(NULL);  // Obtain current time.
  105.  
  106.     if (current_time == ((time_t)-1))
  107.     {
  108.         (void) fprintf(stderr, "Failure to obtain the current time.\n");
  109.         exit(EXIT_FAILURE);
  110.     }
  111.  
  112.     c_time_string = ctime(&current_time);   // Convert to local time format.
  113.  
  114.     if (c_time_string == NULL)
  115.     {
  116.         (void) fprintf(stderr, "Failure to convert the current time.\n");
  117.         exit(EXIT_FAILURE);
  118.     }
  119.  
  120.     // Print to stdout. ctime() has already added a terminating newline character.
  121.     (void) printf(" Current time is %s \n", c_time_string);
  122.  
  123.     exit(EXIT_SUCCESS);
  124.  
  125.  
  126.     return 0;
  127.  
  128. } // main()
  129.  
RAW Paste Data Copied