Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static char *strdelta(int delta, char *buf, int compact) {
- *buf = '\0';
- const char *tail = delta < 0 ? "ago" : "from now";
- struct { const char *fmt; int div; } tab[] = {
- { "%d day%s ", 86400 },
- { "%d hour%s ", 3600 },
- { "%d minute%s ", 60 },
- { "%d second%s ", 1 },
- { NULL, 0 }
- };
- int i;
- delta = abs(delta);
- int sigfigs = 0;
- for (i = 0; tab[i].fmt; i++) {
- int res = (i > 0 ? (delta % tab[i - 1].div) : delta) / tab[i].div;
- if (res == 0 && sigfigs == 0) continue;
- sigfigs++;
- if (delta >= tab[i].div && sigfigs < 2) {
- sprintf(buf + strlen(buf), tab[i].fmt, res, res != 1 ? "s" : "");
- if (compact) break;
- }
- }
- if (*buf) {
- strcat(buf, tail);
- } else {
- strcpy(buf, "now");
- }
- return buf;
- }
Advertisement
Add Comment
Please, Sign In to add comment