On 06/10/16 12:53, Pádraig Brady wrote:
> On 06/10/16 11:11, john woods wrote:
>> date +%YW%V gives week and year; it would be nice if date +%YQ%q could
>> return, eg 2016Q3 as having a quarters code in date would save AWKwardness
>> or shell arithmetic.
>>
>> Perhaps we could have
>> %q 1,2,3,4
>> %Q 1st quarter; 2nd quarter; 3rd quarter; 4th quarter
>>
>> Regards ...
> 
> This is one of those marginal calls.
> 
> It's not that awkward to get the quarter number: $(( ($(date +%-m)-1)/3+1 ))
> But I agree it would be useful to give the number directly.
> 
> Note you may want to raise your proposal with the glibc team also in their
> strftime(3) interface; coreutils is hesitant to add new % modifiers in
> date(1) without prior practice from C libraries.
> 
> Note ruby uses %Q to give the number of milliseconds since epoch.
> So we might skip that?
> BTW 1 -> 1st etc. functionality might be useful to add to numfmt(1).
> 
> Note perl Date::Format uses %q to give the quarter number, starting with 1
> 
> I'd be 60:40 for adding it.

Propose patch attached,
which we can add after the corresponding gnulib patch lands.

>From ff8c54eae9578f64b1bac70289087191bbad2382 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Wed, 19 Oct 2016 14:57:57 -0700
Subject: [PATCH] date: add %q to output the quarter of the year

* doc/coreutils.texi (date invocation): Document %q.
* src/date.c (usage): Likewise.
* tests/misc/date.pl: Add a test case.
* NEWS: Mention the new feature.
---
 NEWS               | 2 ++
 doc/coreutils.texi | 2 ++
 src/date.c         | 1 +
 tests/misc/date.pl | 5 +++++
 4 files changed, 10 insertions(+)

diff --git a/NEWS b/NEWS
index 9548de8..8f5ce06 100644
--- a/NEWS
+++ b/NEWS
@@ -90,6 +90,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   date now accepts the --debug option, to annotate the parsed date string,
   display timezone information, and warn about potential misuse.
 
+  date now accepts the %q format to output the quarter of the year.
+
 
 * Noteworthy changes in release 8.25 (2016-01-20) [stable]
 
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 95419b4..f1019d9 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -15196,6 +15196,8 @@ same as @samp{%b}
 day of year (@samp{001}@dots{}@samp{366})
 @item %m
 month (@samp{01}@dots{}@samp{12})
+@item %q
+quarter of year (@samp{1}@dots{}@samp{4})
 @item %u
 day of week (@samp{1}@dots{}@samp{7}) with @samp{1} corresponding to Monday
 @item %U
diff --git a/src/date.c b/src/date.c
index 9598fc6..ddb011e 100644
--- a/src/date.c
+++ b/src/date.c
@@ -214,6 +214,7 @@ FORMAT controls the output.  Interpreted sequences are:\n\
   %N   nanoseconds (000000000..999999999)\n\
   %p   locale's equivalent of either AM or PM; blank if not known\n\
   %P   like %p, but lower case\n\
+  %q   quarter of year (1..4)\n\
   %r   locale's 12-hour clock time (e.g., 11:11:04 PM)\n\
   %R   24-hour hour and minute; same as %H:%M\n\
   %s   seconds since 1970-01-01 00:00:00 UTC\n\
diff --git a/tests/misc/date.pl b/tests/misc/date.pl
index 4449a48..b53ca81 100755
--- a/tests/misc/date.pl
+++ b/tests/misc/date.pl
@@ -86,6 +86,11 @@ my @Tests =
      ['W92-2', "--date '1992-1-5' +%W", {OUT=>"00"}],
      ['W92-3', "--date '1992-1-6' +%W", {OUT=>"01"}],
 
+     ['q-1', "--date '1000-1-1' +%q", {OUT=>"1"}],
+     ['q-2', "--date '1000-4-1' +%q", {OUT=>"2"}],
+     ['q-3', "--date '1000-7-1' +%q", {OUT=>"3"}],
+     ['q-4', "--date '1000-10-1' +%q", {OUT=>"4"}],
+
      ['millen-1', "--date '1998-1-1 3 years' +%Y", {OUT=>"2001"}],
 
      ['rel-0', "-d '$d1 now' '+%Y-%m-%d %T'", {OUT=>"$d0 $t0"}],
-- 
2.5.5

Reply via email to