From patchwork Mon Jan 21 05:31:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stephen P. Smith" X-Patchwork-Id: 10772607 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9DB361580 for ; Mon, 21 Jan 2019 07:13:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8551929F0D for ; Mon, 21 Jan 2019 07:13:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 79E5129F0E; Mon, 21 Jan 2019 07:13:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.6 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5FF7429F0F for ; Mon, 21 Jan 2019 07:13:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728126AbfAUHNM (ORCPT ); Mon, 21 Jan 2019 02:13:12 -0500 Received: from fed1rmfepi108.cox.net ([68.230.241.139]:53128 "EHLO fed1rmfepi108.cox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728074AbfAUHNL (ORCPT ); Mon, 21 Jan 2019 02:13:11 -0500 Received: from fed1rmimpo109.cox.net ([68.230.241.158]) by fed1rmfepo201.cox.net (InterMail vM.8.01.05.28 201-2260-151-171-20160122) with ESMTP id <20190121053113.YCBW4108.fed1rmfepo201.cox.net@fed1rmimpo109.cox.net> for ; Mon, 21 Jan 2019 00:31:13 -0500 Received: from thunderbird.smith.home (localhost [127.0.0.1]) by thunderbird.smith.home (Postfix) with ESMTP id B7A90B82301; Sun, 20 Jan 2019 22:31:12 -0700 (MST) X-CT-Class: Clean X-CT-Score: 0.00 X-CT-RefID: str=0001.0A090207.5C455921.0023,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 X-CT-Spam: 0 X-Authority-Analysis: v=2.3 cv=OtL7NB3t c=1 sm=1 tr=0 a=BlDZPKRk22kUaIvSBqmi8w==:117 a=BlDZPKRk22kUaIvSBqmi8w==:17 a=3JhidrIBZZsA:10 a=WDhBSedXqNQA:10 a=Z4Rwk6OoAAAA:8 a=kviXuzpPAAAA:8 a=4XSfByAG1AGq6XVSogIA:9 a=HkZW87K1Qel5hWWM3VKY:22 a=qrIFiuKZe2vaD64auk6j:22 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (LOGIN) smtp.auth=ischis2@cox.net From: "Stephen P. Smith" To: git@vger.kernel.org Cc: Linus Torvalds , Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= , Jeff King , Philip Oakley , Johannes Sixt Subject: [PATCH v3 1/5] Add 'human' date format Date: Sun, 20 Jan 2019 22:31:08 -0700 Message-Id: <20190121053112.4736-2-ischis2@cox.net> X-Mailer: git-send-email 2.20.1.2.gb21ebb671b In-Reply-To: <20190121053112.4736-1-ischis2@cox.net> References: <20190118061805.19086-1-ischis2@cox.net> <20190121053112.4736-1-ischis2@cox.net> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Linus Torvalds This adds --date=human, which skips the timezone if it matches the current time-zone, and doesn't print the whole date if that matches (ie skip printing year for dates that are "this year", but also skip the whole date itself if it's in the last few days and we can just say what weekday it was). For really recent dates (same day), use the relative date stamp, while for old dates (year doesn't match), don't bother with time and timezone. Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do git config --add log.date auto and your "git log" commands will show the human-legible format unless you're scripting things. Note that this time format still shows the timezone for recent enough events (but not so recent that they show up as relative dates). You can combine it with the "-local" suffix to never show timezones for an even more simplified view. Signed-off-by: Linus Torvalds Signed-off-by: Stephen P. Smith --- builtin/blame.c | 4 ++ cache.h | 1 + date.c | 130 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 115 insertions(+), 20 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 6d798f9939..f684e31d82 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix) */ blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */ break; + case DATE_HUMAN: + /* If the year is shown, no time is shown */ + blame_date_width = sizeof("Thu Oct 19 16:00"); + break; case DATE_NORMAL: blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700"); break; diff --git a/cache.h b/cache.h index 49713cc5a5..34c33e6a28 100644 --- a/cache.h +++ b/cache.h @@ -1439,6 +1439,7 @@ extern struct object *peel_to_type(const char *name, int namelen, enum date_mode_type { DATE_NORMAL = 0, + DATE_HUMAN, DATE_RELATIVE, DATE_SHORT, DATE_ISO8601, diff --git a/date.c b/date.c index 9bc15df6f9..a8d50eb206 100644 --- a/date.c +++ b/date.c @@ -77,22 +77,16 @@ static struct tm *time_to_tm_local(timestamp_t time) } /* - * What value of "tz" was in effect back then at "time" in the - * local timezone? + * Fill in the localtime 'struct tm' for the supplied time, + * and return the local tz. */ -static int local_tzoffset(timestamp_t time) +static int local_time_tzoffset(time_t t, struct tm *tm) { - time_t t, t_local; - struct tm tm; + time_t t_local; int offset, eastwest; - if (date_overflows(time)) - die("Timestamp too large for this system: %"PRItime, time); - - t = (time_t)time; - localtime_r(&t, &tm); - t_local = tm_to_time_t(&tm); - + localtime_r(&t, tm); + t_local = tm_to_time_t(tm); if (t_local == -1) return 0; /* error; just use +0000 */ if (t_local < t) { @@ -107,6 +101,20 @@ static int local_tzoffset(timestamp_t time) return offset * eastwest; } +/* + * What value of "tz" was in effect back then at "time" in the + * local timezone? + */ +static int local_tzoffset(timestamp_t time) +{ + struct tm tm; + + if (date_overflows(time)) + die("Timestamp too large for this system: %"PRItime, time); + + return local_time_tzoffset((time_t)time, &tm); +} + void show_date_relative(timestamp_t time, int tz, const struct timeval *now, struct strbuf *timebuf) @@ -191,9 +199,80 @@ struct date_mode *date_mode_from_type(enum date_mode_type type) return &mode; } +static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local) +{ + struct { + unsigned int year:1, + date:1, + wday:1, + time:1, + seconds:1, + tz:1; + } hide = { 0 }; + + hide.tz = local || tz == human_tz; + hide.year = tm->tm_year == human_tm->tm_year; + if (hide.year) { + if (tm->tm_mon == human_tm->tm_mon) { + if (tm->tm_mday > human_tm->tm_mday) { + /* Future date: think timezones */ + } else if (tm->tm_mday == human_tm->tm_mday) { + hide.date = hide.wday = 1; + } else if (tm->tm_mday + 5 > human_tm->tm_mday) { + /* Leave just weekday if it was a few days ago */ + hide.date = 1; + } + } + } + + /* Show "today" times as just relative times */ + if (hide.wday) { + struct timeval now; + gettimeofday(&now, NULL); + show_date_relative(time, tz, &now, buf); + return; + } + + /* + * Always hide seconds for human-readable. + * Hide timezone if showing date. + * Hide weekday and time if showing year. + * + * The logic here is two-fold: + * (a) only show details when recent enough to matter + * (b) keep the maximum length "similar", and in check + */ + if (human_tm->tm_year) { + hide.seconds = 1; + hide.tz |= !hide.date; + hide.wday = hide.time = !hide.year; + } + + if (!hide.wday) + strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]); + if (!hide.date) + strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday); + + /* Do we want AM/PM depending on locale? */ + if (!hide.time) { + strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min); + if (!hide.seconds) + strbuf_addf(buf, ":%02d", tm->tm_sec); + } else + strbuf_rtrim(buf); + + if (!hide.year) + strbuf_addf(buf, " %d", tm->tm_year + 1900); + + if (!hide.tz) + strbuf_addf(buf, " %+05d", tz); +} + const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) { struct tm *tm; + struct tm human_tm = { 0 }; + int human_tz = -1; static struct strbuf timebuf = STRBUF_INIT; if (mode->type == DATE_UNIX) { @@ -202,6 +281,15 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) return timebuf.buf; } + if (mode->type == DATE_HUMAN) { + struct timeval now; + + gettimeofday(&now, NULL); + + /* Fill in the data for "current time" in human_tz and human_tm */ + human_tz = local_time_tzoffset(now.tv_sec, &human_tm); + } + if (mode->local) tz = local_tzoffset(time); @@ -258,14 +346,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz, !mode->local); else - strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d", - weekday_names[tm->tm_wday], - month_names[tm->tm_mon], - tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, - tm->tm_year + 1900, - mode->local ? 0 : ' ', - tz); + show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local); return timebuf.buf; } @@ -802,6 +883,11 @@ int parse_date(const char *date, struct strbuf *result) return 0; } +static int auto_date_style(void) +{ + return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL; +} + static enum date_mode_type parse_date_type(const char *format, const char **end) { if (skip_prefix(format, "relative", end)) @@ -819,6 +905,10 @@ static enum date_mode_type parse_date_type(const char *format, const char **end) return DATE_SHORT; if (skip_prefix(format, "default", end)) return DATE_NORMAL; + if (skip_prefix(format, "human", end)) + return DATE_HUMAN; + if (skip_prefix(format, "auto", end)) + return auto_date_style(); if (skip_prefix(format, "raw", end)) return DATE_RAW; if (skip_prefix(format, "unix", end)) From patchwork Mon Jan 21 05:31:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stephen P. Smith" X-Patchwork-Id: 10772609 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 71F151390 for ; Mon, 21 Jan 2019 07:13:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5E3B929F0D for ; Mon, 21 Jan 2019 07:13:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5286329F0F; Mon, 21 Jan 2019 07:13:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.6 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F41AC29F0D for ; Mon, 21 Jan 2019 07:13:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728129AbfAUHNP (ORCPT ); Mon, 21 Jan 2019 02:13:15 -0500 Received: from fed1rmfepi102.cox.net ([68.230.241.133]:43375 "EHLO fed1rmfepi102.cox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728061AbfAUHNL (ORCPT ); Mon, 21 Jan 2019 02:13:11 -0500 Received: from fed1rmimpo109.cox.net ([68.230.241.158]) by fed1rmfepo101.cox.net (InterMail vM.8.01.05.28 201-2260-151-171-20160122) with ESMTP id <20190121053113.ETQJ4064.fed1rmfepo101.cox.net@fed1rmimpo109.cox.net> for ; Mon, 21 Jan 2019 00:31:13 -0500 Received: from thunderbird.smith.home (localhost [127.0.0.1]) by thunderbird.smith.home (Postfix) with ESMTP id CD2FBB82303; Sun, 20 Jan 2019 22:31:12 -0700 (MST) X-CT-Class: Clean X-CT-Score: 0.00 X-CT-RefID: str=0001.0A090208.5C455921.0020,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 X-CT-Spam: 0 X-Authority-Analysis: v=2.3 cv=OtL7NB3t c=1 sm=1 tr=0 a=BlDZPKRk22kUaIvSBqmi8w==:117 a=BlDZPKRk22kUaIvSBqmi8w==:17 a=3JhidrIBZZsA:10 a=WDhBSedXqNQA:10 a=kviXuzpPAAAA:8 a=PzJFpfYIsgoufMCHn0sA:9 a=qrIFiuKZe2vaD64auk6j:22 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (LOGIN) smtp.auth=ischis2@cox.net From: "Stephen P. Smith" To: git@vger.kernel.org Cc: Linus Torvalds , Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= , Jeff King , Philip Oakley , Johannes Sixt Subject: [PATCH v3 2/5] Replace the proposed 'auto' mode with 'auto:' Date: Sun, 20 Jan 2019 22:31:09 -0700 Message-Id: <20190121053112.4736-3-ischis2@cox.net> X-Mailer: git-send-email 2.20.1.2.gb21ebb671b In-Reply-To: <20190121053112.4736-1-ischis2@cox.net> References: <20190118061805.19086-1-ischis2@cox.net> <20190121053112.4736-1-ischis2@cox.net> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface. Added the ability to specify mode 'foo' if the pager is being used by using auto:foo syntax. Therefore, 'auto:human' date mode defaults to human if we're using the pager. So you can do git config --add log.date auto:human and your "git log" commands will show the human-legible format unless you're scripting things. Signed-off-by: Stephen P. Smith --- date.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/date.c b/date.c index a8d50eb206..43c3a84e25 100644 --- a/date.c +++ b/date.c @@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result) return 0; } -static int auto_date_style(void) -{ - return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL; -} - static enum date_mode_type parse_date_type(const char *format, const char **end) { if (skip_prefix(format, "relative", end)) @@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end) return DATE_NORMAL; if (skip_prefix(format, "human", end)) return DATE_HUMAN; - if (skip_prefix(format, "auto", end)) - return auto_date_style(); if (skip_prefix(format, "raw", end)) return DATE_RAW; if (skip_prefix(format, "unix", end)) @@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode) { const char *p; + /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */ + if (skip_prefix(format, "auto:", &p)) { + if (isatty(1) || pager_in_use()) + format = p; + else + format = "default"; + } + /* historical alias */ if (!strcmp(format, "local")) format = "default-local"; From patchwork Mon Jan 21 05:31:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stephen P. Smith" X-Patchwork-Id: 10772621 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EDFF713B4 for ; Mon, 21 Jan 2019 07:13:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DA58329F0F for ; Mon, 21 Jan 2019 07:13:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE12C29F14; Mon, 21 Jan 2019 07:13:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 88F7F29F0F for ; Mon, 21 Jan 2019 07:13:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728162AbfAUHNY (ORCPT ); Mon, 21 Jan 2019 02:13:24 -0500 Received: from fed1rmfepi102.cox.net ([68.230.241.133]:43375 "EHLO fed1rmfepi102.cox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727545AbfAUHNV (ORCPT ); Mon, 21 Jan 2019 02:13:21 -0500 Received: from fed1rmimpo209.cox.net ([68.230.241.160]) by fed1rmfepo202.cox.net (InterMail vM.8.01.05.28 201-2260-151-171-20160122) with ESMTP id <20190121053113.UKWK4175.fed1rmfepo202.cox.net@fed1rmimpo209.cox.net> for ; Mon, 21 Jan 2019 00:31:13 -0500 Received: from thunderbird.smith.home (localhost [127.0.0.1]) by thunderbird.smith.home (Postfix) with ESMTP id E5CE0B8230E; Sun, 20 Jan 2019 22:31:12 -0700 (MST) X-CT-Class: Clean X-CT-Score: 0.00 X-CT-RefID: str=0001.0A09020F.5C455921.0032,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 X-CT-Spam: 0 X-Authority-Analysis: v=2.3 cv=Iouqj43g c=1 sm=1 tr=0 a=BlDZPKRk22kUaIvSBqmi8w==:117 a=BlDZPKRk22kUaIvSBqmi8w==:17 a=3JhidrIBZZsA:10 a=WDhBSedXqNQA:10 a=kviXuzpPAAAA:8 a=ZRg8tKC3IdvpwZkzal4A:9 a=qrIFiuKZe2vaD64auk6j:22 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (LOGIN) smtp.auth=ischis2@cox.net From: "Stephen P. Smith" To: git@vger.kernel.org Cc: Linus Torvalds , Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= , Jeff King , Philip Oakley , Johannes Sixt Subject: [PATCH v3 3/5] Add 'human' date format documentation Date: Sun, 20 Jan 2019 22:31:10 -0700 Message-Id: <20190121053112.4736-4-ischis2@cox.net> X-Mailer: git-send-email 2.20.1.2.gb21ebb671b In-Reply-To: <20190121053112.4736-1-ischis2@cox.net> References: <20190118061805.19086-1-ischis2@cox.net> <20190121053112.4736-1-ischis2@cox.net> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Display date and time information in a format similar to how people write dates in other contexts. If the year isn't specified then, the reader infers the date is given is in the current year. By not displaying the redundant information, the reader concentrates on the information that is different. The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed. While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human. If the 'relative' date format wasn't already implemented then using 'relative' would have been appropriate. Signed-off-by: Stephen P. Smith --- Documentation/git-log.txt | 4 ++++ Documentation/rev-list-options.txt | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Documentation/git-log.txt b/Documentation/git-log.txt index 90761f1694..b02e922dc3 100644 --- a/Documentation/git-log.txt +++ b/Documentation/git-log.txt @@ -192,6 +192,10 @@ log.date:: Default format for human-readable dates. (Compare the `--date` option.) Defaults to "default", which means to write dates like `Sat May 8 19:35:34 2010 -0500`. ++ +If the format is set to "auto:foo" and the pager is in use, format +"foo" will be the used for the date format. Otherwise "default" will +be used. log.follow:: If `true`, `git log` will act as if the `--follow` option was used when diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt index 98b538bc77..867a063a1c 100644 --- a/Documentation/rev-list-options.txt +++ b/Documentation/rev-list-options.txt @@ -831,6 +831,13 @@ Note that the `-local` option does not affect the seconds-since-epoch value (which is always measured in UTC), but does switch the accompanying timezone value. + +`--date=human` shows the timezone if the timezone does not match the +current time-zone, and doesn't print the whole date if that matches +(ie skip printing year for dates that are "this year", but also skip +the whole date itself if it's in the last few days and we can just say +what weekday it was). For older dates the hour and minute is also +omitted. ++ `--date=unix` shows the date as a Unix epoch timestamp (seconds since 1970). As with `--raw`, this is always in UTC and therefore `-local` has no effect. From patchwork Mon Jan 21 05:31:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stephen P. Smith" X-Patchwork-Id: 10772623 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3671B13B4 for ; Mon, 21 Jan 2019 07:13:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2332628497 for ; Mon, 21 Jan 2019 07:13:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 177A928509; Mon, 21 Jan 2019 07:13:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9FC8528497 for ; Mon, 21 Jan 2019 07:13:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728164AbfAUHNu (ORCPT ); Mon, 21 Jan 2019 02:13:50 -0500 Received: from fed1rmfepi108.cox.net ([68.230.241.139]:53128 "EHLO fed1rmfepi108.cox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728111AbfAUHNN (ORCPT ); Mon, 21 Jan 2019 02:13:13 -0500 Received: from fed1rmimpo305.cox.net ([68.230.241.173]) by fed1rmfepo101.cox.net (InterMail vM.8.01.05.28 201-2260-151-171-20160122) with ESMTP id <20190121053113.ETQQ4064.fed1rmfepo101.cox.net@fed1rmimpo305.cox.net> for ; Mon, 21 Jan 2019 00:31:13 -0500 Received: from thunderbird.smith.home (localhost [127.0.0.1]) by thunderbird.smith.home (Postfix) with ESMTP id 07C96B8230F; Sun, 20 Jan 2019 22:31:13 -0700 (MST) X-CT-Class: Clean X-CT-Score: 0.00 X-CT-RefID: str=0001.0A090208.5C455921.002B,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 X-CT-Spam: 0 X-Authority-Analysis: v=2.3 cv=St8kF8G0 c=1 sm=1 tr=0 a=BlDZPKRk22kUaIvSBqmi8w==:117 a=BlDZPKRk22kUaIvSBqmi8w==:17 a=3JhidrIBZZsA:10 a=kviXuzpPAAAA:8 a=p8WoR9vAhiq33K7Opl0A:9 a=qrIFiuKZe2vaD64auk6j:22 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (LOGIN) smtp.auth=ischis2@cox.net From: "Stephen P. Smith" To: git@vger.kernel.org Cc: Linus Torvalds , Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= , Jeff King , Philip Oakley , Johannes Sixt Subject: [PATCH v3 4/5] Add `human` format to test-tool Date: Sun, 20 Jan 2019 22:31:11 -0700 Message-Id: <20190121053112.4736-5-ischis2@cox.net> X-Mailer: git-send-email 2.20.1.2.gb21ebb671b In-Reply-To: <20190121053112.4736-1-ischis2@cox.net> References: <20190118061805.19086-1-ischis2@cox.net> <20190121053112.4736-1-ischis2@cox.net> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the human format support to the test tool so that TEST_DATE_NOW can be used to specify the current time. The get_time() helper function was created and and checks the TEST_DATE_NOW environment variable. If TEST_DATE_NOW is set, then that date is used instead of the date returned by by gettimeofday(). All calls to gettimeofday() were replaced by calls to get_time(). Signed-off-by: Stephen P. Smith --- cache.h | 2 ++ date.c | 21 +++++++++++++++++---- t/helper/test-date.c | 11 +++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cache.h b/cache.h index 34c33e6a28..fe00ddf910 100644 --- a/cache.h +++ b/cache.h @@ -1467,6 +1467,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type); const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode); void show_date_relative(timestamp_t time, int tz, const struct timeval *now, struct strbuf *timebuf); +void show_date_human(timestamp_t time, int tz, const struct timeval *now, + struct strbuf *timebuf); int parse_date(const char *date, struct strbuf *out); int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset); int parse_expiry_date(const char *date, timestamp_t *timestamp); diff --git a/date.c b/date.c index 43c3a84e25..5f9ea7d5d7 100644 --- a/date.c +++ b/date.c @@ -115,6 +115,19 @@ static int local_tzoffset(timestamp_t time) return local_time_tzoffset((time_t)time, &tm); } +static void get_time(struct timeval *now) +{ + const char *x; + + x = getenv("TEST_DATE_NOW"); + if (x) { + now->tv_sec = atoi(x); + now->tv_usec = 0; + } + else + gettimeofday(now, NULL); +} + void show_date_relative(timestamp_t time, int tz, const struct timeval *now, struct strbuf *timebuf) @@ -228,7 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm /* Show "today" times as just relative times */ if (hide.wday) { struct timeval now; - gettimeofday(&now, NULL); + get_time(&now); show_date_relative(time, tz, &now, buf); return; } @@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) if (mode->type == DATE_HUMAN) { struct timeval now; - gettimeofday(&now, NULL); + get_time(&now); /* Fill in the data for "current time" in human_tz and human_tm */ human_tz = local_time_tzoffset(now.tv_sec, &human_tm); @@ -303,7 +316,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) struct timeval now; strbuf_reset(&timebuf); - gettimeofday(&now, NULL); + get_time(&now); show_date_relative(time, tz, &now, &timebuf); return timebuf.buf; } @@ -1296,7 +1309,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret) return timestamp; } - gettimeofday(&tv, NULL); + get_time(&tv); return approxidate_str(date, &tv, error_ret); } diff --git a/t/helper/test-date.c b/t/helper/test-date.c index a0837371ab..811b116c7e 100644 --- a/t/helper/test-date.c +++ b/t/helper/test-date.c @@ -3,6 +3,7 @@ static const char *usage_msg = "\n" " test-tool date relative [time_t]...\n" +" test-tool date human [time_t]...\n" " test-tool date show: [time_t]...\n" " test-tool date parse [date]...\n" " test-tool date approxidate [date]...\n" @@ -22,6 +23,14 @@ static void show_relative_dates(const char **argv, struct timeval *now) strbuf_release(&buf); } +static void show_human_dates(const char **argv) +{ + for (; *argv; argv++) { + time_t t = atoi(*argv); + printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN))); + } +} + static void show_dates(const char **argv, const char *format) { struct date_mode mode; @@ -100,6 +109,8 @@ int cmd__date(int argc, const char **argv) usage(usage_msg); if (!strcmp(*argv, "relative")) show_relative_dates(argv+1, &now); + else if (!strcmp(*argv, "human")) + show_human_dates(argv+1); else if (skip_prefix(*argv, "show:", &x)) show_dates(argv+1, x); else if (!strcmp(*argv, "parse")) From patchwork Mon Jan 21 05:31:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stephen P. Smith" X-Patchwork-Id: 10772611 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 170231390 for ; Mon, 21 Jan 2019 07:13:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0468729F0D for ; Mon, 21 Jan 2019 07:13:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED02D29F0F; Mon, 21 Jan 2019 07:13:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.6 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9A99629F0D for ; Mon, 21 Jan 2019 07:13:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728128AbfAUHNP (ORCPT ); Mon, 21 Jan 2019 02:13:15 -0500 Received: from fed1rmfepi102.cox.net ([68.230.241.133]:43375 "EHLO fed1rmfepi102.cox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728097AbfAUHNM (ORCPT ); Mon, 21 Jan 2019 02:13:12 -0500 Received: from fed1rmimpo109.cox.net ([68.230.241.158]) by fed1rmfepo102.cox.net (InterMail vM.8.01.05.28 201-2260-151-171-20160122) with ESMTP id <20190121053114.SHCX12708.fed1rmfepo102.cox.net@fed1rmimpo109.cox.net> for ; Mon, 21 Jan 2019 00:31:14 -0500 Received: from thunderbird.smith.home (localhost [127.0.0.1]) by thunderbird.smith.home (Postfix) with ESMTP id 25557B82310; Sun, 20 Jan 2019 22:31:13 -0700 (MST) X-CT-Class: Clean X-CT-Score: 0.00 X-CT-RefID: str=0001.0A090207.5C455922.000B,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 X-CT-Spam: 0 X-Authority-Analysis: v=2.3 cv=OtL7NB3t c=1 sm=1 tr=0 a=BlDZPKRk22kUaIvSBqmi8w==:117 a=BlDZPKRk22kUaIvSBqmi8w==:17 a=3JhidrIBZZsA:10 a=WDhBSedXqNQA:10 a=kviXuzpPAAAA:8 a=qrhz1JM_Fw-vKaZgdOYA:9 a=qrIFiuKZe2vaD64auk6j:22 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (LOGIN) smtp.auth=ischis2@cox.net From: "Stephen P. Smith" To: git@vger.kernel.org Cc: Linus Torvalds , Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= , Jeff King , Philip Oakley , Johannes Sixt Subject: [PATCH v3 5/5] Add `human` date format tests. Date: Sun, 20 Jan 2019 22:31:12 -0700 Message-Id: <20190121053112.4736-6-ischis2@cox.net> X-Mailer: git-send-email 2.20.1.2.gb21ebb671b In-Reply-To: <20190121053112.4736-1-ischis2@cox.net> References: <20190118061805.19086-1-ischis2@cox.net> <20190121053112.4736-1-ischis2@cox.net> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When using `human` several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed. Use TEST_DATE_NOW environment variable when using the test-tool to hold the expected output strings constant. Signed-off-by: Stephen P. Smith --- t/t0006-date.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/t/t0006-date.sh b/t/t0006-date.sh index ffb2975e48..35f7b52dd7 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -128,4 +128,22 @@ check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00' check_approxidate '2008-12-01' '2008-12-01 19:20:00' check_approxidate '2009-12-01' '2009-12-01 19:20:00' +check_date_format_human() { + t=$(($TEST_DATE_NOW - $1)) + echo "$t -> $2" >expect + test_expect_success "human date $t" ' + test-tool date human $t >actual && + test_cmp expect actual +' +} + +check_date_format_human 18000 "5 hours ago" # 5 hours ago +check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago +check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago +check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago +check_date_format_human 31449600 "Aug 31 2008" # 12 months ago +check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago +check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago +check_date_format_human 630000000 "Sep 13 1989" # 20 years ago + test_done