1/*
2 * Copyright (c) 1999, 2009 Todd C. Miller <Todd.Miller@courtesan.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <config.h>
18
19#include <sys/types.h>
20#include <stdio.h>
21#ifdef STDC_HEADERS
22# include <stdlib.h>
23# include <stddef.h>
24#else
25# ifdef HAVE_STDLIB_H
26#  include <stdlib.h>
27# endif
28#endif /* STDC_HEADERS */
29#include <time.h>
30
31#include "missing.h"
32
33char *get_timestr	__P((time_t, int));
34
35/*
36 * Return an ascii string with the current date + time
37 * Uses strftime() if available, else falls back to ctime().
38 */
39char *
40get_timestr(tstamp, log_year)
41    time_t tstamp;
42    int log_year;
43{
44    char *s;
45#ifdef HAVE_STRFTIME
46    static char buf[128];
47    struct tm *timeptr;
48
49    timeptr = localtime(&tstamp);
50    if (log_year)
51	s = "%h %e %T %Y";
52    else
53	s = "%h %e %T";
54
55    /* strftime() does not guarantee to NUL-terminate so we must check. */
56    buf[sizeof(buf) - 1] = '\0';
57    if (strftime(buf, sizeof(buf), s, timeptr) && buf[sizeof(buf) - 1] == '\0')
58	return buf;
59
60#endif /* HAVE_STRFTIME */
61
62    s = ctime(&tstamp) + 4;		/* skip day of the week */
63    if (log_year)
64	s[20] = '\0';			/* avoid the newline */
65    else
66	s[15] = '\0';			/* don't care about year */
67
68    return s;
69}
70