xref: /freebsd/usr.bin/w/pr_time.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 
33 #ifndef lint
34 static const char sccsid[] = "@(#)pr_time.c	8.2 (Berkeley) 4/4/94";
35 #endif
36 
37 #include <sys/types.h>
38 #include <sys/time.h>
39 
40 #include <stdio.h>
41 #include <string.h>
42 #include <wchar.h>
43 #include <libxo/xo.h>
44 
45 #include "extern.h"
46 
47 /*
48  * pr_attime --
49  *	Print the time since the user logged in.
50  */
51 int
52 pr_attime(time_t *started, time_t *now)
53 {
54 	static wchar_t buf[256];
55 	struct tm tp, tm;
56 	time_t diff;
57 	const wchar_t *fmt;
58 	int len, width, offset = 0;
59 
60 	tp = *localtime(started);
61 	tm = *localtime(now);
62 	diff = *now - *started;
63 
64 	/* If more than a week, use day-month-year. */
65 	if (diff > 86400 * 7)
66 		fmt = L"%d%b%y";
67 
68 	/* If not today, use day-hour-am/pm. */
69 	else if (tm.tm_mday != tp.tm_mday ||
70 		 tm.tm_mon != tp.tm_mon ||
71 		 tm.tm_year != tp.tm_year) {
72 	/* The line below does not take DST into consideration */
73 	/* else if (*now / 86400 != *started / 86400) { */
74 		fmt = use_ampm ? L"%a%I%p" : L"%a%H";
75 	}
76 
77 	/* Default is hh:mm{am,pm}. */
78 	else {
79 		fmt = use_ampm ? L"%l:%M%p" : L"%k:%M";
80 	}
81 
82 	(void)wcsftime(buf, sizeof(buf), fmt, &tp);
83 	len = wcslen(buf);
84 	width = wcswidth(buf, len);
85 	xo_attr("since", "%lu", (unsigned long) *started);
86 	xo_attr("delta", "%lu", (unsigned long) diff);
87 	if (len == width)
88 		xo_emit("{:login-time/%-7.7ls/%ls}", buf);
89 	else if (width < 7)
90 		xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, "      ");
91 	else {
92 		xo_emit("{:login-time/%ls}", buf);
93 		offset = width - 7;
94 	}
95 	return (offset);
96 }
97 
98 /*
99  * pr_idle --
100  *	Display the idle time.
101  *	Returns number of excess characters that were used for long idle time.
102  */
103 int
104 pr_idle(time_t idle)
105 {
106 	/* If idle more than 36 hours, print as a number of days. */
107 	if (idle >= 36 * 3600) {
108 		int days = idle / 86400;
109 		xo_emit(" {:idle/%dday%s} ", days, days > 1 ? "s" : " " );
110 		if (days >= 100)
111 			return (2);
112 		if (days >= 10)
113 			return (1);
114 	}
115 
116 	/* If idle more than an hour, print as HH:MM. */
117 	else if (idle >= 3600)
118 		xo_emit(" {:idle/%2d:%02d/} ",
119 		    (int)(idle / 3600), (int)((idle % 3600) / 60));
120 
121 	else if (idle / 60 == 0)
122 		xo_emit("     - ");
123 
124 	/* Else print the minutes idle. */
125 	else
126 		xo_emit("    {:idle/%2d} ", (int)(idle / 60));
127 
128 	return (0); /* not idle longer than 9 days */
129 }
130