summaryrefslogtreecommitdiff
path: root/npc/functions/time.txt
blob: 9ae2f436c1e3ff11618effaf42faad51980d52c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Evol Script
// Authors: Gumi, Jesusalva

function	script	now	{
    return gettimetick(2);
}

// Returns current time. A SQL update superseeded this.
// santime(  )
function	script	santime	{
    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);
}


// 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
//     4  show "from now" instead of "in" when in the future
//
//     <precision> is the number of units to show,
//     by default uses two (eg. 2m30s or 1h20m).
//     Use '99' for max precision

function	script	FuzzyTime	{
    .@future = getarg(0, now());
    .@options = getarg(1, 3);
    .@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	time_stamp	{
    @ts_date$ = sprintf("%04d-%02d-%02d", gettime(7), gettime(6), gettime(5));
    @ts_time$ = sprintf("%02d:%02d:%02d", gettime(3), gettime(2), gettime(1));
    return (@ts_date$+" "+@ts_time$);
}





// FIXME
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;
}