ctime.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Implementation of the CTIME and FDATE g77 intrinsics.
  2. Copyright (C) 2005-2022 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 <string.h>
  23. /* Maximum space a ctime-like string might need. A "normal" ctime
  24. string is 26 bytes, and in our case 24 bytes as we don't include
  25. the trailing newline and null. However, the longest possible year
  26. number is -2,147,481,748 (1900 - 2,147,483,648, since tm_year is a
  27. 32-bit signed integer) so an extra 7 bytes are needed. */
  28. #define CTIME_BUFSZ 31
  29. /* Thread-safe ctime-like function that fills a Fortran
  30. string. ctime_r is a portability headache and marked as obsolescent
  31. in POSIX 2008, which recommends strftime in its place. However,
  32. strftime(..., "%c",...) doesn't produce ctime-like output on
  33. MinGW, so do it manually with snprintf. */
  34. static int
  35. gf_ctime (char *s, size_t max, const time_t timev)
  36. {
  37. struct tm ltm;
  38. int failed;
  39. char buf[CTIME_BUFSZ + 1];
  40. /* Some targets provide a localtime_r based on a draft of the POSIX
  41. standard where the return type is int rather than the
  42. standardized struct tm*. */
  43. __builtin_choose_expr (__builtin_classify_type (localtime_r (&timev, &ltm))
  44. == 5,
  45. failed = localtime_r (&timev, &ltm) == NULL,
  46. failed = localtime_r (&timev, &ltm) != 0);
  47. if (failed)
  48. goto blank;
  49. int n = snprintf (buf, sizeof (buf),
  50. "%3.3s %3.3s%3d %.2d:%.2d:%.2d %d",
  51. "SunMonTueWedThuFriSat" + ltm.tm_wday * 3,
  52. "JanFebMarAprMayJunJulAugSepOctNovDec" + ltm.tm_mon * 3,
  53. ltm.tm_mday, ltm.tm_hour, ltm.tm_min, ltm.tm_sec,
  54. 1900 + ltm.tm_year);
  55. if (n < 0)
  56. goto blank;
  57. if ((size_t) n <= max)
  58. {
  59. cf_strcpy (s, max, buf);
  60. return n;
  61. }
  62. blank:
  63. memset (s, ' ', max);
  64. return 0;
  65. }
  66. extern void fdate (char **, gfc_charlen_type *);
  67. export_proto(fdate);
  68. void
  69. fdate (char ** date, gfc_charlen_type * date_len)
  70. {
  71. time_t now = time(NULL);
  72. *date = xmalloc (CTIME_BUFSZ);
  73. *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
  74. }
  75. extern void fdate_sub (char *, gfc_charlen_type);
  76. export_proto(fdate_sub);
  77. void
  78. fdate_sub (char * date, gfc_charlen_type date_len)
  79. {
  80. time_t now = time(NULL);
  81. gf_ctime (date, date_len, now);
  82. }
  83. extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
  84. export_proto_np(PREFIX(ctime));
  85. void
  86. PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
  87. {
  88. time_t now = t;
  89. *date = xmalloc (CTIME_BUFSZ);
  90. *date_len = gf_ctime (*date, CTIME_BUFSZ, now);
  91. }
  92. extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
  93. export_proto(ctime_sub);
  94. void
  95. ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
  96. {
  97. time_t now = *t;
  98. gf_ctime (date, date_len, now);
  99. }