summaryrefslogtreecommitdiffstats
path: root/src/show-date.c
blob: ffa3d02b7cb4ca7b8140e250ae93c3e00d06fe4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <config.h>
#include <stdio.h>

#include "system.h"
#include "fprintftime.h"
#include "parse-datetime.h"
#include "quote.h"
#include "show-date.h"
#include "stat-time.h"

/* Display the date and/or time in WHEN according to the format specified
   in FORMAT, followed by a newline.

   If successful, return true.
   If unsuccessful, prints an error message to STDERR and returns false.
   If unsuccessful and ON_ERROR_PRINT_UNFORMATTED, also prints WHEN.TV_SEC
   to STDOUT.  */

extern bool
show_date (char const *format, struct timespec when, timezone_t tz)
{
  struct tm tm;

  if (localtime_rz (tz, &when.tv_sec, &tm))
    {
      fprintftime (stdout, format, &tm, tz, when.tv_nsec);
      return true;
    }
  else
    {
      char buf[INT_BUFSIZE_BOUND (intmax_t)];
      error (0, 0, _("time %s is out of range"),
             quote (timetostr (when.tv_sec, buf)));
      return false;
    }
}