summaryrefslogtreecommitdiff
path: root/npc/functions/time.txt
diff options
context:
space:
mode:
Diffstat (limited to 'npc/functions/time.txt')
-rwxr-xr-x[-rw-r--r--]npc/functions/time.txt247
1 files changed, 149 insertions, 98 deletions
diff --git a/npc/functions/time.txt b/npc/functions/time.txt
index 8376d6a0..21b94ac5 100644..100755
--- a/npc/functions/time.txt
+++ b/npc/functions/time.txt
@@ -1,108 +1,159 @@
-function script now {
- return gettimetick(2);
-}
-
-
-function script time_from_ms {
- return now() + (getarg(0) / 1000);
-}
-
-function script time_from_seconds {
- return now() + getarg(0);
-}
-function script time_from_minutes {
- return now() + (getarg(0) * 60);
-}
-
-function script time_from_hours {
- return now() + (getarg(0) * 3600);
-}
-function script time_from_days {
- return now() + (getarg(0) * 86400);
+function script time_stamp {
+ // local variables
+ // if there is reasonable demand, these might be exported
+ // (that is what the builtin is likely to do)
+ @ts_year = gettime(7);
+ @ts_month = gettime(6);
+ @ts_mday = gettime(5);
+ //set @ts_wday, gettime(4);
+ @ts_hour = gettime(3);
+ @ts_minute = gettime(2);
+ @ts_second = gettime(1);
+
+ // locals used to generate leading zeroes
+ @ts_month_pad$ = "";
+ @ts_mday_pad$ = "";
+ @ts_hour_pad$ = "";
+ @ts_minute_pad$ = "";
+ @ts_second_pad$ = "";
+
+ if (@ts_month < 10)
+ @ts_month_pad$ = "0";
+ if (@ts_mday < 10)
+ @ts_mday_pad$ = "0";
+ if (@ts_hour < 10)
+ @ts_hour_pad$ = "0";
+ if (@ts_minute < 10)
+ @ts_minute_pad$ = "0";
+ if (@ts_second < 10)
+ @ts_second_pad$ = "0";
+
+ @ts_date$ = @ts_year + "-" + @ts_month_pad$ + @ts_month + "-" + @ts_mday_pad$ + @ts_mday;
+ @ts_time$ = @ts_hour_pad$ + @ts_hour + ":" + @ts_minute_pad$ + @ts_minute + ":" +@ts_second_pad$ + @ts_second;
+
+ // cleanup
+ @ts_year = 0;
+ @ts_month = 0;
+ @ts_mday = 0;
+ @ts_hour = 0;
+ @ts_minute = 0;
+ @ts_second = 0;
+ @ts_month_pad$ = "";
+ @ts_mday_pad$ = "";
+ @ts_hour_pad$ = "";
+ @ts_minute_pad$ = "";
+ @ts_second_pad$ = "";
+
+ return;
}
-// FuzzyTime(<unix timestamp>{, <options>{, <precision>}})
-// gives time in a human-readable format
-//
-// <options> is bitmasked:
-// 1 do not show "ago" when in past
-// 2 do not show "in" when in the future (default)
-// 4 show "from now" instead of "in" when in the future
-//
-// <precision> is the number of units to show,
-// do not exceed 99 (default is 2)
-
-function script FuzzyTime {
- .@future = getarg(0, now());
- .@options = getarg(1, 2);
- .@precision = getarg(2, 2);
- .@diff = (.@future - now());
-
- // check if in the past, or in the future
- if (.@diff < 0) {
- .@diff *= -1;
- .@past = true;
- }
-
- .@diff = max(1, .@diff);
-
- if (.@diff >= 31536000) {
- .@years = (.@diff / 31536000);
- .@diff = (++.@s == .@precision ? 0 : (.@diff % 31536000));
- .@ret$ += sprintf("%d %s", .@years, (.@years > 1 ? "years" : "year"));
- }
-
- if (.@diff >= 86400) {
- .@days = (.@diff / 86400);
- .@diff = (++.@s == .@precision ? 0 : (.@diff % 86400));
-
- if (.@s > 1) {
- .@ret$ += (.@diff > 0 ? ", " : " and ");
- }
-
- .@ret$ += sprintf("%d %s", .@days, (.@days > 1 ? "days" : "day"));
- }
-
- if (.@diff >= 3600) {
- .@hours = (.@diff / 3600);
- .@diff = (++.@s == .@precision ? 0 : (.@diff % 3600));
-
- if (.@s > 1) {
- .@ret$ += (.@diff > 0 ? ", " : (.@s >= 3 ? ", " : " ") + "and ");
- }
-
- .@ret$ += sprintf("%d %s", .@hours, (.@hours > 1 ? "hours" : "hour"));
- }
-
- if (.@diff >= 60) {
- .@minutes = (.@diff / 60);
- .@diff = (++.@s == .@precision ? 0 : (.@diff % 60));
-
- if (.@s > 1) {
- .@ret$ += (.@diff > 0 ? ", " : (.@s >= 3 ? ", " : " ") + "and ");
- }
-
- .@ret$ += sprintf("%d %s", .@minutes, (.@minutes > 1 ? "minutes" : "minute"));
- }
-
- if (.@diff >= 1) {
- if (++.@s > 1) {
- .@ret$ += (.@s >= 3 ? ", " : " ") + "and ";
- }
- .@ret$ += sprintf("%d %s", .@diff, (.@diff > 1 ? "seconds" : "second"));
- }
- if (.@past && !(.@options & 1)) {
- .@ret$ += " ago";
- }
- if (!(.@past) && !(.@options & 2)) {
- .@ret$ = ((.@options & 4) ? sprintf("%s from now", .@ret$) : sprintf("in %s", .@ret$));
- }
- return .@ret$;
+function script HumanTime {
+ @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
+ @seconds = @ms / 1000;
+ @ms = @ms % 1000;
+ if(@seconds < 60) goto L_Seconds;
+ @minutes = @seconds / 60;
+ @seconds = @seconds % 60;
+ if(@minutes < 60) goto L_Minutes;
+ @hours = @minutes / 60;
+ @minutes = @minutes % 60;
+ if(@hours < 24) goto L_Hours;
+ @days = @hours / 24;
+ @hours = @hours % 24;
+ if(@days) goto L_Days;
+ goto L_Clean;
+
+L_Millis:
+ @time$ = @ms + "ms";
+ return;
+
+L_Seconds:
+ @unit$ = "second";
+ if(@seconds > 1) set @unit$, "seconds";
+ @unit2$ = "millisecond";
+ if(@ms > 1) set @unit2$, "milliseconds";
+ @time$ = @seconds + " " + @unit$;
+ if(@ms) set @time$, @time$ + " and " + @ms + " " + @unit2$;
+ goto L_Clean;
+
+L_Minutes:
+ @unit$ = "minute";
+ if(@minutes > 1) set @unit$, "minutes";
+ @unit2$ = "second";
+ if(@seconds > 1) set @unit2$, "seconds";
+ @unit3$ = "millisecond";
+ if(@ms > 1) set @unit3$, "milliseconds";
+ @time$ = @minutes + " " + @unit$;
+ @separator$ = " and ";
+ if(@ms) set @separator$, ", ";
+ if(@seconds) set @time$, @time$ + @separator$ + @seconds + " " + @unit2$;
+ if(@ms) set @time$, @time$ + " and " + @ms + " " + @unit3$;
+ goto L_Clean;
+
+L_Hours:
+ @unit$ = "hour";
+ if(@hours > 1) set @unit$, "hours";
+ @unit2$ = "minute";
+ if(@minutes > 1) set @unit2$, "minutes";
+ @unit3$ = "second";
+ if(@seconds > 1) set @unit3$, "seconds";
+ @unit4$ = "millisecond";
+ if(@ms > 1) set @unit4$, "milliseconds";
+ @time$ = @hours + " " + @unit$;
+ @separator$ = " and ";
+ if(@seconds || @ms) set @separator$, ", ";
+ if(@minutes) set @time$, @time$ + @separator$ + @minutes + " " + @unit2$;
+ @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_Days:
+ @unit$ = "day";
+ if(@hours > 1) set @unit$, "days";
+ @unit2$ = "hour";
+ if(@hours > 1) set @unit2$, "hours";
+ @unit3$ = "minute";
+ if(@minutes > 1) set @unit3$, "minutes";
+ @unit4$ = "second";
+ if(@seconds > 1) set @unit4$, "seconds";
+ @unit5$ = "millisecond";
+ if(@ms > 1) set @unit5$, "milliseconds";
+ @time$ = @days + " " + @unit$;
+ @separator$ = " and ";
+ if(@minutes || @seconds || @ms) set @separator$, ", ";
+ if(@hours) set @time$, @time$ + @separator$ + @hours + " " + @unit2$;
+ @separator$ = " and ";
+ if(@seconds || @ms) set @separator$, ", ";
+ if(@minutes) set @time$, @time$ + @separator$ + @minutes + " " + @unit3$;
+ @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:
+ @unit$ = "";
+ @unit2$ = "";
+ @unit3$ = "";
+ @unit4$ = "";
+ @unit5$ = "";
+ @seconds = 0;
+ @minutes = 0;
+ @hours = 0;
+ @days = 0;
+ @separator$ = "";
+ return;
}