74 lines
1.4 KiB
C
74 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
struct time {
|
|
unsigned int hours, minutes, seconds;
|
|
};
|
|
|
|
unsigned int timeToSeconds(struct time);
|
|
struct time secondsToTime(unsigned int);
|
|
void displayTime(unsigned int);
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct time t;
|
|
t.seconds = 0;
|
|
t.minutes = 0;
|
|
t.hours = 0;
|
|
char c;
|
|
|
|
if (argc == 1) {
|
|
printf("No time passed--using default: 1 min\n");
|
|
t.minutes = 1;
|
|
} else {
|
|
while ((c = getopt(argc, argv, "h:m:s:")) != -1) {
|
|
switch (c) {
|
|
case 'h':
|
|
t.hours = atoi(optarg);
|
|
break;
|
|
case 'm':
|
|
t.minutes = atoi(optarg);
|
|
break;
|
|
case 's':
|
|
t.seconds = atoi(optarg);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
unsigned int seconds = timeToSeconds(t);
|
|
while (seconds > 0) {
|
|
|
|
displayTime(seconds);
|
|
sleep(1);
|
|
seconds -= 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
unsigned int timeToSeconds(struct time t) {
|
|
unsigned int seconds = 0;
|
|
|
|
seconds = t.seconds;
|
|
seconds += t.minutes * 60;
|
|
seconds += t.hours * 60 * 60;
|
|
return seconds;
|
|
}
|
|
struct time secondsToTime(unsigned int seconds) {
|
|
struct time t;
|
|
|
|
t.hours = seconds / 3600;
|
|
t.minutes = (seconds - (3600 * t.hours)) / 60;
|
|
t.seconds = (seconds - (3600 * t.hours)) - (t.minutes * 60);
|
|
|
|
return t;
|
|
}
|
|
|
|
void displayTime(unsigned int seconds) {
|
|
system("clear");
|
|
struct time t = secondsToTime(seconds);
|
|
printf("%02d:%02d:%02d\n", t.hours, t.minutes, t.seconds);
|
|
}
|