File: dround.c

package info (click to toggle)
dateutils 0.4.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,072 kB
  • sloc: ansic: 22,115; makefile: 1,686; yacc: 202; sh: 168; lex: 108
file content (880 lines) | stat: -rw-r--r-- 19,107 bytes parent folder | download
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
/*** dround.c -- perform simple date arithmetic, round to duration
 *
 * Copyright (C) 2012-2018 Sebastian Freundt
 *
 * Author:  Sebastian Freundt <freundt@ga-group.nl>
 *
 * This file is part of dateutils.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the author nor the names of any contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 **/
#if defined HAVE_CONFIG_H
# include "config.h"
#endif	/* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>

#include "dt-core.h"
#include "dt-io.h"
#include "dt-core-tz-glue.h"
#include "dt-locale.h"
#include "prchunk.h"
/* parsers and formatters */
#include "date-core-strpf.h"
#include "date-core-private.h"

#if !defined assert
# define assert(x)
#endif	/* !assert */

const char *prog = "dround";


static struct dt_t_s
tround_tdur_cocl(struct dt_t_s t, struct dt_dtdur_s dur, bool nextp)
{
/* this will return the rounded to DUR time of T and, to encode carry
 * (which can only take values 0 or 1), we will use t's neg bit */
	unsigned int tunp;
	signed int sdur;
	bool downp = false;

	/* we won't have carries, ever */
	t.carry = 0;

	/* get directions, no dur is a no-op */
	if (UNLIKELY(!(sdur = dur.dv))) {
		/* IPO/LTO hack */
		goto out;
	} else if (sdur < 0) {
		downp = true;
		sdur = -sdur;
	} else if (dur.neg) {
		downp = true;
	}

	switch (dur.durtyp) {
	case DT_DURH:
		sdur *= MINS_PER_HOUR;
		/*@fallthrough@*/
	case DT_DURM:
		sdur *= SECS_PER_MIN;
		/*@fallthrough@*/
	case DT_DURS:
		/* only accept values whose remainder is 0 */
		if (LIKELY(!(SECS_PER_DAY % (unsigned int)sdur))) {
			break;
		}
		/*@fallthrough@*/
	default:
		/* IPO/LTO hack */
		goto out;
	}
	/* unpack t */
	tunp = (t.hms.h * MINS_PER_HOUR + t.hms.m) * SECS_PER_MIN + t.hms.s;
	with (unsigned int diff = tunp % (unsigned int)sdur) {
		if (!diff && !nextp) {
			/* do nothing, i.e. really nothing,
			 * in particular, don't set the slots again in the
			 * assign section
			 * this is not some obscure optimisation but to
			 * support special notations like, military midnight
			 * or leap seconds */
			goto out;
		} else if (!downp) {
			tunp += sdur - diff;
		} else if (!diff/* && downp && nextp*/) {
			tunp -= sdur;
		} else {
			tunp -= diff;
		}
	}

	/* assign */
	t.hms.ns = 0;
	t.hms.s = tunp % SECS_PER_MIN;
	tunp /= SECS_PER_MIN;
	t.hms.m = tunp % MINS_PER_HOUR;
	tunp /= MINS_PER_HOUR;
	t.hms.h = tunp % HOURS_PER_DAY;
out:
	return t;
}

