From a85d79c9397ec32b5bbd4e8b3d2fb6ff1f29cf39 Mon Sep 17 00:00:00 2001
From: mekolat <mekolat@gmail.com>
Date: Tue, 3 Mar 2015 15:55:56 -0500
Subject: improve humantime

---
 world/map/npc/functions/time.txt | 57 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/world/map/npc/functions/time.txt b/world/map/npc/functions/time.txt
index 695c4cec..7d16a200 100644
--- a/world/map/npc/functions/time.txt
+++ b/world/map/npc/functions/time.txt
@@ -61,13 +61,20 @@ function|script|time_stamp
 // HumanTime - returns a human-readable time
 // author: meko
 
-// Variables:
+// Internal variables:
+// @separator$, @unit$, @unit2$, @unit3$, @unit4$, @unit5$
+// @seconds, @minutes, @hours, @days
+
+// Public Variables:
 // input @ms integer
 // output @time$ string
 
 function|script|HumanTime
 {
     set @time$, "now";
+    if(@seconds) set @ms, @ms + (@seconds * 1000);
+    if(@minutes) set @ms, @ms + (@minutes * 60000);
+    if(@days)    set @ms, @ms + (@days    * 1440000);
     if(@ms < 1000) goto L_Millis; // under 1 second we have nothing to count
     set @seconds, @ms / 1000;
     set @ms, @ms % 1000;
@@ -77,8 +84,11 @@ function|script|HumanTime
     if(@minutes < 60) goto L_Minutes;
     set @hours, @minutes / 60;
     set @minutes, @minutes % 60;
-    if(@hours < 60) goto L_Hours;
-    return;
+    if(@hours < 24) goto L_Hours;
+    set @days, @hours / 24;
+    set @hours, @hours % 24;
+    if(@days) goto L_Days;
+    goto L_Clean;
 
 L_Millis:
     set @time$, @ms + "ms";
@@ -91,7 +101,7 @@ L_Seconds:
     if(@ms > 1) set @unit2$, "milliseconds";
     set @time$, @seconds + " " + @unit$;
     if(@ms) set @time$, @time$ + " and " + @ms + " " + @unit2$;
-    return;
+    goto L_Clean;
 
 L_Minutes:
     set @unit$, "minute";
@@ -105,7 +115,7 @@ L_Minutes:
     if(@ms) set @separator$, ", ";
     if(@seconds) set @time$, @time$ + @separator$ + @seconds + " " + @unit2$;
     if(@ms) set @time$, @time$ + " and " + @ms + " " + @unit3$;
-    return;
+    goto L_Clean;
 
 L_Hours:
     set @unit$, "hour";
@@ -124,5 +134,42 @@ L_Hours:
     if(@ms) set @separator$, ", ";
     if(@seconds) set @time$, @time$ + @separator$ + @seconds + " " + @unit3$;
     if(@ms) set @time$, @time$ + " and " + @ms + " " + @unit4$;
+    goto L_Clean;
+
+L_Days:
+    set @unit$, "day";
+    if(@hours > 1) set @unit$, "days";
+    set @unit2$, "hour";
+    if(@hours > 1) set @unit2$, "hours";
+    set @unit3$, "minute";
+    if(@minutes > 1) set @unit3$, "minutes";
+    set @unit4$, "second";
+    if(@seconds > 1) set @unit4$, "seconds";
+    set @unit5$, "millisecond";
+    if(@ms > 1) set @unit5$, "milliseconds";
+    set @time$, @days + " " + @unit$;
+    set @separator$, " and ";
+    if(@minutes || @seconds || @ms) set @separator$, ", ";
+    if(@hours) set @time$, @time$ + @separator$ + @hours + " " + @unit2$;
+    set @separator$, " and ";
+    if(@seconds || @ms) set @separator$, ", ";
+    if(@minutes) set @time$, @time$ + @separator$ + @minutes + " " + @unit3$;
+    set @separator$, " and ";
+    if(@ms) set @separator$, ", ";
+    if(@seconds) set @time$, @time$ + @separator$ + @seconds + " " + @unit3$;
+    if(@ms) set @time$, @time$ + " and " + @ms + " " + @unit4$;
+    goto L_Clean;
+
+L_Clean:
+    set @unit$,      "";
+    set @unit2$,     "";
+    set @unit3$,     "";
+    set @unit4$,     "";
+    set @unit5$,     "";
+    set @seconds,    0;
+    set @minutes,    0;
+    set @hours,      0;
+    set @days,       0;
+    set @separator$, "";
     return;
 }
-- 
cgit v1.2.3-70-g09d2