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