xref: /original-bsd/libexec/getty/get_date.c (revision 4ec22e22)
1 /*
2  * Copyright (c) 1985 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)get_date.c	5.4 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include <stdio.h>
13 #include <sys/time.h>
14 
15 static char *days[] = {
16 	"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"
17 };
18 
19 static char *months[] = {
20 	"Jan", "Feb", "Mar", "Apr", "May", "June",
21 	"July", "Aug", "Sept", "Oct", "Nov", "Dec"
22 };
23 
24 #define AM "am"
25 #define PM "pm"
26 
27 get_date(datebuffer)
28 	char *datebuffer;
29 {
30 	struct tm *localtime(), *tmp;
31 	struct timeval tv;
32 	int realhour;
33 	char *zone;
34 
35 	gettimeofday(&tv, 0);
36 	tmp = localtime(&tv.tv_sec);
37 
38 	realhour = tmp->tm_hour;
39 	zone = AM;			/* default to morning */
40 	if (tmp->tm_hour == 0)
41 		realhour = 12;		/* midnight */
42 	else if (tmp->tm_hour == 12)
43 		zone = PM;		/* noon */
44 	else if (tmp->tm_hour >= 13 && tmp->tm_hour <= 23) { /* afternoon */
45 		realhour = realhour - 12;
46 		zone = PM;
47 	}
48 
49 	/* format is '8:10pm on Sunday, 16 Sept 1973' */
50 
51 	(void)sprintf(datebuffer, "%d:%02d%s on %s, %d %s %d",
52 		realhour,
53 		tmp->tm_min,
54 		zone,
55 		days[tmp->tm_wday],
56 		tmp->tm_mday,
57 		months[tmp->tm_mon],
58 		1900 + tmp->tm_year);
59 }
60