cyter

raterhub_logger.c

Jul 23rd, 2018
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7.  
  8.  
  9. void welcome_msg();
  10.  
  11. void session(const int time_base); // starts and logs/records a new session (punch in)
  12.  
  13. void punch_out(FILE *fp, unsigned int h, unsigned int m, unsigned int s);
  14.  
  15. void reset(unsigned int* time_unit, const int time_base); // resets time_unit to remainder
  16.  
  17. void time_stamp(FILE* fp, char* time_code); // Time code is punched in or punched out
  18.  
  19. void goodbye_msg();
  20.  
  21.  
  22.  
  23.  
  24. int main(){
  25.     const int time_base = 60;  //time base unit, a constant
  26.  
  27.     welcome_msg();
  28.    
  29.     char answer = getchar();
  30.  
  31.     // New session
  32.     if(answer == 's' || answer == 'S'){
  33.         session(time_base);
  34.     }
  35.     goodbye_msg();
  36.  
  37.     return 0;
  38. }
  39.  
  40.  
  41.  
  42. /*
  43.  * Function definitions
  44.  * */
  45.  
  46. void reset(unsigned int* time_unit, const int time_base)
  47. {
  48.     *time_unit = *time_unit % time_base;
  49. }
  50.  
  51. void welcome_msg()
  52. {
  53.     printf("\nWelcome to the worktime tracking system.\nEnter:\n");
  54.     printf("\tS\\s: To start a new worktime session.\n\n");
  55.     printf("\tE\\e: To exit. (CTRL-D is an equivalent of EOF)\n");
  56. }
  57.  
  58. void goodbye_msg()
  59. {
  60.     printf("\nThank you for using the worktime tracking system.\n");
  61.     printf("Goodbye!\n");
  62. }
  63.  
  64. /*
  65. *   TIME_STAMP
  66. *   inputs: FILE *fp to write to, time_code (whether punching in or out)
  67. */
  68. void time_stamp(FILE* fp, char* time_code)
  69. {
  70.     time_t current_time;
  71.     char* c_time_string;
  72.  
  73.     // Obtain current time.
  74.     current_time = time(NULL);
  75.  
  76.     if(current_time == ((time_t)-1))
  77.     {
  78.         (void)fprintf(stderr, "Failure to obtain time.\n");
  79.         exit(EXIT_FAILURE);
  80.     }
  81.  
  82.     // Convert to local time format.
  83.     c_time_string = ctime(&current_time);
  84.  
  85.     if(c_time_string == NULL)
  86.     {
  87.         (void) fprintf(stderr, "Failure to convert the current time.\n");  
  88.         exit(EXIT_FAILURE);
  89.     }
  90.  
  91.     // Print to stdout. ctime() has already added a terminating newline character.
  92.     strcat(time_code, c_time_string);
  93.     (void) fprintf(fp, "\nYou %s.", time_code);
  94. }
  95.  
  96. /*
  97. *   PUNCH_OUT
  98. *   inputs: FILE *fp, unsigned in h, unsigned int m, unsinged int s
  99. *   outputs: void
  100. */
  101. void punch_out(FILE *fp, unsigned int h, unsigned int m, unsigned int s){
  102.     char time_code[45] = "punched out: ";
  103.  
  104.     // print to stdout
  105.     printf(
  106.         "You have worked: %d hours, %d minutes, and %d seconds.\n",
  107.         h, m, s
  108.     );
  109.  
  110.     //print to file
  111.     fprintf(
  112.         fp,
  113.         "You have worked: %d hours, %d minutes, and %d seconds.",
  114.         h, m, s
  115.     );
  116.  
  117.     time_stamp(fp, time_code);// Ending time for a session
  118. }
  119.  
  120. /*
  121. *   SESSION
  122. *   inputs: const int time_base
  123. */
  124. void session(const int time_base)
  125. {
  126.     unsigned int hours = 0, minutes = 0, seconds = 0, session_count = 0;
  127.     double new_minutes;
  128.     FILE *fp = fopen("hours.txt","a");
  129.     char time_code[45] = "punched in: ";
  130.  
  131.     if(fp){
  132.     (void) fprintf(fp, "\nStarting new session.");
  133.         time_stamp(fp, time_code);
  134.     } else{
  135.         perror("Failed to open the file for writing.\nExiting.\n");
  136.         exit(EXIT_FAILURE);
  137.     }
  138.  
  139.     printf("Starting new session for time tracking.\n");
  140.     printf("CTRL-D at any time to save state and exit.\n");
  141.  
  142.     printf("Add minutes: ");
  143.     while(scanf("%lf", &new_minutes) != EOF){      
  144.         minutes += (unsigned int)new_minutes;
  145.         seconds += (new_minutes - floor(new_minutes))*100 + 1;//recover lost second
  146.  
  147.         if( minutes >= time_base )
  148.         {
  149.             hours += minutes / time_base;
  150.             reset(&minutes, time_base);
  151.         }
  152.         if( seconds >= time_base)
  153.         {
  154.             minutes += seconds / time_base;
  155.             reset(&seconds, time_base);
  156.         }   session_count++;   
  157.         printf("Add minutes: ");       
  158.     }
  159.  
  160.     // We are done with the session/exited, time to cleanup
  161.     if(session_count < 1){
  162.         // this is an exit
  163.         fclose(fp);
  164.     } else{// valid session so log out
  165.         punch_out(fp, hours, minutes, seconds);
  166.         (void) fprintf(fp, "Ending session.\n");
  167.         fclose(fp);
  168.     }
  169. }
Add Comment
Please, Sign In to add comment