Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <time.h>
- void welcome_msg();
- void session(const int time_base); // starts and logs/records a new session (punch in)
- void punch_out(FILE *fp, unsigned int h, unsigned int m, unsigned int s);
- void reset(unsigned int* time_unit, const int time_base); // resets time_unit to remainder
- void time_stamp(FILE* fp, char* time_code); // Time code is punched in or punched out
- void goodbye_msg();
- int main(){
- const int time_base = 60; //time base unit, a constant
- welcome_msg();
- char answer = getchar();
- // New session
- if(answer == 's' || answer == 'S'){
- session(time_base);
- }
- goodbye_msg();
- return 0;
- }
- /*
- * Function definitions
- * */
- void reset(unsigned int* time_unit, const int time_base)
- {
- *time_unit = *time_unit % time_base;
- }
- void welcome_msg()
- {
- printf("\nWelcome to the worktime tracking system.\nEnter:\n");
- printf("\tS\\s: To start a new worktime session.\n\n");
- printf("\tE\\e: To exit. (CTRL-D is an equivalent of EOF)\n");
- }
- void goodbye_msg()
- {
- printf("\nThank you for using the worktime tracking system.\n");
- printf("Goodbye!\n");
- }
- /*
- * TIME_STAMP
- * inputs: FILE *fp to write to, time_code (whether punching in or out)
- */
- void time_stamp(FILE* fp, char* time_code)
- {
- time_t current_time;
- char* c_time_string;
- // Obtain current time.
- current_time = time(NULL);
- if(current_time == ((time_t)-1))
- {
- (void)fprintf(stderr, "Failure to obtain time.\n");
- exit(EXIT_FAILURE);
- }
- // Convert to local time format.
- c_time_string = ctime(¤t_time);
- 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.
- strcat(time_code, c_time_string);
- (void) fprintf(fp, "\nYou %s.", time_code);
- }
- /*
- * PUNCH_OUT
- * inputs: FILE *fp, unsigned in h, unsigned int m, unsinged int s
- * outputs: void
- */
- void punch_out(FILE *fp, unsigned int h, unsigned int m, unsigned int s){
- char time_code[45] = "punched out: ";
- // print to stdout
- printf(
- "You have worked: %d hours, %d minutes, and %d seconds.\n",
- h, m, s
- );
- //print to file
- fprintf(
- fp,
- "You have worked: %d hours, %d minutes, and %d seconds.",
- h, m, s
- );
- time_stamp(fp, time_code);// Ending time for a session
- }
- /*
- * SESSION
- * inputs: const int time_base
- */
- void session(const int time_base)
- {
- unsigned int hours = 0, minutes = 0, seconds = 0, session_count = 0;
- double new_minutes;
- FILE *fp = fopen("hours.txt","a");
- char time_code[45] = "punched in: ";
- if(fp){
- (void) fprintf(fp, "\nStarting new session.");
- time_stamp(fp, time_code);
- } else{
- perror("Failed to open the file for writing.\nExiting.\n");
- exit(EXIT_FAILURE);
- }
- printf("Starting new session for time tracking.\n");
- printf("CTRL-D at any time to save state and exit.\n");
- printf("Add minutes: ");
- while(scanf("%lf", &new_minutes) != EOF){
- minutes += (unsigned int)new_minutes;
- seconds += (new_minutes - floor(new_minutes))*100 + 1;//recover lost second
- if( minutes >= time_base )
- {
- hours += minutes / time_base;
- reset(&minutes, time_base);
- }
- if( seconds >= time_base)
- {
- minutes += seconds / time_base;
- reset(&seconds, time_base);
- } session_count++;
- printf("Add minutes: ");
- }
- // We are done with the session/exited, time to cleanup
- if(session_count < 1){
- // this is an exit
- fclose(fp);
- } else{// valid session so log out
- punch_out(fp, hours, minutes, seconds);
- (void) fprintf(fp, "Ending session.\n");
- fclose(fp);
- }
- }
Add Comment
Please, Sign In to add comment