static struct dt_t_s
tround_tdur(struct dt_t_s t, struct dt_dtdur_s dur, bool nextp)
{
/* this will return the rounded to DUR time of T and, to encode carry
 * (which can only take values 0 or 1), we will use t's neg bit */
	bool downp = false;
	signed int dv;

	/* initialise carry */
	t.carry = 0;
	/* get directions */
	if ((dv = dur.dv) < 0) {
		downp = true;
		dv = -dv;
	} else if (dur.neg) {
		downp = true;
	}

	switch (dur.durtyp) {
	case DT_DURH:
		if ((!downp && t.hms.h < dv) ||
		    (downp && t.hms.h > dv)) {
			/* no carry adjustments */
			t.hms.h = dv;
		} else if (t.hms.h == dv && !nextp) {
			/* we're on the hour in question */
			t.hms.h = dv;
		} else if (!downp) {
			t.hms.h = dv;
		hour_oflo:
			t.carry = 1;
		} else {
			t.hms.h = dv;
		hour_uflo:
			t.carry = -1;
		}
		break;
	case DT_DURM:
		if ((!downp && t.hms.m < dv) ||
		    (downp && t.hms.m > dv)) {
			/* no carry adjustments */
			t.hms.m = dv;
		} else if (t.hms.m == dv && !nextp) {
			/* we're on the hour in question */
			t.hms.m = dv;
		} else if (!downp) {
			t.hms.m = dv;
		min_oflo:
			if (UNLIKELY(++t.hms.h >= HOURS_PER_DAY)) {
				t.hms.h = 0;
				goto hour_oflo;
			}
		} else {
			t.hms.m = dv;
		min_uflo:
			if (UNLIKELY(!t.hms.h--)) {
				t.hms.h = HOURS_PER_DAY - 1;
				goto hour_uflo;
			}
		}
		break;
	case DT_DURS:
		if ((!downp && t.hms.s < dv) ||
		    (downp && t.hms.s > dv)) {
			/* no carry adjustments */
			t.hms.s = dv;
		} else if (t.hms.s == dv && !nextp) {
			/* we're on the hour in question */
			t.hms.s = dv;
		} else if (!downp) {
			t.hms.s = dv;
			if (UNLIKELY(++t.hms.m >= MINS_PER_HOUR)) {
				t.hms.m = 0;
				goto min_oflo;
			}
		} else {
			t.hms.s = dv;
			if (UNLIKELY(!t.hms.m--)) {
				t.hms.m = MINS_PER_HOUR - 1;
				goto min_uflo;
			}
		}
		break;
	default:
		break;
	}
	return t;
}

