commit e06c047c96ab8ba1924a291bd049dd4892c91330
parent 4a6628c744ccce5e3bb8fa7e02419c0f59a40247
Author: William Casarin <jb55@jb55.com>
Date: Thu, 20 Oct 2022 12:11:24 -0700
timeago: fix implementation
Should be seconds not milliseconds...
Also add some docs
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/README b/README
@@ -8,6 +8,7 @@ format unix timestamps from stdin
-p, --past only format timestamps in the past
--version display version information and exit
-m, --ms interpret timestamps as milliseconds instead of seconds
+ -t, --timeago custom format: show age of timestamp (1h, 2d, etc)
FORMAT
a strftime format string, defaults to '%F %R'
diff --git a/datefmt.c b/datefmt.c
@@ -84,26 +84,26 @@ static void print_timeago(int64_t previous)
{
time_t now = time(NULL);
- int ms_per_minute = 60 * 1000;
- int ms_per_hour = ms_per_minute * 60;
- int ms_per_day = ms_per_hour * 24;
- int ms_per_month = ms_per_day * 30;
- int ms_per_year = ms_per_day * 365;
+ int s_per_minute = 60;
+ int s_per_hour = s_per_minute * 60;
+ int s_per_day = s_per_hour * 24;
+ int s_per_month = s_per_day * 30;
+ int s_per_year = s_per_month * 365;
int elapsed = now - previous;
- if (elapsed < ms_per_minute) {
- printf("%ds", elapsed / 1000);
- } else if (elapsed < ms_per_hour) {
- printf("%dm", elapsed / ms_per_minute);
- } else if (elapsed < ms_per_day) {
- printf("%dh", elapsed / ms_per_hour);
- } else if (elapsed < ms_per_month) {
- printf("%dd", elapsed / ms_per_day);
- } else if (elapsed < ms_per_year) {
- printf("%dmth", elapsed / ms_per_month);
+ if (elapsed < s_per_minute) {
+ printf("%ds", elapsed);
+ } else if (elapsed < s_per_hour) {
+ printf("%dm", elapsed / s_per_minute);
+ } else if (elapsed < s_per_day) {
+ printf("%dh", elapsed / s_per_hour);
+ } else if (elapsed < s_per_month) {
+ printf("%dd", elapsed / s_per_day);
+ } else if (elapsed < s_per_year) {
+ printf("%dmth", elapsed / s_per_month);
} else {
- printf("%dyr", elapsed / ms_per_year);
+ printf("%dyr", elapsed / s_per_year);
}
}