about summary refs log tree commit diff
path: root/lr.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2015-12-27 09:40:56 +0100
committerChristian Neukirchen <chneukirchen@gmail.com>2015-12-27 09:40:56 +0100
commit1ca8ef43eb1f9d5e5904f92045c52b1b91a88f26 (patch)
tree115dad01eac6830bce3a8714d3c446897b69e643 /lr.c
parent1f3e1628b0d9d97b274c87ecc9f19be316d71c6a (diff)
downloadlr-1ca8ef43eb1f9d5e5904f92045c52b1b91a88f26.tar.gz
lr-1ca8ef43eb1f9d5e5904f92045c52b1b91a88f26.tar.xz
lr-1ca8ef43eb1f9d5e5904f92045c52b1b91a88f26.zip
add relative date formatting
Diffstat (limited to 'lr.c')
-rw-r--r--lr.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/lr.c b/lr.c
index 1c8b228..29c59e8 100644
--- a/lr.c
+++ b/lr.c
@@ -108,6 +108,8 @@ static blkcnt_t maxblocks;
 static int maxdepth;
 static int uwid, gwid, fwid;
 
+static time_t now;
+
 struct fileinfo {
 	char *fpath;
 	size_t prefixl;
@@ -1145,13 +1147,22 @@ print_format(struct fileinfo *fi)
 			s++;
 			if (!*s)
 				break;
-			tfmt[1] = *s;
-			strftime(buf, sizeof buf, tfmt,
-			    localtime(
-			    *s == 'A' ? &fi->sb.st_atime :
-			    *s == 'C' ? &fi->sb.st_ctime :
-			    &fi->sb.st_mtime));
-			printf("%s", buf);
+
+                        time_t t = (*s == 'A' ? fi->sb.st_atime :
+			    *s == 'C' ? fi->sb.st_ctime :
+			    fi->sb.st_mtime);
+
+			if (*s == '-') {
+                                printf("%3ldd%3ldh%3ldm%3lds",
+                                    ((now - t) / (60*60*24)),
+                                    ((now - t) / (60*60)) % 24,
+                                    ((now - t) / 60) % 60,
+                                    (now - t) % 60);
+			} else {
+				tfmt[1] = *s;
+				strftime(buf, sizeof buf, tfmt, localtime(&t));
+				printf("%s", buf);
+			}
 			break;
 		}
 		case 'm': printf("%04o", (unsigned int)fi->sb.st_mode & 07777); break;
@@ -1345,6 +1356,7 @@ main(int argc, char *argv[])
 	format = default_format;
 	ordering = default_ordering;
 	argv0 = argv[0];
+	now = time(0);
 
 	while ((c = getopt(argc, argv, "01ADFHLQSUdf:lho:st:x")) != -1)
 		switch(c) {