+ /* oh well, may be someone some day will invent a formula for this stuff */
+ y = 1970; /* start "guessing" */
+ while (days >= (ISLEAP(y)?366:365)) {
+ ny = (y + days/366);
+ days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
+ y = ny;
+ }
+ md[1] = ISLEAP(y)?29:28;
+ for (m=0; days >= md[m]; m++)
+ days -= md[m];
+
+ tm->tm_year = y; /* unlike gmtime_r we store complete year here */
+ tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
+ tm->tm_mday = days+1;
+
+ return tm;
+}
+
+/* get a positive number between n_min and n_max, for a maximum length
+ of len_max. Return -1 if error. */
+static int date_get_num(const char **pp,
+ int n_min, int n_max, int len_max)
+{
+ int i, val, c;
+ const char *p;
+
+ p = *pp;
+ val = 0;
+ for(i = 0; i < len_max; i++) {
+ c = *p;
+ if (!isdigit(c))