static struct dt_d_s
dround_ddur(struct dt_d_s d, struct dt_ddur_s dur, bool nextp)
{
	switch (dur.durtyp) {
		unsigned int tgt;
		bool forw;
	case DT_DURD:
		if (dur.dv > 0) {
			tgt = dur.dv;
			forw = true;
		} else if (dur.dv < 0) {
			tgt = -dur.dv;
			forw = false;
		} else {
			/* user is an idiot */
			break;
		}

		switch (d.typ) {
			unsigned int mdays;
		case DT_YMD:
			if ((forw && d.ymd.d < tgt) ||
			    (!forw && d.ymd.d > tgt)) {
				/* no month or year adjustment */
				;
			} else if (d.ymd.d == tgt && !nextp) {
				/* we're ON the date already and no
				 * next/prev date is requested */
				;
			} else if (forw) {
				if (LIKELY(d.ymd.m < GREG_MONTHS_P_YEAR)) {
					d.ymd.m++;
				} else {
					d.ymd.m = 1;
					d.ymd.y++;
				}
			} else {
				if (UNLIKELY(--d.ymd.m < 1)) {
					d.ymd.m = GREG_MONTHS_P_YEAR;
					d.ymd.y--;
				}
			}
			/* get ultimo */
			mdays = __get_mdays(d.ymd.y, d.ymd.m);
			if (UNLIKELY(tgt > mdays)) {
				tgt = mdays;
			}
			/* final assignment */
			d.ymd.d = tgt;
			break;
		default:
			break;
		}
		break;

	case DT_DURBD:
		/* bizsis only work on bizsidurs atm */
		if (dur.dv > 0) {
			tgt = dur.dv;
			forw = true;
		} else if (dur.dv < 0) {
			tgt = -dur.dv;
			forw = false;
		} else {
			/* user is an idiot */
			break;
		}

		switch (d.typ) {
			unsigned int bdays;
		case DT_BIZDA:
			if ((forw && d.bizda.bd < tgt) ||
			    (!forw && d.bizda.bd > tgt)) {
				/* no month or year adjustment */
				;
			} else if (d.bizda.bd == tgt && !nextp) {
				/* we're ON the date already and no
				 * next/prev date is requested */
				;
			} else if (forw) {
				if (LIKELY(d.bizda.m < GREG_MONTHS_P_YEAR)) {
					d.bizda.m++;
				} else {
					d.bizda.m = 1;
					d.bizda.y++;
				}
			} else {
				if (UNLIKELY(--d.bizda.m < 1)) {
					d.bizda.m = GREG_MONTHS_P_YEAR;
					d.bizda.y--;
				}
			}
			/* get ultimo */
			bdays = __get_bdays(d.bizda.y, d.bizda.m);
			if (UNLIKELY(tgt > bdays)) {
				tgt = bdays;
			}
			/* final assignment */
			d.bizda.bd = tgt;
			break;
		default:
			break;
		}
		break;

	case DT_DURYMD:
		switch (d.typ) {
			unsigned int mdays;
		case DT_YMD:
			tgt = dur.ymd.m;
			forw = !dt_dur_neg_p(dur);

			if ((forw && d.ymd.m < tgt) ||
			    (!forw && d.ymd.m > tgt)) {
				/* no year adjustment */
				;
			} else if (d.ymd.m == tgt && !nextp) {
				/* we're IN the month already and no
				 * next/prev date is requested */
				;
			} else if (forw) {
				/* years don't wrap around */
				d.ymd.y++;
			} else {
				/* years don't wrap around */
				d.ymd.y--;
			}
			/* final assignment */
			d.ymd.m = tgt;
			/* fixup ultimo mismatches */
			mdays = __get_mdays(d.ymd.y, d.ymd.m);
			if (UNLIKELY(d.ymd.d > mdays)) {
				d.ymd.d = mdays;
			}
			break;
		default:
			break;
		}
		break;

	case DT_DURYMCW: {
		struct dt_d_s tmp;
		unsigned int wday;
		signed int diff;

		forw = !dt_dur_neg_p(dur);
		tgt = dur.ymcw.w;

		tmp = dt_dconv(DT_DAISY, d);
		wday = dt_get_wday(tmp);
		diff = (signed)tgt - (signed)wday;


		if ((forw && wday < tgt) ||
		    (!forw && wday > tgt)) {
			/* nothing to do */
			;
		} else if (wday == tgt && !nextp) {
			/* we're on WDAY already, do fuckall */
			;
		} else if (forw) {
			/* week wrap */
			diff += 7;
		} else {
			/* week wrap */
			diff -= 7;
		}

		/* final assignment */
		tmp.daisy += diff;
		d = dt_dconv(d.typ, tmp);
		break;
	}

	case DT_DURWK:
		if (dur.dv > 0) {
			tgt = dur.dv;
			forw = true;
		} else if (dur.dv < 0) {
			tgt = -dur.dv;
			forw = false;
		} else {
			/* user is an idiot */
			break;
		}

		switch (d.typ) {
			unsigned int nw;
		case DT_YWD:
			if ((forw && d.ywd.c < tgt) ||
			    (!forw && d.ywd.c > tgt)) {
				/* no year adjustment */
				;
			} else if (d.ywd.c == tgt && !nextp) {
				/* we're IN the week already and no
				 * next/prev date is requested */
				;
			} else if (forw) {
				/* years don't wrap around */
				d.ywd.y++;
			} else {
				/* years don't wrap around */
				d.ywd.y--;
			}
			/* final assignment */
			d.ywd.c = tgt;
			/* fixup ultimo mismatches */
			nw = __get_isowk(d.ywd.y);
			if (UNLIKELY(d.ywd.c > nw)) {
				d.ywd.c = nw;
			}
			break;
		default:
			break;
		}
		break;

	default:
		break;
	}
	return d;
}

