From patchwork Thu Apr 23 13:52:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= X-Patchwork-Id: 11505721 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4CAA41893 for ; Thu, 23 Apr 2020 13:52:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 33EE82077D for ; Thu, 23 Apr 2020 13:52:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EIbB+VqU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728645AbgDWNwv (ORCPT ); Thu, 23 Apr 2020 09:52:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728506AbgDWNwu (ORCPT ); Thu, 23 Apr 2020 09:52:50 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9588C08E934 for ; Thu, 23 Apr 2020 06:52:50 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id y25so2973765pfn.5 for ; Thu, 23 Apr 2020 06:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=E6Pa10RZbqUp9fkgDKJ5iLiLAKb3oXXpq5PDrmLBUk0=; b=EIbB+VqUYHZJAfzBBP7fxYxbqwIFZbakL5tSbEnWEkWJBCrSAnlr3u//3yMJstB2z7 UOz7LXY7Aj/I750wxK80sPucKZr+AsO9r4844aepKP8QiaBrgqHvpeVxqk17m5hdfSBk qPwgXrrFdE80BTstj1gigSDBLxPkAr1aFV7vIw6jdtCGmVjv5E8JXqxtIyw6zWMUU+nc 7ZMGCaag2U8es5fg95ofjj6lHCBDDza43tvaZLZgCwFbUzWH/BHb6u90cpyQmUSMxMje SKpJtedg5AxKY4VpKr0WPXjKFZGuW3EG25IzVjQxsgJws78guJG6W0HfnGNV1mfDlY6v KmAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=E6Pa10RZbqUp9fkgDKJ5iLiLAKb3oXXpq5PDrmLBUk0=; b=kYpPuGJkxnI70Xcmcx9DqcgNZY/x1SFxV8KYLBvMsaPLwYTQfMM/uN9vhl5f4yzU2i EEJWUdH41F40vRdgV3SjvLDjq8XOHTBJaCGDiIPAJ5Nobv2xS4We+vlB5DB6HFxe4h8o SAy6qA7MBe2Pj5M1aa4kOUUT+PDmDUJnjPoI32GhZEtEVMZ5Llj74gbUuSQ7VgMP/Yl8 LvMP8CKlyPC38xe9mBFyR0WHpuTn5Zp46oZxqZc7UCyWNj6DJhos6mo/KLBKwbcghqu2 ueFf+G/4b080kgg3/SYVXyrW9/pfNCNtxI1nBhmVGLAD8rGiYnIL0FtFR0CYoi6IAB3Z x8qw== X-Gm-Message-State: AGi0PubWij1Q24a4E8wbLtS/XRwkSz9gyhG++0c59O9CTkCQKBSwFh0D wxVHNzcjFb0YkoPStHMpRvDM9R81 X-Google-Smtp-Source: APiQypKV51va6TZj2nfRrMi8xpiAoPksV4f1uMbCXElFfKSb71VzsNiJi0uK7HaYuZR1iTQdyRg5+Q== X-Received: by 2002:a63:4920:: with SMTP id w32mr4174950pga.119.1587649969953; Thu, 23 Apr 2020 06:52:49 -0700 (PDT) Received: from localhost.localdomain ([2402:800:6374:f359:1ce8:a621:5f80:1116]) by smtp.gmail.com with ESMTPSA id u15sm2430335pjm.47.2020.04.23.06.52.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Apr 2020 06:52:49 -0700 (PDT) From: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= Subject: [PATCH v4 1/4] date.c: s/is_date/set_date/ Date: Thu, 23 Apr 2020 20:52:38 +0700 Message-Id: <1fe69008fc79e6a74e8613011504bc7e342291ab.1587644889.git.congdanhqx@gmail.com> X-Mailer: git-send-email 2.26.2.384.g435bf60bd5 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The function is_date, confusingly also set tm_year. tm_mon, and tm_mday after validating input. Rename to set_date to reflect its real usage. Also, change return value is 0 on success and -1 on failure following our convention on function do some real work. Signed-off-by: Đoàn Trần Công Danh --- date.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/date.c b/date.c index b0d9a8421d..b67c5abe24 100644 --- a/date.c +++ b/date.c @@ -497,7 +497,7 @@ static int match_alpha(const char *date, struct tm *tm, int *offset) return skip_alpha(date); } -static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) +static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) { if (month > 0 && month < 13 && day > 0 && day < 32) { struct tm check = *tm; @@ -518,9 +518,9 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, else if (year < 38) r->tm_year = year + 100; else - return 0; + return -1; if (!now_tm) - return 1; + return 0; specified = tm_to_time_t(r); @@ -529,14 +529,14 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, * sure it is not later than ten days from now... */ if ((specified != -1) && (now + 10*24*3600 < specified)) - return 0; + return -1; tm->tm_mon = r->tm_mon; tm->tm_mday = r->tm_mday; if (year != -1) tm->tm_year = r->tm_year; - return 1; + return 0; } - return 0; + return -1; } static int match_multi_number(timestamp_t num, char c, const char *date, @@ -575,10 +575,10 @@ static int match_multi_number(timestamp_t num, char c, const char *date, if (num > 70) { /* yyyy-mm-dd? */ - if (is_date(num, num2, num3, NULL, now, tm)) + if (set_date(num, num2, num3, NULL, now, tm) == 0) break; /* yyyy-dd-mm? */ - if (is_date(num, num3, num2, NULL, now, tm)) + if (set_date(num, num3, num2, NULL, now, tm) == 0) break; } /* Our eastern European friends say dd.mm.yy[yy] @@ -586,14 +586,14 @@ static int match_multi_number(timestamp_t num, char c, const char *date, * mm/dd/yy[yy] form only when separator is not '.' */ if (c != '.' && - is_date(num3, num, num2, refuse_future, now, tm)) + set_date(num3, num, num2, refuse_future, now, tm) == 0) break; /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ - if (is_date(num3, num2, num, refuse_future, now, tm)) + if (set_date(num3, num2, num, refuse_future, now, tm) == 0) break; /* Funny European mm.dd.yy */ if (c == '.' && - is_date(num3, num, num2, refuse_future, now, tm)) + set_date(num3, num, num2, refuse_future, now, tm) == 0) break; return 0; } From patchwork Thu Apr 23 13:52:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= X-Patchwork-Id: 11505723 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 768CA159A for ; Thu, 23 Apr 2020 13:52:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5F17B2077D for ; Thu, 23 Apr 2020 13:52:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gjgYMFgX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728648AbgDWNwx (ORCPT ); Thu, 23 Apr 2020 09:52:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728506AbgDWNwx (ORCPT ); Thu, 23 Apr 2020 09:52:53 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C5F5C08E934 for ; Thu, 23 Apr 2020 06:52:52 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id a31so646598pje.1 for ; Thu, 23 Apr 2020 06:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rH6kH/tz9sHgN6M255BbLTntiUtcudBE99s4fQjBjsc=; b=gjgYMFgX2Vh8sSJ5ZV6E+xn7Kx2vyOfQBevpqbofUCyjfrg+9ScOeZJ2A13FCklcMT sKBDV0rksKqVWz9q74ml4Zei5x7DXjZYOQzsGfuKwKNmabtX9Wdu6KIpVpqzQMSvvrAk IvFx7c182p8a7M1W0rPwfl3Nb8jLFqa08vAK9IgIej8JAKk5JBbDXWfeuJeAWQ37SUaQ wy0rGwls1GmFyFBcev+gJl/YRA2WeFRCXt73ctuYuCIz46Zc5ZnKW2bW9g+7J73f2LMv JBp+AQQRvleghyzDOgQVDkDO7wyDfF91UsNMt/+ssWHCLhle2edpvXAurPotndIbnxQ6 pJ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rH6kH/tz9sHgN6M255BbLTntiUtcudBE99s4fQjBjsc=; b=hzvTNgMQjLVIqwxkDAOnamZ8YhxWOoHMK8vl0y2JQBKxiUbboyy+BAiDtyd20bMy0A D86sp8ByFuypuLgKUu3zm85/UDZ7D3rBqV7Rg8ik7Wu510ZsgQ9hM/Oljp0NWaCU6HX4 98+60j9v+dNMo1LHmH3zibzoVVCGT5IlMT6cKCQu2MeFfjf/CQGuljCGfO38mIqZh1d7 4oIiyYlzK4FU7sebng6JfI2ULcy6wpU/zyN9CGb2JzlRkKjzc7syVm7O+opMJxbh0Oci xsTwHYMoHZjVm96bIkk5NSyA+uATfylfgxvvMFDV+WzRn0gJDmXIRVz/+N5VEYyz1im+ dMIA== X-Gm-Message-State: AGi0Pub1i2i3PUCyGZ9CBPRuVRSt5Q7EL4ZnNZG0XY2c/WnuP+5O6kxR exmYtBsGBz6D0gsRqVSfhPlHvHgd X-Google-Smtp-Source: APiQypKTc8LWuIqccjgWQaGZuowmTz51jVpZJlkDJ0tzGM4NIwC1buox54K7IobsS7GG/x1w6rtfeQ== X-Received: by 2002:a17:902:ea8a:: with SMTP id x10mr4119347plb.166.1587649971774; Thu, 23 Apr 2020 06:52:51 -0700 (PDT) Received: from localhost.localdomain ([2402:800:6374:f359:1ce8:a621:5f80:1116]) by smtp.gmail.com with ESMTPSA id u15sm2430335pjm.47.2020.04.23.06.52.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Apr 2020 06:52:51 -0700 (PDT) From: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= Subject: [PATCH v4 2/4] date.c: validate and set time in a helper function Date: Thu, 23 Apr 2020 20:52:39 +0700 Message-Id: <0d0e4d8edce37dfef13e573588f0c043ddf07f6a.1587644889.git.congdanhqx@gmail.com> X-Mailer: git-send-email 2.26.2.384.g435bf60bd5 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In a later patch, we will reuse this logic, move it to a helper, now. While we're at it, explicit states that we intentionally ignore old-and-defective 2nd leap second. Signed-off-by: Đoàn Trần Công Danh --- date.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/date.c b/date.c index b67c5abe24..f5d5a91208 100644 --- a/date.c +++ b/date.c @@ -539,6 +539,22 @@ static int set_date(int year, int month, int day, struct tm *now_tm, time_t now, return -1; } +static int set_time(long hour, long minute, long second, struct tm *tm) +{ + /* C90 and old POSIX accepts 2 leap seconds, it's a defect, + * ignore second number 61 + */ + if (0 <= hour && hour <= 24 && + 0 <= minute && minute < 60 && + 0 <= second && second <= 60) { + tm->tm_hour = hour; + tm->tm_min = minute; + tm->tm_sec = second; + return 0; + } + return -1; +} + static int match_multi_number(timestamp_t num, char c, const char *date, char *end, struct tm *tm, time_t now) { @@ -556,12 +572,8 @@ static int match_multi_number(timestamp_t num, char c, const char *date, case ':': if (num3 < 0) num3 = 0; - if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { - tm->tm_hour = num; - tm->tm_min = num2; - tm->tm_sec = num3; + if (set_time(num, num2, num3, tm) == 0) break; - } return 0; case '-': From patchwork Thu Apr 23 13:52:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= X-Patchwork-Id: 11505727 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A8B4C13B2 for ; Thu, 23 Apr 2020 13:52:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FEDB20728 for ; Thu, 23 Apr 2020 13:52:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KwxPnVuY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728652AbgDWNwz (ORCPT ); Thu, 23 Apr 2020 09:52:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728506AbgDWNwy (ORCPT ); Thu, 23 Apr 2020 09:52:54 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8676CC08E934 for ; Thu, 23 Apr 2020 06:52:54 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id a7so2533293pju.2 for ; Thu, 23 Apr 2020 06:52:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xqDp34wYEx7XQtXTcUgUTfsBDYojN9icFny+gUg4q0Y=; b=KwxPnVuYg18ZqFESD00stV9fDLh+uNijaJOAJlNNCn1IgiTVXR6/3ofrO/pAn1ir1Z p2uAJ7IohzSq2rsPD/tS4wiA3GVi1EYiyV+JvQr50t5UOcKDSVlku5I3cWQ9DX2K2/5/ w6mvnVK/cEc69h/HXdBOzfHJALzWfbzksQdNmjKDwcArUV+7xw3bVNcLmza+SY8Yltfm 50j8tU8RhHVjRRlka36Jv1f15GgjK1Fk0iXbcurbmf6ovrmI6Uswhp2ROSnQ3x/9Hpn0 n+HSl6seOLCi1T3a4J9TRdEU3nPlBRYuiYqUuC/VUCIwvXmFQa7HNA1xnOiYZldiH6+4 973w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xqDp34wYEx7XQtXTcUgUTfsBDYojN9icFny+gUg4q0Y=; b=gnKn7aoTLRnicQzWvg2MVT30PrplfskVetMR0VAO/T4xswrD30Wn48743PE+dXk3to oDeq9ucrIznY35ajKO7WRzBsr47mXZaPml+pBgBIfGkQADnH9NYj8/N4IOtcnqo8fhIW 6FYa9wM8/sOirst1Q8iyTS0K1car/xiVKuv47ZXXQPmSHuZs8IRTmAIoC+K9P1/0PSBC XzzniqUFqE0qgCv3JHKSovIJd1Vr1hsrHrzvckgqP/Mr2Ae3qU5/PhrEVtpbVsK+prKW Rir0GOovJX4QbJkayMV1mxybl0V40irxaBAVBVlqiKkHlJ1nKcamThqNOHKc6w75lR+u pyuQ== X-Gm-Message-State: AGi0PuYhALduDISkbILuR5D6h6Gm8oH240pkUwHL2kiqupn3E902+hqU VPlW8abROzmoNbQUbdrqwPv2eA2h X-Google-Smtp-Source: APiQypJgfvLNpgxR4Bx571FXaG/TKhohkfntGC7xmFBXCwQ80wqScE1FcLs5ofZ+QEeTe8OQvzhmXw== X-Received: by 2002:a17:902:8543:: with SMTP id d3mr3743479plo.314.1587649973654; Thu, 23 Apr 2020 06:52:53 -0700 (PDT) Received: from localhost.localdomain ([2402:800:6374:f359:1ce8:a621:5f80:1116]) by smtp.gmail.com with ESMTPSA id u15sm2430335pjm.47.2020.04.23.06.52.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Apr 2020 06:52:53 -0700 (PDT) From: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= , "Brian M . Carlson" Subject: [PATCH v4 3/4] date.c: skip fractional second part of ISO-8601 Date: Thu, 23 Apr 2020 20:52:40 +0700 Message-Id: <8b18d0ee5d6f08394e54e16ca7618c687791a509.1587644889.git.congdanhqx@gmail.com> X-Mailer: git-send-email 2.26.2.384.g435bf60bd5 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org git-commit(1) says ISO-8601 is one of our supported date format. ISO-8601 allows timestamps to have a fractional number of seconds. We represent time only in terms of whole seconds, so we never bothered parsing fractional seconds. However, it's better for us to parse and throw away the fractional part than to refuse to parse the timestamp at all. And refusing parsing fractional second part may confuse the parse to think fractional and timezone as day and month in this example: 2008-02-14 20:30:45.019-04:00 While doing this, make sure that we only interpret the number after the second and the dot as fractional when and only when the date is known, since only ISO-8601 allows the fractional part, and we've taught our users to interpret "12:34:56.7.days.ago" as a way to specify a time relative to current time. Reported-by: Brian M. Carlson Signed-off-by: Đoàn Trần Công Danh --- Documentation/date-formats.txt | 5 ++++- date.c | 8 +++++++- t/t0006-date.sh | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Documentation/date-formats.txt b/Documentation/date-formats.txt index 6926e0a4c8..7e7eaba643 100644 --- a/Documentation/date-formats.txt +++ b/Documentation/date-formats.txt @@ -20,7 +20,10 @@ RFC 2822:: ISO 8601:: Time and date specified by the ISO 8601 standard, for example `2005-04-07T22:13:13`. The parser accepts a space instead of the - `T` character as well. + `T` character as well. Fractional parts of a second will be ignored, + for example `2005-04-07T22:13:13.019` will be treated as + `2005-04-07T22:13:13` + + NOTE: In addition, the date part is accepted in the following formats: `YYYY.MM.DD`, `MM/DD/YYYY` and `DD.MM.YYYY`. diff --git a/date.c b/date.c index f5d5a91208..5d635031e0 100644 --- a/date.c +++ b/date.c @@ -570,8 +570,14 @@ static int match_multi_number(timestamp_t num, char c, const char *date, /* Time? Date? */ switch (c) { case ':': - if (num3 < 0) + if (num3 < 0) { num3 = 0; + } else if (*end == '.' && isdigit(end[1]) && + tm->tm_year != -1 && tm->tm_mon != -1 && tm->tm_mday != -1 && + set_time(num, num2, num3, tm) == 0) { + /* %Y%m%d is known, ignore fractional in HHMMSS. */ + strtol(end + 1, &end, 10); + } if (set_time(num, num2, num3, tm) == 0) break; return 0; diff --git a/t/t0006-date.sh b/t/t0006-date.sh index d9fcc829a9..80917c81c3 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -81,6 +81,8 @@ check_parse 2008-02 bad check_parse 2008-02-14 bad check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000' check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500' +check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500' +check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400' check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015' check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000' check_parse '2008-02-14 20:30:45 -5:' '2008-02-14 20:30:45 +0000' @@ -103,6 +105,7 @@ check_approxidate 5.seconds.ago '2009-08-30 19:19:55' check_approxidate 10.minutes.ago '2009-08-30 19:10:00' check_approxidate yesterday '2009-08-29 19:20:00' check_approxidate 3.days.ago '2009-08-27 19:20:00' +check_approxidate '12:34:56.3.days.ago' '2009-08-27 12:34:56' check_approxidate 3.weeks.ago '2009-08-09 19:20:00' check_approxidate 3.months.ago '2009-05-30 19:20:00' check_approxidate 2.years.3.months.ago '2007-05-30 19:20:00' From patchwork Thu Apr 23 13:52:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= X-Patchwork-Id: 11505729 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E476D159A for ; Thu, 23 Apr 2020 13:52:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CCEF92077D for ; Thu, 23 Apr 2020 13:52:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KW5wQ6JM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728655AbgDWNw5 (ORCPT ); Thu, 23 Apr 2020 09:52:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1728653AbgDWNw5 (ORCPT ); Thu, 23 Apr 2020 09:52:57 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D387C08E934 for ; Thu, 23 Apr 2020 06:52:56 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id o15so2932898pgi.1 for ; Thu, 23 Apr 2020 06:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=empvCX3u642Njh8JgYcw/Sl4jNFokXmJsZFZd0Nzdmk=; b=KW5wQ6JM/k16VEIbID47pA1EX8ioTbhRzo3VH+JtLgal2C53o8J/Hp0jomv5FuKZSY p0YdEYnEMulzucHjhk7VOMeSinzri9BLMq86YKFevIn2fHuiA97Ch9I0lSWHe4QukSlz h2pf2/uVwX2gtyGCaf+csfoHPMOH1sJH/XHk8+DBxorKRrxeO71jykYqd4/eE4PSh8rT ne5YKb0rVXgXqPI6lLncqgE6TdPGwi42iYQDi3+5HJccKo4vZ1tN0GeWFBEhD8BrsPEt +C02yFT7l9ElNfqSnReFWA/hNHxJ9Ig5RePNBmUP2PJUYd0e+eNDGbeShLuiU2l9Gcvu FApA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=empvCX3u642Njh8JgYcw/Sl4jNFokXmJsZFZd0Nzdmk=; b=h7f9r5AgXoFaDVxIjgBRp9aarrqLVXkkSE2c+x3Ab71RowClWSRySxIlXZ3O+m6wml ycqi37UVPJKtbCpuu6w17YJJOmsCuQtDY8293qHssBW1fWTIK9PRzivr1kdz0HRyl8JY meqy7Sk2v6nxEd9wSeu75rC68DVRDQlk3P85b0X2in2jQjs75Y8aAhDS1xWZYCSICNgg j0PYCB80erWgrcznLmvIBtAgf4Ey0eGEl3p+iojSHwdKeHP1VHAru1Tp7p7hd+x+YW7w I38CrqNRxe3MQ+9lFgThP/UUq1iQQAiAQiGvwqMWVAud5fnMWCvA+P7bRJdy0HDCRbYb ADTw== X-Gm-Message-State: AGi0PuZhJcJzzBCGJiFuELyHhD5ew0+PbftG9V7TJTNFPde5cdYv6nrc RGxF0eo408eiQzTeWeEDtGxyNFrR X-Google-Smtp-Source: APiQypJqFM/h1RmmrQMNKet4AXgHns36t/R8ljxw+mQ1vhJMYzWQyDtv15q4QmoyPuYTXuDUj+/AeQ== X-Received: by 2002:a63:8d43:: with SMTP id z64mr3932027pgd.77.1587649975502; Thu, 23 Apr 2020 06:52:55 -0700 (PDT) Received: from localhost.localdomain ([2402:800:6374:f359:1ce8:a621:5f80:1116]) by smtp.gmail.com with ESMTPSA id u15sm2430335pjm.47.2020.04.23.06.52.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Apr 2020 06:52:54 -0700 (PDT) From: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= Subject: [PATCH v4 4/4] date.c: allow compact version of ISO-8601 datetime Date: Thu, 23 Apr 2020 20:52:41 +0700 Message-Id: <2812439a26c3c98150bb7be3c6e603e2ed95fab3.1587644889.git.congdanhqx@gmail.com> X-Mailer: git-send-email 2.26.2.384.g435bf60bd5 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Đoàn Trần Công Danh --- date.c | 14 ++++++++++++++ t/t0006-date.sh | 3 +++ 2 files changed, 17 insertions(+) diff --git a/date.c b/date.c index 5d635031e0..0c66e5a5ae 100644 --- a/date.c +++ b/date.c @@ -682,6 +682,20 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt n++; } while (isdigit(date[n])); + /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */ + /* 6 digits, compact style of ISO-8601's time: HHMMSS */ + if (n == 8 || n == 6) { + unsigned int num1 = num / 10000; + unsigned int num2 = (num % 10000) / 100; + unsigned int num3 = num % 100; + if (n == 8) + set_date(num1, num2, num3, NULL, time(NULL), tm); + else if (n == 6 && set_time(num1, num2, num3, tm) == 0 && + *end == '.' && isdigit(end[1])) + strtoul(end + 1, &end, 10); + return end - date; + } + /* Four-digit year or a timezone? */ if (n == 4) { if (num <= 1400 && *offset == -1) { diff --git a/t/t0006-date.sh b/t/t0006-date.sh index 80917c81c3..75ee9a96b8 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -82,6 +82,9 @@ check_parse 2008-02-14 bad check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000' check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500' check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500' +check_parse '20080214T203045-04:00' '2008-02-14 20:30:45 -0400' +check_parse '20080214T203045 -04:00' '2008-02-14 20:30:45 -0400' +check_parse '20080214T203045.019-04:00' '2008-02-14 20:30:45 -0400' check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400' check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015' check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'