File: date-core-strpf.c

package info (click to toggle)
dateutils 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,924 kB
  • sloc: ansic: 22,097; makefile: 1,666; yacc: 198; sh: 168; lex: 108
file content (796 lines) | stat: -rw-r--r-- 17,082 bytes parent folder | download | duplicates (2)
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
/*** date-core-strpf.c -- parser and formatter funs for date-core
 *
 * Copyright (C) 2011-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.
 *
 **/
/* implementation part of date-core-strpf.h */
#if !defined INCLUDED_date_core_strpf_c_
#define INCLUDED_date_core_strpf_c_

#if defined HAVE_CONFIG_H
# include "config.h"
#endif	/* HAVE_CONFIG_H */
#include "strops.h"
#include "token.h"
#include "date-core.h"
#include "date-core-strpf.h"
#include "dt-locale.h"

#if defined __INTEL_COMPILER
/* we MUST return a char* */
# pragma warning (disable:2203)
#elif defined __GNUC__
# pragma GCC diagnostic ignored "-Wcast-qual"
#endif	/* __INTEL_COMPILER */

#if !defined DEFUN
# define DEFUN
#endif	/* !DEFUN */
#if !defined DEFVAR
# define DEFVAR
#endif	/* !DEFVAR */


DEFUN struct dt_d_s
__strpd_std(const char *str, char **ep)
{
/* code dupe, see __strpdt_std() */
	struct dt_d_s res = dt_d_initialiser();
	struct strpd_s d;
	const char *sp;

	if ((sp = str) == NULL) {
		goto out;
	}

	d = strpd_initialiser();
	/* read the year */
	d.y = strtoi(sp, &sp);
	if (d.y < DT_MIN_YEAR || d.y > DT_MAX_YEAR || *sp++ != '-') {
		goto fucked;
	}
	/* check for ywd dates */
	if (UNLIKELY(*sp == 'W')) {
		/* brilliant */
		sp++, d.c = strtoi(sp, &sp);
		if (d.c < 0 || d.c > 53 || *sp++ != '-') {
			goto fucked;
		}
		d.flags.c_wcnt_p = 1;
		d.flags.wk_cnt = YWD_ISOWK_CNT;
		goto dow;
	}
	/* read the month, then day count */
	with (const char *tmp) {
		d.m = strtoi(sp, &tmp);
		if (d.m < 0 || d.m > 366) {
			goto fucked;
		} else if (UNLIKELY(*tmp != '-')) {
			/* oh, could be an ordinal date */
			if (tmp - sp >= 3U) {
				d.d = d.m;
				d.m = 0U;
				d.flags.d_dcnt_p = 1U;
			} else if ((unsigned int)d.m <= GREG_MONTHS_P_YEAR) {
				;
			} else {
				goto fucked;
			}
			sp = tmp;
		} else if (d.d = strtoi(++tmp, &sp), d.d < 0 || d.d > 31) {
			/* didn't work, fuck off */
			goto fucked;
		}
	}
	/* check the date type */
	switch (*sp) {
	case '-':
		/* it is a YMCW date */
		if ((d.c = d.d) > 5) {
			/* nope, it was bollocks */
			break;
		}
		d.d = 0;
		sp++;
	dow:
		if (d.w = strtoi(sp, &sp),
		    d.w < 0 || (unsigned int)d.w > GREG_DAYS_P_WEEK) {
			/* didn't work, fuck off */
			goto fucked;
		}
		/* fix up d.w right away */
		d.w = d.w ?: DT_SUNDAY;
		break;
	case 'B':
		/* it's a bizda/YMDU before ultimo date */
		d.flags.ab = BIZDA_BEFORE;
	case 'b':
		/* it's a bizda/YMDU after ultimo date */
		d.flags.bizda = 1;
		d.b = d.d;
		d.d = 0;
		sp++;
		break;
	default:
		/* we don't care */
		break;
	}
	/* guess what we're doing */
	res = __guess_dtyp(d);
out:
	if (ep != NULL) {
		*ep = (char*)sp;
	}
	return res;
fucked:
	if (ep != NULL) {
		*ep = (char*)str;
	}
	return dt_d_initialiser();
}