static dt_sexy_t
sxround_dur_cocl(dt_sexy_t t, struct dt_dtdur_s dur, bool nextp)
{
/* this will return the rounded to DUR time of T and, to encode carry
 * (which can only take values 0 or 1), we will use t's neg bit */
	dt_ssexy_t sdur;
	bool downp = false;

	/* get directions, no dur is a no-op */
	if (UNLIKELY(!(sdur = dur.dv))) {
		return t;
	} else if (sdur < 0) {
		downp = true;
		sdur = -sdur;
	} else if (dur.neg) {
		downp = true;
	}

	switch (dur.durtyp) {
	case DT_DURH:
		sdur *= MINS_PER_HOUR;
		/*@fallthrough@*/
	case DT_DURM:
		sdur *= SECS_PER_MIN;
		/*@fallthrough@*/
	case DT_DURS:
		/* only accept values whose remainder is 0 */
		if (LIKELY(!(SECS_PER_DAY % (unsigned int)sdur))) {
			break;
		}
		/*@fallthrough@*/
	default:
		return t;
	}
	/* unpack t */
	with (unsigned int diff = t % (dt_sexy_t)sdur) {
		if (!diff && !nextp) {
			/* do nothing, i.e. really nothing,
			 * in particular, don't set the slots again in the
			 * assign section
			 * this is not some obscure optimisation but to
			 * support special notations like, military midnight
			 * or leap seconds */
			return t;
		} else if (!downp) {
			t += sdur - diff;
		} else if (!diff/* && downp && nextp*/) {
			t -= sdur;
		} else {
			t -= diff;
		}
	}
	return t;
}


static struct dt_dt_s
dt_round(struct dt_dt_s d, struct dt_dtdur_s dur, bool nextp)
{
	switch (d.typ) {
	default:
		switch (dur.durtyp) {
		default:
			/* all the date durs */
			break;

		case DT_DURH:
		case DT_DURM:
		case DT_DURS:
		case DT_DURNANO:
			if (UNLIKELY(dt_sandwich_only_d_p(d))) {
				/* this doesn't make sense */
				break;
			}
			if (!dur.cocl) {
				d.t = tround_tdur(d.t, dur, nextp);
			} else {
				d.t = tround_tdur_cocl(d.t, dur, nextp);
			}
			break;
		}
		/* check carry */
		if (UNLIKELY(d.t.carry)) {
			/* we need to add a day */
			struct dt_ddur_s one_day =
				dt_make_ddur(DT_DURD, d.t.carry);
			d.t.carry = 0;
			d.d = dt_dadd(d.d, one_day);
		}
		with (unsigned int sw = d.sandwich) {
			d.d = dround_ddur(d.d, dur.d, nextp);
			d.sandwich = (uint8_t)sw;
		}
		break;
	case DT_SEXY:
	case DT_SEXYTAI:
		if (UNLIKELY(!dur.cocl)) {
			error("Error: \
Epoch date/times have no divisions to round to.");
			break;
		}
		/* just keep it sexy */
		d.sexy = sxround_dur_cocl(d.sexy, dur, nextp);
		break;
	}
	return d;
}


static struct dt_dt_s
dround(struct dt_dt_s d, struct dt_dtdur_s dur[], size_t ndur, bool nextp)
{
	for (size_t i = 0; i < ndur; i++) {
		d = dt_round(d, dur[i], nextp);
	}
	return d;
}

/* extended duration reader */
static int
dt_io_strpdtrnd(struct __strpdtdur_st_s *st, const char *str)
{
	char *sp = NULL;
	struct strpd_s d = strpd_initialiser();
	struct dt_spec_s s = spec_initialiser();
	struct dt_dtdur_s payload = {(dt_dtdurtyp_t)DT_DURUNK};
	bool negp = false;
	bool coclp = true;

	if (dt_io_strpdtdur(st, str) >= 0) {
		return 0;
	}

	/* check for co-classes */
	if (*str == '/') {
		coclp = true;
		str++;
	}
	/* check if there's a sign + or - */
	if (*str == '-') {
		negp = true;
		str++;
	} else if (*str == '+') {
		str++;
	}

	/* try weekdays, set up s */
	s.spfl = DT_SPFL_S_WDAY;
	s.abbr = DT_SPMOD_NORM;
	if (__strpd_card(&d, str, s, &sp) >= 0) {
#if defined HAVE_ANON_STRUCTS_INIT
		payload.d = (struct dt_ddur_s){
			DT_DURYMCW,
			.neg = negp,
			.cocl = coclp,
			.ymcw.w = d.w,
		};
#else
		payload.d.durtyp = DT_DURYMCW;
		payload.d.neg = negp;
		payload.d.cocl = coclp;
		payload.d.ymcw.w = d.w;
#endif
		goto out;
	}

	/* try months, set up s */
	s.spfl = DT_SPFL_S_MON;
	s.abbr = DT_SPMOD_NORM;
	if (__strpd_card(&d, str, s, &sp) >= 0) {
#if defined HAVE_ANON_STRUCTS_INIT
		payload.d = (struct dt_ddur_s){
			DT_DURYMD,
			.neg = negp,
			.ymd.m = d.m,
		};
#else
		payload.d.durtyp = DT_DURYMD;
		payload.d.neg = negp;
		payload.d.ymd.m = d.m;
#endif
		goto out;
	}

	/* bugger */
	st->istr = str;
	return -1;
out:
	st->sign = 0;
	st->cont = NULL;
	return __add_dur(st, payload);
}

