Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp717240ybz; Wed, 15 Apr 2020 17:24:04 -0700 (PDT) X-Google-Smtp-Source: APiQypK4TYLy+1/b5LGYOAKUe2WkKzD4drLplACwcCCfYyat2KHIoijKfH+IgX9Jfg7vdYRtpU78 X-Received: by 2002:a50:d0d5:: with SMTP id g21mr16768985edf.92.1586996644162; Wed, 15 Apr 2020 17:24:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586996644; cv=none; d=google.com; s=arc-20160816; b=HTuymHGs8dr/xB5KDI0iafWTEbcq3ErK86CmmCriZ92/2LoWpejn0C2Hug7u7trrpc KBRvvarQpxPSVY4OFptE/nyNqd6jsdyg8u71Th+Ue/ZQA+KN/hLyqB5A1EzGIcuK9USc JmEe2htZYLVaMxHrkh46K+f71QPKkqmk6doT3tg1fwZhM4con44Q2I4b86cpt6rxhyqx sHqHJOSQ2jJmkPTiZP3aHTZtA6aSkKFPxA/Y+q5Mpyu0owQKNmp+VObrvGMnjW75ESyZ HnEPxASiBPcxqpDv7uJzSpW3zSOKD1goUVJ9jDmecfdofozOxLddSlCsBCPnPqz6jo7C MaKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=m+RTfDCKYzXCCNoscY5mh1ipnL/O6FW6hoVVTwV8lSs=; b=M5lHFMbH7Tyf7Oy0+K9BFNxGPUUZWicvm7ZXo4r2tRbhXOi6CyDT1iKEwAVp03lBNJ HIErjEn9WfmCg5ZnLrS7faeaJ/zRyupQ4DbaipkVUYNHZ7U4YKDmO4THeNW6s0LhngAf 58zUk3OfLUV9wrVN9s3QiGrzaFYXO2k+h3udp/O73P4VhQ4cfwf44Itjdr2zxk/FGLMa 3URDjnpZ4sZtXqaHZ2+6xvtzLMAyiJx43APE+OYsPdLTKi67hk2ckeCcvrwi1MnfKF/X Nyo1xLdXuvoLpxKe6ymuqDOArvEv5YebjmduiNtYkYrxcjg9ggFbWKF3ZOs0+LDFsY0r 0mXQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bi17si11956053edb.370.2020.04.15.17.23.40; Wed, 15 Apr 2020 17:24:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2410619AbgDORBY (ORCPT + 99 others); Wed, 15 Apr 2020 13:01:24 -0400 Received: from mga14.intel.com ([192.55.52.115]:22236 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404670AbgDORBS (ORCPT ); Wed, 15 Apr 2020 13:01:18 -0400 IronPort-SDR: L+R0NlKsfGVcTrep24C7Mvx4DSdB81kv5p57GJpvYZWjJp2exyZg8hCxeDCy5oOTuvw/sFvsHV zCjqQEMtTWbg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Apr 2020 10:01:16 -0700 IronPort-SDR: Uocg5YvN76heP6eHdO9njZeTPxjFiWVi5/EKTn7lxuT1CjK1I6x/tOtd1F9dE1vBilk62k9mPs 20WhJ5fICpEg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,387,1580803200"; d="scan'208";a="332554878" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga001.jf.intel.com with ESMTP; 15 Apr 2020 10:01:11 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 2C30F190; Wed, 15 Apr 2020 20:01:08 +0300 (EEST) From: Andy Shevchenko To: Petr Mladek , Steven Rostedt , Sergey Senozhatsky , linux-kernel@vger.kernel.org, Rasmus Villemoes , Andrew Morton Cc: Andy Shevchenko , Hans Verkuil , Jonathan Corbet , Alexandre Belloni Subject: [PATCH v3 1/3] lib/vsprintf: Print time64_t in human readable format Date: Wed, 15 Apr 2020 20:00:44 +0300 Message-Id: <20200415170046.33374-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200415170046.33374-1-andriy.shevchenko@linux.intel.com> References: <20200415170046.33374-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are users which print time and date represented by content of time64_t type in human readable format. Instead of open coding that each time introduce %ptT[dt][r] specifier. Few test cases for %ptT specifier has been added as well. Cc: Hans Verkuil Cc: Jonathan Corbet Signed-off-by: Andy Shevchenko Reviewed-by: Alexandre Belloni --- Documentation/core-api/printk-formats.rst | 22 ++++++++-------- lib/test_printf.c | 13 +++++++--- lib/vsprintf.c | 31 +++++++++++++++++++++-- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst index 8ebe46b1af39..a407cdd09083 100644 --- a/Documentation/core-api/printk-formats.rst +++ b/Documentation/core-api/printk-formats.rst @@ -468,21 +468,23 @@ Examples (OF):: %pfwf /ocp@68000000/i2c@48072000/camera@10/port/endpoint - Full name %pfwP endpoint - Node name -Time and date (struct rtc_time) -------------------------------- +Time and date +------------- :: - %ptR YYYY-mm-ddTHH:MM:SS - %ptRd YYYY-mm-dd - %ptRt HH:MM:SS - %ptR[dt][r] + %pt[RT] YYYY-mm-ddTHH:MM:SS + %pt[RT]d YYYY-mm-dd + %pt[RT]t HH:MM:SS + %pt[RT][dt][r] -For printing date and time as represented by struct rtc_time structure in -human readable format. +For printing date and time as represented by + R struct rtc_time structure + T time64_t type +in human readable format. -By default year will be incremented by 1900 and month by 1. Use %ptRr (raw) -to suppress this behaviour. +By default year will be incremented by 1900 and month by 1. +Use %pt[RT]r (raw) to suppress this behaviour. Passed by reference. diff --git a/lib/test_printf.c b/lib/test_printf.c index 2d9f520d2f27..6dc0a6c33b8c 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -478,7 +478,7 @@ struct_va_format(void) } static void __init -struct_rtc_time(void) +time_and_date(void) { /* 1543210543 */ const struct rtc_time tm = { @@ -489,14 +489,21 @@ struct_rtc_time(void) .tm_mon = 10, .tm_year = 118, }; + /* 2019-01-04T15:32:23 */ + time64_t t = 1546615943; - test("(%ptR?)", "%pt", &tm); + test("(%pt?)", "%pt", &tm); test("2018-11-26T05:35:43", "%ptR", &tm); test("0118-10-26T05:35:43", "%ptRr", &tm); test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm); test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm); test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm); test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm); + + test("2019-01-04T15:32:23", "%ptT", &t); + test("0119-00-04T15:32:23", "%ptTr", &t); + test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t); + test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t); } static void __init @@ -661,7 +668,7 @@ test_pointer(void) uuid(); dentry(); struct_va_format(); - struct_rtc_time(); + time_and_date(); struct_clk(); bitmap(); netdev_features(); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 7c488a1ce318..adbcca9c9cba 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -1819,6 +1820,29 @@ char *rtc_str(char *buf, char *end, const struct rtc_time *tm, return buf; } +static noinline_for_stack +char *time64_str(char *buf, char *end, const time64_t time, + struct printf_spec spec, const char *fmt) +{ + struct rtc_time rtc_time; + struct tm tm; + + time64_to_tm(time, 0, &tm); + + rtc_time.tm_sec = tm.tm_sec; + rtc_time.tm_min = tm.tm_min; + rtc_time.tm_hour = tm.tm_hour; + rtc_time.tm_mday = tm.tm_mday; + rtc_time.tm_mon = tm.tm_mon; + rtc_time.tm_year = tm.tm_year; + rtc_time.tm_wday = tm.tm_wday; + rtc_time.tm_yday = tm.tm_yday; + + rtc_time.tm_isdst = 0; + + return rtc_str(buf, end, &rtc_time, spec, fmt); +} + static noinline_for_stack char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, const char *fmt) @@ -1826,8 +1850,10 @@ char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, switch (fmt[1]) { case 'R': return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); + case 'T': + return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); default: - return error_string(buf, end, "(%ptR?)", spec); + return error_string(buf, end, "(%pt?)", spec); } } @@ -2143,8 +2169,9 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, * - 'd[234]' For a dentry name (optionally 2-4 last components) * - 'D[234]' Same as 'd' but for a struct file * - 'g' For block_device name (gendisk + partition number) - * - 't[R][dt][r]' For time and date as represented: + * - 't[RT][dt][r]' For time and date as represented by: * R struct rtc_time + * T time64_t * - 'C' For a clock, it prints the name (Common Clock Framework) or address * (legacy clock framework) of the clock * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address -- 2.25.1