DEFUN int
__strpd_card(struct strpd_s *d, const char *sp, struct dt_spec_s s, char **ep)
{
	/* we're really pessimistic, aren't we? */
	int res = -1;

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		break;
	case DT_SPFL_N_DSTD:
		d->y = strtoi_lim(sp, &sp, DT_MIN_YEAR, DT_MAX_YEAR);
		sp++;
		d->m = strtoi_lim(sp, &sp, 0, GREG_MONTHS_P_YEAR);
		sp++;
		d->d = strtoi_lim(sp, &sp, 0, 31);
		res = 0 - (d->y < 0 || d->m < 0 || d->d < 0);
		break;
	case DT_SPFL_N_YEAR:
		switch (s.abbr) {
		case DT_SPMOD_LONG:
			d->y = strtoi_lim(sp, &sp, DT_MIN_YEAR, DT_MAX_YEAR);
			break;
		case DT_SPMOD_NORM:
			d->y = strtoi_lim(sp, &sp, 0, 99);
			if (UNLIKELY(d->y < 0)) {
				break;
			}
#if defined LIBDUT
			with (struct dt_d_s b = dt_get_dbase()) {
				unsigned int by = b.ymd.y - 50U;
				int c = by / 100U;
				int y = by % 100U;

				if (d->y < y) {
					c++;
				}
				d->y += c * 100;
			}
#else  /* !LIBDUT */
			/* hardcode base as 2015-01-01 */
			if ((d->y += 2000) > 2065) {
				d->y -= 100;
			}
#endif	/* LIBDUT */
			break;
		case DT_SPMOD_ABBR:
			if (UNLIKELY((unsigned char)(*sp ^ '0') >= 10)) {
				d->y = -1;
				break;
			}
			d->y = (*sp++ ^ '0');
#if defined LIBDUT
			with (struct dt_d_s b = dt_get_dbase()) {
				unsigned int by = b.ymd.y;
				int c = by / 10U;
				int y = by % 10U;

				if (d->y < y) {
					c++;
				}
				d->y += c * 10;
			}
#else  /* !LIBDUT */
			/* hardcode base as 2015-01-01 */
			if ((d->y += 2010) < 2015) {
				d->y += 10;
			}
#endif	/* LIBDUT */
			break;
		}
		res = 0 - (d->y < 0);
		break;
	case DT_SPFL_N_MON:
		d->m = strtoi_lim(sp, &sp, 0, GREG_MONTHS_P_YEAR);
		res = 0 - (d->m < 0);
		break;
	case DT_SPFL_N_DCNT_MON:
		/* ymd mode? */
		if (LIKELY(!s.bizda)) {
			d->d = padstrtoi_lim(sp, &sp, 0, 31);
			res = 0 - (d->d < 0);
		} else {
			d->b = strtoi_lim(sp, &sp, 0, 23);
			res = 0 - (d->b < 0);
		}
		break;
	case DT_SPFL_N_DCNT_WEEK:
		/* ymcw mode? */
		d->w = strtoi_lim(sp, &sp, 0, GREG_DAYS_P_WEEK);
		/* fix up d->w right away */
		d->w = d->w ?: DT_SUNDAY;
		res = 0 - (d->w < 0);
		break;
	case DT_SPFL_N_WCNT_MON:
		/* ymcw mode? */
		d->c = strtoi_lim(sp, &sp, 0, 5);
		res = 0 - (d->c < 0);
		break;
	case DT_SPFL_S_WDAY:
		/* ymcw mode? */
		switch (s.abbr) {
		case DT_SPMOD_NORM:
			d->w = strtoarri(
				sp, &sp,
				dut_abbr_wday, dut_nabbr_wday);
			break;
		case DT_SPMOD_LONG:
			d->w = strtoarri(
				sp, &sp,
				dut_long_wday, dut_nlong_wday);
			break;
		case DT_SPMOD_ABBR: {
			const char *pos;
			if ((pos = strchr(dut_abab_wday, *sp++)) != NULL) {
				d->w = pos - dut_abab_wday;
			} else {
				d->w = -1;
			}
			break;
		}
		case DT_SPMOD_ILL:
		default:
			break;
		}
		res = 0 - (d->w < 0);
		break;
	case DT_SPFL_S_MON:
		switch (s.abbr) {
		case DT_SPMOD_NORM:
			d->m = strtoarri(
				sp, &sp,
				dut_abbr_mon, dut_nabbr_mon);
			break;
		case DT_SPMOD_LONG:
			d->m = strtoarri(
				sp, &sp,
				dut_long_mon, dut_nlong_mon);
			break;
		case DT_SPMOD_ABBR: {
			const char *pos;
			if ((pos = strchr(dut_abab_mon, *sp++)) != NULL) {
				d->m = pos - dut_abab_mon;
			} else {
				d->m = -1;
			}
			break;
		}
		case DT_SPMOD_ILL:
		default:
			break;
		}
		res = 0 - (d->m < 0);
		break;
	case DT_SPFL_S_QTR:
		if (*sp++ != 'Q') {
			break;
		}
	case DT_SPFL_N_QTR:
		if (d->m == 0) {
			int q;
			if ((q = strtoi_lim(sp, &sp, 1, 4)) >= 0) {
				d->m = q * 3 - 2;
				res = 0;
			}
		}
		break;

	case DT_SPFL_LIT_PERCENT:
		if (*sp++ == '%') {
			res = 0;
		}
		break;
	case DT_SPFL_LIT_TAB:
		if (*sp++ == '\t') {
			res = 0;
		}
		break;
	case DT_SPFL_LIT_NL:
		if (*sp++ == '\n') {
			res = 0;
		}
		break;
	case DT_SPFL_N_DCNT_YEAR:
		/* was %D and %j, cannot be used at the moment */
		if ((d->d = strtoi_lim(sp, &sp, 1, 366)) >= 0) {
			res = 0;
			d->flags.d_dcnt_p = 1;
		}
		break;
	case DT_SPFL_N_WCNT_YEAR:
		/* was %C, cannot be used at the moment */
		d->c = strtoi_lim(sp, &sp, 0, 53);
		d->flags.wk_cnt = s.wk_cnt;
		/* let everyone know d->c has a week-count in there */
		d->flags.c_wcnt_p = 1;
		res = 0;
		break;
	}
	/* assign end pointer */
	if (ep != NULL) {
		*ep = (char*)sp;
	}
	return res;
}

