util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()
@ 2022-09-06  7:04 Peter Ujfalusi
  2022-09-06  8:47 ` Karel Zak
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Ujfalusi @ 2022-09-06  7:04 UTC (permalink / raw)
  To: kzak; +Cc: util-linux, peter.ujfalusi

The %s comes handy with for example dmesg with the --since within scripts
to handle log rages.

Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
---
 lib/timeutils.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/lib/timeutils.c b/lib/timeutils.c
index 2e28ada8bc47..6dda2e8deefb 100644
--- a/lib/timeutils.c
+++ b/lib/timeutils.c
@@ -181,6 +181,7 @@ int parse_timestamp(const char *t, usec_t *usec)
 	 *
 	 *   2012-09-22 16:34:22
 	 *   2012-09-22T16:34:22
+	 *   1348331662		  (seconds since the Epoch (1970-01-01 00:00 UTC))
 	 *   2012-09-22 16:34	  (seconds will be set to 0)
 	 *   2012-09-22		  (time will be set to 00:00:00)
 	 *   16:34:22		  (date will be set to today)
@@ -325,6 +326,13 @@ int parse_timestamp(const char *t, usec_t *usec)
 		goto finish;
 	}
 
+	tm = copy;
+	k = strptime(t, "%s", &tm);
+	if (k && *k == 0) {
+		tm.tm_sec = 0;
+		goto finish;
+	}
+
 	return -EINVAL;
 
  finish:
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()
  2022-09-06  7:04 [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp() Peter Ujfalusi
@ 2022-09-06  8:47 ` Karel Zak
  2022-09-06 16:31   ` Péter Ujfalusi
  0 siblings, 1 reply; 6+ messages in thread
From: Karel Zak @ 2022-09-06  8:47 UTC (permalink / raw)
  To: Peter Ujfalusi; +Cc: util-linux

On Tue, Sep 06, 2022 at 10:04:36AM +0300, Peter Ujfalusi wrote:
>  lib/timeutils.c | 8 ++++++++
>  1 file changed, 8 insertions(+)

Applied, thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()
  2022-09-06  8:47 ` Karel Zak
@ 2022-09-06 16:31   ` Péter Ujfalusi
  2022-09-07  7:20     ` Karel Zak
  0 siblings, 1 reply; 6+ messages in thread
From: Péter Ujfalusi @ 2022-09-06 16:31 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux

Hi Karel,

On 06/09/2022 11:47, Karel Zak wrote:
> On Tue, Sep 06, 2022 at 10:04:36AM +0300, Peter Ujfalusi wrote:
>>  lib/timeutils.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
> 
> Applied, thanks.

After some tinkering with a script where I needed this, would it make
sense to change the way how one should be specifying seconds since the
Epoch time to
'@'<number>

This would match how for example systemd.time requires it [1]

Basically I (or anyone) can use dmesg instead of journalctl in a script
with the same --since=@<timestamp> parameter.

'date +%s' prints out just a number.

If not, then I have made a rookie mistake with the patch, which should
be fixed (discarding the seconds we got out from the timestamp).
/me hides

[1] https://www.man7.org/linux/man-pages/man7/systemd.time.7.html

-- 
Péter

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()
  2022-09-06 16:31   ` Péter Ujfalusi
@ 2022-09-07  7:20     ` Karel Zak
  2022-09-07 12:57       ` Dmitry V. Levin
  2022-09-08  9:29       ` Péter Ujfalusi
  0 siblings, 2 replies; 6+ messages in thread
From: Karel Zak @ 2022-09-07  7:20 UTC (permalink / raw)
  To: Péter Ujfalusi; +Cc: util-linux

On Tue, Sep 06, 2022 at 07:31:07PM +0300, Péter Ujfalusi wrote:
> On 06/09/2022 11:47, Karel Zak wrote:
> > On Tue, Sep 06, 2022 at 10:04:36AM +0300, Peter Ujfalusi wrote:
> >>  lib/timeutils.c | 8 ++++++++
> >>  1 file changed, 8 insertions(+)
> > 
> > Applied, thanks.
> 
> After some tinkering with a script where I needed this, would it make
> sense to change the way how one should be specifying seconds since the
> Epoch time to
> '@'<number>

Yes, '@' seems more readable. I have no strong opinion about it, maybe
we can support both convention (with and without @).

