util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/timeutils: Require '@' prefix for seconds since the Epoch timestamp
@ 2022-09-07  5:41 Peter Ujfalusi
  2022-09-12 12:23 ` Karel Zak
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Ujfalusi @ 2022-09-07  5:41 UTC (permalink / raw)
  To: kzak; +Cc: util-linux, peter.ujfalusi

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.

Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
---
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(-)

diff --git a/lib/timeutils.c b/lib/timeutils.c
index 6dda2e8deefb..4eaef9533ab2 100644
--- a/lib/timeutils.c
+++ b/lib/timeutils.c
@@ -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:
-- 
2.37.3


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

end of thread, other threads:[~2022-09-13 18:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07  5:41 [PATCH] lib/timeutils: Require '@' prefix for seconds since the Epoch timestamp Peter Ujfalusi
2022-09-12 12:23 ` Karel Zak
2022-09-13 11:26   ` Péter Ujfalusi
2022-09-13 18:24     ` Karel Zak

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