Proposal: %T Prompt parameter for psql for current time (like Oracle has)

Started by Kirk Wolakalmost 3 years ago7 messages
#1Kirk Wolak
wolakk@gmail.com
1 attachment(s)

Attachments:

v2-0001-Time-option-added-to-psql-prompt.patchtext/plain; charset=US-ASCII; name=v2-0001-Time-option-added-to-psql-prompt.patchDownload
From b9db157177bbdeeeb6d35c3623ca9355141419d7 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Wed, 1 Mar 2023 00:07:55 +0100
Subject: [PATCH v2] Time option added to psql prompt

This adds a useful time option to the prompt: %T. Which does not
require a wasteful backquoted shell command which is also not
compatible between operating systems.
The format is simply HH24:MI:SS no other options available by design!

Author: Kirk Wolak <wolakk@gmail.com>
Reviewed-By: Andrey Borodin <amborodin@acm.org>
Reviewed-By: Nikolay Samokhvalov <samokhvalov@gmail.com>
Thread: https://postgr.es/m/CACLU5mSRwHr_8z%3DenMj-nXF1tmC7%2BJn5heZQNiKuLyxYUtL2fg%40mail.gmail.com
---
 doc/src/sgml/ref/psql-ref.sgml |  9 +++++++++
 src/bin/psql/prompt.c          | 10 +++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index dc6528dc11..04ab9eeb8c 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -4575,6 +4575,15 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
         </listitem>
       </varlistentry>
 
+      <varlistentry id="app-psql-prompting-t-uc">
+        <term><literal>%T</literal></term>
+        <listitem>
+         <para>
+          The current time on the client in HH24:MI:SS format.
+         </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-prompting-x">
         <term><literal>%x</literal></term>
         <listitem>
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index 969cd9908e..0c0c725df5 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -41,6 +41,7 @@
  *			or a ! if session is not connected to a database;
  *		in prompt2 -, *, ', or ";
  *		in prompt3 nothing
+ * %T - time in HH24:MI:SS format
  * %x - transaction status: empty, *, !, ? (unknown or no connection)
  * %l - The line number inside the current statement, starting from 1.
  * %? - the error code of the last query (not yet implemented)
@@ -223,7 +224,14 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
 							break;
 					}
 					break;
