#include <stdio.h>

static int
bump_r(char *d, int i, int mod)
{
    int r, c = -1, nmod = 0;

    if (i < 14) {
        /* mod for the next two digits */

        if (i == 5) {
            /* figure out # of days */
            if (d[4] == '0' && d[5] == '2') { /* february? */
                if ((!(((d[0] - '0') * 10 + d[1] - '0') & 3) && d[2] == '0' &&
                  d[3] == '0') && ((d[3] & 3) || d[3] == '0' && d[2] == '0'))
                    nmod = 0x0300;
                else
                    nmod = 0x0209;
            } else {
                nmod = 0x0301 + ((d[4] == '0' && d[5] < '7') ?
                  (d[5] & 1) : !(d[5] & 1));
            }
        } else if (i & 1) {
            static int nmods[] = {
              0, 0x103 /* months */, 0, 0x204 /* hours */,
              0x0600 /* mins */, 0x0600 /* secs */};

            nmod = nmods[i >> 1];
        }

        r = 9 - (d[i] - '0') + bump_r(d, i + 1, nmod); /* flip and dec digit */
        c = r >> 4; /* carry */
        d[i] = '0' + ((9 - r) & ~c); /* flip back, mask out result if > '9' */

        if (mod && ((d[i] - '0' << 8) | (d[i + 1] - '0')) == mod) {
            d[i] = '0';
            d[i + 1] = '0' + (i == 4 || i == 6);
            c = -1;
        }
    }

    return c;
}

int
bump(char *d)
{
    bump_r(d, 0, 0);
}

int
main(int argc, char *argv[])
{
    char time[] = "19991231235959";

    puts(time);
    bump(time);
    puts(time);

    return 0;
}

