xref: /dflybsd-src/lib/libc/stdio/xprintf_time.c (revision e0f95098eeba0176864b9cafe6d69b5b7bc0e73f)
1*e0f95098SPeter Avalos /*-
2*e0f95098SPeter Avalos  * Copyright (c) 2005 Poul-Henning Kamp
3*e0f95098SPeter Avalos  * Copyright (c) 1990, 1993
4*e0f95098SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
5*e0f95098SPeter Avalos  *
6*e0f95098SPeter Avalos  * This code is derived from software contributed to Berkeley by
7*e0f95098SPeter Avalos  * Chris Torek.
8*e0f95098SPeter Avalos  *
9*e0f95098SPeter Avalos  * Redistribution and use in source and binary forms, with or without
10*e0f95098SPeter Avalos  * modification, are permitted provided that the following conditions
11*e0f95098SPeter Avalos  * are met:
12*e0f95098SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
13*e0f95098SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
14*e0f95098SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
15*e0f95098SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
16*e0f95098SPeter Avalos  *    documentation and/or other materials provided with the distribution.
17*e0f95098SPeter Avalos  * 3. Neither the name of the University nor the names of its contributors
18*e0f95098SPeter Avalos  *    may be used to endorse or promote products derived from this software
19*e0f95098SPeter Avalos  *    without specific prior written permission.
20*e0f95098SPeter Avalos  *
21*e0f95098SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*e0f95098SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*e0f95098SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*e0f95098SPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*e0f95098SPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*e0f95098SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*e0f95098SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*e0f95098SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*e0f95098SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*e0f95098SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*e0f95098SPeter Avalos  * SUCH DAMAGE.
32*e0f95098SPeter Avalos  *
33*e0f95098SPeter Avalos  * $FreeBSD: src/lib/libc/stdio/xprintf_time.c,v 1.3 2006/02/04 14:35:01 phk Exp $
34*e0f95098SPeter Avalos  */
35*e0f95098SPeter Avalos 
36*e0f95098SPeter Avalos #include "namespace.h"
37*e0f95098SPeter Avalos #include <stdio.h>
38*e0f95098SPeter Avalos #include <wchar.h>
39*e0f95098SPeter Avalos #include <stdint.h>
40*e0f95098SPeter Avalos #include <assert.h>
41*e0f95098SPeter Avalos #include <sys/time.h>
42*e0f95098SPeter Avalos #include "un-namespace.h"
43*e0f95098SPeter Avalos #include "printf.h"
44*e0f95098SPeter Avalos 
45*e0f95098SPeter Avalos int
46*e0f95098SPeter Avalos __printf_arginfo_time(const struct printf_info *pi, size_t n, int *argt)
47*e0f95098SPeter Avalos {
48*e0f95098SPeter Avalos 
49*e0f95098SPeter Avalos 	assert(n >= 1);
50*e0f95098SPeter Avalos 	argt[0] = PA_POINTER;
51*e0f95098SPeter Avalos 	return (1);
52*e0f95098SPeter Avalos }
53*e0f95098SPeter Avalos #define	MINUTE 60
54*e0f95098SPeter Avalos #define	HOUR	(60 * MINUTE)
55*e0f95098SPeter Avalos #define	DAY	(24 * HOUR)
56*e0f95098SPeter Avalos #define	YEAR	(365 * DAY)
57*e0f95098SPeter Avalos 
58*e0f95098SPeter Avalos int
59*e0f95098SPeter Avalos __printf_render_time(struct __printf_io *io, const struct printf_info *pi,
60*e0f95098SPeter Avalos 		     const void *const *arg)
61*e0f95098SPeter Avalos {
62*e0f95098SPeter Avalos 	char buf[100];
63*e0f95098SPeter Avalos 	char *p;
64*e0f95098SPeter Avalos 	struct timeval *tv;
65*e0f95098SPeter Avalos 	struct timespec *ts;
66*e0f95098SPeter Avalos 	time_t *tp;
67*e0f95098SPeter Avalos 	intmax_t t, tx;
68*e0f95098SPeter Avalos 	int i, prec, nsec;
69*e0f95098SPeter Avalos 
70*e0f95098SPeter Avalos 	prec = 0;
71*e0f95098SPeter Avalos 	if (pi->is_long) {
72*e0f95098SPeter Avalos 		tv = *((struct timeval **)arg[0]);
73*e0f95098SPeter Avalos 		t = tv->tv_sec;
74*e0f95098SPeter Avalos 		nsec = tv->tv_usec * 1000;
75*e0f95098SPeter Avalos 		prec = 6;
76*e0f95098SPeter Avalos 	} else if (pi->is_long_double) {
77*e0f95098SPeter Avalos 		ts = *((struct timespec **)arg[0]);
78*e0f95098SPeter Avalos 		t = ts->tv_sec;
79*e0f95098SPeter Avalos 		nsec = ts->tv_nsec;
80*e0f95098SPeter Avalos 		prec = 9;
81*e0f95098SPeter Avalos 	} else {
82*e0f95098SPeter Avalos 		tp = *((time_t **)arg[0]);
83*e0f95098SPeter Avalos 		t = *tp;
84*e0f95098SPeter Avalos 	}
85*e0f95098SPeter Avalos 
86*e0f95098SPeter Avalos 	p = buf;
87*e0f95098SPeter Avalos 	if (pi->alt) {
88*e0f95098SPeter Avalos 		tx = t;
89*e0f95098SPeter Avalos 		if (t >= YEAR) {
90*e0f95098SPeter Avalos 			p += sprintf(p, "%jdy", t / YEAR);
91*e0f95098SPeter Avalos 			t %= YEAR;
92*e0f95098SPeter Avalos 		}
93*e0f95098SPeter Avalos 		if (t >= DAY && t != 0) {
94*e0f95098SPeter Avalos 			p += sprintf(p, "%jdd", t / DAY);
95*e0f95098SPeter Avalos 			t %= DAY;
96*e0f95098SPeter Avalos 		}
97*e0f95098SPeter Avalos 		if (t >= HOUR && t != 0) {
98*e0f95098SPeter Avalos 			p += sprintf(p, "%jdh", t / HOUR);
99*e0f95098SPeter Avalos 			t %= HOUR;
100*e0f95098SPeter Avalos 		}
101*e0f95098SPeter Avalos 		if (t >= MINUTE && t != 0) {
102*e0f95098SPeter Avalos 			p += sprintf(p, "%jdm", t / MINUTE);
103*e0f95098SPeter Avalos 			t %= MINUTE;
104*e0f95098SPeter Avalos 		}
105*e0f95098SPeter Avalos 		if (t != 0 || tx == 0)
106*e0f95098SPeter Avalos 			p += sprintf(p, "%jds", t);
107*e0f95098SPeter Avalos 	} else  {
108*e0f95098SPeter Avalos 		p += sprintf(p, "%jd", (intmax_t)t);
109*e0f95098SPeter Avalos 	}
110*e0f95098SPeter Avalos 	if (pi->is_long || pi->is_long_double) {
111*e0f95098SPeter Avalos 		if (pi->prec >= 0)
112*e0f95098SPeter Avalos 			prec = pi->prec;
113*e0f95098SPeter Avalos 		for (i = prec; i < 9; i++)
114*e0f95098SPeter Avalos 			nsec /= 10;
115*e0f95098SPeter Avalos 		p += sprintf(p, ".%.*d", prec, nsec);
116*e0f95098SPeter Avalos 	}
117*e0f95098SPeter Avalos 	return(__printf_out(io, pi, buf, p - buf));
118*e0f95098SPeter Avalos }
119