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