struct prln_ctx_s {
	struct grep_atom_soa_s *ndl;
	const char *ofmt;
	zif_t fromz;
	zif_t outz;
	int sed_mode_p;
	int quietp;

	const struct __strpdtdur_st_s *st;
	bool nextp;
};

static int
proc_line(struct prln_ctx_s ctx, char *line, size_t llen)
{
	struct dt_dt_s d;
	char *sp = NULL;
	char *ep = NULL;
	int rc = 0;

	do {
		/* check if line matches, */
		d = dt_io_find_strpdt2(
			line, llen, ctx.ndl, &sp, &ep, ctx.fromz);

		if (!dt_unk_p(d)) {
			if (UNLIKELY(d.fix) && !ctx.quietp) {
				rc = 2;
			}
			/* perform addition now */
			d = dround(d, ctx.st->durs, ctx.st->ndurs, ctx.nextp);

			if (ctx.fromz != NULL) {
				/* fixup zone */
				d = dtz_forgetz(d, ctx.fromz);
			}

			if (ctx.sed_mode_p) {
				__io_write(line, sp - line, stdout);
				dt_io_write(d, ctx.ofmt, ctx.outz, '\0');
				llen -= (ep - line);
				line = ep;
			} else {
				dt_io_write(d, ctx.ofmt, ctx.outz, '\n');
				break;
			}
		} else if (ctx.sed_mode_p) {
			line[llen] = '\n';
			__io_write(line, llen + 1, stdout);
			break;
		} else {
			/* obviously unmatched, warn about it in non -q mode */
			if (!ctx.quietp) {
				dt_io_warn_strpdt(line);
				rc = 2;
			}
			break;
		}
	} while (1);
	return rc;
}


#include "dround.yucc"

