* gcc-interface/decl.c (gnat_to_gnu_entity): Adjust comment.
[official-gcc.git] / libgfortran / intrinsics / ctime.c
blob9cda39b0e9ab5912448604cbfdd80679af3d40ba
1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2 Copyright (C) 2005-2014 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 <stdlib.h>
31 #include <string.h>
34 /* Maximum space a ctime-like string might need. A "normal" ctime
35 string is 26 bytes, and in our case 24 bytes as we don't include
36 the trailing newline and null. However, the longest possible year
37 number is -2,147,481,748 (1900 - 2,147,483,648, since tm_year is a
38 32-bit signed integer) so an extra 7 bytes are needed. */
39 #define CTIME_BUFSZ 31
42 /* Thread-safe ctime-like function that fills a Fortran
43 string. ctime_r is a portability headache and marked as obsolescent
44 in POSIX 2008, which recommends strftime in its place. However,
45 strftime(..., "%c",...) doesn't produce ctime-like output on
46 MinGW, so do it manually with snprintf. */
48 static int
49 gf_ctime (char *s, size_t max, const time_t timev)
51 struct tm ltm;
52 int failed;
53 char buf[CTIME_BUFSZ + 1];
54 /* Some targets provide a localtime_r based on a draft of the POSIX
55 standard where the return type is int rather than the
56 standardized struct tm*. */
57 __builtin_choose_expr (__builtin_classify_type (localtime_r (&timev, &ltm))
58 == 5,
59 failed = localtime_r (&timev, &ltm) == NULL,
60 failed = localtime_r (&timev, &ltm) != 0);
61 if (failed)
62 goto blank;
63 int n = snprintf (buf, sizeof (buf),
64 "%3.3s %3.3s%3d %.2d:%.2d:%.2d %d",
65 "SunMonTueWedThuFriSat" + ltm.tm_wday * 3,
66 "JanFebMarAprMayJunJulAugSepOctNovDec" + ltm.tm_mon * 3,
67 ltm.tm_mday, ltm.tm_hour, ltm.tm_min, ltm.tm_sec,
68 1900 + ltm.tm_year);
69 if (n < 0)
70 goto blank;
71 if ((size_t) n <= max)
73 cf_strcpy (s, max, buf);
74 return n;
76 blank:
77 memset (s, ' ', max);
78 return 0;
82 extern void fdate (char **, gfc_charlen_type *);
83 export_proto(fdate);
85 void
86 fdate (char ** date, gfc_charlen_type * date_len)
88 time_t now = time(NULL);
89 *date = xmalloc (CTIME_BUFSZ);
90 *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
94 extern void fdate_sub (char *, gfc_charlen_type);
95 export_proto(fdate_sub);
97 void
98 fdate_sub (char * date, gfc_charlen_type date_len)
100 time_t now = time(NULL);
101 gf_ctime (date, date_len, now);
106 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
107 export_proto_np(PREFIX(ctime));
109 void
110 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
112 time_t now = t;
113 *date = xmalloc (CTIME_BUFSZ);
114 *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
118 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
119 export_proto(ctime_sub);
121 void
122 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
124 time_t now = *t;
125 gf_ctime (date, date_len, now);