xprintf_time.c revision 155300
1153486Sphk/*- 2153486Sphk * Copyright (c) 2005 Poul-Henning Kamp 3153486Sphk * Copyright (c) 1990, 1993 4153486Sphk * The Regents of the University of California. All rights reserved. 5153486Sphk * 6153486Sphk * This code is derived from software contributed to Berkeley by 7153486Sphk * Chris Torek. 8153486Sphk * 9153486Sphk * Redistribution and use in source and binary forms, with or without 10153486Sphk * modification, are permitted provided that the following conditions 11153486Sphk * are met: 12153486Sphk * 1. Redistributions of source code must retain the above copyright 13153486Sphk * notice, this list of conditions and the following disclaimer. 14153486Sphk * 2. Redistributions in binary form must reproduce the above copyright 15153486Sphk * notice, this list of conditions and the following disclaimer in the 16153486Sphk * documentation and/or other materials provided with the distribution. 17153486Sphk * 3. Neither the name of the University nor the names of its contributors 18153486Sphk * may be used to endorse or promote products derived from this software 19153486Sphk * without specific prior written permission. 20153486Sphk * 21153486Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22153486Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23153486Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24153486Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25153486Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26153486Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27153486Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28153486Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29153486Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30153486Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31153486Sphk * SUCH DAMAGE. 32153486Sphk * 33153486Sphk * $FreeBSD: head/lib/libc/stdio/xprintf_time.c 155300 2006-02-04 14:35:01Z phk $ 34153486Sphk */ 35153486Sphk#include <namespace.h> 36153486Sphk#include <stdio.h> 37153486Sphk#include <wchar.h> 38153486Sphk#include <stdint.h> 39153486Sphk#include <assert.h> 40153486Sphk#include <sys/time.h> 41153486Sphk#include "printf.h" 42153486Sphk 43153486Sphkint 44153486Sphk__printf_arginfo_time(const struct printf_info *pi, size_t n, int *argt) 45153486Sphk{ 46153486Sphk 47153486Sphk assert(n >= 1); 48153486Sphk argt[0] = PA_POINTER; 49153486Sphk return (1); 50153486Sphk} 51153486Sphk#define MINUTE 60 52153486Sphk#define HOUR (60 * MINUTE) 53153486Sphk#define DAY (24 * HOUR) 54153486Sphk#define YEAR (365 * DAY) 55153486Sphk 56153486Sphkint 57153486Sphk__printf_render_time(struct __printf_io *io, const struct printf_info *pi, const void *const *arg) 58153486Sphk{ 59153486Sphk char buf[100]; 60153486Sphk char *p; 61153486Sphk struct timeval *tv; 62153486Sphk struct timespec *ts; 63153486Sphk time_t *tp; 64155300Sphk intmax_t t, tx; 65153486Sphk int i, prec, nsec; 66153486Sphk 67153486Sphk prec = 0; 68153486Sphk if (pi->is_long) { 69153486Sphk tv = *((struct timeval **)arg[0]); 70153486Sphk t = tv->tv_sec; 71153486Sphk nsec = tv->tv_usec * 1000; 72153486Sphk prec = 6; 73153486Sphk } else if (pi->is_long_double) { 74153486Sphk ts = *((struct timespec **)arg[0]); 75153486Sphk t = ts->tv_sec; 76153486Sphk nsec = ts->tv_nsec; 77153486Sphk prec = 9; 78153486Sphk } else { 79153486Sphk tp = *((time_t **)arg[0]); 80153486Sphk t = *tp; 81153486Sphk } 82153486Sphk 83153486Sphk p = buf; 84153486Sphk if (pi->alt) { 85155300Sphk tx = t; 86153486Sphk if (t >= YEAR) { 87153486Sphk p += sprintf(p, "%jdy", t / YEAR); 88153486Sphk t %= YEAR; 89153486Sphk } 90153486Sphk if (t >= DAY && t != 0) { 91153486Sphk p += sprintf(p, "%jdd", t / DAY); 92153486Sphk t %= DAY; 93153486Sphk } 94153486Sphk if (t >= HOUR && t != 0) { 95153486Sphk p += sprintf(p, "%jdh", t / HOUR); 96153486Sphk t %= HOUR; 97153486Sphk } 98153486Sphk if (t >= MINUTE && t != 0) { 99153486Sphk p += sprintf(p, "%jdm", t / MINUTE); 100153486Sphk t %= MINUTE; 101153486Sphk } 102155300Sphk if (t != 0 || tx == 0) 103155085Sphk p += sprintf(p, "%jds", t); 104153486Sphk } else { 105153486Sphk p += sprintf(p, "%jd", (intmax_t)t); 106153486Sphk } 107153486Sphk if (pi->is_long || pi->is_long_double) { 108153486Sphk if (pi->prec >= 0) 109153486Sphk prec = pi->prec; 110153486Sphk for (i = prec; i < 9; i++) 111153486Sphk nsec /= 10; 112153486Sphk p += sprintf(p, ".%.*d", prec, nsec); 113153486Sphk } 114153486Sphk return(__printf_out(io, pi, buf, p - buf)); 115153486Sphk} 116