summaryrefslogtreecommitdiff
path: root/src/bin/elapsedtime.c
blob: bb91d99810706d152ac7f9c7ba1bbc44c956f214 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <string.h> //strcmp, strchr
#include <stdlib.h>

int main(int argc,char *argv[]) {
  char c;//comma?
  long long ll;
  short s=0;
  if(argc < 2) {
    printf("usage: elapsedtime [-s|--short] seconds\n");
    return 1;
  }
  argv++;
  if(!strcmp(*argv,"-s") || !strcmp(*argv,"--short")) {
    s=1;
    argv++;
  }
  char *f=strchr(*argv,'.');
  ll=atoll(*argv);
  if(s) {
    if(ll / 3600) {
      printf("%lld:%lld:%02lld%s\n",ll / 3600, ll / 60 % 60,ll % 60,f?f:"");
    } else {
      printf("%lld:%02lld%s\n",ll / 60 % 60,ll % 60,f?f:"");
    }
    return 0;
  }
  if(ll / 31557600      ) { if(c) printf(", ");c=1;printf("%lld year%s",  ll / 31557600,    ll / 31557600 == 1 ? "" : "s"); }
  if(ll % 31557600 / 86400) { if(c) printf(", ");c=1;printf("%lld day%s",   ll % 31557600 / 86400, ll % 31557600 / 86400 == 1 ? "" : "s"); }
  if(ll / 3600     %  24) { if(c) printf(", ");c=1;printf("%lld hour%s",  ll / 3600 % 24,   ll / 3600 % 24 == 1 ? "" : "s"); }
  if(ll / 60       %  60) { if(c) printf(", ");c=1;printf("%lld minute%s",ll / 60 % 60,     ll / 60 % 60 == 1 ? "" : "s"); }
  if(ll            %  60) { if(c) printf(", ");c=1;printf("%lld%s second%s",ll % 60,f?f:"",          ll % 60 == 1 && !f ? "" : "s"); }
  putchar('\n');
  return 0;
}