xref: /original-bsd/usr.bin/w/pr_time.c (revision 37071c60)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)pr_time.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <sys/time.h>
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <tzfile.h>
18 
19 #include "extern.h"
20 
21 /*
22  * pr_attime --
23  *	Print the time since the user logged in.
24  *
25  *	Note: SCCS forces the bizarre string manipulation, things like
26  *	8.1 get replaced in the source code.
27  */
28 void
29 pr_attime(started, now)
30 	time_t *started, *now;
31 {
32 	static char buf[256];
33 	struct tm *tp;
34 	time_t diff;
35 	char fmt[20];
36 
37 	tp = localtime(started);
38 	diff = *now - *started;
39 
40 	/* If more than a week, use day-month-year. */
41 	if (diff > SECSPERDAY * DAYSPERWEEK)
42 		(void)strcpy(fmt, "%d%b%y");
43 
44 	/* If not today, use day-hour-am/pm. */
45 	else if (*now / SECSPERDAY != *started / SECSPERDAY) {
46 		(void)strcpy(fmt, "%a%%%p");
47 		fmt[3] = 'I';
48 	}
49 
50 	/* Default is hh:mm{am,pm}. */
51 	else {
52 		(void)strcpy(fmt, "%l:%%%p");
53 		fmt[4] = 'M';
54 	}
55 
56 	(void)strftime(buf, sizeof(buf), fmt, tp);
57 	(void)printf("%s", buf);
58 }
59 
60 /*
61  * pr_idle --
62  *	Display the idle time.
63  */
64 void
65 pr_idle(idle)
66 	time_t idle;
67 {
68 	/* If idle more than 36 hours, print as a number of days. */
69 	if (idle >= 36 * SECSPERHOUR)
70 		(void)printf(" %ddays ", idle / SECSPERDAY);
71 
72 	/* If idle more than an hour, print as HH:MM. */
73 	else if (idle >= SECSPERHOUR)
74 		(void)printf(" %2d:%02d ",
75 		    idle / SECSPERHOUR, (idle % SECSPERHOUR) / SECSPERMIN);
76 
77 	/* Else print the minutes idle. */
78 	else
79 		(void)printf("    %2d ", idle / SECSPERMIN);
80 }
81