aboutsummaryrefslogtreecommitdiffstats
path: root/timeprefix.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-02-19 15:14:17 +0100
committerMattias Andrée <maandree@kth.se>2021-02-19 15:14:17 +0100
commitb5d4ae90fc9fbd803f00d44eadf378c8fa45b257 (patch)
treea085859684571a092351611fe577d21e5807ca06 /timeprefix.c
parentmakefile: add check (diff)
downloadtimeprefix-b5d4ae90fc9fbd803f00d44eadf378c8fa45b257.tar.gz
timeprefix-b5d4ae90fc9fbd803f00d44eadf378c8fa45b257.tar.bz2
timeprefix-b5d4ae90fc9fbd803f00d44eadf378c8fa45b257.tar.xz
Add leap second support and reject arguments
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--timeprefix.c37
1 files changed, 32 insertions, 5 deletions
diff --git a/timeprefix.c b/timeprefix.c
index 1b2544b..9cc9585 100644
--- a/timeprefix.c
+++ b/timeprefix.c
@@ -1,18 +1,38 @@
/* See LICENSE file for copyright and license details. */
+#include <sys/timex.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <unistd.h>
+
static void
printline(const char *line)
{
- static struct timespec boottime;
- time_t realtime;
+ static struct timex timex; /* static to zero initialise it */
+ struct timespec boottime;
struct tm *utctime;
+ int r;
clock_gettime(CLOCK_BOOTTIME, &boottime);
- realtime = time(NULL);
- utctime = gmtime(&realtime);
+ r = adjtimex(&timex);
+ if (timex.time.tv_sec % (24 * 60 * 60) == 0) {
+ if (r == TIME_INS) {
+ timex.time.tv_sec -= 1;
+ utctime = gmtime(&timex.time.tv_sec);
+ utctime->tm_sec += 1;
+ } else if (r == TIME_DEL) {
+ timex.time.tv_sec += 1;
+ utctime = gmtime(&timex.time.tv_sec);
+ } else {
+ utctime = gmtime(&timex.time.tv_sec);
+ }
+ } else if (r == TIME_OOP) {
+ utctime = gmtime(&timex.time.tv_sec);
+ utctime->tm_sec += 1;
+ } else {
+ utctime = gmtime(&timex.time.tv_sec);
+ }
printf("[%010lu.%04lu %i-%02i-%02i %02i:%02i:%02i UTC] %s",
boottime.tv_sec, boottime.tv_nsec / 100000,
@@ -24,11 +44,18 @@ printline(const char *line)
}
int
-main(void)
+main(int argc, char *argv[])
{
char *buf = NULL;
size_t siz = 0;
+ if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
+ argc -= 1;
+ if (argc > 1) {
+ fprintf(stderr, "usage: %s\n", argv[0]);
+ return 1;
+ }
+
printline("--- Program started ---\n");
while (getline(&buf, &siz, stdin) != -1)
printline(buf);