]>
Commit | Line | Data |
---|---|---|
35059811 | 1 | /* Implementation of the CTIME and FDATE g77 intrinsics. |
a5544970 | 2 | Copyright (C) 2005-2019 Free Software Foundation, Inc. |
35059811 FXC |
3 | Contributed by François-Xavier Coudert <coudert@clipper.ens.fr> |
4 | ||
b5fa435a | 5 | This file is part of the GNU Fortran runtime library (libgfortran). |
35059811 FXC |
6 | |
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 | |
748086b7 | 10 | version 3 of the License, or (at your option) any later version. |
35059811 FXC |
11 | |
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. | |
16 | ||
748086b7 JJ |
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. | |
20 | ||
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/>. */ | |
35059811 | 25 | |
35059811 FXC |
26 | #include "libgfortran.h" |
27 | ||
8f17e00c | 28 | #include "time_1.h" |
35059811 FXC |
29 | |
30 | #include <string.h> | |
31 | ||
32 | ||
ab75303f JB |
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 | |
39 | ||
40 | ||
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. */ | |
46 | ||
47 | static int | |
48 | gf_ctime (char *s, size_t max, const time_t timev) | |
b5fa435a | 49 | { |
7a9d7a4f | 50 | struct tm ltm; |
246a2730 | 51 | int failed; |
ab75303f | 52 | char buf[CTIME_BUFSZ + 1]; |
246a2730 | 53 | /* Some targets provide a localtime_r based on a draft of the POSIX |
7a9d7a4f JB |
54 | standard where the return type is int rather than the |
55 | standardized struct tm*. */ | |
ab75303f | 56 | __builtin_choose_expr (__builtin_classify_type (localtime_r (&timev, <m)) |
246a2730 | 57 | == 5, |
ab75303f JB |
58 | failed = localtime_r (&timev, <m) == NULL, |
59 | failed = localtime_r (&timev, <m) != 0); | |
246a2730 | 60 | if (failed) |
ab75303f JB |
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) | |
71 | { | |
72 | cf_strcpy (s, max, buf); | |
73 | return n; | |
74 | } | |
75 | blank: | |
76 | memset (s, ' ', max); | |
77 | return 0; | |
b5fa435a | 78 | } |
b5fa435a | 79 | |
b5fa435a | 80 | |
35059811 FXC |
81 | extern void fdate (char **, gfc_charlen_type *); |
82 | export_proto(fdate); | |
83 | ||
84 | void | |
85 | fdate (char ** date, gfc_charlen_type * date_len) | |
86 | { | |
35059811 | 87 | time_t now = time(NULL); |
ab75303f JB |
88 | *date = xmalloc (CTIME_BUFSZ); |
89 | *date_len = gf_ctime (*date, CTIME_BUFSZ, now); | |
35059811 FXC |
90 | } |
91 | ||
92 | ||
93 | extern void fdate_sub (char *, gfc_charlen_type); | |
94 | export_proto(fdate_sub); | |
95 | ||
96 | void | |
97 | fdate_sub (char * date, gfc_charlen_type date_len) | |
98 | { | |
35059811 | 99 | time_t now = time(NULL); |
ab75303f | 100 | gf_ctime (date, date_len, now); |
35059811 FXC |
101 | } |
102 | ||
103 | ||
104 | ||
105 | extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8); | |
106 | export_proto_np(PREFIX(ctime)); | |
107 | ||
108 | void | |
109 | PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t) | |
110 | { | |
35059811 | 111 | time_t now = t; |
ab75303f JB |
112 | *date = xmalloc (CTIME_BUFSZ); |
113 | *date_len = gf_ctime (*date, CTIME_BUFSZ, now); | |
35059811 FXC |
114 | } |
115 | ||
116 | ||
117 | extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type); | |
118 | export_proto(ctime_sub); | |
119 | ||
120 | void | |
121 | ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len) | |
122 | { | |
35059811 | 123 | time_t now = *t; |
ab75303f | 124 | gf_ctime (date, date_len, now); |
35059811 | 125 | } |