Anyway, we really need to add something like "INPUT TIMESTAMP" section
to the man page to describe supported formats.

> This would match how for example systemd.time requires it [1]
> 
> Basically I (or anyone) can use dmesg instead of journalctl in a script
> with the same --since=@<timestamp> parameter.
> 
> 'date +%s' prints out just a number.

It's possible to use --since=$(date +'@%s')

> If not, then I have made a rookie mistake with the patch, which should
> be fixed (discarding the seconds we got out from the timestamp).

Do you mean your copy & past tm.tm_sec = 0;? This should be fixed.

> /me hides

Come back and send a patch :-)

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()
  2022-09-07  7:20     ` Karel Zak
@ 2022-09-07 12:57       ` Dmitry V. Levin
  2022-09-08  9:29       ` Péter Ujfalusi
  1 sibling, 0 replies; 6+ messages in thread
From: Dmitry V. Levin @ 2022-09-07 12:57 UTC (permalink / raw)
  To: util-linux

On Wed, Sep 07, 2022 at 09:20:48AM +0200, Karel Zak wrote:
> On Tue, Sep 06, 2022 at 07:31:07PM +0300, Péter Ujfalusi wrote:
> > On 06/09/2022 11:47, Karel Zak wrote:
> > > On Tue, Sep 06, 2022 at 10:04:36AM +0300, Peter Ujfalusi wrote:
> > >>  lib/timeutils.c | 8 ++++++++
> > >>  1 file changed, 8 insertions(+)
> > > 
> > > Applied, thanks.
> > 
> > After some tinkering with a script where I needed this, would it make
> > sense to change the way how one should be specifying seconds since the
> > Epoch time to
> > '@'<number>
> 
> Yes, '@' seems more readable. I have no strong opinion about it, maybe
> we can support both convention (with and without @).

I suggest supporting just @seconds, this would also match the behaviour of
date(1) and the gnulib module it uses:

$ date -u -d @1348331662
Sat Sep 22 16:34:22 UTC 2012


-- 
ldv

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()
  2022-09-07  7:20     ` Karel Zak
  2022-09-07 12:57       ` Dmitry V. Levin
@ 2022-09-08  9:29       ` Péter Ujfalusi
  1 sibling, 0 replies; 6+ messages in thread
From: Péter Ujfalusi @ 2022-09-08  9:29 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux



On 07/09/2022 10:20, Karel Zak wrote:
> On Tue, Sep 06, 2022 at 07:31:07PM +0300, Péter Ujfalusi wrote:
>> On 06/09/2022 11:47, Karel Zak wrote:
>>> On Tue, Sep 06, 2022 at 10:04:36AM +0300, Peter Ujfalusi wrote:
>>>>  lib/timeutils.c | 8 ++++++++
>>>>  1 file changed, 8 insertions(+)
>>>
>>> Applied, thanks.
>>
>> After some tinkering with a script where I needed this, would it make
>> sense to change the way how one should be specifying seconds since the
>> Epoch time to
>> '@'<number>
> 
> Yes, '@' seems more readable. I have no strong opinion about it, maybe
> we can support both convention (with and without @).
> 
> Anyway, we really need to add something like "INPUT TIMESTAMP" section
> to the man page to describe supported formats.
> 
>> This would match how for example systemd.time requires it [1]
>>
>> Basically I (or anyone) can use dmesg instead of journalctl in a script
>> with the same --since=@<timestamp> parameter.
>>
>> 'date +%s' prints out just a number.
> 
> It's possible to use --since=$(date +'@%s')
> 
>> If not, then I have made a rookie mistake with the patch, which should
>> be fixed (discarding the seconds we got out from the timestamp).
> 
> Do you mean your copy & past tm.tm_sec = 0;? This should be fixed.
> 
>> /me hides
> 
> Come back and send a patch :-)

I have sent a fix patch which requires @ as a prefix and keeps the
seconds intact:
https://lore.kernel.org/util-linux/20220907054141.15608-1-peter.ujfalusi@linux.intel.com/T/#u

-- 
Péter

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-09-08  9:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-06  7:04 [PATCH] lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp() Peter Ujfalusi
2022-09-06  8:47 ` Karel Zak
2022-09-06 16:31   ` Péter Ujfalusi
2022-09-07  7:20     ` Karel Zak
2022-09-07 12:57       ` Dmitry V. Levin
2022-09-08  9:29       ` Péter Ujfalusi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).