DEFUN int
__strpd_rom(struct strpd_s *d, const char *sp, struct dt_spec_s s, char **ep)
{
	int res = -1;

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		break;

	case DT_SPFL_N_YEAR:
		switch (s.abbr) {
		case DT_SPMOD_LONG:
			d->y = romstrtoi_lim(
				sp, &sp, DT_MIN_YEAR, DT_MAX_YEAR);
			break;
		case DT_SPMOD_NORM:
			d->y = romstrtoi_lim(sp, &sp, 0, 99);
			if (UNLIKELY(d->y < 0)) {
				;
			} else if ((d->y += 2000) > 2068) {
				d->y -= 100;
			}
			break;
		case DT_SPMOD_ABBR:
			d->y = romstrtoi_lim(sp, &sp, 0, 9);
			if (UNLIKELY(d->y < 0)) {
				;
			} else {
				d->y += 2010;
			}
			break;
		}
		res = 0 - (d->y < 0);
		break;
	case DT_SPFL_N_MON:
		d->m = romstrtoi_lim(sp, &sp, 0, GREG_MONTHS_P_YEAR);
		res = 0 - (d->m < 0);
		break;
	case DT_SPFL_N_DCNT_MON:
		d->d = romstrtoi_lim(sp, &sp, 0, 31);
		res = 0 - (d->d < 0);
		break;
	case DT_SPFL_N_WCNT_MON:
		d->c = romstrtoi_lim(sp, &sp, 0, 5);
		res = 0 - (d->c < 0);
		break;
	}
	if (ep != NULL) {
		*ep = (char*)sp;
	}
	return res;
}


