chiark / gitweb /
Fix prettyprint_age again
[ypp-sc-tools.db-live.git] / yarrg / CommodsWeb.pm
index d423a299c48136c20d7fe9ab3c408cffba402ec7..d5c77001c60b657800e98701242c6b78be989e83 100644 (file)
@@ -53,7 +53,8 @@ BEGIN {
     @ISA         = qw(Exporter);
     @EXPORT      = qw(&dbw_connect &ocean_list $sourcebasedir
                      &to_json_shim &to_json_protecttags
-                     &set_ctype_utf8);
+                     &set_ctype_utf8
+                     &prettyprint_age &meta_prettyprint_age);
     %EXPORT_TAGS = ( );
 
     @EXPORT_OK   = qw();
@@ -121,4 +122,25 @@ sub to_json_protecttags ($) {
     return $j;
 }
 
+sub meta_prettyprint_age ($$$) {
+    my ($age,$floor,$plus) = @_;
+    return <<END;
+        $age < 60 ?             'less than a minute'                    :
+        $age < 60*2 ?           '1 minute'                              :
+        $age < 3600*2 ?         $floor ($age/60) $plus' minutes'        :
+        $age < 86400*2 ?        $floor ($age/3600) $plus ' hours'       :
+                                $floor ($age/86400) $plus ' days';
+END
+};
+
+BEGIN { eval '
+  sub prettyprint_age ($) {
+               my ($age) = @_;
+               '.meta_prettyprint_age('$age','floor','.').'
+  };
+  1;
+' or die "$@";
+}
+
+
 1;