ctime.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Implementation of the CTIME and FDATE g77 intrinsics.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include "time_1.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. /* Maximum space a ctime-like string might need. A "normal" ctime
  25. string is 26 bytes, and in our case 24 bytes as we don't include
  26. the trailing newline and null. However, the longest possible year
  27. number is -2,147,481,748 (1900 - 2,147,483,648, since tm_year is a
  28. 32-bit signed integer) so an extra 7 bytes are needed. */
  29. #define CTIME_BUFSZ 31
  30. /* Thread-safe ctime-like function that fills a Fortran
  31. string. ctime_r is a portability headache and marked as obsolescent
  32. in POSIX 2008, which recommends strftime in its place. However,
  33. strftime(..., "%c",...) doesn't produce ctime-like output on
  34. MinGW, so do it manually with snprintf. */
  35. static int
  36. gf_ctime (char *s, size_t max, const time_t timev)
  37. {
  38. struct tm ltm;
  39. int failed;
  40. char buf[CTIME_BUFSZ + 1];
  41. /* Some targets provide a localtime_r based on a draft of the POSIX
  42. standard where the return type is int rather than the
  43. standardized struct tm*. */
  44. __builtin_choose_expr (__builtin_classify_type (localtime_r (&timev, &ltm))
  45. == 5,
  46. failed = localtime_r (&timev, &ltm) == NULL,
  47. failed = localtime_r (&timev, &ltm) != 0);
  48. if (failed)
  49. goto blank;
  50. int n = snprintf (buf, sizeof (buf),
  51. "%3.3s %3.3s%3d %.2d:%.2d:%.2d %d",
  52. "SunMonTueWedThuFriSat" + ltm.tm_wday * 3,
  53. "JanFebMarAprMayJunJulAugSepOctNovDec" + ltm.tm_mon * 3,
  54. ltm.tm_mday, ltm.tm_hour, ltm.tm_min, ltm.tm_sec,
  55. 1900 + ltm.tm_year);
  56. if (n < 0)
  57. goto blank;
  58. if ((size_t) n <= max)
  59. {
  60. cf_strcpy (s, max, buf);
  61. return n;
  62. }
  63. blank:
  64. memset (s, ' ', max);
  65. return 0;
  66. }
  67. extern void fdate (char **, gfc_charlen_type *);
  68. export_proto(fdate);
  69. void
  70. fdate (char ** date, gfc_charlen_type * date_len)
  71. {
  72. time_t now = time(NULL);
  73. *date = xmalloc (CTIME_BUFSZ);
  74. *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
  75. }
  76. extern void fdate_sub (char *, gfc_charlen_type);
  77. export_proto(fdate_sub);
  78. void
  79. fdate_sub (char * date, gfc_charlen_type date_len)
  80. {
  81. time_t now = time(NULL);
  82. gf_ctime (date, date_len, now);
  83. }
  84. extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
  85. export_proto_np(PREFIX(ctime));
  86. void
  87. PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
  88. {
  89. time_t now = t;
  90. *date = xmalloc (CTIME_BUFSZ);
  91. *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
  92. }
  93. extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
  94. export_proto(ctime_sub);
  95. void
  96. ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
  97. {
  98. time_t now = *t;
  99. gf_ctime (date, date_len, now);
  100. }