2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgfortran / intrinsics / ctime.c
blob5a762844aac7a50437635345b66160cfc11834f7
1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2 Copyright (C) 2005-2013 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 /* strftime-like function that fills a C string with %c format which
35 is identical to ctime in the default locale. As ctime and ctime_r
36 are poorly specified and their usage not recommended, the
37 implementation instead uses strftime. */
39 static size_t
40 strctime (char *s, size_t max, const time_t *timep)
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);
56 /* In the default locale, the date and time representation fits in 26
57 bytes. However, other locales might need more space. */
58 #define CSZ 100
60 extern void fdate (char **, gfc_charlen_type *);
61 export_proto(fdate);
63 void
64 fdate (char ** date, gfc_charlen_type * date_len)
66 time_t now = time(NULL);
67 *date = xmalloc (CSZ);
68 *date_len = strctime (*date, CSZ, &now);
72 extern void fdate_sub (char *, gfc_charlen_type);
73 export_proto(fdate_sub);
75 void
76 fdate_sub (char * date, gfc_charlen_type date_len)
78 time_t now = time(NULL);
79 char *s = xmalloc (date_len + 1);
80 size_t n = strctime (s, date_len + 1, &now);
81 fstrcpy (date, date_len, s, n);
82 free (s);
87 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
88 export_proto_np(PREFIX(ctime));
90 void
91 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
93 time_t now = t;
94 *date = xmalloc (CSZ);
95 *date_len = strctime (*date, CSZ, &now);
99 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
100 export_proto(ctime_sub);
102 void
103 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
105 time_t now = *t;
106 char *s = xmalloc (date_len + 1);
107 size_t n = strctime (s, date_len + 1, &now);
108 fstrcpy (date, date_len, s, n);
109 free (s);