#include #include #include #include /* * Find Freedom Internet maintenance date and time on their web page. * * Copyright (c) 2023 R.J. van der Putten, Leiden, Holland, * rob at sput dot nl. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ char *months[12] = { \ "januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december" }; /* Find month */ int fndmon(char *s) { int i; i = 0; while (i < 12) { if(strcasecmp(s, months[i]) == 0) break; i++; } return(i); } /* Guess year */ int fndyear(int mon) { int year; time_t t; struct tm *curr; t = 0; year = 0; curr = NULL; t = time(NULL); curr = localtime(&t); year = curr->tm_year; if (mon < 4 && curr->tm_mon > 9) { /* Curr: Oct - Dec and Ann: Jan - Mar; Probably next year */ year++; } else if (curr->tm_mon < 4 && mon > 9) { /* Curr: Jan - Mar and Ann: Oct - Dec; Probably last year */ year--; } return(year); } /* Process date */ int procdate(char *s, struct tm *loc) { int mon, year; char month[16]; mon = 0; year = 0; memset(month, 0, 16); /* * Begintijd: Vrijdag 10 november 2023 01.00 * Begintijd: Vrijdag 10 november 01.00 */ if(strncasecmp(s, "Begintijd: ", 11) != 0) return(-1); if (sscanf(s, "%*s %*s %d %15s %d %d.%d", \ &(loc->tm_mday), month, &year, &(loc->tm_hour), &(loc->tm_min)) != 5) { /* Missing year? */ if (sscanf(s, "%*s %*s %d %15s %d.%d", \ &(loc->tm_mday), month, &(loc->tm_hour), &(loc->tm_min)) != 4) { /* Garbled date! */ return(-1); } year = 0; } mon = fndmon(month); if(mon > 11) return(-1); if (year == 0) { /* No year */ year = fndyear(mon); loc->tm_year = year; } else { loc->tm_year = year - 1900; } loc->tm_mon = mon; loc->tm_isdst = -1; return(0); } int main(int argc, char **argv) { int i, len, lf, loclen; char date[256], line[4096]; time_t t; struct tm loc, *prev; if (argc != 2 || argv[1][0] == '-') { fprintf(stderr, "Usage: proc-storing Location\n"); exit(1); } i = 0; len = 0; lf = 0; /* Location when not zero */ loclen = 0; /* Length of location */ t = 0; /* Epoch */ prev = NULL; /* The previous day */ memset(date, 0, 256); /* Date */ memset(line, 0, 4096); memset(&loc, 0, sizeof(loc)); /* Local time */ loclen = strlen(argv[1]); if (loclen < 2 || loclen > 255) { fprintf(stderr, "Wrong Location length: %d\n", loclen); exit(1); } while (fgets(line, 4096, stdin)) { i = 0; while (line[i] != 0 && i < 4096) { if (line[i] == 10 || line[i] == 13) { line[i] = 0; break; } i++; } len = i + 1; if (len > 4088) { fprintf(stderr, "Line too long %d\n", len); exit(1); } i = 0; while (i < len) { if (strncasecmp(line + i, "Begintijd", 9) == 0) { strncpy(date, line + i, 255); date[255] = 0; lf = 0; break; } else if (strncasecmp(line + i, "* ", 2) == 0 && \ strncasecmp(line + i + 2, argv[1], loclen) == 0) { lf = 1; printf("%s\n", date); break; } else if (lf != 0) { if (strncasecmp(line + i, \ "Het onderhoud start om", 22) == 0) { printf("\t%s\n", line + i); } else if (strncasecmp(line + i, \ "De verwachte duur van het onderhoud is", 38) == 0) { /* All info is available */ if (sscanf(line + i + 39, "%d %*s", &len) == 1 && \ procdate(date, &loc) == 0 && \ (t = mktime(&loc)) != -1) { /* Date looks OK */ t = t - 86400; /* Previous day */ prev = localtime(&t); printf("\t%04d-%02d-%02d\t", prev->tm_year + 1900, \ prev->tm_mon + 1, prev->tm_mday); printf("%04d-%02d-%02d %02d:%02d - %02d:%02d\n", \ loc.tm_year + 1900, loc.tm_mon + 1, loc.tm_mday, \ loc.tm_hour, loc.tm_min, \ loc.tm_hour + len, loc.tm_min); } lf = 0; /* End of this location */ break; } } i++; } /* End of while i < len */ } /* End of while fgets line stdin */ return(0); }