Merge -r 127928:132243 from trunk
[official-gcc.git] / libgfortran / intrinsics / ctime.c
blobab9cfffe3f3a5dd93a044110a881873fef0a5fbc
1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
33 #ifdef TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 # else
40 # ifdef HAVE_TIME_H
41 # include <time.h>
42 # endif
43 # endif
44 #endif
46 #include <string.h>
49 extern void fdate (char **, gfc_charlen_type *);
50 export_proto(fdate);
52 void
53 fdate (char ** date, gfc_charlen_type * date_len)
55 #if defined(HAVE_TIME) && defined(HAVE_CTIME)
56 int i;
57 time_t now = time(NULL);
58 *date = ctime (&now);
59 if (*date != NULL)
61 *date = strdup (*date);
62 *date_len = strlen (*date);
64 i = 0;
65 while ((*date)[i])
67 if ((*date)[i] == '\n')
68 (*date)[i] = ' ';
69 i++;
71 return;
73 #endif
75 *date = NULL;
76 *date_len = 0;
80 extern void fdate_sub (char *, gfc_charlen_type);
81 export_proto(fdate_sub);
83 void
84 fdate_sub (char * date, gfc_charlen_type date_len)
86 #if defined(HAVE_TIME) && defined(HAVE_CTIME)
87 int i;
88 char *d;
89 time_t now = time(NULL);
90 #endif
92 memset (date, ' ', date_len);
93 #if defined(HAVE_TIME) && defined(HAVE_CTIME)
94 d = ctime (&now);
95 if (d != NULL)
97 i = 0;
98 while (*d && *d != '\n' && i < date_len)
99 date[i++] = *(d++);
101 #endif
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 #if defined(HAVE_CTIME)
113 time_t now = t;
114 int i;
115 *date = ctime (&now);
116 if (*date != NULL)
118 *date = strdup (*date);
119 *date_len = strlen (*date);
121 i = 0;
122 while ((*date)[i])
124 if ((*date)[i] == '\n')
125 (*date)[i] = ' ';
126 i++;
128 return;
130 #endif
132 *date = NULL;
133 *date_len = 0;
137 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
138 export_proto(ctime_sub);
140 void
141 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
143 #if defined(HAVE_CTIME)
144 int i;
145 char *d;
146 time_t now = *t;
147 #endif
149 memset (date, ' ', date_len);
150 #if defined(HAVE_CTIME)
151 d = ctime (&now);
152 if (d != NULL)
154 i = 0;
155 while (*d && *d != '\n' && i < date_len)
156 date[i++] = *(d++);
158 #endif