#if defined __INTEL_COMPILER
/* we MUST return a char* */
# pragma warning (default:2203)
#elif defined __GNUC__
# pragma GCC diagnostic warning "-Wcast-qual"
#endif	/* __INTEL_COMPILER */

static void
__strfd_get_md(struct strpd_s *d, struct dt_d_s this)
{
	struct __md_s both = dt_get_md(this);
	d->m = both.m;
	d->d = both.d;
	return;
}

static void
__strfd_get_m(struct strpd_s *d, struct dt_d_s this)
{
	d->m = dt_get_mon(this);
	return;
}

static void
__strfd_get_d(struct strpd_s *d, struct dt_d_s this)
{
	d->d = dt_get_mday(this);
	return;
}

DEFUN size_t
__strfd_card(
	char *buf, size_t bsz, struct dt_spec_s s,
	struct strpd_s *d, struct dt_d_s that)
{
	size_t res = 0;

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		break;
	case DT_SPFL_N_DSTD:
		if (UNLIKELY(!d->m && (!d->d || d->flags.d_dcnt_p))) {
			__strfd_get_md(d, that);
		} else if (UNLIKELY(!d->d)) {
			__strfd_get_d(d, that);
		}
		if (LIKELY(bsz >= 10)) {
			ui9999topstr(buf + 0, bsz, d->y, 4, '0');
			buf[4] = '-';
			ui99topstr(buf + 5, bsz, d->m, 2, '0');
			buf[7] = '-';
			ui99topstr(buf + 8, bsz, d->d, 2, '0');
			res = 10;
		}
		break;
	case DT_SPFL_N_YEAR: {
		unsigned int y = d->y;
		int prec;

		if (UNLIKELY(s.tai && d->flags.real_y_in_q)) {
			y = d->q;
		} else if (s.tai) {
			/* oh we want ISO week dates */
			y = dt_dconv(DT_YWD, that).ywd.y;
		}
		switch (s.abbr) {
		case DT_SPMOD_LONG:
			prec = 4;
			break;
		case DT_SPMOD_NORM:
			prec = 2;
			break;
		case DT_SPMOD_ABBR:
			prec = 1;
			break;
		default:
			/* it's just bollocks */
			return 0U;
		}
		res = ui9999topstr(buf, prec, y, 4U, padchar(s));
		break;
	}
	case DT_SPFL_N_MON:
		if (UNLIKELY(!d->m && (!d->d || d->flags.d_dcnt_p))) {
			__strfd_get_md(d, that);
		} else if (UNLIKELY(!d->m)) {
			__strfd_get_m(d, that);
		}
		res = ui99topstr(
			buf, bsz, d->m,
			2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		break;
	case DT_SPFL_N_DCNT_MON: {
		/* ymd mode check? */
		unsigned int pd;

		if (LIKELY(!s.bizda)) {
			if (UNLIKELY(!d->m && (!d->d || d->flags.d_dcnt_p))) {
				__strfd_get_md(d, that);
			} else if (UNLIKELY(!d->d)) {
				__strfd_get_d(d, that);
			}
			pd = d->d;
		} else {
			/* must be bizda now */
			pd = dt_get_bday_q(
				that, __make_bizda_param(s.ab, BIZDA_ULTIMO));
		}
		res = ui99topstr(
			buf, bsz, pd, 2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		break;
	}
	case DT_SPFL_N_DCNT_WEEK:
		/* ymcw mode check */
		with (unsigned int w = (unsigned)d->w ?: dt_get_wday(that)) {
			const unsigned int ymcwp = s.wk_cnt != YWD_MONWK_CNT;

			if (w == DT_SUNDAY && ymcwp) {
				/* turn Sun 07 to Sun 00 */
				w = 0;
			}
			res = ui99topstr(buf, bsz, w, 1 + ymcwp, padchar(s));
		}
		break;
	case DT_SPFL_N_WCNT_MON: {
		unsigned int c = d->c;

		/* ymcw mode check? */
		if (!c || that.typ == DT_YWD) {
			/* don't store it */
			c = (unsigned int)dt_get_wcnt_mon(that);
		}
		res = ui99topstr(
			buf, bsz, c, 2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		break;
	}
	case DT_SPFL_S_WDAY:
		/* get the weekday in ymd mode!! */
		d->w = d->w ? (dt_dow_t)d->w : dt_get_wday(that);
		switch (s.abbr) {
		case DT_SPMOD_NORM:
			res = arritostr(
				buf, bsz, d->w,
				duf_abbr_wday, dut_nabbr_wday);
			break;
		case DT_SPMOD_LONG:
			res = arritostr(
				buf, bsz, d->w,
				duf_long_wday, dut_nlong_wday);
			break;
		case DT_SPMOD_ABBR:
			/* super abbrev'd wday */
			if (d->w < dut_nabab_wday) {
				buf[res++] = dut_abab_wday[d->w];
			}
			break;
		case DT_SPMOD_ILL:
		default:
			break;
		}
		break;
	case DT_SPFL_S_MON:
		switch (s.abbr) {
		case DT_SPMOD_NORM:
			res = arritostr(
				buf, bsz, d->m,
				duf_abbr_mon, dut_nabbr_mon);
			break;
		case DT_SPMOD_LONG:
			res = arritostr(
				buf, bsz, d->m,
				duf_long_mon, dut_nlong_mon);
			break;
		case DT_SPMOD_ABBR:
			/* super abbrev'd month */
			if (d->m < dut_nabab_mon) {
				buf[res++] = dut_abab_mon[d->m];
			}
			break;
		case DT_SPMOD_ILL:
		default:
			break;
		}
		break;
	case DT_SPFL_S_QTR:
		buf[res++] = 'Q';
		buf[res++] = (char)(dt_get_quarter(that) + '0');
		break;
	case DT_SPFL_N_QTR:
		buf[res++] = '0';
		buf[res++] = (char)(dt_get_quarter(that) + '0');
		break;

	case DT_SPFL_LIT_PERCENT:
		/* literal % */
		buf[res++] = '%';
		break;
	case DT_SPFL_LIT_TAB:
		/* literal tab */
		buf[res++] = '\t';
		break;
	case DT_SPFL_LIT_NL:
		/* literal \n */
		buf[res++] = '\n';
		break;

	case DT_SPFL_N_DCNT_YEAR:
		switch (that.typ) {
		case DT_YMD:
		case DT_BIZDA: {
			/* %j */
			int yd;
			if (LIKELY(!s.bizda)) {
				yd = __ymd_get_yday(that.ymd);
			} else {
				yd = __bizda_get_yday(
					that.bizda, __get_bizda_param(that));
			}
			if (yd >= 0) {
				res = ui999topstr(
					buf, bsz, yd,
					3 - (s.pad == DT_SPPAD_OMIT) << 1U,
					padchar(s));
			} else {
				buf[res++] = '0';
				buf[res++] = '0';
				buf[res++] = '0';
			}
			break;
		}
		case DT_YD:
			res = ui999topstr(
				buf, bsz, d->d,
				3 - (s.pad == DT_SPPAD_OMIT) << 1U, padchar(s));
			break;
		case DT_LDN:
			res = snprintf(buf, bsz, "%u", that.ldn);
			break;
		case DT_JDN:
			res = snprintf(buf, bsz, "%.6f", that.jdn);
			break;
		default:
			break;
		}
		break;
	case DT_SPFL_N_WCNT_YEAR: {
		int yw = dt_get_wcnt_year(that, s.wk_cnt);
		res = ui99topstr(
			buf, bsz, yw, 2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		break;
	}
	}
	return res;
}

DEFUN size_t
__strfd_rom(
	char *buf, size_t bsz, struct dt_spec_s s,
	struct strpd_s *d, struct dt_d_s that)
{
	size_t res = 0;

	if (that.typ != DT_YMD) {
		/* not supported for non-ymds */
		return res;
	}

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		break;
	case DT_SPFL_N_YEAR:
		switch (s.abbr) {
		case DT_SPMOD_LONG:
			res = ui32tostrrom(buf, bsz, d->y);
			break;
		case DT_SPMOD_NORM:
			res = ui32tostrrom(buf, bsz, d->y % 100);
			break;
		case DT_SPMOD_ABBR:
			res = ui32tostrrom(buf, bsz, d->y % 10);
			break;
		case DT_SPMOD_ILL:
		default:
			/* should be impossible */
			break;
		}
		break;
	case DT_SPFL_N_MON:
		res = ui32tostrrom(buf, bsz, d->m);
		break;
	case DT_SPFL_N_DCNT_MON:
		res = ui32tostrrom(buf, bsz, d->d);
		break;
	case DT_SPFL_N_WCNT_MON: {
		unsigned int c = d->c;

		if (!c) {
			/* don't store the result */
			c = (unsigned int)dt_get_wcnt_mon(that);
		}
		res = ui32tostrrom(buf, bsz, c);
		break;
	}
	}
	return res;
}

DEFUN size_t
__strfd_dur(
	char *buf, size_t bsz, struct dt_spec_s s,
	struct strpd_s *d, struct dt_ddur_s UNUSED(that))
{
	size_t res = 0;

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		break;
	case DT_SPFL_N_DSTD:
	case DT_SPFL_N_DCNT_MON:
		res = snprintf(buf, bsz, "%d", d->sd);
		break;
	case DT_SPFL_N_YEAR:
		if (!d->y) {
			/* fill in for a mo, hack hack hack
			 * we'll think about the consequences later */
			d->y = __uidiv(d->m, GREG_MONTHS_P_YEAR);
			d->m = __uimod(d->m, GREG_MONTHS_P_YEAR);
		}
		res = snprintf(buf, bsz, "%d", d->y);
		break;
	case DT_SPFL_N_MON:
		res = snprintf(buf, bsz, "%d", d->m);
		break;
	case DT_SPFL_N_DCNT_WEEK:
		if (!d->w) {
			/* hack hack hack
			 * we'll think about the consequences later */
			d->w = __uidiv(d->d, GREG_DAYS_P_WEEK);
			d->d = __uimod(d->d, GREG_DAYS_P_WEEK);
		}
		res = snprintf(buf, bsz, "%d", d->w);
		break;
	case DT_SPFL_N_WCNT_MON:
		res = snprintf(buf, bsz, "%d", d->c);
		break;
	case DT_SPFL_S_WDAY:
	case DT_SPFL_S_MON:
	case DT_SPFL_S_QTR:
	case DT_SPFL_N_QTR:
	case DT_SPFL_N_DCNT_YEAR:
	case DT_SPFL_N_WCNT_YEAR:
		break;

	case DT_SPFL_LIT_PERCENT:
		/* literal % */
		buf[res++] = '%';
		break;
	case DT_SPFL_LIT_TAB:
		/* literal tab */
		buf[res++] = '\t';
		break;
	case DT_SPFL_LIT_NL:
		/* literal \n */
		buf[res++] = '\n';
		break;
	}
	return res;
}

#endif	/* INCLUDED_date_core_strpf_c_ */