2011-03-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / intrinsics / ctime.c
blob92c0431357e5c0a25ac074954abe47b51237cea3
1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2 Copyright (C) 2005, 2007, 2009, 2011 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 /* strftime-like function that fills a C string with %c format which
34 is identical to ctime in the default locale. As ctime and ctime_r
35 are poorly specified and their usage not recommended, the
36 implementation instead uses strftime. */
38 static size_t
39 strctime (char *s, size_t max, const time_t *timep)
41 #ifdef HAVE_STRFTIME
42 struct tm ltm;
43 int failed;
44 /* Some targets provide a localtime_r based on a draft of the POSIX
45 standard where the return type is int rather than the
46 standardized struct tm*. */
47 __builtin_choose_expr (__builtin_classify_type (localtime_r (timep, &ltm))
48 == 5,
49 failed = localtime_r (timep, &ltm) == NULL,
50 failed = localtime_r (timep, &ltm) != 0);
51 if (failed)
52 return 0;
53 return strftime (s, max, "%c", &ltm);
54 #else
55 return 0;
56 #endif
59 /* In the default locale, the date and time representation fits in 26
60 bytes. However, other locales might need more space. */
61 #define CSZ 100
63 extern void fdate (char **, gfc_charlen_type *);
64 export_proto(fdate);
66 void
67 fdate (char ** date, gfc_charlen_type * date_len)
69 #if defined(HAVE_TIME)
70 time_t now = time(NULL);
71 *date = get_mem (CSZ);
72 *date_len = strctime (*date, CSZ, &now);
73 #else
75 *date = NULL;
76 *date_len = 0;
77 #endif
81 extern void fdate_sub (char *, gfc_charlen_type);
82 export_proto(fdate_sub);
84 void
85 fdate_sub (char * date, gfc_charlen_type date_len)
87 #if defined(HAVE_TIME)
88 time_t now = time(NULL);
89 char *s = get_mem (date_len + 1);
90 size_t n = strctime (s, date_len + 1, &now);
91 fstrcpy (date, date_len, s, n);
92 free (s);
93 #else
94 memset (date, ' ', date_len);
95 #endif
100 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
101 export_proto_np(PREFIX(ctime));
103 void
104 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
106 #if defined(HAVE_TIME)
107 time_t now = t;
108 *date = get_mem (CSZ);
109 *date_len = strctime (*date, CSZ, &now);
110 #else
112 *date = NULL;
113 *date_len = 0;
114 #endif
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 char *s = get_mem (date_len + 1);
126 size_t n = strctime (s, date_len + 1, &now);
127 fstrcpy (date, date_len, s, n);
128 free (s);