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