git
/
date.c
1418 строк · 32.7 Кб
1/*
2* GIT - The information manager from hell
3*
4* Copyright (C) Linus Torvalds, 2005
5*/
6
7#include "git-compat-util.h"8#include "date.h"9#include "gettext.h"10#include "pager.h"11#include "strbuf.h"12
13/*
14* This is like mktime, but without normalization of tm_wday and tm_yday.
15*/
16time_t tm_to_time_t(const struct tm *tm)17{
18static const int mdays[] = {190, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 33420};21int year = tm->tm_year - 70;22int month = tm->tm_mon;23int day = tm->tm_mday;24
25if (year < 0 || year > 129) /* algo only works for 1970-2099 */26return -1;27if (month < 0 || month > 11) /* array bounds */28return -1;29if (month < 2 || (year + 2) % 4)30day--;31if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)32return -1;33return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +34tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;35}
36
37static const char *month_names[] = {38"January", "February", "March", "April", "May", "June",39"July", "August", "September", "October", "November", "December"40};41
42static const char *weekday_names[] = {43"Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"44};45
46static time_t gm_time_t(timestamp_t time, int tz)47{
48int minutes;49
50minutes = tz < 0 ? -tz : tz;51minutes = (minutes / 100)*60 + (minutes % 100);52minutes = tz < 0 ? -minutes : minutes;53
54if (minutes > 0) {55if (unsigned_add_overflows(time, minutes * 60))56die("Timestamp+tz too large: %"PRItime" +%04d",57time, tz);58} else if (time < -minutes * 60)59die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);60time += minutes * 60;61if (date_overflows(time))62die("Timestamp too large for this system: %"PRItime, time);63return (time_t)time;64}
65
66/*
67* The "tz" thing is passed in as this strange "decimal parse of tz"
68* thing, which means that tz -0100 is passed in as the integer -100,
69* even though it means "sixty minutes off"
70*/
71static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)72{
73time_t t = gm_time_t(time, tz);74return gmtime_r(&t, tm);75}
76
77static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)78{
79time_t t = time;80return localtime_r(&t, tm);81}
82
83/*
84* Fill in the localtime 'struct tm' for the supplied time,
85* and return the local tz.
86*/
87static int local_time_tzoffset(time_t t, struct tm *tm)88{
89time_t t_local;90int offset, eastwest;91
92localtime_r(&t, tm);93t_local = tm_to_time_t(tm);94if (t_local == -1)95return 0; /* error; just use +0000 */96if (t_local < t) {97eastwest = -1;98offset = t - t_local;99} else {100eastwest = 1;101offset = t_local - t;102}103offset /= 60; /* in minutes */104offset = (offset % 60) + ((offset / 60) * 100);105return offset * eastwest;106}
107
108/*
109* What value of "tz" was in effect back then at "time" in the
110* local timezone?
111*/
112static int local_tzoffset(timestamp_t time)113{
114struct tm tm;115
116if (date_overflows(time))117die("Timestamp too large for this system: %"PRItime, time);118
119return local_time_tzoffset((time_t)time, &tm);120}
121
122static void get_time(struct timeval *now)123{
124const char *x;125
126x = getenv("GIT_TEST_DATE_NOW");127if (x) {128now->tv_sec = atoi(x);129now->tv_usec = 0;130}131else132gettimeofday(now, NULL);133}
134
135void show_date_relative(timestamp_t time, struct strbuf *timebuf)136{
137struct timeval now;138timestamp_t diff;139
140get_time(&now);141if (now.tv_sec < time) {142strbuf_addstr(timebuf, _("in the future"));143return;144}145diff = now.tv_sec - time;146if (diff < 90) {147strbuf_addf(timebuf,148Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);149return;150}151/* Turn it into minutes */152diff = (diff + 30) / 60;153if (diff < 90) {154strbuf_addf(timebuf,155Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);156return;157}158/* Turn it into hours */159diff = (diff + 30) / 60;160if (diff < 36) {161strbuf_addf(timebuf,162Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);163return;164}165/* We deal with number of days from here on */166diff = (diff + 12) / 24;167if (diff < 14) {168strbuf_addf(timebuf,169Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);170return;171}172/* Say weeks for the past 10 weeks or so */173if (diff < 70) {174strbuf_addf(timebuf,175Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),176(diff + 3) / 7);177return;178}179/* Say months for the past 12 months or so */180if (diff < 365) {181strbuf_addf(timebuf,182Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),183(diff + 15) / 30);184return;185}186/* Give years and months for 5 years or so */187if (diff < 1825) {188timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);189timestamp_t years = totalmonths / 12;190timestamp_t months = totalmonths % 12;191if (months) {192struct strbuf sb = STRBUF_INIT;193strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);194strbuf_addf(timebuf,195/* TRANSLATORS: "%s" is "<n> years" */196Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),197sb.buf, months);198strbuf_release(&sb);199} else200strbuf_addf(timebuf,201Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);202return;203}204/* Otherwise, just years. Centuries is probably overkill. */205strbuf_addf(timebuf,206Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),207(diff + 183) / 365);208}
209
210struct date_mode date_mode_from_type(enum date_mode_type type)211{
212struct date_mode mode = DATE_MODE_INIT;213if (type == DATE_STRFTIME)214BUG("cannot create anonymous strftime date_mode struct");215mode.type = type;216return mode;217}
218
219static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local)220{
221struct {222unsigned int year:1,223date:1,224wday:1,225time:1,226seconds:1,227tz:1;228} hide = { 0 };229
230hide.tz = local || tz == human_tz;231hide.year = tm->tm_year == human_tm->tm_year;232if (hide.year) {233if (tm->tm_mon == human_tm->tm_mon) {234if (tm->tm_mday > human_tm->tm_mday) {235/* Future date: think timezones */236} else if (tm->tm_mday == human_tm->tm_mday) {237hide.date = hide.wday = 1;238} else if (tm->tm_mday + 5 > human_tm->tm_mday) {239/* Leave just weekday if it was a few days ago */240hide.date = 1;241}242}243}244
245/* Show "today" times as just relative times */246if (hide.wday) {247show_date_relative(time, buf);248return;249}250
251/*252* Always hide seconds for human-readable.
253* Hide timezone if showing date.
254* Hide weekday and time if showing year.
255*
256* The logic here is two-fold:
257* (a) only show details when recent enough to matter
258* (b) keep the maximum length "similar", and in check
259*/
260if (human_tm->tm_year) {261hide.seconds = 1;262hide.tz |= !hide.date;263hide.wday = hide.time = !hide.year;264}265
266if (!hide.wday)267strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]);268if (!hide.date)269strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday);270
271/* Do we want AM/PM depending on locale? */272if (!hide.time) {273strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min);274if (!hide.seconds)275strbuf_addf(buf, ":%02d", tm->tm_sec);276} else277strbuf_rtrim(buf);278
279if (!hide.year)280strbuf_addf(buf, " %d", tm->tm_year + 1900);281
282if (!hide.tz)283strbuf_addf(buf, " %+05d", tz);284}
285
286const char *show_date(timestamp_t time, int tz, struct date_mode mode)287{
288struct tm *tm;289struct tm tmbuf = { 0 };290struct tm human_tm = { 0 };291int human_tz = -1;292static struct strbuf timebuf = STRBUF_INIT;293
294if (mode.type == DATE_UNIX) {295strbuf_reset(&timebuf);296strbuf_addf(&timebuf, "%"PRItime, time);297return timebuf.buf;298}299
300if (mode.type == DATE_HUMAN) {301struct timeval now;302
303get_time(&now);304
305/* Fill in the data for "current time" in human_tz and human_tm */306human_tz = local_time_tzoffset(now.tv_sec, &human_tm);307}308
309if (mode.local)310tz = local_tzoffset(time);311
312if (mode.type == DATE_RAW) {313strbuf_reset(&timebuf);314strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);315return timebuf.buf;316}317
318if (mode.type == DATE_RELATIVE) {319strbuf_reset(&timebuf);320show_date_relative(time, &timebuf);321return timebuf.buf;322}323
324if (mode.local)325tm = time_to_tm_local(time, &tmbuf);326else327tm = time_to_tm(time, tz, &tmbuf);328if (!tm) {329tm = time_to_tm(0, 0, &tmbuf);330tz = 0;331}332
333strbuf_reset(&timebuf);334if (mode.type == DATE_SHORT)335strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,336tm->tm_mon + 1, tm->tm_mday);337else if (mode.type == DATE_ISO8601)338strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",339tm->tm_year + 1900,340tm->tm_mon + 1,341tm->tm_mday,342tm->tm_hour, tm->tm_min, tm->tm_sec,343tz);344else if (mode.type == DATE_ISO8601_STRICT) {345strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d",346tm->tm_year + 1900,347tm->tm_mon + 1,348tm->tm_mday,349tm->tm_hour, tm->tm_min, tm->tm_sec);350if (tz == 0) {351strbuf_addch(&timebuf, 'Z');352} else {353strbuf_addch(&timebuf, tz >= 0 ? '+' : '-');354tz = abs(tz);355strbuf_addf(&timebuf, "%02d:%02d", tz / 100, tz % 100);356}357} else if (mode.type == DATE_RFC2822)358strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",359weekday_names[tm->tm_wday], tm->tm_mday,360month_names[tm->tm_mon], tm->tm_year + 1900,361tm->tm_hour, tm->tm_min, tm->tm_sec, tz);362else if (mode.type == DATE_STRFTIME)363strbuf_addftime(&timebuf, mode.strftime_fmt, tm, tz,364!mode.local);365else366show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode.local);367return timebuf.buf;368}
369
370/*
371* Check these. And note how it doesn't do the summer-time conversion.
372*
373* In my world, it's always summer, and things are probably a bit off
374* in other ways too.
375*/
376static const struct {377const char *name;378int offset;379int dst;380} timezone_names[] = {381{ "IDLW", -12, 0, }, /* International Date Line West */382{ "NT", -11, 0, }, /* Nome */383{ "CAT", -10, 0, }, /* Central Alaska */384{ "HST", -10, 0, }, /* Hawaii Standard */385{ "HDT", -10, 1, }, /* Hawaii Daylight */386{ "YST", -9, 0, }, /* Yukon Standard */387{ "YDT", -9, 1, }, /* Yukon Daylight */388{ "PST", -8, 0, }, /* Pacific Standard */389{ "PDT", -8, 1, }, /* Pacific Daylight */390{ "MST", -7, 0, }, /* Mountain Standard */391{ "MDT", -7, 1, }, /* Mountain Daylight */392{ "CST", -6, 0, }, /* Central Standard */393{ "CDT", -6, 1, }, /* Central Daylight */394{ "EST", -5, 0, }, /* Eastern Standard */395{ "EDT", -5, 1, }, /* Eastern Daylight */396{ "AST", -3, 0, }, /* Atlantic Standard */397{ "ADT", -3, 1, }, /* Atlantic Daylight */398{ "WAT", -1, 0, }, /* West Africa */399
400{ "GMT", 0, 0, }, /* Greenwich Mean */401{ "UTC", 0, 0, }, /* Universal (Coordinated) */402{ "Z", 0, 0, }, /* Zulu, alias for UTC */403
404{ "WET", 0, 0, }, /* Western European */405{ "BST", 0, 1, }, /* British Summer */406{ "CET", +1, 0, }, /* Central European */407{ "MET", +1, 0, }, /* Middle European */408{ "MEWT", +1, 0, }, /* Middle European Winter */409{ "MEST", +1, 1, }, /* Middle European Summer */410{ "CEST", +1, 1, }, /* Central European Summer */411{ "MESZ", +1, 1, }, /* Middle European Summer */412{ "FWT", +1, 0, }, /* French Winter */413{ "FST", +1, 1, }, /* French Summer */414{ "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */415{ "EEST", +2, 1, }, /* Eastern European Daylight */416{ "WAST", +7, 0, }, /* West Australian Standard */417{ "WADT", +7, 1, }, /* West Australian Daylight */418{ "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */419{ "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */420{ "EAST", +10, 0, }, /* Eastern Australian Standard */421{ "EADT", +10, 1, }, /* Eastern Australian Daylight */422{ "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */423{ "NZT", +12, 0, }, /* New Zealand */424{ "NZST", +12, 0, }, /* New Zealand Standard */425{ "NZDT", +12, 1, }, /* New Zealand Daylight */426{ "IDLE", +12, 0, }, /* International Date Line East */427};428
429static int match_string(const char *date, const char *str)430{
431int i = 0;432
433for (i = 0; *date; date++, str++, i++) {434if (*date == *str)435continue;436if (toupper(*date) == toupper(*str))437continue;438if (!isalnum(*date))439break;440return 0;441}442return i;443}
444
445static int skip_alpha(const char *date)446{
447int i = 0;448do {449i++;450} while (isalpha(date[i]));451return i;452}
453
454/*
455* Parse month, weekday, or timezone name
456*/
457static int match_alpha(const char *date, struct tm *tm, int *offset)458{
459int i;460
461for (i = 0; i < 12; i++) {462int match = match_string(date, month_names[i]);463if (match >= 3) {464tm->tm_mon = i;465return match;466}467}468
469for (i = 0; i < 7; i++) {470int match = match_string(date, weekday_names[i]);471if (match >= 3) {472tm->tm_wday = i;473return match;474}475}476
477for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {478int match = match_string(date, timezone_names[i].name);479if (match >= 3 || match == strlen(timezone_names[i].name)) {480int off = timezone_names[i].offset;481
482/* This is bogus, but we like summer */483off += timezone_names[i].dst;484
485/* Only use the tz name offset if we don't have anything better */486if (*offset == -1)487*offset = 60*off;488
489return match;490}491}492
493if (match_string(date, "PM") == 2) {494tm->tm_hour = (tm->tm_hour % 12) + 12;495return 2;496}497
498if (match_string(date, "AM") == 2) {499tm->tm_hour = (tm->tm_hour % 12) + 0;500return 2;501}502
503/* ISO-8601 allows yyyymmDD'T'HHMMSS, with less precision */504if (*date == 'T' && isdigit(date[1]) && tm->tm_hour == -1) {505tm->tm_min = tm->tm_sec = 0;506return 1;507}508
509/* BAD CRAP */510return skip_alpha(date);511}
512
513static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)514{
515if (month > 0 && month < 13 && day > 0 && day < 32) {516struct tm check = *tm;517struct tm *r = (now_tm ? &check : tm);518time_t specified;519
520r->tm_mon = month - 1;521r->tm_mday = day;522if (year == -1) {523if (!now_tm)524return 1;525r->tm_year = now_tm->tm_year;526}527else if (year >= 1970 && year < 2100)528r->tm_year = year - 1900;529else if (year > 70 && year < 100)530r->tm_year = year;531else if (year < 38)532r->tm_year = year + 100;533else534return -1;535if (!now_tm)536return 0;537
538specified = tm_to_time_t(r);539
540/* Be it commit time or author time, it does not make541* sense to specify timestamp way into the future. Make
542* sure it is not later than ten days from now...
543*/
544if ((specified != -1) && (now + 10*24*3600 < specified))545return -1;546tm->tm_mon = r->tm_mon;547tm->tm_mday = r->tm_mday;548if (year != -1)549tm->tm_year = r->tm_year;550return 0;551}552return -1;553}
554
555static int set_time(long hour, long minute, long second, struct tm *tm)556{
557/* We accept 61st second because of leap second */558if (0 <= hour && hour <= 24 &&5590 <= minute && minute < 60 &&5600 <= second && second <= 60) {561tm->tm_hour = hour;562tm->tm_min = minute;563tm->tm_sec = second;564return 0;565}566return -1;567}
568
569static int is_date_known(struct tm *tm)570{
571return tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1;572}
573
574static int match_multi_number(timestamp_t num, char c, const char *date,575char *end, struct tm *tm, time_t now)576{
577struct tm now_tm;578struct tm *refuse_future;579long num2, num3;580
581num2 = strtol(end+1, &end, 10);582num3 = -1;583if (*end == c && isdigit(end[1]))584num3 = strtol(end+1, &end, 10);585
586/* Time? Date? */587switch (c) {588case ':':589if (num3 < 0)590num3 = 0;591if (set_time(num, num2, num3, tm) == 0) {592/*593* If %H:%M:%S was just parsed followed by: .<num4>
594* Consider (& discard) it as fractional second
595* if %Y%m%d is parsed before.
596*/
597if (*end == '.' && isdigit(end[1]) && is_date_known(tm))598strtol(end + 1, &end, 10);599break;600}601return 0;602
603case '-':604case '/':605case '.':606if (!now)607now = time(NULL);608refuse_future = NULL;609if (gmtime_r(&now, &now_tm))610refuse_future = &now_tm;611
612if (num > 70) {613/* yyyy-mm-dd? */614if (set_date(num, num2, num3, NULL, now, tm) == 0)615break;616/* yyyy-dd-mm? */617if (set_date(num, num3, num2, NULL, now, tm) == 0)618break;619}620/* Our eastern European friends say dd.mm.yy[yy]621* is the norm there, so giving precedence to
622* mm/dd/yy[yy] form only when separator is not '.'
623*/
624if (c != '.' &&625set_date(num3, num, num2, refuse_future, now, tm) == 0)626break;627/* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */628if (set_date(num3, num2, num, refuse_future, now, tm) == 0)629break;630/* Funny European mm.dd.yy */631if (c == '.' &&632set_date(num3, num, num2, refuse_future, now, tm) == 0)633break;634return 0;635}636return end - date;637}
638
639/*
640* Have we filled in any part of the time/date yet?
641* We just do a binary 'and' to see if the sign bit
642* is set in all the values.
643*/
644static inline int nodate(struct tm *tm)645{
646return (tm->tm_year &647tm->tm_mon &648tm->tm_mday &649tm->tm_hour &650tm->tm_min &651tm->tm_sec) < 0;652}
653
654/*
655* Have we seen an ISO-8601-alike date, i.e. 20220101T0,
656* In which, hour is still unset,
657* and minutes and second has been set to 0.
658*/
659static inline int maybeiso8601(struct tm *tm)660{
661return tm->tm_hour == -1 &&662tm->tm_min == 0 &&663tm->tm_sec == 0;664}
665
666/*
667* We've seen a digit. Time? Year? Date?
668*/
669static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)670{
671int n;672char *end;673timestamp_t num;674
675num = parse_timestamp(date, &end, 10);676
677/*678* Seconds since 1970? We trigger on that for any numbers with
679* more than 8 digits. This is because we don't want to rule out
680* numbers like 20070606 as a YYYYMMDD date.
681*/
682if (num >= 100000000 && nodate(tm)) {683time_t time = num;684if (gmtime_r(&time, tm)) {685*tm_gmt = 1;686return end - date;687}688}689
690/*691* Check for special formats: num[-.:/]num[same]num
692*/
693switch (*end) {694case ':':695case '.':696case '/':697case '-':698if (isdigit(end[1])) {699int match = match_multi_number(num, *end, date, end, tm, 0);700if (match)701return match;702}703}704
705/*706* None of the special formats? Try to guess what
707* the number meant. We use the number of digits
708* to make a more educated guess..
709*/
710n = 0;711do {712n++;713} while (isdigit(date[n]));714
715/* 8 digits, compact style of ISO-8601's date: YYYYmmDD */716/* 6 digits, compact style of ISO-8601's time: HHMMSS */717if (n == 8 || n == 6) {718unsigned int num1 = num / 10000;719unsigned int num2 = (num % 10000) / 100;720unsigned int num3 = num % 100;721if (n == 8)722set_date(num1, num2, num3, NULL, time(NULL), tm);723else if (n == 6 && set_time(num1, num2, num3, tm) == 0 &&724*end == '.' && isdigit(end[1]))725strtoul(end + 1, &end, 10);726return end - date;727}728
729/* reduced precision of ISO-8601's time: HHMM or HH */730if (maybeiso8601(tm)) {731unsigned int num1 = num;732unsigned int num2 = 0;733if (n == 4) {734num1 = num / 100;735num2 = num % 100;736}737if ((n == 4 || n == 2) && !nodate(tm) &&738set_time(num1, num2, 0, tm) == 0)739return n;740/*741* We thought this is an ISO-8601 time string,
742* we set minutes and seconds to 0,
743* turn out it isn't, rollback the change.
744*/
745tm->tm_min = tm->tm_sec = -1;746}747
748/* Four-digit year or a timezone? */749if (n == 4) {750if (num <= 1400 && *offset == -1) {751unsigned int minutes = num % 100;752unsigned int hours = num / 100;753*offset = hours*60 + minutes;754} else if (num > 1900 && num < 2100)755tm->tm_year = num - 1900;756return n;757}758
759/*760* Ignore lots of numerals. We took care of 4-digit years above.
761* Days or months must be one or two digits.
762*/
763if (n > 2)764return n;765
766/*767* NOTE! We will give precedence to day-of-month over month or
768* year numbers in the 1-12 range. So 05 is always "mday 5",
769* unless we already have a mday..
770*
771* IOW, 01 Apr 05 parses as "April 1st, 2005".
772*/
773if (num > 0 && num < 32 && tm->tm_mday < 0) {774tm->tm_mday = num;775return n;776}777
778/* Two-digit year? */779if (n == 2 && tm->tm_year < 0) {780if (num < 10 && tm->tm_mday >= 0) {781tm->tm_year = num + 100;782return n;783}784if (num >= 70) {785tm->tm_year = num;786return n;787}788}789
790if (num > 0 && num < 13 && tm->tm_mon < 0)791tm->tm_mon = num-1;792
793return n;794}
795
796static int match_tz(const char *date, int *offp)797{
798char *end;799int hour = strtoul(date + 1, &end, 10);800int n = end - (date + 1);801int min = 0;802
803if (n == 4) {804/* hhmm */805min = hour % 100;806hour = hour / 100;807} else if (n != 2) {808min = 99; /* random crap */809} else if (*end == ':') {810/* hh:mm? */811min = strtoul(end + 1, &end, 10);812if (end - (date + 1) != 5)813min = 99; /* random crap */814} /* otherwise we parsed "hh" */815
816/*817* Don't accept any random crap. Even though some places have
818* offset larger than 12 hours (e.g. Pacific/Kiritimati is at
819* UTC+14), there is something wrong if hour part is much
820* larger than that. We might also want to check that the
821* minutes are divisible by 15 or something too. (Offset of
822* Kathmandu, Nepal is UTC+5:45)
823*/
824if (min < 60 && hour < 24) {825int offset = hour * 60 + min;826if (*date == '-')827offset = -offset;828*offp = offset;829}830return end - date;831}
832
833static void date_string(timestamp_t date, int offset, struct strbuf *buf)834{
835int sign = '+';836
837if (offset < 0) {838offset = -offset;839sign = '-';840}841strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);842}
843
844/*
845* Parse a string like "0 +0000" as ancient timestamp near epoch, but
846* only when it appears not as part of any other string.
847*/
848static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)849{
850char *end;851timestamp_t stamp;852int ofs;853
854if (*date < '0' || '9' < *date)855return -1;856stamp = parse_timestamp(date, &end, 10);857if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))858return -1;859date = end + 2;860ofs = strtol(date, &end, 10);861if ((*end != '\0' && (*end != '\n')) || end != date + 4)862return -1;863ofs = (ofs / 100) * 60 + (ofs % 100);864if (date[-1] == '-')865ofs = -ofs;866*timestamp = stamp;867*offset = ofs;868return 0;869}
870
871
872/* timestamp of 2099-12-31T23:59:59Z, including 32 leap days */
873static const timestamp_t timestamp_max = (((timestamp_t)2100 - 1970) * 365 + 32) * 24 * 60 * 60 - 1;874
875/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
876(i.e. English) day/month names, and it doesn't work correctly with %z. */
877int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)878{
879struct tm tm;880int tm_gmt;881timestamp_t dummy_timestamp;882int dummy_offset;883
884if (!timestamp)885timestamp = &dummy_timestamp;886if (!offset)887offset = &dummy_offset;888
889memset(&tm, 0, sizeof(tm));890tm.tm_year = -1;891tm.tm_mon = -1;892tm.tm_mday = -1;893tm.tm_isdst = -1;894tm.tm_hour = -1;895tm.tm_min = -1;896tm.tm_sec = -1;897*offset = -1;898tm_gmt = 0;899
900if (*date == '@' &&901!match_object_header_date(date + 1, timestamp, offset))902return 0; /* success */903for (;;) {904int match = 0;905unsigned char c = *date;906
907/* Stop at end of string or newline */908if (!c || c == '\n')909break;910
911if (isalpha(c))912match = match_alpha(date, &tm, offset);913else if (isdigit(c))914match = match_digit(date, &tm, offset, &tm_gmt);915else if ((c == '-' || c == '+') && isdigit(date[1]))916match = match_tz(date, offset);917
918if (!match) {919/* BAD CRAP */920match = 1;921}922
923date += match;924}925
926/* do not use mktime(), which uses local timezone, here */927*timestamp = tm_to_time_t(&tm);928if (*timestamp == -1)929return -1;930
931if (*offset == -1) {932time_t temp_time;933
934/* gmtime_r() in match_digit() may have clobbered it */935tm.tm_isdst = -1;936temp_time = mktime(&tm);937if ((time_t)*timestamp > temp_time) {938*offset = ((time_t)*timestamp - temp_time) / 60;939} else {940*offset = -(int)((temp_time - (time_t)*timestamp) / 60);941}942}943
944if (!tm_gmt) {945if (*offset > 0 && *offset * 60 > *timestamp)946return -1;947if (*offset < 0 && -*offset * 60 > timestamp_max - *timestamp)948return -1;949*timestamp -= *offset * 60;950}951
952return 0; /* success */953}
954
955int parse_expiry_date(const char *date, timestamp_t *timestamp)956{
957int errors = 0;958
959if (!strcmp(date, "never") || !strcmp(date, "false"))960*timestamp = 0;961else if (!strcmp(date, "all") || !strcmp(date, "now"))962/*963* We take over "now" here, which usually translates
964* to the current timestamp. This is because the user
965* really means to expire everything that was done in
966* the past, and by definition reflogs are the record
967* of the past, and there is nothing from the future
968* to be kept.
969*/
970*timestamp = TIME_MAX;971else972*timestamp = approxidate_careful(date, &errors);973
974return errors;975}
976
977int parse_date(const char *date, struct strbuf *result)978{
979timestamp_t timestamp;980int offset;981if (parse_date_basic(date, ×tamp, &offset))982return -1;983date_string(timestamp, offset, result);984return 0;985}
986
987static enum date_mode_type parse_date_type(const char *format, const char **end)988{
989if (skip_prefix(format, "relative", end))990return DATE_RELATIVE;991if (skip_prefix(format, "iso8601-strict", end) ||992skip_prefix(format, "iso-strict", end))993return DATE_ISO8601_STRICT;994if (skip_prefix(format, "iso8601", end) ||995skip_prefix(format, "iso", end))996return DATE_ISO8601;997if (skip_prefix(format, "rfc2822", end) ||998skip_prefix(format, "rfc", end))999return DATE_RFC2822;1000if (skip_prefix(format, "short", end))1001return DATE_SHORT;1002if (skip_prefix(format, "default", end))1003return DATE_NORMAL;1004if (skip_prefix(format, "human", end))1005return DATE_HUMAN;1006if (skip_prefix(format, "raw", end))1007return DATE_RAW;1008if (skip_prefix(format, "unix", end))1009return DATE_UNIX;1010if (skip_prefix(format, "format", end))1011return DATE_STRFTIME;1012/*1013* Please update $__git_log_date_formats in
1014* git-completion.bash when you add new formats.
1015*/
1016
1017die("unknown date format %s", format);1018}
1019
1020void parse_date_format(const char *format, struct date_mode *mode)1021{
1022const char *p;1023
1024/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */1025if (skip_prefix(format, "auto:", &p)) {1026if (isatty(1) || pager_in_use())1027format = p;1028else1029format = "default";1030}1031
1032/* historical alias */1033if (!strcmp(format, "local"))1034format = "default-local";1035
1036mode->type = parse_date_type(format, &p);1037mode->local = 0;1038
1039if (skip_prefix(p, "-local", &p))1040mode->local = 1;1041
1042if (mode->type == DATE_STRFTIME) {1043if (!skip_prefix(p, ":", &p))1044die("date format missing colon separator: %s", format);1045mode->strftime_fmt = xstrdup(p);1046} else if (*p)1047die("unknown date format %s", format);1048}
1049
1050void date_mode_release(struct date_mode *mode)1051{
1052free((char *)mode->strftime_fmt);1053}
1054
1055void datestamp(struct strbuf *out)1056{
1057time_t now;1058int offset;1059struct tm tm = { 0 };1060
1061time(&now);1062
1063offset = tm_to_time_t(localtime_r(&now, &tm)) - now;1064offset /= 60;1065
1066date_string(now, offset, out);1067}
1068
1069/*
1070* Relative time update (eg "2 days ago"). If we haven't set the time
1071* yet, we need to set it from current time.
1072*/
1073static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)1074{
1075time_t n;1076
1077if (tm->tm_mday < 0)1078tm->tm_mday = now->tm_mday;1079if (tm->tm_mon < 0)1080tm->tm_mon = now->tm_mon;1081if (tm->tm_year < 0) {1082tm->tm_year = now->tm_year;1083if (tm->tm_mon > now->tm_mon)1084tm->tm_year--;1085}1086
1087n = mktime(tm) - sec;1088localtime_r(&n, tm);1089return n;1090}
1091
1092/*
1093* Do we have a pending number at the end, or when
1094* we see a new one? Let's assume it's a month day,
1095* as in "Dec 6, 1992"
1096*/
1097static void pending_number(struct tm *tm, int *num)1098{
1099int number = *num;1100
1101if (number) {1102*num = 0;1103if (tm->tm_mday < 0 && number < 32)1104tm->tm_mday = number;1105else if (tm->tm_mon < 0 && number < 13)1106tm->tm_mon = number-1;1107else if (tm->tm_year < 0) {1108if (number > 1969 && number < 2100)1109tm->tm_year = number - 1900;1110else if (number > 69 && number < 100)1111tm->tm_year = number;1112else if (number < 38)1113tm->tm_year = 100 + number;1114/* We screw up for number = 00 ? */1115}1116}1117}
1118
1119static void date_now(struct tm *tm, struct tm *now, int *num)1120{
1121*num = 0;1122update_tm(tm, now, 0);1123}
1124
1125static void date_yesterday(struct tm *tm, struct tm *now, int *num)1126{
1127*num = 0;1128update_tm(tm, now, 24*60*60);1129}
1130
1131static void date_time(struct tm *tm, struct tm *now, int hour)1132{
1133if (tm->tm_hour < hour)1134update_tm(tm, now, 24*60*60);1135tm->tm_hour = hour;1136tm->tm_min = 0;1137tm->tm_sec = 0;1138}
1139
1140static void date_midnight(struct tm *tm, struct tm *now, int *num)1141{
1142pending_number(tm, num);1143date_time(tm, now, 0);1144}
1145
1146static void date_noon(struct tm *tm, struct tm *now, int *num)1147{
1148pending_number(tm, num);1149date_time(tm, now, 12);1150}
1151
1152static void date_tea(struct tm *tm, struct tm *now, int *num)1153{
1154pending_number(tm, num);1155date_time(tm, now, 17);1156}
1157
1158static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)1159{
1160int hour, n = *num;1161*num = 0;1162
1163hour = tm->tm_hour;1164if (n) {1165hour = n;1166tm->tm_min = 0;1167tm->tm_sec = 0;1168}1169tm->tm_hour = (hour % 12) + 12;1170}
1171
1172static void date_am(struct tm *tm, struct tm *now UNUSED, int *num)1173{
1174int hour, n = *num;1175*num = 0;1176
1177hour = tm->tm_hour;1178if (n) {1179hour = n;1180tm->tm_min = 0;1181tm->tm_sec = 0;1182}1183tm->tm_hour = (hour % 12);1184}
1185
1186static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)1187{
1188time_t n = 0;1189localtime_r(&n, tm);1190*num = 0;1191}
1192
1193static const struct special {1194const char *name;1195void (*fn)(struct tm *, struct tm *, int *);1196} special[] = {1197{ "yesterday", date_yesterday },1198{ "noon", date_noon },1199{ "midnight", date_midnight },1200{ "tea", date_tea },1201{ "PM", date_pm },1202{ "AM", date_am },1203{ "never", date_never },1204{ "now", date_now },1205{ NULL }1206};1207
1208static const char *number_name[] = {1209"zero", "one", "two", "three", "four",1210"five", "six", "seven", "eight", "nine", "ten",1211};1212
1213static const struct typelen {1214const char *type;1215int length;1216} typelen[] = {1217{ "seconds", 1 },1218{ "minutes", 60 },1219{ "hours", 60*60 },1220{ "days", 24*60*60 },1221{ "weeks", 7*24*60*60 },1222{ NULL }1223};1224
1225static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)1226{
1227const struct typelen *tl;1228const struct special *s;1229const char *end = date;1230int i;1231
1232while (isalpha(*++end))1233;1234
1235for (i = 0; i < 12; i++) {1236int match = match_string(date, month_names[i]);1237if (match >= 3) {1238tm->tm_mon = i;1239*touched = 1;1240return end;1241}1242}1243
1244for (s = special; s->name; s++) {1245int len = strlen(s->name);1246if (match_string(date, s->name) == len) {1247s->fn(tm, now, num);1248*touched = 1;1249return end;1250}1251}1252
1253if (!*num) {1254for (i = 1; i < 11; i++) {1255int len = strlen(number_name[i]);1256if (match_string(date, number_name[i]) == len) {1257*num = i;1258*touched = 1;1259return end;1260}1261}1262if (match_string(date, "last") == 4) {1263*num = 1;1264*touched = 1;1265}1266return end;1267}1268
1269tl = typelen;1270while (tl->type) {1271int len = strlen(tl->type);1272if (match_string(date, tl->type) >= len-1) {1273update_tm(tm, now, tl->length * *num);1274*num = 0;1275*touched = 1;1276return end;1277}1278tl++;1279}1280
1281for (i = 0; i < 7; i++) {1282int match = match_string(date, weekday_names[i]);1283if (match >= 3) {1284int diff, n = *num -1;1285*num = 0;1286
1287diff = tm->tm_wday - i;1288if (diff <= 0)1289n++;1290diff += 7*n;1291
1292update_tm(tm, now, diff * 24 * 60 * 60);1293*touched = 1;1294return end;1295}1296}1297
1298if (match_string(date, "months") >= 5) {1299int n;1300update_tm(tm, now, 0); /* fill in date fields if needed */1301n = tm->tm_mon - *num;1302*num = 0;1303while (n < 0) {1304n += 12;1305tm->tm_year--;1306}1307tm->tm_mon = n;1308*touched = 1;1309return end;1310}1311
1312if (match_string(date, "years") >= 4) {1313update_tm(tm, now, 0); /* fill in date fields if needed */1314tm->tm_year -= *num;1315*num = 0;1316*touched = 1;1317return end;1318}1319
1320return end;1321}
1322
1323static const char *approxidate_digit(const char *date, struct tm *tm, int *num,1324time_t now)1325{
1326char *end;1327timestamp_t number = parse_timestamp(date, &end, 10);1328
1329switch (*end) {1330case ':':1331case '.':1332case '/':1333case '-':1334if (isdigit(end[1])) {1335int match = match_multi_number(number, *end, date, end,1336tm, now);1337if (match)1338return date + match;1339}1340}1341
1342/* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */1343if (date[0] != '0' || end - date <= 2)1344*num = number;1345return end;1346}
1347
1348static timestamp_t approxidate_str(const char *date,1349const struct timeval *tv,1350int *error_ret)1351{
1352int number = 0;1353int touched = 0;1354struct tm tm, now;1355time_t time_sec;1356
1357time_sec = tv->tv_sec;1358localtime_r(&time_sec, &tm);1359now = tm;1360
1361tm.tm_year = -1;1362tm.tm_mon = -1;1363tm.tm_mday = -1;1364
1365for (;;) {1366unsigned char c = *date;1367if (!c)1368break;1369date++;1370if (isdigit(c)) {1371pending_number(&tm, &number);1372date = approxidate_digit(date-1, &tm, &number, time_sec);1373touched = 1;1374continue;1375}1376if (isalpha(c))1377date = approxidate_alpha(date-1, &tm, &now, &number, &touched);1378}1379pending_number(&tm, &number);1380if (!touched)1381*error_ret = 1;1382return (timestamp_t)update_tm(&tm, &now, 0);1383}
1384
1385timestamp_t approxidate_careful(const char *date, int *error_ret)1386{
1387struct timeval tv;1388timestamp_t timestamp;1389int offset;1390int dummy = 0;1391if (!error_ret)1392error_ret = &dummy;1393
1394if (!parse_date_basic(date, ×tamp, &offset)) {1395*error_ret = 0;1396return timestamp;1397}1398
1399get_time(&tv);1400return approxidate_str(date, &tv, error_ret);1401}
1402
1403int date_overflows(timestamp_t t)1404{
1405time_t sys;1406
1407/* If we overflowed our timestamp data type, that's bad... */1408if ((uintmax_t)t >= TIME_MAX)1409return 1;1410
1411/*1412* ...but we also are going to feed the result to system
1413* functions that expect time_t, which is often "signed long".
1414* Make sure that we fit into time_t, as well.
1415*/
1416sys = t;1417return t != sys || (t < 1) != (sys < 1);1418}
1419