xref: /netbsd-src/external/bsd/ntp/dist/libntp/humandate.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: humandate.c,v 1.10 2024/08/18 20:47:13 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
42950cc38Schristos  * humandate.c - convert an NTP (or the current) time to something readable
5abb0f93cSkardel  */
62950cc38Schristos #include <config.h>
7abb0f93cSkardel #include <stdio.h>
82950cc38Schristos 
9abb0f93cSkardel #include "ntp_fp.h"
10abb0f93cSkardel #include "ntp_unixtime.h"	/* includes <sys/time.h> and <time.h> */
11abb0f93cSkardel #include "ntp_stdlib.h"
12abb0f93cSkardel 
13abb0f93cSkardel 
14abb0f93cSkardel /* This is used in msyslog.c; we don't want to clutter up the log with
15abb0f93cSkardel    the year and day of the week, etc.; just the minimal date and time.  */
16abb0f93cSkardel 
17e19314b7Schristos const char *
18abb0f93cSkardel humanlogtime(void)
19abb0f93cSkardel {
20abb0f93cSkardel 	char *		bp;
213123f114Skardel 	time_t		cursec;
22abb0f93cSkardel 	struct tm *	tm;
23abb0f93cSkardel 
243123f114Skardel 	cursec = time(NULL);
25abb0f93cSkardel 	tm = localtime(&cursec);
26abb0f93cSkardel 	if (!tm)
27abb0f93cSkardel 		return "-- --- --:--:--";
28abb0f93cSkardel 
29abb0f93cSkardel 	LIB_GETBUF(bp);
30abb0f93cSkardel 
313123f114Skardel 	snprintf(bp, LIB_BUFLENGTH, "%2d %s %02d:%02d:%02d",
32abb0f93cSkardel 		 tm->tm_mday, months[tm->tm_mon],
33abb0f93cSkardel 		 tm->tm_hour, tm->tm_min, tm->tm_sec);
34abb0f93cSkardel 
35abb0f93cSkardel 	return bp;
36abb0f93cSkardel }
372950cc38Schristos 
382950cc38Schristos 
392950cc38Schristos /*
402950cc38Schristos  * humantime() -- like humanlogtime() but without date, and with the
412950cc38Schristos  *		  time to display given as an argument.
422950cc38Schristos  */
432950cc38Schristos const char *
442950cc38Schristos humantime(
452950cc38Schristos 	time_t cursec
462950cc38Schristos 	)
472950cc38Schristos {
482950cc38Schristos 	char *		bp;
492950cc38Schristos 	struct tm *	tm;
502950cc38Schristos 
512950cc38Schristos 	tm = localtime(&cursec);
522950cc38Schristos 	if (!tm)
532950cc38Schristos 		return "--:--:--";
542950cc38Schristos 
552950cc38Schristos 	LIB_GETBUF(bp);
562950cc38Schristos 
572950cc38Schristos 	snprintf(bp, LIB_BUFLENGTH, "%02d:%02d:%02d",
582950cc38Schristos 		 tm->tm_hour, tm->tm_min, tm->tm_sec);
592950cc38Schristos 
602950cc38Schristos 	return bp;
612950cc38Schristos }
62