summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/parsers/timeframe.c
blob: 33ea6975030492181b557aa1b5884ab9f3bfb551 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "timeframe.h"

// --------------------------------------------------------------------------------------------------------------------
// timeframe
/*
TIMEFRAME timeframe_parse(const char *txt) {
    if(!txt || !*txt)
        return TIMEFRAME_INVALID;

char buf[strlen(txt) + 1];
memcpy(buf, txt, strlen(txt) + 1);
char *s = trim_all(buf);
if(!s)
    return TIMEFRAME_INVALID;

while(isspace(*s)) s++;

if(strcasecmp(s, "this minute") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_THIS_MINUTE,
        .before = 0,
    };
}
if(strcasecmp(s, "this hour") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_THIS_HOUR,
        .before = 0,
    };
}
if(strcasecmp(s, "today") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_TODAY,
        .before = 0,
    };
}
if(strcasecmp(s, "this week") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_THIS_WEEK,
        .before = 0,
    };
}
if(strcasecmp(s, "this month") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_THIS_MONTH,
        .before = 0,
    };
}
if(strcasecmp(s, "this year") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_THIS_YEAR,
        .before = 0,
    };
}

if(strcasecmp(s, "last minute") == 0) {
    return (TIMEFRAME) {
        .after = -60,
        .before = API_RELATIVE_TIME_THIS_MINUTE,
    };
}
if(strcasecmp(s, "last hour") == 0) {
    return (TIMEFRAME) {
        .after = -3600,
        .before = API_RELATIVE_TIME_THIS_HOUR,
    };
}
if(strcasecmp(s, "yesterday") == 0) {
    return (TIMEFRAME) {
        .after = -86400,
        .before = API_RELATIVE_TIME_TODAY,
    };
}
if(strcasecmp(s, "this week") == 0) {
    return (TIMEFRAME) {
        .after = -86400 * 7,
        .before = API_RELATIVE_TIME_THIS_WEEK,
    };
}
if(strcasecmp(s, "this month") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_LAST_MONTH,
        .before = API_RELATIVE_TIME_THIS_MONTH,
    };
}
if(strcasecmp(s, "this year") == 0) {
    return (TIMEFRAME) {
        .after = API_RELATIVE_TIME_LAST_YEAR,
        .before = API_RELATIVE_TIME_THIS_YEAR,
    };
}

const char *end;
double after = strtondd(s, (char **)&end);

if(end == s)
    return TIMEFRAME_INVALID;

s = end;
while(isspace(*s)) s++;

time_t multiplier = 1;
if(!isdigit(*s) && *s != '-') {
    // after has units
    bool found = false;

    for (size_t i = 0; i < sizeof(units) / sizeof(units[0]); i++) {
        size_t len = strlen(units[i].unit);

        if (units[i].multiplier >= 1 * NSEC_PER_USEC &&
            strncmp(s, units[i].unit, len) == 0 &&
            (isspace(s[len]) || s[len] == '-')) {
            multiplier = units[i].multiplier / NSEC_PER_SEC;
            found = true;
            s += len;
        }
    }

    if(!found)
        return TIMEFRAME_INVALID;
}

const char *dash = strchr(s, '-');
if(!dash) return TIMEFRAME_INVALID;

}
*/