xref: /csrg-svn/usr.bin/w/pr_time.c (revision 66701)
141390Smarc /*-
2*66701Spendry  * Copyright (c) 1990, 1993, 1994
362433Sbostic  *	The Regents of the University of California.  All rights reserved.
441390Smarc  *
541390Smarc  * %sccs.include.redist.c%
641390Smarc  */
741390Smarc 
841390Smarc #ifndef lint
9*66701Spendry static char sccsid[] = "@(#)pr_time.c	8.2 (Berkeley) 04/04/94";
1041390Smarc #endif /* not lint */
1141390Smarc 
1241390Smarc #include <sys/types.h>
1341390Smarc #include <sys/time.h>
1459363Sbostic 
1541390Smarc #include <stdio.h>
1645158Smarc #include <string.h>
1759363Sbostic #include <tzfile.h>
1841390Smarc 
1959363Sbostic #include "extern.h"
2041390Smarc 
2141390Smarc /*
2259363Sbostic  * pr_attime --
2359363Sbostic  *	Print the time since the user logged in.
2459372Sbostic  *
2559372Sbostic  *	Note: SCCS forces the bizarre string manipulation, things like
26*66701Spendry  *	8.2 get replaced in the source code.
2741390Smarc  */
2859363Sbostic void
pr_attime(started,now)2959363Sbostic pr_attime(started, now)
3059363Sbostic 	time_t *started, *now;
3141390Smarc {
3259363Sbostic 	static char buf[256];
3359363Sbostic 	struct tm *tp;
3459363Sbostic 	time_t diff;
3559372Sbostic 	char fmt[20];
3641390Smarc 
3759363Sbostic 	tp = localtime(started);
3859363Sbostic 	diff = *now - *started;
3959363Sbostic 
4059363Sbostic 	/* If more than a week, use day-month-year. */
4159363Sbostic 	if (diff > SECSPERDAY * DAYSPERWEEK)
4259372Sbostic 		(void)strcpy(fmt, "%d%b%y");
4359363Sbostic 
4459363Sbostic 	/* If not today, use day-hour-am/pm. */
4559372Sbostic 	else if (*now / SECSPERDAY != *started / SECSPERDAY) {
46*66701Spendry 		(void)strcpy(fmt, __CONCAT("%a%", "I%p"));
4759372Sbostic 	}
4859363Sbostic 
4959372Sbostic 	/* Default is hh:mm{am,pm}. */
5059372Sbostic 	else {
51*66701Spendry 		(void)strcpy(fmt, __CONCAT("%l:%", "M%p"));
5259372Sbostic 	}
5359363Sbostic 
5459372Sbostic 	(void)strftime(buf, sizeof(buf), fmt, tp);
5559363Sbostic 	(void)printf("%s", buf);
5641390Smarc }
5741390Smarc 
5859363Sbostic /*
5959363Sbostic  * pr_idle --
6059363Sbostic  *	Display the idle time.
6159363Sbostic  */
6259363Sbostic void
pr_idle(idle)6359363Sbostic pr_idle(idle)
6459363Sbostic 	time_t idle;
6541390Smarc {
6659363Sbostic 	/* If idle more than 36 hours, print as a number of days. */
6759363Sbostic 	if (idle >= 36 * SECSPERHOUR)
6859363Sbostic 		(void)printf(" %ddays ", idle / SECSPERDAY);
6941390Smarc 
7059363Sbostic 	/* If idle more than an hour, print as HH:MM. */
7159363Sbostic 	else if (idle >= SECSPERHOUR)
7259363Sbostic 		(void)printf(" %2d:%02d ",
7359363Sbostic 		    idle / SECSPERHOUR, (idle % SECSPERHOUR) / SECSPERMIN);
7459363Sbostic 
7559363Sbostic 	/* Else print the minutes idle. */
7641390Smarc 	else
7759363Sbostic 		(void)printf("    %2d ", idle / SECSPERMIN);
7841390Smarc }
79