Skip to contentSkip to conversation

lib/timeutils: Require '@' prefix for seconds since the Epoch timestamp

PATCH Applied Peter Ujfalusi 2 participants 4 messages Sep 7, 2022 – Sep 13, 2022

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

PATCH 1/1 lib/timeutils: Require '@' prefix for seconds since the Epoch timestamp

Since the seconds since the Epoch is just a number it might be better to require special prefix to indicate the intention that the user wants to interpret the number as seconds since the Epoch.

Use the same '@' character as prefix as used by systemd.time to make it easier to integrate in scripts intended to be used on systems with or without systemd.

Fix also the initial support which discarded the seconds from the converted timestamp.

---
Hi Karel,

I think this will be safer for the enduser. The original patch would interpret _any_ number not matching with the other supported formats interpreted as seconds since the Epoch, which might not something the user had in mind.

Sorry for the oversight on my side...

Kind regards,
Péter

 lib/timeutils.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
lib/timeutils.c +6 -8
index 6dda2e8deefb..4eaef9533ab2 100644
@@ -181,7 +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))
+	 *   @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)
@@ -233,7 +233,12 @@ int parse_timestamp(const char *t, usec_t *usec)
 			return r;
 
 		goto finish;
+	} else if (t[0] == '@') {
+		k = strptime(t + 1, "%s", &tm);
+		if (k && *k == 0)
+			goto finish;
 
+		return -EINVAL;
 	} else if (endswith(t, " ago")) {
 		char *z;
 
@@ -326,13 +331,6 @@ 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 | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

Applied, let's use '@' prefix :-) Thanks.

Karel

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

Applied, let's use '@' prefix :-) Thanks.

I will try to look at documenting the supported time formats within util-linux...

My guess would be to add a new file:
man-common/timestamp-formats.adoc

and link it in dmesg for example?

Quoted 3 lines

Karel

Signature · 1 line
Péter
maintainer
#3 lore raw
Quoted 9 lines · Péter Ujfalusi
Quoted 5 lines · Karel Zak
Quoted 2 lines · Peter Ujfalusi
 lib/timeutils.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

Applied, let's use '@' prefix :-) Thanks.

Quoted 7 lines

I will try to look at documenting the supported time formats within util-linux...

My guess would be to add a new file:
man-common/timestamp-formats.adoc

and link it in dmesg for example?

Yes, sounds good.

We also use parse_timestamp() in rtcwake, cal and last.

Karel

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