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