summaryrefslogtreecommitdiff
path: root/npc/functions/time.txt
blob: eb19d0305d0981d3bacc01189d9acc6c893af9d2 (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
// FuzzyTime( <unix timestamp>, <options> )
// 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

function	script	FuzzyTime	{
    .@now = gettimetick(2);
    .@future = getarg(0, .@now);
    .@options = getarg(1,0);
    .@diff = max(.@future - .@now);
    .@ret$ = "";
    .@past = 0;
    .@s = 0; // for serial comma

    // define units
    .@unit_second = 1;
    .@unit_second$ = l("second");
    .@unit_seconds$ = l("seconds");

    .@unit_minute = (.@unit_second * 60);
    .@unit_minute$ = l("minute");
    .@unit_minutes$ = l("minutes");

    .@unit_hour = (.@unit_minute * 60);
    .@unit_hour$ = l("hour");
    .@unit_hours$ = l("hours");

    .@unit_day = (.@unit_hour * 24);
    .@unit_day$ = l("day");
    .@unit_days$ = l("days");

    .@unit_year = (.@unit_day * 365);
    .@unit_year$ = l("year");
    .@unit_years$ = l("years");

    // check if in the past, or in the future
    if (.@diff < 0)
    {
        .@diff *= -1;
        .@past = 1;
    }

    .@diff = max(1, .@diff);

    if (.@diff >= .@unit_year)
    {
        .@years = (.@diff / .@unit_year);
        .@diff = (.@diff % .@unit_year);
        .@ret$ += .@years + " " + getd(".@unit_year" + (.@years > 1 ? "s$" : "$"));
        ++.@s;
    }

    if (.@diff >= .@unit_day)
    {
        .@days = (.@diff / .@unit_day);
        .@diff = (.@diff % .@unit_day);

        if (.@ret$ != "")
        {
            .@ret$ += .@diff > 0 ? ", " : l(", and ");
        }

        .@ret$ += .@days + " " + getd(".@unit_day" + (.@days > 1 ? "s$" : "$"));
        ++.@s;
    }

    if (.@diff >= .@unit_hour)
    {
        .@hours = (.@diff / .@unit_hour);
        .@diff = (.@diff % .@unit_hour);

        if (.@ret$ != "")
        {
            .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
        }

        .@ret$ += .@hours + " " + getd(".@unit_hour" + (.@hours > 1 ? "s$" : "$"));
        ++.@s;
    }

    if (.@diff >= .@unit_minute)
    {
        .@minutes = (.@diff / .@unit_minute);
        .@diff = (.@diff % .@unit_minute);

        if (.@ret$ != "")
        {
            .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
        }

        .@ret$ += .@minutes + " " + getd(".@unit_minute" + (.@minutes > 1 ? "s$" : "$"));
        ++.@s;
    }

    if (.@diff >= .@unit_second)
    {
        .@seconds = (.@diff / .@unit_second);
        .@diff = (.@diff % .@unit_second);

        if (.@ret$ != "")
        {
            .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
        }

        .@ret$ += .@seconds + " " + getd(".@unit_second" + (.@seconds > 1 ? "s$" : "$"));
        ++.@s;
    }

    if (.@past > 0 && !(.@options & 1))
    {
        .@ret$ += l(" ago");
    }

    if (.@past < 1 && !(.@options & 2))
    {
        .@ret$ = (.@options & 4) ? l("@@ from now", .@ret$) : l("in @@", .@ret$);
    }

    return .@ret$;
}

function	script	FuzzyTimeFromSeconds	{
    return FuzzyTime((gettimetick(2) + getarg(0,0)), getarg(1,0));
}

function	script	FuzzyTimeFromMs	{
    return FuzzyTimeFromSeconds((getarg(0,0) / 1000), getarg(1,0));
}

function	script	FuzzyTimeFromMinutes	{
    return FuzzyTimeFromSeconds((getarg(0,0) * 60), getarg(1,0));
}

function	script	FuzzyTimeFromHours	{
    return FuzzyTimeFromMinutes((getarg(0,0) * 60), getarg(1,0));
}

function	script	FuzzyTimeFromDays	{
    return FuzzyTimeFromHours((getarg(0,0) * 24), getarg(1,0));
}