bcachefs: bch2_prt_datetime()
authorKent Overstreet <kent.overstreet@linux.dev>
Wed, 1 Nov 2023 03:43:47 +0000 (23:43 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 5 Nov 2023 18:12:08 +0000 (13:12 -0500)
Improved, better named version of pr_time().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/sb-errors.c
fs/bcachefs/sb-members.c
fs/bcachefs/super-io.c
fs/bcachefs/util.c
fs/bcachefs/util.h

index 3d66f15ae8f50ed055eff3d70fddb570d174c444..f0930ab7f036eb30fe5d40708f4b82a1e68907f2 100644 (file)
@@ -61,7 +61,6 @@ static void bch2_sb_errors_to_text(struct printbuf *out, struct bch_sb *sb,
 {
        struct bch_sb_field_errors *e = field_to_type(f, errors);
        unsigned i, nr = bch2_sb_field_errors_nr_entries(e);
-       u64 now = ktime_get_real_seconds();
 
        if (out->nr_tabstops <= 1)
                printbuf_tabstop_push(out, 16);
@@ -71,9 +70,7 @@ static void bch2_sb_errors_to_text(struct printbuf *out, struct bch_sb *sb,
                prt_tab(out);
                prt_u64(out, BCH_SB_ERROR_ENTRY_NR(&e->entries[i]));
                prt_tab(out);
-               bch2_pr_time_units(out, (now - le64_to_cpu(e->entries[i].last_error_time)) *
-                                  NSEC_PER_SEC);
-               prt_str(out, " ago");
+               bch2_prt_datetime(out, le64_to_cpu(e->entries[i].last_error_time));
                prt_newline(out);
        }
 }
index ab5de12eca4acdf230a4e26fee884cff059c6ef8..09d5453707fa73b12f03d613d149cc09f5a5f9ae 100644 (file)
@@ -235,7 +235,7 @@ static void member_to_text(struct printbuf *out,
        prt_printf(out, "Last mount:");
        prt_tab(out);
        if (m.last_mount)
-               pr_time(out, le64_to_cpu(m.last_mount));
+               bch2_prt_datetime(out, le64_to_cpu(m.last_mount));
        else
                prt_printf(out, "(never)");
        prt_newline(out);
index cceedd2bc68d65b13e9b978fe6179ae199c7d30b..f4cad903f4d69da7776825f50bf561a1980a02a0 100644 (file)
@@ -1183,7 +1183,7 @@ void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
        prt_printf(out, "Created:");
        prt_tab(out);
        if (sb->time_base_lo)
-               pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
+               bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
        else
                prt_printf(out, "(not set)");
        prt_newline(out);
index 08bac0ba8d0b81824eca69176b9c2192ee43c928..84b142fcc3dfce6cfbdb30647d4aa67609511519 100644 (file)
@@ -467,6 +467,24 @@ static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
        prt_printf(out, "%s", u->name);
 }
 
+#ifndef __KERNEL__
+#include <time.h>
+void bch2_prt_datetime(struct printbuf *out, time64_t sec)
+{
+       time_t t = sec;
+       char buf[64];
+       ctime_r(&t, buf);
+       prt_str(out, buf);
+}
+#else
+void bch2_prt_datetime(struct printbuf *out, time64_t sec)
+{
+       char buf[64];
+       snprintf(buf, sizeof(buf), "%ptT", &sec);
+       prt_u64(out, sec);
+}
+#endif
+
 #define TABSTOP_SIZE 12
 
 static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
index 849a37ae497cc1d1ceacec2161e5e5ab1430a7d4..2984b57b29584f1e4009fea7b88c6115de3d6389 100644 (file)
@@ -245,26 +245,7 @@ do {                                                                       \
 #define prt_bitflags(...)              bch2_prt_bitflags(__VA_ARGS__)
 
 void bch2_pr_time_units(struct printbuf *, u64);
-
-#ifdef __KERNEL__
-static inline void pr_time(struct printbuf *out, u64 time)
-{
-       prt_printf(out, "%llu", time);
-}
-#else
-#include <time.h>
-static inline void pr_time(struct printbuf *out, u64 _time)
-{
-       char time_str[64];
-       time_t time = _time;
-       struct tm *tm = localtime(&time);
-       size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
-       if (!err)
-               prt_printf(out, "(formatting error)");
-       else
-               prt_printf(out, "%s", time_str);
-}
-#endif
+void bch2_prt_datetime(struct printbuf *, time64_t);
 
 #ifdef __KERNEL__
 static inline void uuid_unparse_lower(u8 *uuid, char *out)