* tree-outof-ssa.h (ssaexpand): Add partitions_for_undefined_values.
[official-gcc.git] / libgfortran / intrinsics / ctime.c
blob755a86a3c2966b212524e65c7647c556459ef967
1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
28 #include "time_1.h"
30 #include <string.h>
33 /* Maximum space a ctime-like string might need. A "normal" ctime
34 string is 26 bytes, and in our case 24 bytes as we don't include
35 the trailing newline and null. However, the longest possible year
36 number is -2,147,481,748 (1900 - 2,147,483,648, since tm_year is a
37 32-bit signed integer) so an extra 7 bytes are needed. */
38 #define CTIME_BUFSZ 31
41 /* Thread-safe ctime-like function that fills a Fortran
42 string. ctime_r is a portability headache and marked as obsolescent
43 in POSIX 2008, which recommends strftime in its place. However,
44 strftime(..., "%c",...) doesn't produce ctime-like output on
45 MinGW, so do it manually with snprintf. */
47 static int
48 gf_ctime (char *s, size_t max, const time_t timev)
50 struct tm ltm;
51 int failed;
52 char buf[CTIME_BUFSZ + 1];
53 /* Some targets provide a localtime_r based on a draft of the POSIX
54 standard where the return type is int rather than the
55 standardized struct tm*. */
56 __builtin_choose_expr (__builtin_classify_type (localtime_r (&timev, &ltm))
57 == 5,
58 failed = localtime_r (&timev, &ltm) == NULL,
59 failed = localtime_r (&timev, &ltm) != 0);
60 if (failed)
61 goto blank;
62 int n = snprintf (buf, sizeof (buf),
63 "%3.3s %3.3s%3d %.2d:%.2d:%.2d %d",
64 "SunMonTueWedThuFriSat" + ltm.tm_wday * 3,
65 "JanFebMarAprMayJunJulAugSepOctNovDec" + ltm.tm_mon * 3,
66 ltm.tm_mday, ltm.tm_hour, ltm.tm_min, ltm.tm_sec,
67 1900 + ltm.tm_year);
68 if (n < 0)
69 goto blank;
70 if ((size_t) n <= max)
72 cf_strcpy (s, max, buf);
73 return n;
75 blank:
76 memset (s, ' ', max);
77 return 0;
81 extern void fdate (char **, gfc_charlen_type *);
82 export_proto(fdate);
84 void
85 fdate (char ** date, gfc_charlen_type * date_len)
87 time_t now = time(NULL);
88 *date = xmalloc (CTIME_BUFSZ);
89 *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
93 extern void fdate_sub (char *, gfc_charlen_type);
94 export_proto(fdate_sub);
96 void
97 fdate_sub (char * date, gfc_charlen_type date_len)
99 time_t now = time(NULL);
100 gf_ctime (date, date_len, now);
105 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
106 export_proto_np(PREFIX(ctime));
108 void
109 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
111 time_t now = t;
112 *date = xmalloc (CTIME_BUFSZ);
113 *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
117 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
118 export_proto(ctime_sub);
120 void
121 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
123 time_t now = *t;
124 gf_ctime (date, date_len, now);