-
Notifications
You must be signed in to change notification settings - Fork 10
/
linets.c
647 lines (591 loc) · 26.6 KB
/
linets.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
/*####################################################################
#
# LINETS - Print the Current Timestamp at the Top of Each Line
#
# This command attaches the current time to the top of each line.
# (as the first field of the line) Strictly speaking, "the
# current time" means the instant when this command starts
# writing the line which has been read.
#
# USAGE : linets [-0|-3|-6|-9] [-c|-e|-z|-Z] [-du] [file [...]]
# Args : file ...... Filepath to be attached the current timestamp
# ("-" means STDIN)
# Options : -0,-3,-6,-9 Specify resolution unit of the time. For instance,
# timestamp becomes "YYYYMMDDhhmmss.nnn" when
# "-3" option is set.
# You have to set one of them.
# -0 ... second (default)
# -3 ... millisecond
# -6 ... microsecond
# -9 ... nanosecond
# -c,-e,-z,-Z Specify the format for timestamp. You can choose
# one of them.
# -c ... "YYYYMMDDhhmmss[.n]" (default)
# Calendar-time (standard time) in your
# timezone (".n" is the digits under
# second. It will be attached when -3 or
# -6 or -9 option is specified)
# -e ... "n[.n]"
# The number of seconds since the UNIX
# epoch (".n" is the same as -c)
# -z ... "n[.n]"
# The number of seconds since this command
# started (".n" is the same as -c)
# -Z ... "n[.n]"
# The number of seconds since the first
# line came (".n" is the same as -c)
# -d ........ Insert "delta-t" (the number of seconds since
# started writing the previous line) into the next
# to the current timestamp. So, two fields will
# be attatched when using this option.
# -u ........ Set the date in UTC when -c option is set
# (same as that of date command)
# Retuen : Return 0 only when finished successfully
#
# How to compile : cc -O3 -o __CMDNAME__ __SRCNAME__ -lrt
# (if it doesn't work)
# How to compile : cc -O3 -o __CMDNAME__ __SRCNAME__
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2024-06-23
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/tokideli
#
####################################################################*/
/*####################################################################
# Initial Configuration
####################################################################*/
/*=== Initial Setting ==============================================*/
/*--- headers ------------------------------------------------------*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
/*--- macro constants ----------------------------------------------*/
/* Buffer size for a timestamp string */
#define LINE_BUF 80
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC CLOCK_REALTIME /* for HP-UX */
#endif
/*--- data type definitions ----------------------------------------*/
typedef struct timespec tmsp;
/*--- prototype functions ------------------------------------------*/
int read_1line(FILE *fp);
int read_c1st_1line(FILE *fp);
int read_e1st_1line(FILE *fp);
int read_Z1st_1line(FILE *fp);
void print_cur_timestamp(void);
/*--- global variables ---------------------------------------------*/
char* gpszCmdname ; /* The name of this command */
int giVerbose = 0 ; /* speaks more verbosely by the greater number */
int giFmtType = 'c'; /* 'c':calendar-time (default)
'e':UNIX-epoch-time
'z':command-running-sec */
int giTimeResol = 0 ; /* 0:second(def) 3:millisec 6:microsec 9:nanosec */
int giDeltaMode = 0 ; /* attach the number of seconds since printing
the previous line after the timestamp when >0 */
tmsp gtsZero = {0}; /* Time this command booted */
tmsp gtsPrev = {0}; /* Time the previous line has come (-gtsZero) */
int giHold = 0 ; /* for read_1line(): 1 if next character exists */
int giNextchar ; /* for read_1line(): the next character */
/*=== Define the functions for printing usage and error ============*/
/*--- exit with usage ----------------------------------------------*/
void print_usage_and_exit(void) {
fprintf(stderr,
"USAGE : %s [-0|-3|-6|-9] [-c|-e|-z|-Z] [-du] [file [...]]\n"
"Args : file ...... Filepath to be attached the current timestamp\n"
" (\"-\" means STDIN)\n"
"Options : -0,-3,-6,-9 Specify resolution unit of the time. For instance,\n"
" timestamp becomes \"YYYYMMDDhhmmss.nnn\" when\n"
" \"-3\" option is set. \n"
" You have to set one of them.\n"
" -0 ... second (default)\n"
" -3 ... millisecond\n"
" -6 ... microsecond\n"
" -9 ... nanosecond\n"
" -c,-e,-z,-Z Specify the format for timestamp. You can choose\n"
" one of them.\n"
" -c ... \"YYYYMMDDhhmmss[.n]\" (default)\n"
" Calendar-time (standard time) in your\n"
" timezone (\".n\" is the digits under\n"
" second. It will be attached when -3 or\n"
" -6 or -9 option is specified)\n"
" -e ... \"n[.n]\"\n"
" The number of seconds since the UNIX\n"
" epoch (\".n\" is the same as -c)\n"
" -z ... \"n[.n]\"\n"
" The number of seconds since this command\n"
" started (\".n\" is the same as -c)\n"
" -Z ... \"n[.n]\"\n"
" The number of seconds since the fisrt\n"
" line came (\".n\" is the same as -c)\n"
" -d ........ Insert \"delta-t\" (the number of seconds since\n"
" started writing the previous line) into the next\n"
" to the current timestamp. So, two fields will\n"
" be attatched when using this option.\n"
" -u ........ Set the date in UTC when -c option is set\n"
" (same as that of date command)\n"
"Retuen : Return 0 only when finished successfully\n"
"Version : 2024-06-23 13:28:01 JST\n"
" (POSIX C language)\n"
"\n"
"Shell-Shoccar Japan (@shellshoccarjpn), No rights reserved.\n"
"This is public domain software. (CC0)\n"
"\n"
"The latest version is distributed at the following page.\n"
"https://github.com/ShellShoccar-jpn/tokideli\n"
,gpszCmdname);
exit(1);
}
/*--- print warning message ----------------------------------------*/
void warning(const char* szFormat, ...) {
va_list va;
va_start(va, szFormat);
fprintf(stderr,"%s: ",gpszCmdname);
vfprintf(stderr,szFormat,va);
va_end(va);
return;
}
/*--- exit with error message --------------------------------------*/
void error_exit(int iErrno, const char* szFormat, ...) {
va_list va;
va_start(va, szFormat);
fprintf(stderr,"%s: ",gpszCmdname);
vfprintf(stderr,szFormat,va);
va_end(va);
exit(iErrno);
}
/*####################################################################
# Main
####################################################################*/
/*=== Initialization ===============================================*/
int main(int argc, char *argv[]) {
/*--- Variables ----------------------------------------------------*/
int iRet; /* return code */
int iRet_r1l; /* return value by read_1line() */
char *pszPath; /* filepath on arguments */
char *pszFilename; /* filepath (for message) */
int iFileno; /* file# of filepath */
int iFd; /* file descriptor */
FILE *fp; /* file handle */
int iFirstline='c'; /* >0 when x-opt and no line come yet */
int i; /* all-purpose int */
/*--- Initialize ---------------------------------------------------*/
if (clock_gettime(CLOCK_REALTIME,>sZero) != 0) {
error_exit(errno,"clock_gettime() at initialize: %s\n",strerror(errno));
}
gpszCmdname = argv[0];
for (i=0; *(gpszCmdname+i)!='\0'; i++) {
if (*(gpszCmdname+i)=='/') {gpszCmdname=gpszCmdname+i+1;}
}
if (setenv("POSIXLY_CORRECT","1",1) < 0) {
error_exit(errno,"setenv() at initialization: \n", strerror(errno));
}
/*=== Parse arguments ==============================================*/
/*--- Parse options which start by "-" -----------------------------*/
while ((i=getopt(argc, argv, "0369cezZduvh")) != -1) {
switch (i) {
case '0': giTimeResol = 0 ; break;
case '3': giTimeResol = 3 ; break;
case '6': giTimeResol = 6 ; break;
case '9': giTimeResol = 9 ; break;
case 'c': giFmtType = 'c'; iFirstline='c'; break;
case 'e': giFmtType = 'e'; iFirstline='e'; break;
case 'Z': giFmtType = 'Z'; iFirstline='Z'; break;
case 'z': giFmtType = 'z'; iFirstline='z'; break;
case 'd': giDeltaMode = 1 ; break;
case 'u': (void)setenv("TZ", "UTC0", 1); break;
case 'v': giVerbose++ ; break;
case 'h': print_usage_and_exit();
default : print_usage_and_exit();
}
}
argc -= optind-1;
argv += optind ;
if (giVerbose>0) {warning("verbose mode (level %d)\n",giVerbose);}
/*=== Switch buffer mode ===========================================*/
if (setvbuf(stdout,NULL,_IOLBF,0)!=0) {
error_exit(255,"Failed to switch to line-buffered mode\n");
}
/*=== Each file loop ===============================================*/
iRet = 0;
iFileno = 0;
iFd = -1;
iRet_r1l = 0;
while ((pszPath = argv[iFileno]) != NULL || iFileno == 0) {
/*--- Open one of the input files --------------------------------*/
if (pszPath == NULL || strcmp(pszPath, "-") == 0) {
pszFilename = "stdin" ;
iFd = STDIN_FILENO ;
} else {
pszFilename = pszPath ;
while ((iFd=open(pszPath, O_RDONLY)) < 0) {
if (errno == EINTR) {continue;}
iRet = 1;
warning("%s: %s\n",pszFilename,strerror(errno));
iFileno++;
break;
}
if (iFd < 0) {continue;}
}
if (iFd == STDIN_FILENO) {
fp = stdin;
if (feof(stdin)) {clearerr(stdin);} /* Reset EOF condition when stdin */
} else {
fp = fdopen(iFd, "r");
}
/*--- Reading and writing loop -----------------------------------*/
if (! feof(fp)) {
switch(iFirstline) {
case 'c': iRet_r1l=read_c1st_1line(fp); iFirstline=0; break;
case 'e': iRet_r1l=read_e1st_1line(fp); iFirstline=0; break;
case 'z': gtsPrev.tv_sec =gtsZero.tv_sec;
gtsPrev.tv_nsec=gtsZero.tv_nsec;
iRet_r1l=0; iFirstline=0; break;
case 'Z': iRet_r1l=read_Z1st_1line(fp); iFirstline=0;giFmtType='z'; break;
default : iRet_r1l=0;
}
while (iRet_r1l==0) {iRet_r1l=read_1line(fp);}
}
/*--- Close the input file ---------------------------------------*/
if (fp != stdin) {fclose(fp);}
/*--- End loop ---------------------------------------------------*/
if (pszPath == NULL) {break;}
iFileno++;
}
/*=== Finish normally ==============================================*/
return(iRet);}
/*####################################################################
# Functions
####################################################################*/
/*=== Read and write only one line ===================================
* [in] giHold : (must be defined as a global variable)
* giNextchar : (must be defined as a global variable)
* [ret] 0 : Finished reading/writing due to '\n'
* 1 : Finished reading/writing due to '\n',
* which is the last char of the file
* EOF : Finished reading/writing due to EOF */
int read_1line(FILE *fp) {
/*--- Variables --------------------------------------------------*/
int iChar;
/*--- Reading and writing a line (1st letter of the line) --------*/
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
print_cur_timestamp();
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_1line(): putchar() #1: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
/*--- Reading and writing a line ---------------------------------*/
while (1) {
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_1line(): putchar() #2: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
}
}
/*=== Read and write only one line (for c-option and 1st line ) ======
* [in] giHold : (must be defined as a global variable)
* giNextchar : (must be defined as a global variable)
* [ret] 0 : Finished reading/writing due to '\n'
* 1 : Finished reading/writing due to '\n',
* which is the last char of the file
* EOF : Finished reading/writing due to EOF */
int read_c1st_1line(FILE *fp) {
/*--- Variables --------------------------------------------------*/
tmsp tsNow;
struct tm *ptm ;
int iChar;
/*--- Reading and writing a line (1st letter of the line) --------*/
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
if (clock_gettime(CLOCK_REALTIME,&tsNow) != 0) {
error_exit(errno,"read_c1st_1line(): clock_gettime(): %s\n",
strerror(errno) );
}
if ((ptm=localtime(&tsNow.tv_sec)) == NULL) {
error_exit(255,"read_c1st_1line(): localtime(): returned NULL\n");
}
printf("%04d%02d%02d%02d%02d%02d",
ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
ptm->tm_hour , ptm->tm_min , ptm->tm_sec );
switch (giTimeResol) {
case 0 : putchar(' ') ; break;
case 3 : printf(".%03d " , (int)(tsNow.tv_nsec+500000)/1000000); break;
case 6 : printf(".%06d " , (int)(tsNow.tv_nsec+ 500)/ 1000); break;
case 9 : printf(".%09ld ", tsNow.tv_nsec ); break;
default: error_exit(255,"read_e1st_1line(): Unknown resolution\n");
}
if (giDeltaMode) {
printf("0 ");
gtsPrev.tv_sec=tsNow.tv_sec; gtsPrev.tv_nsec=tsNow.tv_nsec;
}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_c1st_1line(): putchar() #1: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
/*--- Reading and writing a line ---------------------------------*/
while (1) {
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_c1st_1line(): putchar() #2: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
}
}
/*=== Read and write only one line (for e-option and 1st line ) ======
* [in] giHold : (must be defined as a global variable)
* giNextchar : (must be defined as a global variable)
* [ret] 0 : Finished reading/writing due to '\n'
* 1 : Finished reading/writing due to '\n',
* which is the last char of the file
* EOF : Finished reading/writing due to EOF */
int read_e1st_1line(FILE *fp) {
/*--- Variables --------------------------------------------------*/
tmsp tsNow ;
struct tm *ptm ;
char szBuf[LINE_BUF];
int iChar ;
/*--- Reading and writing a line (1st letter of the line) --------*/
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
if (clock_gettime(CLOCK_REALTIME,&tsNow) != 0) {
error_exit(errno,"read_e1st_1line(): clock_gettime(): %s\n",
strerror(errno) );
}
if ((ptm=localtime(&tsNow.tv_sec)) == NULL) {
error_exit(255,"read_e1st_1line(): localtime(): returned NULL\n");
}
strftime(szBuf, LINE_BUF, "%s", ptm);
printf("%s", szBuf);
switch (giTimeResol) {
case 0 : putchar(' ') ; break;
case 3 : printf(".%03d " , (int)(tsNow.tv_nsec+500000)/1000000); break;
case 6 : printf(".%06d " , (int)(tsNow.tv_nsec+ 500)/ 1000); break;
case 9 : printf(".%09ld ", tsNow.tv_nsec ); break;
default: error_exit(255,"read_e1st_1line(): Unknown resolution\n");
}
if (giDeltaMode) {
printf("0 ");
gtsPrev.tv_sec=tsNow.tv_sec; gtsPrev.tv_nsec=tsNow.tv_nsec;
}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_e1st_1line(): putchar() #1: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
/*--- Reading and writing a line ---------------------------------*/
while (1) {
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_e1st_1line(): putchar() #2: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
}
}
/*=== Read and write only one line (for Z-option and 1st line ) ======
* [in] giHold : (must be defined as a global variable)
* giNextchar : (must be defined as a global variable)
* [ret] 0 : Finished reading/writing due to '\n'
* 1 : Finished reading/writing due to '\n',
* which is the last char of the file
* EOF : Finished reading/writing due to EOF */
int read_Z1st_1line(FILE *fp) {
/*--- Variables --------------------------------------------------*/
int iChar;
/*--- Reading and writing a line (1st letter of the line) --------*/
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
if (clock_gettime(CLOCK_REALTIME,>sZero) != 0) {
error_exit(errno,"read_Z1st_1line(): clock_gettime(): %s\n",
strerror(errno) );
}
if (giDeltaMode) {
printf("0 0 ");
gtsPrev.tv_sec=gtsZero.tv_sec; gtsPrev.tv_nsec=gtsZero.tv_nsec;
} else {
printf("0 ");
}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_Z1st_1line(): putchar() #1: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
/*--- Reading and writing a line ---------------------------------*/
while (1) {
if (giHold) {iChar=giNextchar; giHold=0;} else {iChar=getc(fp);}
if (iChar == EOF) {return(EOF);}
while (putchar(iChar)==EOF) {
if (errno == EINTR) {continue;}
error_exit(errno,"read_Z1st_1line(): putchar() #2: %s\n",strerror(errno));
}
if (iChar == '\n') {
giNextchar = getc(fp);
if (giNextchar!=EOF) {giHold=1;return 0;}
else { return 1;}
}
}
}
/*=== Write the current timestamp to stdout ==========================
* [in] giFmtType : (must be defined as a global variable)
* giTimeResol : (must be defined as a global variable)
* giDeltaMode : (must be defined as a global variable)
* gtsZero : (must be defined as a global variable)
* gtsPrev : (must be defined as a global variable) */
void print_cur_timestamp(void) {
/*--- Variables --------------------------------------------------*/
tmsp tsNow ; /* Current time but substructed by gtsZero */
tmsp tsDiff ;
tmsp ts ;
struct tm *ptm ;
char szBuf[LINE_BUF];
char szDec[LINE_BUF]; /* for the Decimal part */
/*--- Get the current time ---------------------------------------*/
if (clock_gettime(CLOCK_REALTIME,&tsNow) != 0) {
error_exit(errno,"clock_gettime()#1: %s\n",strerror(errno));
}
/*--- Print the current timestamp --------------------------------*/
switch (giFmtType) {
case 'c':
ts.tv_sec=tsNow.tv_sec; ts.tv_nsec=tsNow.tv_nsec;
switch (giTimeResol) {
case 0 : if (ts.tv_nsec>=500000000L) {ts.tv_sec++;ts.tv_nsec=0;}
szDec[0]=0;
break;
case 3 : if (ts.tv_nsec>=999500000L) {ts.tv_sec++;ts.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%03ld",ts.tv_nsec/1000000);
break;
case 6 : if (ts.tv_nsec>=999999500L) {ts.tv_sec++;ts.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%06ld",ts.tv_nsec/ 1000);
break;
default: snprintf(szDec,LINE_BUF,".%09ld",ts.tv_nsec );
break;
}
ptm = localtime(&ts.tv_sec);
if (ptm==NULL) {error_exit(255,"localtime(): returned NULL\n");}
printf("%04d%02d%02d%02d%02d%02d%s ",
ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
ptm->tm_hour , ptm->tm_min , ptm->tm_sec , szDec);
break;
case 'e':
ts.tv_sec=tsNow.tv_sec; ts.tv_nsec=tsNow.tv_nsec;
switch (giTimeResol) {
case 0 : if (ts.tv_nsec>=500000000L) {ts.tv_sec++;ts.tv_nsec=0;}
szDec[0]=0;
break;
case 3 : if (ts.tv_nsec>=999500000L) {ts.tv_sec++;ts.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%03ld",ts.tv_nsec/1000000);
break;
case 6 : if (ts.tv_nsec>=999999500L) {ts.tv_sec++;ts.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%06ld",ts.tv_nsec/ 1000);
break;
default: snprintf(szDec,LINE_BUF,".%09ld",ts.tv_nsec );
break;
}
ptm = localtime(&ts.tv_sec);
strftime(szBuf, LINE_BUF, "%s", ptm);
printf("%s%s ", szBuf, szDec);
break;
case 'z':
if ((tsNow.tv_nsec - gtsZero.tv_nsec) < 0) {
ts.tv_sec = tsNow.tv_sec - gtsZero.tv_sec - 1;
ts.tv_nsec = tsNow.tv_nsec - gtsZero.tv_nsec + 1000000000;
} else {
ts.tv_sec = tsNow.tv_sec - gtsZero.tv_sec ;
ts.tv_nsec = tsNow.tv_nsec - gtsZero.tv_nsec;
}
switch (giTimeResol) {
case 0 : if (ts.tv_nsec>=500000000L) {ts.tv_sec++;ts.tv_nsec=0;}
szDec[0]=0;
break;
case 3 : if (ts.tv_nsec>=999500000L) {ts.tv_sec++;ts.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%03ld",ts.tv_nsec/1000000);
break;
case 6 : if (ts.tv_nsec>=999999500L) {ts.tv_sec++;ts.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%06ld",ts.tv_nsec/ 1000);
break;
default: snprintf(szDec,LINE_BUF,".%09ld",ts.tv_nsec );
break;
}
ptm = localtime(&ts.tv_sec);
strftime(szBuf, LINE_BUF, "%s", ptm);
printf("%s%s ", szBuf, szDec);
break;
default : error_exit(255,"print_cur_timestamp(): Unknown format\n");
}
/*--- Print the delta-t if required ------------------------------*/
if (giDeltaMode) {
if ((tsNow.tv_nsec - gtsPrev.tv_nsec) < 0) {
tsDiff.tv_sec = tsNow.tv_sec - gtsPrev.tv_sec - 1;
tsDiff.tv_nsec = tsNow.tv_nsec - gtsPrev.tv_nsec + 1000000000;
} else {
tsDiff.tv_sec = tsNow.tv_sec - gtsPrev.tv_sec ;
tsDiff.tv_nsec = tsNow.tv_nsec - gtsPrev.tv_nsec;
}
gtsPrev.tv_sec=tsNow.tv_sec; gtsPrev.tv_nsec=tsNow.tv_nsec;
switch (giTimeResol) {
case 0 : if(tsDiff.tv_nsec>=500000000L){tsDiff.tv_sec++;tsDiff.tv_nsec=0;}
szDec[0]=0;
break;
case 3 : if(tsDiff.tv_nsec>=999500000L){tsDiff.tv_sec++;tsDiff.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%03ld",tsDiff.tv_nsec/1000000);
break;
case 6 : if(tsDiff.tv_nsec>=999999500L){tsDiff.tv_sec++;tsDiff.tv_nsec=0;}
snprintf(szDec,LINE_BUF,".%06ld",tsDiff.tv_nsec/ 1000);
break;
default: snprintf(szDec,LINE_BUF,".%09ld",tsDiff.tv_nsec );
break;
}
ptm = localtime(&tsDiff.tv_sec);
strftime(szBuf, LINE_BUF, "%s", ptm);
printf("%s%s ", szBuf, szDec);
}
/*--- Finish -----------------------------------------------------*/
return;
}