-
+					/* output HH24:MI:SS */
+				case 'T':
+					{
+						time_t current_time = time(NULL);
+						struct tm *tm_info = localtime(&current_time);
+						sprintf(buf, "%02d:%02d:%02d", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
+					}
+					break;
 				case 'x':
 					if (!pset.db)
 						buf[0] = '?';
-- 
2.25.1

#2Jim Jones
jim.jones@uni-muenster.de
In reply to: Kirk Wolak (#1)
Re: Proposal: %T Prompt parameter for psql for current time (like Oracle has)

On 01.03.23 01:59, Kirk Wolak wrote:

I cannot get the last email to show up for the commitfest.
This is version 2 of the original patch. [1]
Thanks Jim!

[1]/messages/by-id/CACLU5mSRwHr_8z=enMj-nXF1tmC7+Jn5heZQNiKuLyxYUtL2fg@mail.gmail.com

Regards Kirk.

The patch didn't pass the SanityCheck:

https://cirrus-ci.com/task/5445242183221248?logs=build#L1337

missing a header perhaps?

#include "time.h"

Best, Jim

#3Kirk Wolak
wolakk@gmail.com
In reply to: Jim Jones (#2)
1 attachment(s)
Re: Proposal: %T Prompt parameter for psql for current time (like Oracle has)

On Wed, Mar 1, 2023 at 4:41 AM Jim Jones <jim.jones@uni-muenster.de> wrote:

On 01.03.23 01:59, Kirk Wolak wrote:

I cannot get the last email to show up for the commitfest.
This is version 2 of the original patch. [1]
Thanks Jim!

[1]

/messages/by-id/CACLU5mSRwHr_8z=enMj-nXF1tmC7+Jn5heZQNiKuLyxYUtL2fg@mail.gmail.com

Regards Kirk.

The patch didn't pass the SanityCheck:

https://cirrus-ci.com/task/5445242183221248?logs=build#L1337

missing a header perhaps?

#include "time.h"

Best, Jim

Thanks, corrected, and confirmed Unix line endings.
FWIW, the simplest way to test it is with this command (I usually get it
wrong on the first guess)

\set PROMPT1 %T ' ' :PROMPT1

Kirk

Attachments:

v3-0001-Time-option-added-to-psql-prompt.patchtext/plain; charset=US-ASCII; name=v3-0001-Time-option-added-to-psql-prompt.patchDownload
From bfafeaec64d01a404fe36f26ec4355607776e66b Mon Sep 17 00:00:00 2001
From: Kirk Wolak <wolakk@gmail.com>
Date: Wed, 1 Mar 2023 16:02:10 +0000
Subject: [PATCH] [PATCH v3] Time option added to psql prompt

This adds a useful time option to the prompt: %T. Which does not
require a wasteful backquoted shell command which is also not
compatible between operating systems.
The format is simply HH24:MI:SS no other options available by design!

Author: Kirk Wolak <wolakk@gmail.com>
Reviewed-By: Andrey Borodin <amborodin@acm.org>
Reviewed-By: Nikolay Samokhvalov <samokhvalov@gmail.com>
Thread: https://postgr.es/m/CACLU5mSRwHr_8z%3DenMj-nXF1tmC7%2BJn5heZQNiKuLyxYUtL2fg%40mail.gmail.com
---
 doc/src/sgml/ref/psql-ref.sgml |  9 +++++++++
 src/bin/psql/prompt.c          | 11 ++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index dc6528dc11d..04ab9eeb8c0 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -4575,6 +4575,15 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
         </listitem>
       </varlistentry>
 
+      <varlistentry id="app-psql-prompting-t-uc">
+        <term><literal>%T</literal></term>
+        <listitem>
+         <para>
+          The current time on the client in HH24:MI:SS format.
+         </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry id="app-psql-prompting-x">
         <term><literal>%x</literal></term>
         <listitem>
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index 969cd9908e5..24dffcd461c 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -18,6 +18,7 @@
 #include "libpq/pqcomm.h"
 #include "prompt.h"
 #include "settings.h"
+#include "time.h"
 
 /*--------------------------
  * get_prompt
@@ -41,6 +42,7 @@
  *			or a ! if session is not connected to a database;
  *		in prompt2 -, *, ', or ";
  *		in prompt3 nothing
+ * %T - time in HH24:MI:SS format
  * %x - transaction status: empty, *, !, ? (unknown or no connection)
  * %l - The line number inside the current statement, starting from 1.
  * %? - the error code of the last query (not yet implemented)
@@ -223,7 +225,14 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
 							break;
 					}
 					break;
-
+					/* output HH24:MI:SS */
+				case 'T':
+					{
+						time_t current_time = time(NULL);
+						struct tm *tm_info = localtime(&current_time);
+						sprintf(buf, "%02d:%02d:%02d", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
+					}
+					break;
 				case 'x':
 					if (!pset.db)
 						buf[0] = '?';
-- 
GitLab

#4Jim Jones
jim.jones@uni-muenster.de
In reply to: Kirk Wolak (#3)
Re: Proposal: %T Prompt parameter for psql for current time (like Oracle has)

On 01.03.23 17:13, Kirk Wolak wrote:

Thanks, corrected, and confirmed Unix line endings.
FWIW, the simplest way to test it is with this command (I usually get
it wrong on the first guess)

\set PROMPT1 %T ' ' :PROMPT1

Kirk

Nice. The patch applies clean and the cfbots seem much happier now - all
passed.

17:23:19 postgres=# SELECT now();
              now
-------------------------------
 2023-03-01 17:23:19.807339+01
(1 row)

The docs render also just fine. I'm now wondering if HH24:MI:SS should
be formatted with, e.g. using <literal>

"The current time on the client in <literal>HH24:MI:SS</literal> format."

But that I'll leave to the docs experts to judge :)

Best, Jim

#5Kirk Wolak
wolakk@gmail.com
In reply to: Jim Jones (#4)
Re: Proposal: %T Prompt parameter for psql for current time (like Oracle has)

On Wed, Mar 1, 2023 at 11:55 AM Jim Jones <jim.jones@uni-muenster.de> wrote:

On 01.03.23 17:13, Kirk Wolak wrote:

Thanks, corrected, and confirmed Unix line endings.
FWIW, the simplest way to test it is with this command (I usually get it
wrong on the first guess)

\set PROMPT1 %T ' ' :PROMPT1

Kirk

Nice. The patch applies clean and the cfbots seem much happier now - all
passed.

17:23:19 postgres=# SELECT now();
now
-------------------------------
2023-03-01 17:23:19.807339+01
(1 row)

The docs render also just fine. I'm now wondering if HH24:MI:SS should be
formatted with, e.g. using <literal>

"The current time on the client in <literal>HH24:MI:SS</literal> format."

But that I'll leave to the docs experts to judge :)

Best, Jim

Thanks Jim.

I hope one of the Docs experts chime in. It's easy enough to fix. Just
not sure if it's required.
What a great learning experience!

#6Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Kirk Wolak (#3)
Re: Proposal: %T Prompt parameter for psql for current time (like Oracle has)

On Wed, 2023-03-01 at 11:13 -0500, Kirk Wolak wrote:

Thanks, corrected, and confirmed Unix line endings.

The patch builds fine and works as intended.

I leave it to the committers to decide whether the patch is worth the
effort or not, given that you can get a similar effect with %`date`.
It adds some value by being simpler and uniform across all platforms.

I'll mark the patch as "ready for committer".

Yours,
Laurenz Albe

#7Kirk Wolak
wolakk@gmail.com
In reply to: Laurenz Albe (#6)
Re: Proposal: %T Prompt parameter for psql for current time (like Oracle has)

On Thu, Mar 2, 2023 at 9:56 AM Laurenz Albe <laurenz.albe@cybertec.at>
wrote:

On Wed, 2023-03-01 at 11:13 -0500, Kirk Wolak wrote:

Thanks, corrected, and confirmed Unix line endings.

The patch builds fine and works as intended.

I leave it to the committers to decide whether the patch is worth the
effort or not, given that you can get a similar effect with %`date`.
It adds some value by being simpler and uniform across all platforms.

I'll mark the patch as "ready for committer".

Yours,
Laurenz Albe

Thanks Laurenz.

To be clear, I use windows AND linux, and I share my file between them.

in linux: `date +%H:%M:%S` is used
in windows: `ECHO %time%`

so, I wrote a ts.cmd and ts.sh so I could share one prompt: `ts`
but now every time I connect a new account to this file, I have to go
find/copy my ts file.
Same when I share it with other developers.

This was the pain that started the quest.
Thanks to everyone for their support!