-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.c
1120 lines (956 loc) · 33.1 KB
/
ts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2023, 2024, Andrew McDermott. All rights reserved.
// This file is part of the https://github.com/frobware/ts project.
// For the full copyright and license information, please view the
// LICENSE file that was distributed with this source code.
// Feature test macro to enable clock_gettime.
#define _POSIX_C_SOURCE 200809L
// Feature test macro to enable strptime.
#define _XOPEN_SOURCE
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define NELEMENTS(A) (sizeof(A) / sizeof((A)[0]))
// MIN_TIME_BUFSZ - The minimum buffer size for formatting
// relative time differences.
//
// Calculation for the initial size of 136 characters:
// - Assumes time_t can have a maximum of 19 digits.
//
// - TIME_UNIT_COUNT is 6, representing years, months, days, hours,
// minutes, seconds.
//
// - Allocates 2 additional characters per unit for the symbol and a
// separator, adding 12 characters in total.
//
// - Accounts for a maximum of 9 characters for the direction strings
// (i.e., " from now" and " ago").
//
// - Includes 1 character for the null terminator.
//
// This results in an initial buffer size calculation of (6 * (19 +
// 2)) + 9 + 1 = 136 characters.
//
// The size is then rounded up to the next power of 2, which is 256,
// to potentially benefit from memory alignment optimizations. This
// approach acknowledges that the buffer size is way larger than
// strictly necessary but prefers uniformity and simplicity,
// considering the buffer size remains relatively small and efficient.
#define MIN_TIME_BUFSZ 256
#ifndef MAX_TIME_BUFSZ
#define MAX_TIME_BUFSZ 4096
#endif
#define COMP_TIME_INIT(COMP_TIME, Y, D, H, M, S) \
do { \
(COMP_TIME)[YEAR_UNIT] = (Y); \
(COMP_TIME)[DAY_UNIT] = (D); \
(COMP_TIME)[HOUR_UNIT] = (H); \
(COMP_TIME)[MINUTE_UNIT] = (M); \
(COMP_TIME)[SECOND_UNIT] = (S); \
} while(0)
#define COMP_TIME_ASSERT(COMP_TIME, Y, D, H, M, S) \
do { \
assert((COMP_TIME)[YEAR_UNIT] == (Y)); \
assert((COMP_TIME)[DAY_UNIT] == (D)); \
assert((COMP_TIME)[HOUR_UNIT] == (H)); \
assert((COMP_TIME)[MINUTE_UNIT] == (M)); \
assert((COMP_TIME)[SECOND_UNIT] == (S)); \
} while (0)
enum {
YEAR_UNIT,
DAY_UNIT,
HOUR_UNIT,
MINUTE_UNIT,
SECOND_UNIT,
TIME_UNIT_COUNT
};
enum sanitise_time_format_op {
COLLAPSE_MICROSECOND_SPECFIERS = 1,
EXPAND_MICROSECOND_SPECIFIERS,
};
struct ts_fmt {
struct ts_opt *opt;
char *sanitised_time_format;
size_t n_microseconds_specifiers;
char *buf;
size_t bufsz;
};
struct ts_opt {
bool flag_inc;
bool flag_mono;
bool flag_rel;
bool flag_sincestart;
bool hires_timestamping;
bool user_format_specified;
const char *format;
int flag_precision;
};
struct timestamp_pattern {
const char *const re;
const char *const description;
const char *strptime_format;
pcre2_code *pcre;
pcre2_match_data *match_data;
};
typedef time_t composite_time[TIME_UNIT_COUNT];
static struct timestamp_pattern timestamps[] = {{
.re = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{9}Z",
.description = "Kubernetes pod log entry with timestamp",
.strptime_format = "%Y-%m-%dT%H:%M:%S",
}, {
.re = "\\d{2}\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{6}",
.description = "Kubernetes client-go log format with microseconds",
.strptime_format = "%m%d %H:%M:%S",
}, {
.re = "\\d+\\s+\\w\\w\\w\\s+\\d\\d+\\s+\\d\\d:\\d\\d:\\d\\d\\s+[+-]\\d\\d\\d\\d",
.description = "16 Jun 94 07:29:35 with timezone",
.strptime_format = "%d %b %y %H:%M:%S %z",
}, {
.re = "\\d\\d[-\\s\\/]\\w\\w\\w\\/\\d\\d+\\s+\\d\\d:\\d\\d:\\d\\d\\s+[+-]\\d\\d\\d\\d",
.description = "21 dec/93 17:05:30 +0000",
.strptime_format = "%d %b/%y %H:%M:%S %z",
}, {
.re = "\\d\\d[-\\s\\/]\\w\\w\\w\\s+\\d\\d:\\d\\d:\\d\\d\\s+[+-]\\d\\d\\d\\d",
.description = "21 dec 17:05:30 +0000",
.strptime_format = "%d %b %H:%M:%S %z",
}, {
.re = "\\d\\d[-\\s\\/]\\w\\w\\w\\/\\d\\d+\\s+\\d\\d:\\d\\d",
.description = "21 dec/93 17:05 without seconds and timezone",
.strptime_format = "%d %b/%y %H:%M",
}, {
.re = "\\d\\d[-\\s\\/]\\w\\w\\w\\s+\\d\\d:\\d\\d",
.description = "21 dec 17:05 without seconds and timezone",
.strptime_format = "%d %b %H:%M",
}, {
.re = "\\d\\d\\d\\d[-:]\\d\\d[-:]\\d\\dT\\d\\d:\\d\\d:\\d\\d",
.description = "ISO-8601 format",
.strptime_format = "%Y-%m-%dT%H:%M:%S",
}, {
.re = "\\w\\w\\w\\s+\\w\\w\\w\\s+\\d\\d\\s+\\d\\d:\\d\\d",
.description = "Lastlog format",
.strptime_format = "%a %b %d %H:%M",
}, {
.re = "\\w{3}\\s+\\d{1,2}\\s+\\d\\d:\\d\\d:\\d\\d",
.description = "Syslog format with day",
.strptime_format = "%b %d %H:%M:%S",
},
};
static const int DAYS_PER_YEAR = 365;
static const int HOURS_PER_DAY = 24;
static const int MINUTES_PER_HOUR = 60;
static const int SECONDS_PER_MINUTE = 60;
static const int NANOSECONDS_PER_SECOND = 1000000000;
static const time_t SECONDS_PER_YEAR = DAYS_PER_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
static const time_t SECONDS_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
static const time_t SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
static const int MAX_VALUES[TIME_UNIT_COUNT] = {
[YEAR_UNIT] = INT_MAX,
[DAY_UNIT] = DAYS_PER_YEAR,
[HOUR_UNIT] = HOURS_PER_DAY,
[MINUTE_UNIT] = MINUTES_PER_HOUR,
[SECOND_UNIT] = SECONDS_PER_MINUTE
};
static const char *time_unit_symbol(int index)
{
switch (index) {
case YEAR_UNIT:
return "year";
case DAY_UNIT:
return "day";
case HOUR_UNIT:
return "hour";
case MINUTE_UNIT:
return "minute";
case SECOND_UNIT:
return "second";
default:
return "unknown";
}
}
static void seconds_to_composite_time(time_t seconds, composite_time comp_time)
{
time_t remainder = seconds;
comp_time[YEAR_UNIT] = remainder / SECONDS_PER_YEAR;
remainder %= SECONDS_PER_YEAR;
comp_time[DAY_UNIT] = remainder / SECONDS_PER_DAY;
remainder %= SECONDS_PER_DAY;
comp_time[HOUR_UNIT] = remainder / SECONDS_PER_HOUR;
remainder %= SECONDS_PER_HOUR;
comp_time[MINUTE_UNIT] = remainder / SECONDS_PER_MINUTE;
remainder %= SECONDS_PER_MINUTE;
comp_time[SECOND_UNIT] = remainder;
}
static time_t composite_time_to_seconds(composite_time comp_time)
{
time_t total = 0;
total += comp_time[YEAR_UNIT] * SECONDS_PER_YEAR;
total += comp_time[DAY_UNIT] * SECONDS_PER_DAY;
total += comp_time[HOUR_UNIT] * SECONDS_PER_HOUR;
total += comp_time[MINUTE_UNIT] * SECONDS_PER_MINUTE;
total += comp_time[SECOND_UNIT];
return total;
}
// approximate_comp_time: Normalises time units to a set precision.
//
// Modifies an array of time_unit structs, ensuring no unit exceeds
// its max value while retaining a specified count of non-zero units.
// The units are ordered from largest to smallest (e.g., years to
// seconds). The first unit (usually years) is treated specially and
// is never considered improper or over any limit. Rounds up the next
// significant unit if at least half its max value, resetting the
// current and less significant units to 0. Also handles cases where a
// unit's count equals or exceeds its max value by resetting it to 0
// and incrementing the previous unit. Repeats until non-zero units
// meet precision or all units are proper.
//
// The first unit is never reset or considered "too many" non-zeros,
// as it is the most significant.
//
// Params:
// - units: Array of time_unit structs from largest to smallest.
// - unitCount: Total units in the array.
// - precision: Max non-zero units to keep.
//
// Precision Behaviour:
// - 0: Resets all units to 0, nullifying duration.
// - 1: Retains only the most significant non-zero unit, rounding
// up as needed. The first unit (e.g., years) is always
// retained regardless of its value.
// - N (2 to `unitCount`-1): Keeps N most significant non-zero units,
// rounding (N+1)th if needed.
// - >= `unitCount`: No discarding or rounding, full detail kept.
//
// Operation:
// 1. Loop through units array.
// 2. Count non-zero units, skipping the first unit.
// 3. If non-zero units exceed precision:
// - Increment previous unit if current is at least half its max.
// - Reset current and subsequent units to 0.
// - Restart approximation.
// 4. If unit count equals/exceeds max (improper), increment previous,
// reset current to 0, restart approximation.
// 5. Continue until units are proper and precision is met.
//
// Example Usage:
// - Given units: 0y, 0h, 1m, 2s.
// - Precision = 1: Results in 0y, 1m (0h, 0s).
// - Precision = 2: Results in 0y, 1m, 2s (0h).
static void approximate_time(int precision, composite_time comp_time)
{
int overflowing_index;
int non_zero_count;
reapproximate:
overflowing_index = -1;
non_zero_count = 0;
for (size_t i = 0; i < TIME_UNIT_COUNT; i++) {
if (comp_time[i] == 0)
continue;
non_zero_count++;
if (i == YEAR_UNIT) {
// Years never overflow.
continue;
}
if (non_zero_count > precision) {
if (comp_time[i] >= MAX_VALUES[i] / 2) {
comp_time[i - 1]++;
}
// Reset this and subsequent values to 0.
for (size_t j = i; j < TIME_UNIT_COUNT; j++) {
comp_time[j] = 0;
}
goto reapproximate;
} else if (comp_time[i] >= MAX_VALUES[i]) {
overflowing_index = i;
}
}
if (overflowing_index != -1) {
// Adjust one overflowing time unit per iteration.
comp_time[overflowing_index - 1]++;
comp_time[overflowing_index] = 0;
goto reapproximate;
}
}
// Checks if a placeholder (%.S, %.s, or %.T) is found at the current
// position in format.
static bool is_microsecond_placeholder(const char *format, size_t format_length, size_t pos, char *spec) {
if (pos + 2 < format_length &&
format[pos] == '%' && format[pos + 1] == '.' &&
(format[pos + 2] == 'S' || format[pos + 2] == 's' || format[pos + 2] == 'T')) {
if (spec != NULL)
*spec = format[pos + 2];
return true;
}
return false;
}
static size_t count_microsecond_specifiers(const char *format)
{
size_t count = 0;
size_t format_len = strlen(format);
for (size_t i = 0; i < format_len; i++) {
if (is_microsecond_placeholder(format, format_len, i, NULL)) {
count++;
i += 2;
}
}
return count;
}
static int sanitise_time_format(const char *format,
char **pbuf,
size_t *n_microsecond_specifiers,
enum sanitise_time_format_op op)
{
*n_microsecond_specifiers = count_microsecond_specifiers(format);
size_t format_length = strlen(format);
size_t required_capacity = format_length + 1;
if (op == EXPAND_MICROSECOND_SPECIFIERS) {
required_capacity += *n_microsecond_specifiers * 6;
}
if ((*pbuf = (char *)calloc(required_capacity, 1)) == NULL) {
return -1;
}
for (size_t i = 0, wr = 0; i < format_length; i++) {
char specifier;
if (is_microsecond_placeholder(format, format_length, i, &specifier)) {
(*pbuf)[wr++] = '%';
(*pbuf)[wr++] = specifier;
if (op == EXPAND_MICROSECOND_SPECIFIERS) {
memcpy(*pbuf + wr, ".000000", 7);
wr += 7;
}
i += 2; // i++ in the loop corrects to 3.
} else {
(*pbuf)[wr++] = format[i];
}
}
return 0;
}
static int validate_time_format(const char *format, char **pbuf, size_t *bufsz)
{
struct tm time_info = { 0 };
char *buf = NULL;
*bufsz = MIN_TIME_BUFSZ;
while (*bufsz <= MAX_TIME_BUFSZ) {
size_t n;
char *new_buf;
if ((new_buf = realloc(buf, *bufsz)) == NULL) {
free(buf);
return -1;
}
buf = new_buf;
n = strftime(buf, *bufsz, format, &time_info);
if (n > 0) {
*pbuf = buf;
return 0;
} else if (n == 0 && *bufsz < MAX_TIME_BUFSZ) {
*bufsz = (*bufsz < MAX_TIME_BUFSZ / 2)
? *bufsz * 2
: MAX_TIME_BUFSZ;
} else {
// Reached the permissible maximum or strftime
// legitimately returned 0 (empty string).
*pbuf = buf;
return (n == 0 && *bufsz == MAX_TIME_BUFSZ) ? 0 : -1;
}
}
if (buf != NULL)
free(buf);
return -1;
}
static size_t write_ull_padded(char *buf, size_t offset, unsigned long long value, size_t width)
{
unsigned long long temp = value;
int ndigits = (temp == 0) ? 1 : 0;
while (temp > 0) {
ndigits++;
temp /= 10;
}
int required_padding = width - ndigits;
if (required_padding < 0 || width == 0) {
required_padding = 0;
}
for (int i = 0; i < required_padding; i++) {
buf[offset+i] = '0';
}
temp = value;
for (int i = ndigits - 1; i >= 0; i--) {
buf[offset + i] = (temp % 10) + '0';
temp /= 10;
}
return ndigits + required_padding;
}
static void format_comp_time(char *buf, const composite_time comp_time, const char *const direction, size_t direction_len)
{
size_t offset = 0;
for (size_t i = 0; i < TIME_UNIT_COUNT; i++) {
if (comp_time[i] > 0) {
offset += write_ull_padded(buf, offset, comp_time[i], 0);
buf[offset++] = *time_unit_symbol(i);
}
}
for (size_t i = 0; i < direction_len; i++) {
buf[offset++] = direction[i];
}
buf[offset] = '\0';
}
static bool match_timestamp(char *subject, ssize_t len, size_t *match_start, size_t *match_end, const char **strptime_fmt)
{
*match_start = *match_end = 0;
*strptime_fmt = NULL;
for (size_t i = 0; i < NELEMENTS(timestamps); i++) {
if (pcre2_match(timestamps[i].pcre, (PCRE2_SPTR)subject, len, 0, 0, timestamps[i].match_data, NULL) < 0)
continue; // No match.
size_t *ovector = pcre2_get_ovector_pointer(timestamps[i].match_data);
assert(ovector);
*match_start = ovector[0];
*match_end = ovector[1];
*strptime_fmt = timestamps[i].strptime_format;
return true;
}
return false;
}
// Calculates a timestamp based on various modes and flags. This
// function handles both high-resolution (hires) and
// non-high-resolution (non-hires) timestamping.
//
// In high-resolution mode, it accounts for nanoseconds and adjusts
// the timestamp based on the monotonic clock offset (monodelta) if
// required. It also normalises nanoseconds to ensure they are within
// the standard range.
//
// In incremental mode (flag_inc), it calculates the delta (time
// difference) since the last timestamp and updates last_seconds and
// last_nanoseconds for the next calculation. This delta calculation
// is crucial for the correct functioning of the incremental
// timestamping.
//
// The flag_sincestart is used to calculate the time elapsed since the
// start of the program, providing timestamps relative to the
// program's start time rather than real-world time.
//
// The function maintains separate logic for hires and non-hires modes
// due to the different handling of nanoseconds.
//
// @param last_seconds Pointer to the variable holding the seconds part
// of the last timestamp.
// @param last_nanoseconds Pointer to the variable holding the nanoseconds
// part of the last timestamp.
// @param flag_mono Indicates if the monotonic clock is used.
// @param flag_inc Indicates if incremental mode is active.
// @param flag_sincestart Indicates if the timestamp should be calculated
// since the start of the program.
// @param monodelta The offset to be added in monotonic mode to align
// with real-world time.
// @param hires_timestamping Indicates if high-resolution timestamping is
// used.
// @return A timespec struct representing the calculated
// timestamp.
static bool gettime(const struct ts_opt *const ts, struct timespec *now, long *last_seconds, long *last_nanoseconds, long monodelta)
{
if (clock_gettime(ts->flag_mono ? CLOCK_MONOTONIC : CLOCK_REALTIME, now) != 0)
return false;
if (ts->hires_timestamping) {
if (ts->flag_mono) {
now->tv_sec += monodelta;
}
if (now->tv_nsec >= NANOSECONDS_PER_SECOND) {
now->tv_sec++;
now->tv_nsec -= NANOSECONDS_PER_SECOND;
}
}
if (ts->flag_inc || ts->flag_sincestart) {
long delta_seconds = now->tv_sec - *last_seconds;
long delta_nanoseconds = ts->hires_timestamping ? now->tv_nsec - *last_nanoseconds : 0;
if (ts->hires_timestamping && delta_nanoseconds < 0) {
delta_seconds--;
delta_nanoseconds += NANOSECONDS_PER_SECOND;
}
if (ts->flag_inc) {
*last_seconds = now->tv_sec;
*last_nanoseconds = ts->hires_timestamping ? now->tv_nsec : 0;
}
now->tv_sec = delta_seconds;
now->tv_nsec = delta_nanoseconds;
}
return true;
}
static void fmt_time_rel(struct ts_fmt *fmt, char *line, ssize_t line_len, size_t *match_end, struct timespec now)
{
size_t match_start;
const char *strptime_fmt = NULL;
*match_end = 0;
fmt->buf[0] = '\0';
if (!match_timestamp(line, line_len, &match_start, match_end, &strptime_fmt)) {
return;
}
// Isolate the timestamp within the line before parsing.
char old_char = line[*match_end];
line[*match_end] = '\0';
// strptime only sets the fields in struct tm that correspond
// to the components it finds in the input string based on the
// provided format string. This means not all fields in struct
// tm might be set by strptime if they're not represented in
// the input string.
struct tm parsed_tm = { 0 };
if (strptime(&line[match_start], strptime_fmt, &parsed_tm) == NULL) {
line[*match_end] = old_char;
return;
}
line[*match_end] = old_char;
if (parsed_tm.tm_year == 0) {
struct tm *current_tm = localtime(&now.tv_sec);
parsed_tm.tm_year = current_tm->tm_year;
}
// Convert the parsed timestamp to time_t to assess its
// chronological relationship with the current time. In the
// context of analyzing historic logs, it's essential to
// verify that timestamps reflect past events. This step helps
// identify and correct situations where the parsed timestamp,
// due to format ambiguities or incomplete data (e.g., missing
// year information), might erroneously be interpreted as
// being in the future.
// Let mktime() determine DST.
parsed_tm.tm_isdst = -1;
time_t parsed_time_t = mktime(&parsed_tm);
if (parsed_time_t > now.tv_sec) {
parsed_tm.tm_year--;
// Let mktime() determine DST.
parsed_tm.tm_isdst = -1;
parsed_time_t = mktime(&parsed_tm);
}
if (fmt->opt->user_format_specified) {
strftime(fmt->buf, fmt->bufsz, fmt->sanitised_time_format, &parsed_tm);
} else {
time_t seconds_diff = difftime(now.tv_sec, parsed_time_t);
if (seconds_diff == 0) {
snprintf(fmt->buf, fmt->bufsz, "right now");
return;
}
composite_time comp_time;
seconds_to_composite_time(labs(seconds_diff), comp_time);
approximate_time(fmt->opt->flag_precision, comp_time);
format_comp_time(fmt->buf,
comp_time,
seconds_diff >= 0 ? " ago" : " from now",
seconds_diff >= 0 ? 4 : 9);
}
}
static void fmt_time_now(struct ts_fmt *fmt, struct timespec now)
{
*fmt->buf = '\0';
size_t n = strftime(fmt->buf, fmt->bufsz, fmt->sanitised_time_format, localtime(&now.tv_sec));
for (size_t i = 0; n > 0 && i < fmt->n_microseconds_specifiers; i++) {
char *placeholder = strstr(fmt->buf, ".000000");
if (placeholder != NULL) {
write_ull_padded(placeholder, 1, now.tv_nsec / 1000, 6);
}
}
}
static void must_init_timestamp_patterns(void)
{
for (size_t i = 0; i < NELEMENTS(timestamps); i++) {
PCRE2_SIZE offset;
PCRE2_SPTR pattern = (PCRE2_SPTR)timestamps[i].re;
int rc;
uint32_t options = PCRE2_UTF | PCRE2_UCP;
timestamps[i].pcre = pcre2_compile(
pattern, // the pattern
PCRE2_ZERO_TERMINATED, // indicates the pattern is zero-terminated
options, // options
&rc, // for error number
&offset, // for error offset
NULL // use default compile context
);
if (timestamps[i].pcre == NULL) {
PCRE2_UCHAR buf[256];
pcre2_get_error_message(rc, buf, sizeof(buf));
fprintf(stderr, "PCRE2 compilation error for pattern#%zd: '%s', error='%s', offset=%ld.\n", i, timestamps[i].re, buf, offset);
exit(EXIT_FAILURE);
}
timestamps[i].match_data = pcre2_match_data_create_from_pattern(timestamps[i].pcre, NULL);
if (timestamps[i].match_data == NULL) {
fprintf(stderr, "Failed to create match data for pattern %zu\n", i);
exit(EXIT_FAILURE);
}
}
}
static bool init_clocks(const struct ts_opt *const ts, long *last_seconds, long *last_nanoseconds, long *monodelta)
{
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) != 0) {
return false;
}
*last_seconds = now.tv_sec;
*last_nanoseconds = ts->hires_timestamping ? now.tv_nsec : 0;
*monodelta = 0;
if (ts->flag_mono) {
struct timespec real_time;
if (clock_gettime(CLOCK_MONOTONIC, &real_time) != 0) {
return false;
}
if (now.tv_sec >= real_time.tv_sec) {
*monodelta = now.tv_sec - real_time.tv_sec;
*last_seconds = real_time.tv_sec + *monodelta;
*last_nanoseconds = real_time.tv_nsec;
} else {
fprintf(stderr, "fatal error: real time is less than monotonic time!\n");
exit(EXIT_FAILURE);
}
}
return true;
}
static struct ts_opt parse_options(int argc, char *argv[])
{
struct ts_opt option = { 0 };
int opt;
char *value_endptr;
long value;
option.flag_precision = 2; /* default */
while ((opt = getopt(argc, argv, "imrsp:")) != -1) {
switch (opt) {
case 'i':
option.flag_inc = true;
break;
case 'm':
option.flag_mono = true;
break;
case 'r':
option.flag_rel = true;
break;
case 's':
option.flag_sincestart = true;
break;
case 'p':
value = strtol(optarg, &value_endptr, 10);
if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || (errno != 0 && value == 0)) {
fprintf(stderr, "Error: -p %ld: %s.\n", value, strerror(errno));
exit(EXIT_FAILURE);
}
if (value_endptr == optarg || *value_endptr != '\0' ||
value < 1 || value >= TIME_UNIT_COUNT) {
fprintf(stderr, "Error: -p %ld is out of range. Valid values are between 1 and %d inclusive.\n", value, TIME_UNIT_COUNT-1);
exit(EXIT_FAILURE);
}
option.flag_precision = value;
break;
default:
fprintf(stderr, "Usage: ts [-r] [-i | -s] [-m] [-p precision] [format]\n");
exit(EXIT_FAILURE);
}
}
if (option.flag_inc && option.flag_sincestart) {
fprintf(stderr, "Options '-i' and '-s' cannot be used together.\n");
exit(EXIT_FAILURE);
}
/*
* %b = Abbreviated month name
* %d = The day of the month as a decimal number
* %H = Hours
* % %M = Minutes
* % %S = Seconds
*/
const char *final_format = "%b %d %H:%M:%S";
if (optind < argc) {
final_format = argv[optind];
}
if (option.flag_inc || option.flag_sincestart) {
// This is a departure from the moreutils version of
// ts. If we have a user-supplied format, then use
// that in preference to %H:%M:%S.
if (optind == argc) {
final_format = "%H:%M:%S";
}
setenv("TZ", "GMT", 1);
tzset();
}
option.format = final_format;
option.hires_timestamping = count_microsecond_specifiers(option.format) > 0 || option.flag_mono;
option.user_format_specified = optind < argc;
return option;
}
static void test_precision_variations(void)
{
composite_time comp_time;
time_t timestamp;
// Test rounding up with precision level 3; hours increase due
// to minutes and seconds.
COMP_TIME_INIT(comp_time, 1, 2, 3, 45, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(3, comp_time);
COMP_TIME_ASSERT(comp_time, 1, 2, 4, 0, 0);
// Test detailed representation with precision level 4;
// minutes increase due to seconds.
COMP_TIME_INIT(comp_time, 1, 2, 3, 45, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 1, 2, 3, 46, 0);
// Test with 59 minutes, 59 seconds at precision level 4;
// expecting no change since all units are significant.
COMP_TIME_INIT(comp_time, 0, 0, 0, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 0, 59, 59);
// Test with 1 hour, 59 minutes, 59 seconds at precision level
// 3; expecting no change as rounding not applied due to
// precision.
COMP_TIME_INIT(comp_time, 0, 0, 1, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(3, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 1, 59, 59);
// Test rounding up from minutes to hours with precision level
// 2; hours should increase, minutes reset.
COMP_TIME_INIT(comp_time, 0, 0, 1, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 2, 0, 0);
// Simplify to most significant unit (days) with precision
// level 1 from days, hours, and minutes.
COMP_TIME_INIT(comp_time, 0, 1, 2, 28, 30);
timestamp = composite_time_to_seconds(comp_time);
assert(timestamp == 95310);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(1, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 0, 0, 0);
// Retain days and hours with precision level 2, simplifying
// minutes and seconds.
COMP_TIME_INIT(comp_time, 0, 1, 2, 28, 30);
timestamp = composite_time_to_seconds(comp_time);
assert(timestamp == 95310);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 2, 0, 0);
// Retain days, hours, and approximate minutes with precision
// level 3.
COMP_TIME_INIT(comp_time, 0, 1, 2, 28, 30);
timestamp = composite_time_to_seconds(comp_time);
assert(timestamp == 95310);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(3, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 2, 29, 0);
// Full detail maintained with precision level 4, no
// approximation.
COMP_TIME_INIT(comp_time, 0, 1, 2, 28, 30);
timestamp = composite_time_to_seconds(comp_time);
assert(timestamp == 95310);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 2, 28, 30);
// Test high precision with minimal input (1 second), expecting no approximation.
COMP_TIME_INIT(comp_time, 0, 0, 0, 0, 1);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 0, 0, 1);
// Test for minute to hour rollover with precision level 2.
COMP_TIME_INIT(comp_time, 0, 0, 1, 59, 30);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 2, 0, 0);
// Test for no change with hours near maximum but within
// precision level 2.
COMP_TIME_INIT(comp_time, 0, 0, 23, 45, 0);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 23, 45, 0);
// Test day to year rollover with precision level 2; days
// reset, year increments.
COMP_TIME_INIT(comp_time, 1, 364, 23, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 2, 0, 0, 0, 0);
// Test handling when non-zero units exceed precision level 2;
// hours and less significant units reset.
COMP_TIME_INIT(comp_time, 0, 0, 23, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 0, 0, 0);
// Test handling when all units are set to zero, with precision level 2.
COMP_TIME_INIT(comp_time, 0, 0, 0, 0, 0);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 0, 0, 0);
// Test edge case for seconds rolling over to minutes, with
// precision level 2.
COMP_TIME_INIT(comp_time, 0, 0, 0, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 0, 59, 59);
// Test high precision with minimal input (1 second), expecting no approximation.
COMP_TIME_INIT(comp_time, 0, 0, 0, 0, 1);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 0, 0, 1);
// Test rollover across multiple units (seconds to minutes to
// hours), with precision level 3.
COMP_TIME_INIT(comp_time, 0, 0, 23, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(3, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 23, 59, 59);
// Test mixed zero and non-zero units, with precision level 2.
COMP_TIME_INIT(comp_time, 1, 0, 1, 0, 1);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 1, 0, 1, 0, 0);
// Test days to years rollover with minimal hours input, with
// precision level 2.
COMP_TIME_INIT(comp_time, 0, 364, 23, 0, 0);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 364, 23, 0, 0);
// Test with maximum values just before rollover for each
// unit, with precision level 4. Expecting no change with
// precision 4, as all units are significant.
COMP_TIME_INIT(comp_time, 0, 364, 23, 59, 59);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 364, 23, 59, 59);
// Test handling of cascading rollover from minutes to hours
// to days, with precision level 2.
COMP_TIME_INIT(comp_time, 0, 0, 23, 59, 30);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(2, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 0, 0, 0);
// Test transition at midnight with precision level 3.
COMP_TIME_INIT(comp_time, 0, 0, 23, 59, 60);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(3, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 1, 0, 0, 0);
// Test with minimal non-zero units and lower precision.
COMP_TIME_INIT(comp_time, 0, 0, 0, 1, 30);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(1, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 0, 2, 0);
// Test sparse non-zero units with high precision.
COMP_TIME_INIT(comp_time, 1, 0, 0, 0, 5);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(4, comp_time);
COMP_TIME_ASSERT(comp_time, 1, 0, 0, 0, 5);
// Test rounding in the middle of the spectrum with precision
// 3. Assuming no change as it's already concise.
COMP_TIME_INIT(comp_time, 0, 0, 12, 30, 0);
timestamp = composite_time_to_seconds(comp_time);
seconds_to_composite_time(timestamp, comp_time);
approximate_time(3, comp_time);
COMP_TIME_ASSERT(comp_time, 0, 0, 12, 30, 0);