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