Julio Biason
4 years ago
5 changed files with 221 additions and 5 deletions
@ -0,0 +1,112 @@
|
||||
#include <math.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <time.h> |
||||
#include "hours.h" |
||||
|
||||
struct hoursStruct { |
||||
int minutes; |
||||
}; |
||||
|
||||
/**
|
||||
* Create a new hours structure. |
||||
*/ |
||||
hours* hours_new(char *input) |
||||
{ |
||||
struct hoursStruct* result = NULL; |
||||
|
||||
char *duplicate = malloc(sizeof(char) * strlen(input) + 1); |
||||
sprintf(duplicate, "%s", input); |
||||
|
||||
char *colon = strchr(duplicate, ':'); |
||||
if (colon == NULL) { |
||||
goto cleanup; |
||||
} |
||||
|
||||
char* minutes_pos = colon + 1; |
||||
*colon = '\0'; |
||||
|
||||
int hours = atoi(duplicate); |
||||
int minutes = atoi(minutes_pos); |
||||
|
||||
if (hours < 0 || hours > 23) { |
||||
goto cleanup; |
||||
} |
||||
|
||||
if (minutes < 0 || minutes > 59) { |
||||
goto cleanup; |
||||
} |
||||
|
||||
int total_minutes = hours * 60 + minutes; |
||||
|
||||
result = malloc(sizeof(struct hoursStruct)); |
||||
result->minutes = total_minutes; |
||||
|
||||
cleanup: |
||||
free(duplicate); |
||||
return result; |
||||
} |
||||
|
||||
/**
|
||||
* Create an Hours with the current time. |
||||
*/ |
||||
hours* hours_right_now() |
||||
{ |
||||
struct hoursStruct* result = malloc(sizeof(struct hoursStruct)); |
||||
time_t right_now; |
||||
time(&right_now); |
||||
struct tm* local = localtime(&right_now); |
||||
|
||||
result->minutes = local->tm_hour * 60 + local->tm_min; |
||||
return result; |
||||
} |
||||
|
||||
/**
|
||||
* Substract one hour from another. Returns a new hours struct. |
||||
*/ |
||||
hours* hours_sub(hours* self, hours* other) |
||||
{ |
||||
struct hoursStruct* result = malloc(sizeof(struct hoursStruct)); |
||||
result->minutes = other->minutes - self->minutes; |
||||
return result; |
||||
} |
||||
|
||||
/**
|
||||
* Add one hour to to another. Returns a new hours struct. |
||||
*/ |
||||
hours* hours_add(hours* self, hours* other) |
||||
{ |
||||
struct hoursStruct* result = malloc(sizeof(struct hoursStruct)); |
||||
result->minutes = other->minutes + self->minutes; |
||||
return result; |
||||
} |
||||
|
||||
/**
|
||||
* Pretty display for hours. |
||||
*/ |
||||
char *hours_display(hours* self) |
||||
{ |
||||
int hours = (int)ceil(self->minutes / 60); |
||||
int minutes = self->minutes - (hours * 60); |
||||
|
||||
char* result = malloc(sizeof(char) * 6); // XX:XX\0
|
||||
sprintf(result, "%02d:%02d", hours, minutes); |
||||
return result; |
||||
} |
||||
|
||||
/**
|
||||
* Checks if the hours happens before the other; returns -1 if it is earlier or 1 if it is later. |
||||
*/ |
||||
int hours_preceed(hours* self, hours* other) { |
||||
return self->minutes < other->minutes |
||||
? -1 |
||||
: 1; |
||||
} |
||||
|
||||
/**
|
||||
* Free the memory used by the hours. |
||||
*/ |
||||
void hours_free(hours* self) { |
||||
free(self); |
||||
} |
@ -0,0 +1,14 @@
|
||||
#ifndef HOURS_H |
||||
#define HOURS_H |
||||
|
||||
typedef struct hoursStruct hours; |
||||
|
||||
hours* hours_new(char *input); |
||||
hours* hours_right_now(); |
||||
hours* hours_sub(hours* self, hours* other); |
||||
hours* hours_add(hours* self, hours* other); |
||||
char* hours_display(hours* self); |
||||
void hours_free(hours* self); |
||||
int hours_preceed(hours* self, hours* other); |
||||
|
||||
#endif |
@ -0,0 +1,85 @@
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "hours.h" |
||||
|
||||
int main(int argc, char* argv[]) { |
||||
hours *start = NULL; |
||||
hours *lunch_break = NULL; |
||||
hours *lunch_return = NULL; |
||||
|
||||
if (argc <= 1) { |
||||
printf("Usage: wh START_TIME [LUNCH_BREAK] [LUNCH_RETURN]\n"); |
||||
return EXIT_FAILURE; |
||||
} |
||||
|
||||
if (argc >= 2) { |
||||
start = hours_new(argv[1]); |
||||
if (start == NULL) { |
||||
printf("Invalid START_TIME\n"); |
||||
return EXIT_FAILURE; |
||||
} |
||||
} |
||||
|
||||
if (argc >= 3) { |
||||
lunch_break = hours_new(argv[2]); |
||||
if (lunch_break == NULL) { |
||||
printf("Invalid LUNCH_BREAK\n"); |
||||
return EXIT_FAILURE; |
||||
} |
||||
} else { |
||||
lunch_break = hours_new("12:00"); |
||||
} |
||||
|
||||
if (argc >= 4) { |
||||
lunch_return = hours_new(argv[3]); |
||||
if (lunch_return == NULL) { |
||||
printf("Invalid LUNCH_RETURN\n"); |
||||
return EXIT_FAILURE; |
||||
} |
||||
} else { |
||||
lunch_return = hours_new("13:00"); |
||||
} |
||||
|
||||
hours* now = hours_right_now(); |
||||
hours* so_far; |
||||
if (hours_preceed(now, lunch_return) == -1) { |
||||
so_far = hours_sub(start, now); |
||||
} else { |
||||
hours* before_lunch = hours_sub(start, lunch_break); |
||||
hours* after_lunch = hours_sub(lunch_return, now); |
||||
|
||||
so_far = hours_add(before_lunch, after_lunch); |
||||
|
||||
hours_free(before_lunch); |
||||
hours_free(after_lunch); |
||||
} |
||||
|
||||
char *display = hours_display(so_far); |
||||
|
||||
printf("So far, you've worked %s\n", display); |
||||
|
||||
hours* must_work = hours_new("08:48"); |
||||
if (hours_preceed(so_far, must_work) == -1) { |
||||
hours* remaining = hours_sub(so_far, must_work); |
||||
char *still = hours_display(remaining); |
||||
printf("You still need to work %s\n", still); |
||||
free(still); |
||||
hours_free(remaining); |
||||
} else { |
||||
hours* exceeding = hours_sub(must_work, so_far); |
||||
char* extra = hours_display(exceeding); |
||||
printf("You did %s of extra work\n", extra); |
||||
free(extra); |
||||
hours_free(exceeding); |
||||
} |
||||
|
||||
free(display); |
||||
hours_free(so_far); |
||||
hours_free(now); |
||||
hours_free(start); |
||||
hours_free(lunch_break); |
||||
hours_free(lunch_return); |
||||
|
||||
return EXIT_SUCCESS; |
||||
} |
Loading…
Reference in new issue