Skip to contentSkip to conversation

lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()

PATCH Applied Peter Ujfalusi 3 participants 6 messages Sep 6, 2022 – Sep 8, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/timeutils: Add %s (seconds since the Epoch) to parse_timestamp()

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

---
 lib/timeutils.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/timeutils.c +8 -0
index 2e28ada8bc47..6dda2e8deefb 100644
@@ -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:
Signature · 1 line
2.37.3
maintainer Applied
#1 lore raw
Quoted 2 lines · Peter Ujfalusi
 lib/timeutils.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw

Hi Karel,

Quoted 5 lines · Karel Zak
Quoted 2 lines · Peter Ujfalusi
 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

Signature · 1 line
Péter
maintainer
#3 lore raw
Quoted 11 lines · Péter Ujfalusi
Quoted 5 lines · Karel Zak
Quoted 2 lines · Peter Ujfalusi
 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.

Quoted 6 lines

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')

Quoted 2 lines

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.

Quoted 1 line

/me hides

Come back and send a patch :-)

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#4 lore raw
Quoted 8 lines · Karel Zak
Quoted 7 lines · Péter Ujfalusi
Quoted 5 lines · Karel Zak
Quoted 2 lines · Peter Ujfalusi
 lib/timeutils.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Applied, thanks.

Quoted 7 lines
Quoted 4 lines

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
Signature · 1 line
ldv
#5 lore raw
In reply to Karel Zak
Quoted 28 lines · Karel Zak
Quoted 11 lines · Péter Ujfalusi
Quoted 5 lines · Karel Zak
Quoted 2 lines · Peter Ujfalusi
 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.

Quoted 6 lines

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')

Quoted 8 lines
Quoted 2 lines

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.

Quoted 1 line

/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/[email protected]/T/#u

Signature · 1 line
Péter