|
a |
|
b/HRV.src/hours.c |
|
|
1 |
/* hours.c Joe Mietus Oct 7 2008 */ |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
hours : |
|
|
5 |
Usage: hours seconds | - |
|
|
6 |
Convert seconds to hh:mm:ss. |
|
|
7 |
`-' to read stdin |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
#include <stdio.h> |
|
|
11 |
#include <stdlib.h> |
|
|
12 |
|
|
|
13 |
main(argc, argv) |
|
|
14 |
int argc; |
|
|
15 |
char *argv[]; |
|
|
16 |
{ |
|
|
17 |
long sec, atol(); |
|
|
18 |
char *timstr(); |
|
|
19 |
|
|
|
20 |
if (argc < 2) { |
|
|
21 |
usage(argv[0]); |
|
|
22 |
exit(1); |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
if (*argv[1] == '-') |
|
|
26 |
switch (argv[1][1]) { |
|
|
27 |
case '\0' : while (scanf("%ld", &sec) == 1) |
|
|
28 |
printf("%s\n", timstr(sec)); |
|
|
29 |
exit(0); |
|
|
30 |
default : usage(argv[0]); |
|
|
31 |
exit(1); |
|
|
32 |
} |
|
|
33 |
else |
|
|
34 |
printf("%s\n", timstr(atol(argv[1]))); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
char *timstr(time) /* convert seconds to [hh:]mm:ss */ |
|
|
39 |
long time; |
|
|
40 |
{ |
|
|
41 |
int hours, minutes, seconds; |
|
|
42 |
static char buf[9]; |
|
|
43 |
|
|
|
44 |
hours = time / 3600L; time -= (long)hours * 3600; |
|
|
45 |
minutes = time / 60; |
|
|
46 |
seconds = time - minutes * 60; |
|
|
47 |
sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds); |
|
|
48 |
return (buf); |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
|
|
|
52 |
usage(prog) |
|
|
53 |
char *prog; |
|
|
54 |
{ |
|
|
55 |
fprintf(stderr, "Usage: %s seconds | -\n", prog); |
|
|
56 |
fprintf(stderr, " Convert seconds to hh:mm:ss.\n"); |
|
|
57 |
fprintf(stderr, " `-' to read stdin\n"); |
|
|
58 |
} |