xref: /netbsd-src/usr.bin/w/pr_time.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: pr_time.c,v 1.5 1996/11/14 07:34:46 mikel Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)pr_time.c	8.2 (Berkeley) 4/4/94";
39 #else
40 static char rcsid[] = "$NetBSD: pr_time.c,v 1.5 1996/11/14 07:34:46 mikel Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/time.h>
46 
47 #include <stdio.h>
48 #include <string.h>
49 #include <tzfile.h>
50 
51 #include "extern.h"
52 
53 /*
54  * pr_attime --
55  *	Print the time since the user logged in.
56  *
57  *	Note: SCCS forces the bizarre string manipulation, things like
58  *	%I% get replaced in the source code.
59  */
60 void
61 pr_attime(started, now)
62 	time_t *started, *now;
63 {
64 	static char buf[256];
65 	int tnow_yday;
66 	struct tm *tp;
67 	time_t diff;
68 	char *fmt;
69 
70 	tnow_yday = localtime(now)->tm_yday;
71 	tp = localtime(started);
72 	diff = *now - *started;
73 
74 	/* If more than a week, use day-month-year. */
75 	if (diff > SECSPERDAY * DAYSPERWEEK)
76 		fmt = "%d%b%y";
77 
78 	/* If not today, use day-hour-am/pm. */
79 	else if (tp->tm_yday != tnow_yday)
80 		fmt = __CONCAT("%a%", "I%p");
81 
82 	/* Default is hh:mm{am,pm}. */
83 	else
84 		fmt = __CONCAT("%l:%", "M%p");
85 
86 	(void)strftime(buf, sizeof(buf), fmt, tp);
87 	(void)fputs(buf, stdout);
88 }
89 
90 /*
91  * pr_idle --
92  *	Display the idle time.
93  */
94 void
95 pr_idle(idle)
96 	time_t idle;
97 {
98 	int days = idle / SECSPERDAY;
99 
100 	/* If idle more than 36 hours, print as a number of days. */
101 	if (idle >= 36 * SECSPERHOUR)
102 		printf(days == 1 ? "  %dday " : " %ddays ", days);
103 
104 	/* If idle more than an hour, print as HH:MM. */
105 	else if (idle >= SECSPERHOUR)
106 		(void)printf(" %2d:%02d ",
107 		    idle / SECSPERHOUR, (idle % SECSPERHOUR) / SECSPERMIN);
108 
109 	/* Else print the minutes idle. */
110 	else
111 		(void)printf("    %2d ", idle / SECSPERMIN);
112 }
113