OSDN Git Service

PR 47802 Use strftime for CTIME and FDATE intrinsics
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / ctime.c
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>
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
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
10 version 3 of the License, or (at your option) any later version.
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
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/>.  */
25
26 #include "libgfortran.h"
27
28 #include "time_1.h"
29
30 #include <string.h>
31
32
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.  */
37
38 static size_t
39 strctime (char *s, size_t max, const time_t *timep)
40 {
41 #ifdef HAVE_STRFTIME
42   struct tm res;
43   struct tm *ltm = localtime_r (timep, &res);
44   return strftime (s, max, "%c", ltm);
45 #else
46   return 0;
47 #endif
48 }
49
50 /* In the default locale, the date and time representation fits in 26
51    bytes. However, other locales might need more space.  */
52 #define CSZ 100
53
54 extern void fdate (char **, gfc_charlen_type *);
55 export_proto(fdate);
56
57 void
58 fdate (char ** date, gfc_charlen_type * date_len)
59 {
60 #if defined(HAVE_TIME)
61   time_t now = time(NULL);
62   *date = get_mem (CSZ);
63   *date_len = strctime (*date, CSZ, &now);
64 #else
65
66   *date = NULL;
67   *date_len = 0;
68 #endif
69 }
70
71
72 extern void fdate_sub (char *, gfc_charlen_type);
73 export_proto(fdate_sub);
74
75 void
76 fdate_sub (char * date, gfc_charlen_type date_len)
77 {
78 #if defined(HAVE_TIME)
79   time_t now = time(NULL);
80   char *s = get_mem (date_len + 1);
81   size_t n = strctime (s, date_len + 1, &now);
82   fstrcpy (date, date_len, s, n);
83   free (s);
84 #else
85   memset (date, ' ', date_len);
86 #endif
87 }
88
89
90
91 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
92 export_proto_np(PREFIX(ctime));
93
94 void
95 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
96 {
97 #if defined(HAVE_TIME)
98   time_t now = t;
99   *date = get_mem (CSZ);
100   *date_len = strctime (*date, CSZ, &now);
101 #else
102
103   *date = NULL;
104   *date_len = 0;
105 #endif
106 }
107
108
109 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
110 export_proto(ctime_sub);
111
112 void
113 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
114 {
115   time_t now = *t;
116   char *s = get_mem (date_len + 1);
117   size_t n = strctime (s, date_len + 1, &now);
118   fstrcpy (date, date_len, s, n);
119   free (s);
120 }