| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "timeframe.h" |
| 4 | |
| 5 | // -------------------------------------------------------------------------------------------------------------------- |
| 6 | // timeframe |
| 7 | /* |
| 8 | TIMEFRAME timeframe_parse(const char *txt) { |
| 9 | if(!txt || !*txt) |
| 10 | return TIMEFRAME_INVALID; |
| 11 | |
| 12 | char buf[strlen(txt) + 1]; |
| 13 | memcpy(buf, txt, strlen(txt) + 1); |
| 14 | char *s = trim_all(buf); |
| 15 | if(!s || !*s) |
| 16 | return TIMEFRAME_INVALID; |
| 17 | |
| 18 | while(isspace(*s)) s++; |
| 19 | |
| 20 | if(strcasecmp(s, "this minute") == 0) { |
| 21 | return (TIMEFRAME) { |
| 22 | .after = API_RELATIVE_TIME_THIS_MINUTE, |
| 23 | .before = 0, |
| 24 | }; |
| 25 | } |
| 26 | if(strcasecmp(s, "this hour") == 0) { |
| 27 | return (TIMEFRAME) { |
| 28 | .after = API_RELATIVE_TIME_THIS_HOUR, |
| 29 | .before = 0, |
| 30 | }; |
| 31 | } |
| 32 | if(strcasecmp(s, "today") == 0) { |
| 33 | return (TIMEFRAME) { |
| 34 | .after = API_RELATIVE_TIME_TODAY, |
| 35 | .before = 0, |
| 36 | }; |
| 37 | } |
| 38 | if(strcasecmp(s, "this week") == 0) { |
| 39 | return (TIMEFRAME) { |
| 40 | .after = API_RELATIVE_TIME_THIS_WEEK, |
| 41 | .before = 0, |
| 42 | }; |
| 43 | } |
| 44 | if(strcasecmp(s, "this month") == 0) { |
| 45 | return (TIMEFRAME) { |
| 46 | .after = API_RELATIVE_TIME_THIS_MONTH, |
| 47 | .before = 0, |
| 48 | }; |
| 49 | } |
| 50 | if(strcasecmp(s, "this year") == 0) { |
| 51 | return (TIMEFRAME) { |
| 52 | .after = API_RELATIVE_TIME_THIS_YEAR, |
| 53 | .before = 0, |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | if(strcasecmp(s, "last minute") == 0) { |
| 58 | return (TIMEFRAME) { |
| 59 | .after = -60, |
| 60 | .before = API_RELATIVE_TIME_THIS_MINUTE, |
| 61 | }; |
| 62 | } |
| 63 | if(strcasecmp(s, "last hour") == 0) { |
| 64 | return (TIMEFRAME) { |
| 65 | .after = -3600, |
| 66 | .before = API_RELATIVE_TIME_THIS_HOUR, |
| 67 | }; |
| 68 | } |
| 69 | if(strcasecmp(s, "yesterday") == 0) { |
| 70 | return (TIMEFRAME) { |
| 71 | .after = -86400, |
| 72 | .before = API_RELATIVE_TIME_TODAY, |
| 73 | }; |
| 74 | } |
| 75 | if(strcasecmp(s, "this week") == 0) { |
| 76 | return (TIMEFRAME) { |
| 77 | .after = -86400 * 7, |
| 78 | .before = API_RELATIVE_TIME_THIS_WEEK, |
| 79 | }; |
| 80 | } |
| 81 | if(strcasecmp(s, "this month") == 0) { |
| 82 | return (TIMEFRAME) { |
| 83 | .after = API_RELATIVE_TIME_LAST_MONTH, |
| 84 | .before = API_RELATIVE_TIME_THIS_MONTH, |
| 85 | }; |
| 86 | } |
| 87 | if(strcasecmp(s, "this year") == 0) { |
| 88 | return (TIMEFRAME) { |
| 89 | .after = API_RELATIVE_TIME_LAST_YEAR, |
| 90 | .before = API_RELATIVE_TIME_THIS_YEAR, |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | const char *end; |
| 95 | double after = strtondd(s, (char **)&end); |
| 96 | |
| 97 | if(end == s) |
| 98 | return TIMEFRAME_INVALID; |
| 99 | |
| 100 | s = end; |
| 101 | while(isspace(*s)) s++; |
| 102 | |
| 103 | time_t multiplier = 1; |
| 104 | if(!isdigit(*s) && *s != '-') { |
| 105 | // after has units |
| 106 | bool found = false; |
| 107 | |
| 108 | for (size_t i = 0; i < sizeof(units) / sizeof(units[0]); i++) { |
| 109 | size_t len = strlen(units[i].unit); |
| 110 | |
| 111 | if (units[i].multiplier >= 1 * NSEC_PER_USEC && |
| 112 | strncmp(s, units[i].unit, len) == 0 && |
| 113 | (isspace(s[len]) || s[len] == '-')) { |
| 114 | multiplier = units[i].multiplier / NSEC_PER_SEC; |
| 115 | found = true; |
| 116 | s += len; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if(!found) |
| 121 | return TIMEFRAME_INVALID; |
| 122 | } |
| 123 | |
| 124 | const char *dash = strchr(s, '-'); |
| 125 | if(!dash) return TIMEFRAME_INVALID; |
| 126 | |
| 127 | } |
| 128 | */ |