int
main(int argc, char *argv[])
{
	yuck_t argi[1U];
	struct dt_dt_s d;
	struct __strpdtdur_st_s st = __strpdtdur_st_initialiser();
	char *inp;
	const char *ofmt;
	char **fmt;
	size_t nfmt;
	int rc = 0;
	bool dt_given_p = false;
	bool nextp = false;
	zif_t fromz = NULL;
	zif_t z = NULL;

	if (yuck_parse(argi, argc, argv)) {
		rc = 1;
		goto out;
	} else if (argi->nargs == 0U) {
		error("Error: DATE or DURATION must be specified\n");
		yuck_auto_help(argi);
		rc = 1;
		goto out;
	}
	/* init and unescape sequences, maybe */
	ofmt = argi->format_arg;
	fmt = argi->input_format_args;
	nfmt = argi->input_format_nargs;
	if (argi->backslash_escapes_flag) {
		dt_io_unescape(argi->format_arg);
		for (size_t i = 0; i < nfmt; i++) {
			dt_io_unescape(fmt[i]);
		}
	}

	if (argi->from_locale_arg) {
		setilocale(argi->from_locale_arg);
	}
	if (argi->locale_arg) {
		setflocale(argi->locale_arg);
	}

	/* try and read the from and to time zones */
	if (argi->from_zone_arg) {
		fromz = dt_io_zone(argi->from_zone_arg);
	}
	if (argi->zone_arg) {
		z = dt_io_zone(argi->zone_arg);
	}
	if (argi->next_flag) {
		nextp = true;
	}
	if (argi->base_arg) {
		struct dt_dt_s base = dt_strpdt(argi->base_arg, NULL, NULL);
		dt_set_base(base);
	}

	/* check first arg, if it's a date the rest of the arguments are
	 * durations, if not, dates must be read from stdin */
	dt_given_p = !dt_unk_p(d = dt_io_strpdt(*argi->args, fmt, nfmt, NULL));
	for (size_t i = dt_given_p; i < argi->nargs; i++) {
		inp = argi->args[i];
		do {
			if (dt_io_strpdtrnd(&st, inp) < 0) {
				if (UNLIKELY(i == 0)) {
					/* that's ok, must be a date then */
					dt_given_p = true;
				} else {
					serror("Error: \
cannot parse duration/rounding string `%s'", st.istr);
				}
			}
		} while (__strpdtdur_more_p(&st));
	}

	/* sanity checks */
	if (dt_given_p) {
		/* date parsing needed postponing as we need to find out
		 * about the durations
		 * Also from-zone conversion needs postponing as we define
		 * the conversion to take place on the rounded date */
		inp = argi->args[0U];
		if (dt_unk_p(d = dt_io_strpdt(inp, fmt, nfmt, NULL))) {
			error("Error: \
cannot interpret date/time string `%s'", argi->args[0U]);
			rc = 1;
			goto out;
		}
	} else if (st.ndurs == 0) {
		error("Error: \
no durations given");
		rc = 1;
		goto out;
	}

	/* start the actual work */
	if (dt_given_p) {
		if (UNLIKELY(d.fix) && !argi->quiet_flag) {
			rc = 2;
		}
		if (!dt_unk_p(d = dround(d, st.durs, st.ndurs, nextp))) {
			if (fromz != NULL) {
				/* fixup zone */
				d = dtz_forgetz(d, fromz);
			}
			dt_io_write(d, ofmt, z, '\n');
		} else {
			rc = 1;
		}
	} else {
		/* read from stdin */
		size_t lno = 0;
		struct grep_atom_s __nstk[16], *needle = __nstk;
		size_t nneedle = countof(__nstk);
		struct grep_atom_soa_s ndlsoa;
		void *pctx;
		struct prln_ctx_s prln = {
			.ndl = &ndlsoa,
			.ofmt = ofmt,
			.fromz = fromz,
			.outz = z,
			.sed_mode_p = argi->sed_mode_flag,
			.quietp = argi->quiet_flag,
			.st = &st,
			.nextp = nextp,
		};

		/* no threads reading this stream */
		__io_setlocking_bycaller(stdout);

		/* lest we overflow the stack */
		if (nfmt >= nneedle) {
			/* round to the nearest 8-multiple */
			nneedle = (nfmt | 7) + 1;
			needle = calloc(nneedle, sizeof(*needle));
		}
		/* and now build the needle */
		ndlsoa = build_needle(needle, nneedle, fmt, nfmt);


		/* using the prchunk reader now */
		if ((pctx = init_prchunk(STDIN_FILENO)) == NULL) {
			serror("Error: could not open stdin");
			goto ndl_free;
		}
		while (prchunk_fill(pctx) >= 0) {
			for (char *line; prchunk_haslinep(pctx); lno++) {
				size_t llen = prchunk_getline(pctx, &line);

				rc |= proc_line(prln, line, llen);
			}
		}
		/* get rid of resources */
		free_prchunk(pctx);
	ndl_free:
		if (needle != __nstk) {
			free(needle);
		}
		goto out;
	}
	/* free the strpdur status */
	__strpdtdur_free(&st);

	dt_io_clear_zones();
	if (argi->from_locale_arg) {
		setilocale(NULL);
	}
	if (argi->locale_arg) {
		setflocale(NULL);
	}

out:
	yuck_free(argi);
	return rc;
}

/* dround.c ends here */