xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/time.c (revision 2017c965)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<sys/time.h>
28 #include	<string.h>
29 #include	<stdio.h>
30 #include	"_conv.h"
31 #include	"time_msg.h"
32 
33 /*
34  * Translate a struct timeval into a string appropriate for ld(1) and ld.so.1(1)
35  * diagnostics.
36  */
37 const char *
conv_time(struct timeval * oldtime,struct timeval * newtime,Conv_time_buf_t * time_buf)38 conv_time(struct timeval *oldtime, struct timeval *newtime,
39     Conv_time_buf_t *time_buf)
40 {
41 	int		hour, min;
42 	time_t		sec;
43 	suseconds_t	usec;
44 
45 	sec = newtime->tv_sec - oldtime->tv_sec;
46 	if (newtime->tv_usec >= oldtime->tv_usec)
47 		usec = newtime->tv_usec - oldtime->tv_usec;
48 	else {
49 		usec = (newtime->tv_usec + MICROSEC) - oldtime->tv_usec;
50 		sec -= 1;
51 	}
52 
53 	/*
54 	 * The default display is "sec.fraction", but ld(1) has been know to
55 	 * ascend into minutes, and in worst case scenarios, hours.
56 	 */
57 	if ((min = sec / 60) != 0)
58 		sec = sec % 60;
59 	if ((hour = min / 60) != 0)
60 		min = min % 60;
61 
62 	if (hour)
63 		(void) snprintf(time_buf->buf, sizeof (time_buf->buf),
64 		    MSG_ORIG(MSG_TIME_HMSF), hour, min, sec, usec);
65 	else if (min)
66 		(void) snprintf(time_buf->buf, sizeof (time_buf->buf),
67 		    MSG_ORIG(MSG_TIME_MSF), min, sec, usec);
68 	else
69 		(void) snprintf(time_buf->buf, sizeof (time_buf->buf),
70 		    MSG_ORIG(MSG_TIME_SF), sec, usec);
71 
72 	return ((const char *)time_buf);
73 }
74