summaryrefslogtreecommitdiff
path: root/npc/functions/time.txt
blob: be04902a6973c4c049b0b4ef533886df5007794e (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
function	script	now	{
    return gettimetick(2);
}

// Composite time for quests
// Max value is 16,777,215, so we reduce 1514772000 which is the number of seconds
// elapsed from 01/01/1970 00:00 CET to 01/01/2018 00:00 GMT -0200
// That would only hold 194 days, or 6.47 months, so we reduce 6 in 6 months when
// we hit limit, looping monthly
// This value is NOT reversable to FuzzyTime
function	script	santime	{
    .@limit=getarg(0, 16777214);
    .@base=1514772000;
    .@red=(60*60*24*30*6);
    .@val=gettimetick(2)-.@base;
    do
    {
        .@val-=.@red;
    } while (.@val > .@limit);

    return .@val;
}

// Grace period when the month from santime is not matching
function	script	gcsantime	{
    return 0;
}

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$;
}