date.con commit Fix export_marks() error handling. (96d69b5)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6
   7#include "cache.h"
   8
   9/*
  10 * This is like mktime, but without normalization of tm_wday and tm_yday.
  11 */
  12time_t tm_to_time_t(const struct tm *tm)
  13{
  14        static const int mdays[] = {
  15            0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  16        };
  17        int year = tm->tm_year - 70;
  18        int month = tm->tm_mon;
  19        int day = tm->tm_mday;
  20
  21        if (year < 0 || year > 129) /* algo only works for 1970-2099 */
  22                return -1;
  23        if (month < 0 || month > 11) /* array bounds */
  24                return -1;
  25        if (month < 2 || (year + 2) % 4)
  26                day--;
  27        return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
  28                tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
  29}
  30
  31static const char *month_names[] = {
  32        "January", "February", "March", "April", "May", "June",
  33        "July", "August", "September", "October", "November", "December"
  34};
  35
  36static const char *weekday_names[] = {
  37        "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
  38};
  39
  40static time_t gm_time_t(unsigned long time, int tz)
  41{
  42        int minutes;
  43
  44        minutes = tz < 0 ? -tz : tz;
  45        minutes = (minutes / 100)*60 + (minutes % 100);
  46        minutes = tz < 0 ? -minutes : minutes;
  47        return time + minutes * 60;
  48}
  49
  50/*
  51 * The "tz" thing is passed in as this strange "decimal parse of tz"
  52 * thing, which means that tz -0100 is passed in as the integer -100,
  53 * even though it means "sixty minutes off"
  54 */
  55static struct tm *time_to_tm(unsigned long time, int tz)
  56{
  57        time_t t = gm_time_t(time, tz);
  58        return gmtime(&t);
  59}
  60
  61/*
  62 * What value of "tz" was in effect back then at "time" in the
  63 * local timezone?
  64 */
  65static int local_tzoffset(unsigned long time)
  66{
  67        time_t t, t_local;
  68        struct tm tm;
  69        int offset, eastwest;
  70
  71        t = time;
  72        localtime_r(&t, &tm);
  73        t_local = tm_to_time_t(&tm);
  74
  75        if (t_local < t) {
  76                eastwest = -1;
  77                offset = t - t_local;
  78        } else {
  79                eastwest = 1;
  80                offset = t_local - t;
  81        }
  82        offset /= 60; /* in minutes */
  83        offset = (offset % 60) + ((offset / 60) * 100);
  84        return offset * eastwest;
  85}
  86
  87const char *show_date(unsigned long time, int tz, enum date_mode mode)
  88{
  89        struct tm *tm;
  90        static char timebuf[200];
  91
  92        if (mode == DATE_RAW) {
  93                snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
  94                return timebuf;
  95        }
  96
  97        if (mode == DATE_RELATIVE) {
  98                unsigned long diff;
  99                struct timeval now;
 100                gettimeofday(&now, NULL);
 101                if (now.tv_sec < time)
 102                        return "in the future";
 103                diff = now.tv_sec - time;
 104                if (diff < 90) {
 105                        snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
 106                        return timebuf;
 107                }
 108                /* Turn it into minutes */
 109                diff = (diff + 30) / 60;
 110                if (diff < 90) {
 111                        snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
 112                        return timebuf;
 113                }
 114                /* Turn it into hours */
 115                diff = (diff + 30) / 60;
 116                if (diff < 36) {
 117                        snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
 118                        return timebuf;
 119                }
 120                /* We deal with number of days from here on */
 121                diff = (diff + 12) / 24;
 122                if (diff < 14) {
 123                        snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
 124                        return timebuf;
 125                }
 126                /* Say weeks for the past 10 weeks or so */
 127                if (diff < 70) {
 128                        snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
 129                        return timebuf;
 130                }
 131                /* Say months for the past 12 months or so */
 132                if (diff < 360) {
 133                        snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
 134                        return timebuf;
 135                }
 136                /* Give years and months for 5 years or so */
 137                if (diff < 1825) {
 138                        unsigned long years = (diff + 183) / 365;
 139                        unsigned long months = (diff % 365 + 15) / 30;
 140                        int n;
 141                        n = snprintf(timebuf, sizeof(timebuf), "%lu year%s",
 142                                        years, (years > 1 ? "s" : ""));
 143                        if (months)
 144                                snprintf(timebuf + n, sizeof(timebuf) - n,
 145                                        ", %lu month%s ago",
 146                                        months, (months > 1 ? "s" : ""));
 147                        else
 148                                snprintf(timebuf + n, sizeof(timebuf) - n,
 149                                        " ago");
 150                        return timebuf;
 151                }
 152                /* Otherwise, just years. Centuries is probably overkill. */
 153                snprintf(timebuf, sizeof(timebuf), "%lu years ago", (diff + 183) / 365);
 154                return timebuf;
 155        }
 156
 157        if (mode == DATE_LOCAL)
 158                tz = local_tzoffset(time);
 159
 160        tm = time_to_tm(time, tz);
 161        if (!tm)
 162                return NULL;
 163        if (mode == DATE_SHORT)
 164                sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
 165                                tm->tm_mon + 1, tm->tm_mday);
 166        else if (mode == DATE_ISO8601)
 167                sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
 168                                tm->tm_year + 1900,
 169                                tm->tm_mon + 1,
 170                                tm->tm_mday,
 171                                tm->tm_hour, tm->tm_min, tm->tm_sec,
 172                                tz);
 173        else if (mode == DATE_RFC2822)
 174                sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
 175                        weekday_names[tm->tm_wday], tm->tm_mday,
 176                        month_names[tm->tm_mon], tm->tm_year + 1900,
 177                        tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
 178        else
 179                sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
 180                                weekday_names[tm->tm_wday],
 181                                month_names[tm->tm_mon],
 182                                tm->tm_mday,
 183                                tm->tm_hour, tm->tm_min, tm->tm_sec,
 184                                tm->tm_year + 1900,
 185                                (mode == DATE_LOCAL) ? 0 : ' ',
 186                                tz);
 187        return timebuf;
 188}
 189
 190/*
 191 * Check these. And note how it doesn't do the summer-time conversion.
 192 *
 193 * In my world, it's always summer, and things are probably a bit off
 194 * in other ways too.
 195 */
 196static const struct {
 197        const char *name;
 198        int offset;
 199        int dst;
 200} timezone_names[] = {
 201        { "IDLW", -12, 0, },    /* International Date Line West */
 202        { "NT",   -11, 0, },    /* Nome */
 203        { "CAT",  -10, 0, },    /* Central Alaska */
 204        { "HST",  -10, 0, },    /* Hawaii Standard */
 205        { "HDT",  -10, 1, },    /* Hawaii Daylight */
 206        { "YST",   -9, 0, },    /* Yukon Standard */
 207        { "YDT",   -9, 1, },    /* Yukon Daylight */
 208        { "PST",   -8, 0, },    /* Pacific Standard */
 209        { "PDT",   -8, 1, },    /* Pacific Daylight */
 210        { "MST",   -7, 0, },    /* Mountain Standard */
 211        { "MDT",   -7, 1, },    /* Mountain Daylight */
 212        { "CST",   -6, 0, },    /* Central Standard */
 213        { "CDT",   -6, 1, },    /* Central Daylight */
 214        { "EST",   -5, 0, },    /* Eastern Standard */
 215        { "EDT",   -5, 1, },    /* Eastern Daylight */
 216        { "AST",   -3, 0, },    /* Atlantic Standard */
 217        { "ADT",   -3, 1, },    /* Atlantic Daylight */
 218        { "WAT",   -1, 0, },    /* West Africa */
 219
 220        { "GMT",    0, 0, },    /* Greenwich Mean */
 221        { "UTC",    0, 0, },    /* Universal (Coordinated) */
 222
 223        { "WET",    0, 0, },    /* Western European */
 224        { "BST",    0, 1, },    /* British Summer */
 225        { "CET",   +1, 0, },    /* Central European */
 226        { "MET",   +1, 0, },    /* Middle European */
 227        { "MEWT",  +1, 0, },    /* Middle European Winter */
 228        { "MEST",  +1, 1, },    /* Middle European Summer */
 229        { "CEST",  +1, 1, },    /* Central European Summer */
 230        { "MESZ",  +1, 1, },    /* Middle European Summer */
 231        { "FWT",   +1, 0, },    /* French Winter */
 232        { "FST",   +1, 1, },    /* French Summer */
 233        { "EET",   +2, 0, },    /* Eastern Europe, USSR Zone 1 */
 234        { "EEST",  +2, 1, },    /* Eastern European Daylight */
 235        { "WAST",  +7, 0, },    /* West Australian Standard */
 236        { "WADT",  +7, 1, },    /* West Australian Daylight */
 237        { "CCT",   +8, 0, },    /* China Coast, USSR Zone 7 */
 238        { "JST",   +9, 0, },    /* Japan Standard, USSR Zone 8 */
 239        { "EAST", +10, 0, },    /* Eastern Australian Standard */
 240        { "EADT", +10, 1, },    /* Eastern Australian Daylight */
 241        { "GST",  +10, 0, },    /* Guam Standard, USSR Zone 9 */
 242        { "NZT",  +12, 0, },    /* New Zealand */
 243        { "NZST", +12, 0, },    /* New Zealand Standard */
 244        { "NZDT", +12, 1, },    /* New Zealand Daylight */
 245        { "IDLE", +12, 0, },    /* International Date Line East */
 246};
 247
 248static int match_string(const char *date, const char *str)
 249{
 250        int i = 0;
 251
 252        for (i = 0; *date; date++, str++, i++) {
 253                if (*date == *str)
 254                        continue;
 255                if (toupper(*date) == toupper(*str))
 256                        continue;
 257                if (!isalnum(*date))
 258                        break;
 259                return 0;
 260        }
 261        return i;
 262}
 263
 264static int skip_alpha(const char *date)
 265{
 266        int i = 0;
 267        do {
 268                i++;
 269        } while (isalpha(date[i]));
 270        return i;
 271}
 272
 273/*
 274* Parse month, weekday, or timezone name
 275*/
 276static int match_alpha(const char *date, struct tm *tm, int *offset)
 277{
 278        int i;
 279
 280        for (i = 0; i < 12; i++) {
 281                int match = match_string(date, month_names[i]);
 282                if (match >= 3) {
 283                        tm->tm_mon = i;
 284                        return match;
 285                }
 286        }
 287
 288        for (i = 0; i < 7; i++) {
 289                int match = match_string(date, weekday_names[i]);
 290                if (match >= 3) {
 291                        tm->tm_wday = i;
 292                        return match;
 293                }
 294        }
 295
 296        for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
 297                int match = match_string(date, timezone_names[i].name);
 298                if (match >= 3) {
 299                        int off = timezone_names[i].offset;
 300
 301                        /* This is bogus, but we like summer */
 302                        off += timezone_names[i].dst;
 303
 304                        /* Only use the tz name offset if we don't have anything better */
 305                        if (*offset == -1)
 306                                *offset = 60*off;
 307
 308                        return match;
 309                }
 310        }
 311
 312        if (match_string(date, "PM") == 2) {
 313                tm->tm_hour = (tm->tm_hour % 12) + 12;
 314                return 2;
 315        }
 316
 317        if (match_string(date, "AM") == 2) {
 318                tm->tm_hour = (tm->tm_hour % 12) + 0;
 319                return 2;
 320        }
 321
 322        /* BAD CRAP */
 323        return skip_alpha(date);
 324}
 325
 326static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
 327{
 328        if (month > 0 && month < 13 && day > 0 && day < 32) {
 329                struct tm check = *tm;
 330                struct tm *r = (now_tm ? &check : tm);
 331                time_t specified;
 332
 333                r->tm_mon = month - 1;
 334                r->tm_mday = day;
 335                if (year == -1) {
 336                        if (!now_tm)
 337                                return 1;
 338                        r->tm_year = now_tm->tm_year;
 339                }
 340                else if (year >= 1970 && year < 2100)
 341                        r->tm_year = year - 1900;
 342                else if (year > 70 && year < 100)
 343                        r->tm_year = year;
 344                else if (year < 38)
 345                        r->tm_year = year + 100;
 346                else
 347                        return 0;
 348                if (!now_tm)
 349                        return 1;
 350
 351                specified = tm_to_time_t(r);
 352
 353                /* Be it commit time or author time, it does not make
 354                 * sense to specify timestamp way into the future.  Make
 355                 * sure it is not later than ten days from now...
 356                 */
 357                if (now + 10*24*3600 < specified)
 358                        return 0;
 359                tm->tm_mon = r->tm_mon;
 360                tm->tm_mday = r->tm_mday;
 361                if (year != -1)
 362                        tm->tm_year = r->tm_year;
 363                return 1;
 364        }
 365        return 0;
 366}
 367
 368static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
 369{
 370        time_t now;
 371        struct tm now_tm;
 372        struct tm *refuse_future;
 373        long num2, num3;
 374
 375        num2 = strtol(end+1, &end, 10);
 376        num3 = -1;
 377        if (*end == c && isdigit(end[1]))
 378                num3 = strtol(end+1, &end, 10);
 379
 380        /* Time? Date? */
 381        switch (c) {
 382        case ':':
 383                if (num3 < 0)
 384                        num3 = 0;
 385                if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
 386                        tm->tm_hour = num;
 387                        tm->tm_min = num2;
 388                        tm->tm_sec = num3;
 389                        break;
 390                }
 391                return 0;
 392
 393        case '-':
 394        case '/':
 395        case '.':
 396                now = time(NULL);
 397                refuse_future = NULL;
 398                if (gmtime_r(&now, &now_tm))
 399                        refuse_future = &now_tm;
 400
 401                if (num > 70) {
 402                        /* yyyy-mm-dd? */
 403                        if (is_date(num, num2, num3, refuse_future, now, tm))
 404                                break;
 405                        /* yyyy-dd-mm? */
 406                        if (is_date(num, num3, num2, refuse_future, now, tm))
 407                                break;
 408                }
 409                /* Our eastern European friends say dd.mm.yy[yy]
 410                 * is the norm there, so giving precedence to
 411                 * mm/dd/yy[yy] form only when separator is not '.'
 412                 */
 413                if (c != '.' &&
 414                    is_date(num3, num, num2, refuse_future, now, tm))
 415                        break;
 416                /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
 417                if (is_date(num3, num2, num, refuse_future, now, tm))
 418                        break;
 419                /* Funny European mm.dd.yy */
 420                if (c == '.' &&
 421                    is_date(num3, num, num2, refuse_future, now, tm))
 422                        break;
 423                return 0;
 424        }
 425        return end - date;
 426}
 427
 428/* Have we filled in any part of the time/date yet? */
 429static inline int nodate(struct tm *tm)
 430{
 431        return tm->tm_year < 0 &&
 432                tm->tm_mon < 0 &&
 433                tm->tm_mday < 0 &&
 434                !(tm->tm_hour | tm->tm_min | tm->tm_sec);
 435}
 436
 437/*
 438 * We've seen a digit. Time? Year? Date?
 439 */
 440static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
 441{
 442        int n;
 443        char *end;
 444        unsigned long num;
 445
 446        num = strtoul(date, &end, 10);
 447
 448        /*
 449         * Seconds since 1970? We trigger on that for any numbers with
 450         * more than 8 digits. This is because we don't want to rule out
 451         * numbers like 20070606 as a YYYYMMDD date.
 452         */
 453        if (num >= 100000000 && nodate(tm)) {
 454                time_t time = num;
 455                if (gmtime_r(&time, tm)) {
 456                        *tm_gmt = 1;
 457                        return end - date;
 458                }
 459        }
 460
 461        /*
 462         * Check for special formats: num[-.:/]num[same]num
 463         */
 464        switch (*end) {
 465        case ':':
 466        case '.':
 467        case '/':
 468        case '-':
 469                if (isdigit(end[1])) {
 470                        int match = match_multi_number(num, *end, date, end, tm);
 471                        if (match)
 472                                return match;
 473                }
 474        }
 475
 476        /*
 477         * None of the special formats? Try to guess what
 478         * the number meant. We use the number of digits
 479         * to make a more educated guess..
 480         */
 481        n = 0;
 482        do {
 483                n++;
 484        } while (isdigit(date[n]));
 485
 486        /* Four-digit year or a timezone? */
 487        if (n == 4) {
 488                if (num <= 1400 && *offset == -1) {
 489                        unsigned int minutes = num % 100;
 490                        unsigned int hours = num / 100;
 491                        *offset = hours*60 + minutes;
 492                } else if (num > 1900 && num < 2100)
 493                        tm->tm_year = num - 1900;
 494                return n;
 495        }
 496
 497        /*
 498         * Ignore lots of numerals. We took care of 4-digit years above.
 499         * Days or months must be one or two digits.
 500         */
 501        if (n > 2)
 502                return n;
 503
 504        /*
 505         * NOTE! We will give precedence to day-of-month over month or
 506         * year numbers in the 1-12 range. So 05 is always "mday 5",
 507         * unless we already have a mday..
 508         *
 509         * IOW, 01 Apr 05 parses as "April 1st, 2005".
 510         */
 511        if (num > 0 && num < 32 && tm->tm_mday < 0) {
 512                tm->tm_mday = num;
 513                return n;
 514        }
 515
 516        /* Two-digit year? */
 517        if (n == 2 && tm->tm_year < 0) {
 518                if (num < 10 && tm->tm_mday >= 0) {
 519                        tm->tm_year = num + 100;
 520                        return n;
 521                }
 522                if (num >= 70) {
 523                        tm->tm_year = num;
 524                        return n;
 525                }
 526        }
 527
 528        if (num > 0 && num < 32) {
 529                tm->tm_mday = num;
 530        } else if (num > 0 && num < 13) {
 531                tm->tm_mon = num-1;
 532        }
 533
 534        return n;
 535}
 536
 537static int match_tz(const char *date, int *offp)
 538{
 539        char *end;
 540        int offset = strtoul(date+1, &end, 10);
 541        int min, hour;
 542        int n = end - date - 1;
 543
 544        min = offset % 100;
 545        hour = offset / 100;
 546
 547        /*
 548         * Don't accept any random crap.. At least 3 digits, and
 549         * a valid minute. We might want to check that the minutes
 550         * are divisible by 30 or something too.
 551         */
 552        if (min < 60 && n > 2) {
 553                offset = hour*60+min;
 554                if (*date == '-')
 555                        offset = -offset;
 556
 557                *offp = offset;
 558        }
 559        return end - date;
 560}
 561
 562static int date_string(unsigned long date, int offset, char *buf, int len)
 563{
 564        int sign = '+';
 565
 566        if (offset < 0) {
 567                offset = -offset;
 568                sign = '-';
 569        }
 570        return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
 571}
 572
 573/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
 574   (i.e. English) day/month names, and it doesn't work correctly with %z. */
 575int parse_date(const char *date, char *result, int maxlen)
 576{
 577        struct tm tm;
 578        int offset, tm_gmt;
 579        time_t then;
 580
 581        memset(&tm, 0, sizeof(tm));
 582        tm.tm_year = -1;
 583        tm.tm_mon = -1;
 584        tm.tm_mday = -1;
 585        tm.tm_isdst = -1;
 586        offset = -1;
 587        tm_gmt = 0;
 588
 589        for (;;) {
 590                int match = 0;
 591                unsigned char c = *date;
 592
 593                /* Stop at end of string or newline */
 594                if (!c || c == '\n')
 595                        break;
 596
 597                if (isalpha(c))
 598                        match = match_alpha(date, &tm, &offset);
 599                else if (isdigit(c))
 600                        match = match_digit(date, &tm, &offset, &tm_gmt);
 601                else if ((c == '-' || c == '+') && isdigit(date[1]))
 602                        match = match_tz(date, &offset);
 603
 604                if (!match) {
 605                        /* BAD CRAP */
 606                        match = 1;
 607                }
 608
 609                date += match;
 610        }
 611
 612        /* mktime uses local timezone */
 613        then = tm_to_time_t(&tm);
 614        if (offset == -1)
 615                offset = (then - mktime(&tm)) / 60;
 616
 617        if (then == -1)
 618                return -1;
 619
 620        if (!tm_gmt)
 621                then -= offset * 60;
 622        return date_string(then, offset, result, maxlen);
 623}
 624
 625enum date_mode parse_date_format(const char *format)
 626{
 627        if (!strcmp(format, "relative"))
 628                return DATE_RELATIVE;
 629        else if (!strcmp(format, "iso8601") ||
 630                 !strcmp(format, "iso"))
 631                return DATE_ISO8601;
 632        else if (!strcmp(format, "rfc2822") ||
 633                 !strcmp(format, "rfc"))
 634                return DATE_RFC2822;
 635        else if (!strcmp(format, "short"))
 636                return DATE_SHORT;
 637        else if (!strcmp(format, "local"))
 638                return DATE_LOCAL;
 639        else if (!strcmp(format, "default"))
 640                return DATE_NORMAL;
 641        else if (!strcmp(format, "raw"))
 642                return DATE_RAW;
 643        else
 644                die("unknown date format %s", format);
 645}
 646
 647void datestamp(char *buf, int bufsize)
 648{
 649        time_t now;
 650        int offset;
 651
 652        time(&now);
 653
 654        offset = tm_to_time_t(localtime(&now)) - now;
 655        offset /= 60;
 656
 657        date_string(now, offset, buf, bufsize);
 658}
 659
 660static void update_tm(struct tm *tm, unsigned long sec)
 661{
 662        time_t n = mktime(tm) - sec;
 663        localtime_r(&n, tm);
 664}
 665
 666static void date_yesterday(struct tm *tm, int *num)
 667{
 668        update_tm(tm, 24*60*60);
 669}
 670
 671static void date_time(struct tm *tm, int hour)
 672{
 673        if (tm->tm_hour < hour)
 674                date_yesterday(tm, NULL);
 675        tm->tm_hour = hour;
 676        tm->tm_min = 0;
 677        tm->tm_sec = 0;
 678}
 679
 680static void date_midnight(struct tm *tm, int *num)
 681{
 682        date_time(tm, 0);
 683}
 684
 685static void date_noon(struct tm *tm, int *num)
 686{
 687        date_time(tm, 12);
 688}
 689
 690static void date_tea(struct tm *tm, int *num)
 691{
 692        date_time(tm, 17);
 693}
 694
 695static void date_pm(struct tm *tm, int *num)
 696{
 697        int hour, n = *num;
 698        *num = 0;
 699
 700        hour = tm->tm_hour;
 701        if (n) {
 702                hour = n;
 703                tm->tm_min = 0;
 704                tm->tm_sec = 0;
 705        }
 706        tm->tm_hour = (hour % 12) + 12;
 707}
 708
 709static void date_am(struct tm *tm, int *num)
 710{
 711        int hour, n = *num;
 712        *num = 0;
 713
 714        hour = tm->tm_hour;
 715        if (n) {
 716                hour = n;
 717                tm->tm_min = 0;
 718                tm->tm_sec = 0;
 719        }
 720        tm->tm_hour = (hour % 12);
 721}
 722
 723static void date_never(struct tm *tm, int *num)
 724{
 725        time_t n = 0;
 726        localtime_r(&n, tm);
 727}
 728
 729static const struct special {
 730        const char *name;
 731        void (*fn)(struct tm *, int *);
 732} special[] = {
 733        { "yesterday", date_yesterday },
 734        { "noon", date_noon },
 735        { "midnight", date_midnight },
 736        { "tea", date_tea },
 737        { "PM", date_pm },
 738        { "AM", date_am },
 739        { "never", date_never },
 740        { NULL }
 741};
 742
 743static const char *number_name[] = {
 744        "zero", "one", "two", "three", "four",
 745        "five", "six", "seven", "eight", "nine", "ten",
 746};
 747
 748static const struct typelen {
 749        const char *type;
 750        int length;
 751} typelen[] = {
 752        { "seconds", 1 },
 753        { "minutes", 60 },
 754        { "hours", 60*60 },
 755        { "days", 24*60*60 },
 756        { "weeks", 7*24*60*60 },
 757        { NULL }
 758};
 759
 760static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
 761{
 762        const struct typelen *tl;
 763        const struct special *s;
 764        const char *end = date;
 765        int i;
 766
 767        while (isalpha(*++end));
 768                ;
 769
 770        for (i = 0; i < 12; i++) {
 771                int match = match_string(date, month_names[i]);
 772                if (match >= 3) {
 773                        tm->tm_mon = i;
 774                        return end;
 775                }
 776        }
 777
 778        for (s = special; s->name; s++) {
 779                int len = strlen(s->name);
 780                if (match_string(date, s->name) == len) {
 781                        s->fn(tm, num);
 782                        return end;
 783                }
 784        }
 785
 786        if (!*num) {
 787                for (i = 1; i < 11; i++) {
 788                        int len = strlen(number_name[i]);
 789                        if (match_string(date, number_name[i]) == len) {
 790                                *num = i;
 791                                return end;
 792                        }
 793                }
 794                if (match_string(date, "last") == 4)
 795                        *num = 1;
 796                return end;
 797        }
 798
 799        tl = typelen;
 800        while (tl->type) {
 801                int len = strlen(tl->type);
 802                if (match_string(date, tl->type) >= len-1) {
 803                        update_tm(tm, tl->length * *num);
 804                        *num = 0;
 805                        return end;
 806                }
 807                tl++;
 808        }
 809
 810        for (i = 0; i < 7; i++) {
 811                int match = match_string(date, weekday_names[i]);
 812                if (match >= 3) {
 813                        int diff, n = *num -1;
 814                        *num = 0;
 815
 816                        diff = tm->tm_wday - i;
 817                        if (diff <= 0)
 818                                n++;
 819                        diff += 7*n;
 820
 821                        update_tm(tm, diff * 24 * 60 * 60);
 822                        return end;
 823                }
 824        }
 825
 826        if (match_string(date, "months") >= 5) {
 827                int n = tm->tm_mon - *num;
 828                *num = 0;
 829                while (n < 0) {
 830                        n += 12;
 831                        tm->tm_year--;
 832                }
 833                tm->tm_mon = n;
 834                return end;
 835        }
 836
 837        if (match_string(date, "years") >= 4) {
 838                tm->tm_year -= *num;
 839                *num = 0;
 840                return end;
 841        }
 842
 843        return end;
 844}
 845
 846static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
 847{
 848        char *end;
 849        unsigned long number = strtoul(date, &end, 10);
 850
 851        switch (*end) {
 852        case ':':
 853        case '.':
 854        case '/':
 855        case '-':
 856                if (isdigit(end[1])) {
 857                        int match = match_multi_number(number, *end, date, end, tm);
 858                        if (match)
 859                                return date + match;
 860                }
 861        }
 862
 863        /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
 864        if (date[0] != '0' || end - date <= 2)
 865                *num = number;
 866        return end;
 867}
 868
 869unsigned long approxidate(const char *date)
 870{
 871        int number = 0;
 872        struct tm tm, now;
 873        struct timeval tv;
 874        time_t time_sec;
 875        char buffer[50];
 876
 877        if (parse_date(date, buffer, sizeof(buffer)) > 0)
 878                return strtoul(buffer, NULL, 10);
 879
 880        gettimeofday(&tv, NULL);
 881        time_sec = tv.tv_sec;
 882        localtime_r(&time_sec, &tm);
 883        now = tm;
 884        for (;;) {
 885                unsigned char c = *date;
 886                if (!c)
 887                        break;
 888                date++;
 889                if (isdigit(c)) {
 890                        date = approxidate_digit(date-1, &tm, &number);
 891                        continue;
 892                }
 893                if (isalpha(c))
 894                        date = approxidate_alpha(date-1, &tm, &number);
 895        }
 896        if (number > 0 && number < 32)
 897                tm.tm_mday = number;
 898        if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
 899                tm.tm_year--;
 900        return mktime(&tm);
 901}