pr_time.c revision 10948
172445Sassar/*- 272445Sassar * Copyright (c) 1990, 1993, 1994 372445Sassar * The Regents of the University of California. All rights reserved. 472445Sassar * 572445Sassar * Redistribution and use in source and binary forms, with or without 672445Sassar * modification, are permitted provided that the following conditions 772445Sassar * are met: 872445Sassar * 1. Redistributions of source code must retain the above copyright 972445Sassar * notice, this list of conditions and the following disclaimer. 1072445Sassar * 2. Redistributions in binary form must reproduce the above copyright 1172445Sassar * notice, this list of conditions and the following disclaimer in the 1272445Sassar * documentation and/or other materials provided with the distribution. 1372445Sassar * 3. All advertising materials mentioning features or use of this software 1472445Sassar * must display the following acknowledgement: 1572445Sassar * This product includes software developed by the University of 1672445Sassar * California, Berkeley and its contributors. 1772445Sassar * 4. Neither the name of the University nor the names of its contributors 1872445Sassar * may be used to endorse or promote products derived from this software 1972445Sassar * without specific prior written permission. 2072445Sassar * 2172445Sassar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2272445Sassar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2372445Sassar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2472445Sassar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2572445Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2672445Sassar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2772445Sassar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2872445Sassar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2972445Sassar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3072445Sassar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3172445Sassar * SUCH DAMAGE. 3272445Sassar */ 3372445Sassar 3472445Sassar#ifndef lint 3572445Sassarstatic char sccsid[] = "@(#)pr_time.c 8.2 (Berkeley) 4/4/94"; 3672445Sassar#endif /* not lint */ 3772445Sassar 3872445Sassar#include <sys/types.h> 3972445Sassar#include <sys/time.h> 4072445Sassar 4172445Sassar#include <stdio.h> 4272445Sassar#include <string.h> 4372445Sassar 4472445Sassar#include "extern.h" 4572445Sassar 4672445Sassar/* 4772445Sassar * pr_attime -- 4872445Sassar * Print the time since the user logged in. 4972445Sassar * 5072445Sassar * Note: SCCS forces the bizarre string manipulation, things like 5172445Sassar * 8.2 get replaced in the source code. 5272445Sassar */ 5372445Sassarvoid 5472445Sassarpr_attime(started, now) 5572445Sassar time_t *started, *now; 5672445Sassar{ 5772445Sassar static char buf[256]; 58 struct tm *tp; 59 time_t diff; 60 char fmt[20]; 61 62 tp = localtime(started); 63 diff = *now - *started; 64 65 /* If more than a week, use day-month-year. */ 66 if (diff > 86400 * 7) 67 (void)strcpy(fmt, "%d%b%y"); 68 69 /* If not today, use day-hour-am/pm. */ 70 else if (*now / 86400 != *started / 86400) { 71 (void)strcpy(fmt, __CONCAT("%a%", "I%p")); 72 } 73 74 /* Default is hh:mm{am,pm}. */ 75 else { 76 (void)strcpy(fmt, __CONCAT("%l:%", "M%p")); 77 } 78 79 (void)strftime(buf, sizeof(buf), fmt, tp); 80 (void)printf("%s", buf); 81} 82 83/* 84 * pr_idle -- 85 * Display the idle time. 86 */ 87void 88pr_idle(idle) 89 time_t idle; 90{ 91 /* If idle more than 36 hours, print as a number of days. */ 92 if (idle >= 36 * 3600) { 93 int days = idle / 86400; 94 (void)printf(" %dday%s ", days, days > 1 ? "s" : "" ); 95 } 96 97 /* If idle more than an hour, print as HH:MM. */ 98 else if (idle >= 3600) 99 (void)printf(" %2d:%02d ", 100 idle / 3600, (idle % 3600) / 60); 101 102 else if (idle / 60 == 0) 103 (void)printf(" - "); 104 105 /* Else print the minutes idle. */ 106 else 107 (void)printf(" %2d ", idle / 60); 108} 109