Imported Upstream version 1.22.4
[platform/upstream/groff.git] / src / libs / libgroff / curtime.cpp
1 /* Copyright (C) 2015-2018 Free Software Foundation, Inc.
2
3 This file is part of groff.
4
5 groff is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 groff is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 for more details.
14
15 The GNU General Public License version 2 (GPL2) is available in the
16 internet at <http://www.gnu.org/licenses/gpl-2.0.txt>. */
17
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "errarg.h"
25 #include "error.h"
26
27 #ifdef LONG_FOR_TIME_T
28 long
29 #else
30 time_t
31 #endif
32 current_time()
33 {
34   char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
35
36   if (source_date_epoch) {
37     errno = 0;
38     char *endptr;
39     long epoch = strtol(source_date_epoch, &endptr, 10);
40
41     if ((errno == ERANGE && (epoch == LONG_MAX || epoch == LONG_MIN)) ||
42         (errno != 0 && epoch == 0))
43       fatal("$SOURCE_DATE_EPOCH: strtol: %1", strerror(errno));
44     if (endptr == source_date_epoch)
45       fatal("$SOURCE_DATE_EPOCH: no digits found: %1", endptr);
46     if (*endptr != '\0')
47       fatal("$SOURCE_DATE_EPOCH: trailing garbage: %1", endptr);
48     return epoch;
49   } else
50     return time(0);
51 }