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