+
+#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
+time_t
+parsetime(char *p)
+{
+ struct tm *lt;
+ int bigyear;
+ int yearset = 0;
+ time_t tval;
+ unsigned char *t;
+
+ for (t = p; *t; ++t) {
+ if (isdigit(*t))
+ continue;
+ badformat();
+ }
+
+ tval = time(NULL);
+ lt = localtime(&tval);
+ lt->tm_sec = 0;
+ lt->tm_min = 0;
+
+ switch (strlen(p)) {
+ case 10: /* yyyy */
+ bigyear = ATOI2(p);
+ lt->tm_year = bigyear * 100 - 1900;
+ yearset = 1;
+ /* FALLTHROUGH */
+ case 8: /* yy */
+ if (yearset) {
+ lt->tm_year += ATOI2(p);
+ } else {
+ lt->tm_year = ATOI2(p);
+ if (lt->tm_year < 69) /* hack for 2000 */
+ lt->tm_year += 100;
+ }
+ /* FALLTHROUGH */
+ case 6: /* mm */
+ lt->tm_mon = ATOI2(p);
+ if ((lt->tm_mon > 12) || !lt->tm_mon)
+ badformat();
+ --lt->tm_mon; /* time struct is 0 - 11 */
+ /* FALLTHROUGH */
+ case 4: /* dd */
+ lt->tm_mday = ATOI2(p);
+ if ((lt->tm_mday > 31) || !lt->tm_mday)
+ badformat();
+ /* FALLTHROUGH */
+ case 2: /* HH */
+ lt->tm_hour = ATOI2(p);
+ if (lt->tm_hour > 23)
+ badformat();
+ break;
+ default:
+ badformat();
+ }
+ /* The calling code needs a valid tm_ydays and this is the easiest
+ * way to get one */
+ if ((tval = mktime(lt)) == -1)
+ errx(1, "specified date is outside allowed range");
+ return (tval);
+}
+
+void
+badformat(void)
+{
+ warnx("illegal time format");
+ (void)fprintf(stderr, "usage: pom [[[[[cc]yy]mm]dd]HH]\n");
+ exit(EXIT_FAILURE);
+}