commit: c3e46cca9d8808e96ccffb5e97d596203294f753
parent 9f8c1f1231447c647f08f28e208e28ae047a2cfd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 15 Mar 2024 11:39:15 +0100
cmd/humanize: move time into a function
Diffstat:
1 file changed, 28 insertions(+), 23 deletions(-)
diff --git a/cmd/humanize.c b/cmd/humanize.c
@@ -32,6 +32,33 @@ human_num(long double num, bool iec)
}
static void
+human_time(long int epoch)
+{
+ int year = 0, month = 0, mday = 0, hour = 0, min = 0, sec = 0;
+
+ year = epoch / 31556926; // round(year)
+ epoch %= 31556926;
+ month = epoch / 2629743; // year/12
+ epoch %= 2629743;
+ mday = epoch / 86400;
+ epoch %= 86400;
+
+ hour = epoch / 3600;
+ epoch %= 3600;
+ min = epoch / 60;
+ epoch %= 60;
+ sec = epoch;
+
+ if(year > 0) printf("%d年 ", year);
+ if(month > 0) printf("%d月 ", month);
+ if(mday > 0) printf("%d日 ", mday);
+ if(hour > 0) printf("%dh ", hour);
+ if(min > 0) printf("%dm ", min);
+ if(sec > 0) printf("%ds", sec);
+ printf("\n");
+}
+
+static void
usage()
{
fprintf(stderr, "Usage: humanize [-bdt] number\n");
@@ -82,7 +109,6 @@ main(int argc, char *argv[])
// Keep sscanf here for easier error handling, rest being pure math
if(time)
{
- int year = 0, month = 0, mday = 0, hour = 0, min = 0, sec = 0;
long int epoch = 0;
if(sscanf(argv[argi], "%ld", &epoch) < 1)
@@ -91,28 +117,7 @@ main(int argc, char *argv[])
return 1;
}
- year = epoch / 31556926; // round(year)
- epoch %= 31556926;
- month = epoch / 2629743; // year/12
- epoch %= 2629743;
- mday = epoch / 86400;
- epoch %= 86400;
-
- hour = epoch / 3600;
- epoch %= 3600;
- min = epoch / 60;
- epoch %= 60;
- sec = epoch;
-
- if(year > 0) printf("%d年 ", year);
- if(month > 0) printf("%d月 ", month);
- if(mday > 0) printf("%d日 ", mday);
- if(hour > 0) printf("%dh ", hour);
- if(min > 0) printf("%dm ", min);
- if(sec > 0) printf("%ds", sec);
- printf("\n");
-
- continue;
+ human_time(epoch);
}
else
{