xref: /freebsd/contrib/ntp/libntp/humandate.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1c0b746e5SOllivier Robert /*
2*2b15cb3dSCy Schubert  * humandate.c - convert an NTP (or the current) time to something readable
3c0b746e5SOllivier Robert  */
4*2b15cb3dSCy Schubert #include <config.h>
5c0b746e5SOllivier Robert #include <stdio.h>
6*2b15cb3dSCy Schubert 
7c0b746e5SOllivier Robert #include "ntp_fp.h"
8224ba2bdSOllivier Robert #include "ntp_unixtime.h"	/* includes <sys/time.h> and <time.h> */
9c0b746e5SOllivier Robert #include "ntp_stdlib.h"
10c0b746e5SOllivier Robert 
11c0b746e5SOllivier Robert 
12c0b746e5SOllivier Robert /* This is used in msyslog.c; we don't want to clutter up the log with
13c0b746e5SOllivier Robert    the year and day of the week, etc.; just the minimal date and time.  */
14c0b746e5SOllivier Robert 
15*2b15cb3dSCy Schubert const char *
16c0b746e5SOllivier Robert humanlogtime(void)
17c0b746e5SOllivier Robert {
18c0b746e5SOllivier Robert 	char *		bp;
19*2b15cb3dSCy Schubert 	time_t		cursec;
20ea906c41SOllivier Robert 	struct tm *	tm;
21c0b746e5SOllivier Robert 
22*2b15cb3dSCy Schubert 	cursec = time(NULL);
23ea906c41SOllivier Robert 	tm = localtime(&cursec);
249c2daa00SOllivier Robert 	if (!tm)
259c2daa00SOllivier Robert 		return "-- --- --:--:--";
269c2daa00SOllivier Robert 
27c0b746e5SOllivier Robert 	LIB_GETBUF(bp);
28c0b746e5SOllivier Robert 
29*2b15cb3dSCy Schubert 	snprintf(bp, LIB_BUFLENGTH, "%2d %s %02d:%02d:%02d",
30c0b746e5SOllivier Robert 		 tm->tm_mday, months[tm->tm_mon],
31c0b746e5SOllivier Robert 		 tm->tm_hour, tm->tm_min, tm->tm_sec);
32c0b746e5SOllivier Robert 
33c0b746e5SOllivier Robert 	return bp;
34c0b746e5SOllivier Robert }
35*2b15cb3dSCy Schubert 
36*2b15cb3dSCy Schubert 
37*2b15cb3dSCy Schubert /*
38*2b15cb3dSCy Schubert  * humantime() -- like humanlogtime() but without date, and with the
39*2b15cb3dSCy Schubert  *		  time to display given as an argument.
40*2b15cb3dSCy Schubert  */
41*2b15cb3dSCy Schubert const char *
42*2b15cb3dSCy Schubert humantime(
43*2b15cb3dSCy Schubert 	time_t cursec
44*2b15cb3dSCy Schubert 	)
45*2b15cb3dSCy Schubert {
46*2b15cb3dSCy Schubert 	char *		bp;
47*2b15cb3dSCy Schubert 	struct tm *	tm;
48*2b15cb3dSCy Schubert 
49*2b15cb3dSCy Schubert 	tm = localtime(&cursec);
50*2b15cb3dSCy Schubert 	if (!tm)
51*2b15cb3dSCy Schubert 		return "--:--:--";
52*2b15cb3dSCy Schubert 
53*2b15cb3dSCy Schubert 	LIB_GETBUF(bp);
54*2b15cb3dSCy Schubert 
55*2b15cb3dSCy Schubert 	snprintf(bp, LIB_BUFLENGTH, "%02d:%02d:%02d",
56*2b15cb3dSCy Schubert 		 tm->tm_hour, tm->tm_min, tm->tm_sec);
57*2b15cb3dSCy Schubert 
58*2b15cb3dSCy Schubert 	return bp;
59*2b15cb3dSCy Schubert }
60