xref: /freebsd/usr.bin/w/pr_time.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
19b50d902SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
328b56c58bSMark Murray 
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #include <sys/types.h>
359b50d902SRodney W. Grimes #include <sys/time.h>
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #include <stdio.h>
38821df508SXin LI #include <string.h>
39e39ee421SAndrey A. Chernov #include <wchar.h>
4076c0abf1SMarcel Moolenaar #include <libxo/xo.h>
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include "extern.h"
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * pr_attime --
469b50d902SRodney W. Grimes  *	Print the time since the user logged in.
479b50d902SRodney W. Grimes  */
4834903a55SHajimu UMEMOTO int
pr_attime(time_t * started,time_t * now)49e8e649ccSJuli Mallett pr_attime(time_t *started, time_t *now)
509b50d902SRodney W. Grimes {
5134903a55SHajimu UMEMOTO 	static wchar_t buf[256];
5265824845SDavid Nugent 	struct tm tp, tm;
539b50d902SRodney W. Grimes 	time_t diff;
54277dbce9SXin LI 	const wchar_t *fmt;
5534903a55SHajimu UMEMOTO 	int len, width, offset = 0;
569b50d902SRodney W. Grimes 
5765824845SDavid Nugent 	tp = *localtime(started);
5865824845SDavid Nugent 	tm = *localtime(now);
599b50d902SRodney W. Grimes 	diff = *now - *started;
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes 	/* If more than a week, use day-month-year. */
62656dcd43SGarrett Wollman 	if (diff > 86400 * 7)
6334903a55SHajimu UMEMOTO 		fmt = L"%d%b%y";
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes 	/* If not today, use day-hour-am/pm. */
6665824845SDavid Nugent 	else if (tm.tm_mday != tp.tm_mday ||
6765824845SDavid Nugent 		 tm.tm_mon != tp.tm_mon ||
6865824845SDavid Nugent 		 tm.tm_year != tp.tm_year) {
6965824845SDavid Nugent 	/* The line below does not take DST into consideration */
7065824845SDavid Nugent 	/* else if (*now / 86400 != *started / 86400) { */
7134903a55SHajimu UMEMOTO 		fmt = use_ampm ? L"%a%I%p" : L"%a%H";
729b50d902SRodney W. Grimes 	}
739b50d902SRodney W. Grimes 
749b50d902SRodney W. Grimes 	/* Default is hh:mm{am,pm}. */
759b50d902SRodney W. Grimes 	else {
7634903a55SHajimu UMEMOTO 		fmt = use_ampm ? L"%l:%M%p" : L"%k:%M";
779b50d902SRodney W. Grimes 	}
789b50d902SRodney W. Grimes 
7934903a55SHajimu UMEMOTO 	(void)wcsftime(buf, sizeof(buf), fmt, &tp);
8034903a55SHajimu UMEMOTO 	len = wcslen(buf);
8134903a55SHajimu UMEMOTO 	width = wcswidth(buf, len);
8276c0abf1SMarcel Moolenaar 	xo_attr("since", "%lu", (unsigned long) *started);
8376c0abf1SMarcel Moolenaar 	xo_attr("delta", "%lu", (unsigned long) diff);
8434903a55SHajimu UMEMOTO 	if (len == width)
8576c0abf1SMarcel Moolenaar 		xo_emit("{:login-time/%-7.7ls/%ls}", buf);
8634903a55SHajimu UMEMOTO 	else if (width < 7)
8776c0abf1SMarcel Moolenaar 		xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, "      ");
8834903a55SHajimu UMEMOTO 	else {
8976c0abf1SMarcel Moolenaar 		xo_emit("{:login-time/%ls}", buf);
9034903a55SHajimu UMEMOTO 		offset = width - 7;
9134903a55SHajimu UMEMOTO 	}
9234903a55SHajimu UMEMOTO 	return (offset);
939b50d902SRodney W. Grimes }
949b50d902SRodney W. Grimes 
959b50d902SRodney W. Grimes /*
969b50d902SRodney W. Grimes  * pr_idle --
979b50d902SRodney W. Grimes  *	Display the idle time.
9869b41093SPeter Wemm  *	Returns number of excess characters that were used for long idle time.
999b50d902SRodney W. Grimes  */
10070498d5aSDaniel O'Callaghan int
pr_idle(time_t idle)101e8e649ccSJuli Mallett pr_idle(time_t idle)
1029b50d902SRodney W. Grimes {
1039b50d902SRodney W. Grimes 	/* If idle more than 36 hours, print as a number of days. */
10489cc2fabSDima Ruban 	if (idle >= 36 * 3600) {
10589cc2fabSDima Ruban 		int days = idle / 86400;
10676c0abf1SMarcel Moolenaar 		xo_emit(" {:idle/%dday%s} ", days, days > 1 ? "s" : " " );
10769b41093SPeter Wemm 		if (days >= 100)
10869b41093SPeter Wemm 			return (2);
10970498d5aSDaniel O'Callaghan 		if (days >= 10)
11070498d5aSDaniel O'Callaghan 			return (1);
11189cc2fabSDima Ruban 	}
1129b50d902SRodney W. Grimes 
1139b50d902SRodney W. Grimes 	/* If idle more than an hour, print as HH:MM. */
114656dcd43SGarrett Wollman 	else if (idle >= 3600)
11576c0abf1SMarcel Moolenaar 		xo_emit(" {:idle/%2d:%02d/} ",
1168109e672SAlexander Langer 		    (int)(idle / 3600), (int)((idle % 3600) / 60));
1179b50d902SRodney W. Grimes 
118656dcd43SGarrett Wollman 	else if (idle / 60 == 0)
11976c0abf1SMarcel Moolenaar 		xo_emit("     - ");
120255318a8SAndrey A. Chernov 
1219b50d902SRodney W. Grimes 	/* Else print the minutes idle. */
1229b50d902SRodney W. Grimes 	else
12376c0abf1SMarcel Moolenaar 		xo_emit("    {:idle/%2d} ", (int)(idle / 60));
12470498d5aSDaniel O'Callaghan 
12570498d5aSDaniel O'Callaghan 	return (0); /* not idle longer than 9 days */
1269b50d902SRodney W. Grimes }
127