date_and_time.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* Implementation of the DATE_AND_TIME intrinsic.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Contributed by Steven Bosscher.
  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 <string.h>
  22. #include <assert.h>
  23. #include "time_1.h"
  24. /* If the re-entrant version of gmtime is not available, provide a
  25. fallback implementation. On some targets where the _r version is
  26. not available, gmtime uses thread-local storage so it's
  27. threadsafe. */
  28. #ifndef HAVE_GMTIME_R
  29. /* If _POSIX is defined gmtime_r gets defined by mingw-w64 headers. */
  30. #ifdef gmtime_r
  31. #undef gmtime_r
  32. #endif
  33. static struct tm *
  34. gmtime_r (const time_t * timep, struct tm * result)
  35. {
  36. *result = *gmtime (timep);
  37. return result;
  38. }
  39. #endif
  40. /* DATE_AND_TIME ([DATE, TIME, ZONE, VALUES])
  41. Description: Returns data on the real-time clock and date in a form
  42. compatible with the representations defined in ISO 8601:1988.
  43. Class: Non-elemental subroutine.
  44. Arguments:
  45. DATE (optional) shall be scalar and of type default character.
  46. It is an INTENT(OUT) argument. It is assigned a value of the
  47. form CCYYMMDD, where CC is the century, YY the year within the
  48. century, MM the month within the year, and DD the day within the
  49. month. If there is no date available, they are assigned blanks.
  50. TIME (optional) shall be scalar and of type default character.
  51. It is an INTENT(OUT) argument. It is assigned a value of the
  52. form hhmmss.sss, where hh is the hour of the day, mm is the
  53. minutes of the hour, and ss.sss is the seconds and milliseconds
  54. of the minute. If there is no clock available, they are assigned
  55. blanks.
  56. ZONE (optional) shall be scalar and of type default character.
  57. It is an INTENT(OUT) argument. It is assigned a value of the
  58. form [+-]hhmm, where hh and mm are the time difference with
  59. respect to Coordinated Universal Time (UTC) in hours and parts
  60. of an hour expressed in minutes, respectively. If there is no
  61. clock available, they are assigned blanks.
  62. VALUES (optional) shall be of type default integer and of rank
  63. one. It is an INTENT(OUT) argument. Its size shall be at least
  64. 8. The values returned in VALUES are as follows:
  65. VALUES(1) the year (for example, 2003), or -HUGE(0) if there is
  66. no date available;
  67. VALUES(2) the month of the year, or -HUGE(0) if there
  68. is no date available;
  69. VALUES(3) the day of the month, or -HUGE(0) if there is no date
  70. available;
  71. VALUES(4) the time difference with respect to Coordinated
  72. Universal Time (UTC) in minutes, or -HUGE(0) if this information
  73. is not available;
  74. VALUES(5) the hour of the day, in the range of 0 to 23, or
  75. -HUGE(0) if there is no clock;
  76. VALUES(6) the minutes of the hour, in the range 0 to 59, or
  77. -HUGE(0) if there is no clock;
  78. VALUES(7) the seconds of the minute, in the range 0 to 60, or
  79. -HUGE(0) if there is no clock;
  80. VALUES(8) the milliseconds of the second, in the range 0 to
  81. 999, or -HUGE(0) if there is no clock.
  82. NULL pointer represent missing OPTIONAL arguments. All arguments
  83. have INTENT(OUT). Because of the -i8 option, we must implement
  84. VALUES for INTEGER(kind=4) and INTEGER(kind=8).
  85. Based on libU77's date_time_.c.
  86. */
  87. #define DATE_LEN 8
  88. #define TIME_LEN 10
  89. #define ZONE_LEN 5
  90. #define VALUES_SIZE 8
  91. extern void date_and_time (char *, char *, char *, gfc_array_i4 *,
  92. GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
  93. export_proto(date_and_time);
  94. void
  95. date_and_time (char *__date, char *__time, char *__zone,
  96. gfc_array_i4 *__values, GFC_INTEGER_4 __date_len,
  97. GFC_INTEGER_4 __time_len, GFC_INTEGER_4 __zone_len)
  98. {
  99. int i, delta_day;
  100. char date[DATE_LEN + 1];
  101. char timec[TIME_LEN + 1];
  102. char zone[ZONE_LEN + 1];
  103. GFC_INTEGER_4 values[VALUES_SIZE];
  104. time_t lt;
  105. struct tm local_time;
  106. struct tm UTC_time;
  107. long usecs;
  108. if (!gf_gettime (&lt, &usecs))
  109. {
  110. values[7] = usecs / 1000;
  111. localtime_r (&lt, &local_time);
  112. gmtime_r (&lt, &UTC_time);
  113. /* All arguments can be derived from VALUES. */
  114. values[0] = 1900 + local_time.tm_year;
  115. values[1] = 1 + local_time.tm_mon;
  116. values[2] = local_time.tm_mday;
  117. /* Day difference with UTC should always be -1, 0 or +1.
  118. Near year boundaries, we may obtain a large positive (+364,
  119. or +365 on leap years) or negative (-364, or -365 on leap years)
  120. number, which we have to handle.
  121. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98507
  122. */
  123. delta_day = local_time.tm_yday - UTC_time.tm_yday;
  124. if (delta_day < -1)
  125. delta_day = 1;
  126. else if (delta_day > 1)
  127. delta_day = -1;
  128. values[3] = local_time.tm_min - UTC_time.tm_min
  129. + 60 * (local_time.tm_hour - UTC_time.tm_hour + 24 * delta_day);
  130. values[4] = local_time.tm_hour;
  131. values[5] = local_time.tm_min;
  132. values[6] = local_time.tm_sec;
  133. if (__date)
  134. snprintf (date, DATE_LEN + 1, "%04d%02d%02d",
  135. values[0], values[1], values[2]);
  136. if (__time)
  137. snprintf (timec, TIME_LEN + 1, "%02d%02d%02d.%03d",
  138. values[4], values[5], values[6], values[7]);
  139. if (__zone)
  140. snprintf (zone, ZONE_LEN + 1, "%+03d%02d",
  141. values[3] / 60, abs (values[3] % 60));
  142. }
  143. else
  144. {
  145. memset (date, ' ', DATE_LEN);
  146. date[DATE_LEN] = '\0';
  147. memset (timec, ' ', TIME_LEN);
  148. timec[TIME_LEN] = '\0';
  149. memset (zone, ' ', ZONE_LEN);
  150. zone[ZONE_LEN] = '\0';
  151. for (i = 0; i < VALUES_SIZE; i++)
  152. values[i] = - GFC_INTEGER_4_HUGE;
  153. }
  154. /* Copy the values into the arguments. */
  155. if (__values)
  156. {
  157. index_type len, delta, elt_size;
  158. elt_size = GFC_DESCRIPTOR_SIZE (__values);
  159. len = GFC_DESCRIPTOR_EXTENT(__values,0);
  160. delta = GFC_DESCRIPTOR_STRIDE(__values,0);
  161. if (delta == 0)
  162. delta = 1;
  163. if (unlikely (len < VALUES_SIZE))
  164. runtime_error ("Incorrect extent in VALUE argument to"
  165. " DATE_AND_TIME intrinsic: is %ld, should"
  166. " be >=%ld", (long int) len, (long int) VALUES_SIZE);
  167. /* Cope with different type kinds. */
  168. if (elt_size == 4)
  169. {
  170. GFC_INTEGER_4 *vptr4 = __values->base_addr;
  171. for (i = 0; i < VALUES_SIZE; i++, vptr4 += delta)
  172. *vptr4 = values[i];
  173. }
  174. else if (elt_size == 8)
  175. {
  176. GFC_INTEGER_8 *vptr8 = (GFC_INTEGER_8 *)__values->base_addr;
  177. for (i = 0; i < VALUES_SIZE; i++, vptr8 += delta)
  178. {
  179. if (values[i] == - GFC_INTEGER_4_HUGE)
  180. *vptr8 = - GFC_INTEGER_8_HUGE;
  181. else
  182. *vptr8 = values[i];
  183. }
  184. }
  185. else
  186. abort ();
  187. }
  188. if (__zone)
  189. fstrcpy (__zone, __zone_len, zone, ZONE_LEN);
  190. if (__time)
  191. fstrcpy (__time, __time_len, timec, TIME_LEN);
  192. if (__date)
  193. fstrcpy (__date, __date_len, date, DATE_LEN);
  194. }
  195. /* SECNDS (X) - Non-standard
  196. Description: Returns the system time of day, or elapsed time, as a GFC_REAL_4
  197. in seconds.
  198. Class: Non-elemental subroutine.
  199. Arguments:
  200. X must be REAL(4) and the result is of the same type. The accuracy is system
  201. dependent.
  202. Usage:
  203. T = SECNDS (X)
  204. yields the time in elapsed seconds since X. If X is 0.0, T is the time in
  205. seconds since midnight. Note that a time that spans midnight but is less than
  206. 24hours will be calculated correctly. */
  207. extern GFC_REAL_4 secnds (GFC_REAL_4 *);
  208. export_proto(secnds);
  209. GFC_REAL_4
  210. secnds (GFC_REAL_4 *x)
  211. {
  212. GFC_INTEGER_4 values[VALUES_SIZE];
  213. GFC_REAL_4 temp1, temp2;
  214. /* Make the INTEGER*4 array for passing to date_and_time, with enough space
  215. for a rank-one array. */
  216. gfc_array_i4 *avalues = xmalloc (sizeof (gfc_array_i4)
  217. + sizeof (descriptor_dimension));
  218. avalues->base_addr = &values[0];
  219. GFC_DESCRIPTOR_DTYPE (avalues).type = BT_REAL;
  220. GFC_DESCRIPTOR_DTYPE (avalues).elem_len = 4;
  221. GFC_DESCRIPTOR_DTYPE (avalues).rank = 1;
  222. GFC_DIMENSION_SET(avalues->dim[0], 0, 7, 1);
  223. date_and_time (NULL, NULL, NULL, avalues, 0, 0, 0);
  224. free (avalues);
  225. temp1 = 3600.0 * (GFC_REAL_4)values[4] +
  226. 60.0 * (GFC_REAL_4)values[5] +
  227. (GFC_REAL_4)values[6] +
  228. 0.001 * (GFC_REAL_4)values[7];
  229. temp2 = fmod (*x, 86400.0);
  230. temp2 = (temp1 - temp2 >= 0.0) ? temp2 : (temp2 - 86400.0);
  231. return temp1 - temp2;
  232. }
  233. /* ITIME(X) - Non-standard
  234. Description: Returns the current local time hour, minutes, and seconds
  235. in elements 1, 2, and 3 of X, respectively. */
  236. static void
  237. itime0 (int x[3])
  238. {
  239. time_t lt;
  240. struct tm local_time;
  241. lt = time (NULL);
  242. if (lt != (time_t) -1)
  243. {
  244. localtime_r (&lt, &local_time);
  245. x[0] = local_time.tm_hour;
  246. x[1] = local_time.tm_min;
  247. x[2] = local_time.tm_sec;
  248. }
  249. }
  250. extern void itime_i4 (gfc_array_i4 *);
  251. export_proto(itime_i4);
  252. void
  253. itime_i4 (gfc_array_i4 *__values)
  254. {
  255. int x[3], i;
  256. index_type len, delta;
  257. GFC_INTEGER_4 *vptr;
  258. /* Call helper function. */
  259. itime0(x);
  260. /* Copy the value into the array. */
  261. len = GFC_DESCRIPTOR_EXTENT(__values,0);
  262. assert (len >= 3);
  263. delta = GFC_DESCRIPTOR_STRIDE(__values,0);
  264. if (delta == 0)
  265. delta = 1;
  266. vptr = __values->base_addr;
  267. for (i = 0; i < 3; i++, vptr += delta)
  268. *vptr = x[i];
  269. }
  270. extern void itime_i8 (gfc_array_i8 *);
  271. export_proto(itime_i8);
  272. void
  273. itime_i8 (gfc_array_i8 *__values)
  274. {
  275. int x[3], i;
  276. index_type len, delta;
  277. GFC_INTEGER_8 *vptr;
  278. /* Call helper function. */
  279. itime0(x);
  280. /* Copy the value into the array. */
  281. len = GFC_DESCRIPTOR_EXTENT(__values,0);
  282. assert (len >= 3);
  283. delta = GFC_DESCRIPTOR_STRIDE(__values,0);
  284. if (delta == 0)
  285. delta = 1;
  286. vptr = __values->base_addr;
  287. for (i = 0; i < 3; i++, vptr += delta)
  288. *vptr = x[i];
  289. }
  290. /* IDATE(X) - Non-standard
  291. Description: Fills TArray with the numerical values at the current
  292. local time. The day (in the range 1-31), month (in the range 1-12),
  293. and year appear in elements 1, 2, and 3 of X, respectively.
  294. The year has four significant digits. */
  295. static void
  296. idate0 (int x[3])
  297. {
  298. time_t lt;
  299. struct tm local_time;
  300. lt = time (NULL);
  301. if (lt != (time_t) -1)
  302. {
  303. localtime_r (&lt, &local_time);
  304. x[0] = local_time.tm_mday;
  305. x[1] = 1 + local_time.tm_mon;
  306. x[2] = 1900 + local_time.tm_year;
  307. }
  308. }
  309. extern void idate_i4 (gfc_array_i4 *);
  310. export_proto(idate_i4);
  311. void
  312. idate_i4 (gfc_array_i4 *__values)
  313. {
  314. int x[3], i;
  315. index_type len, delta;
  316. GFC_INTEGER_4 *vptr;
  317. /* Call helper function. */
  318. idate0(x);
  319. /* Copy the value into the array. */
  320. len = GFC_DESCRIPTOR_EXTENT(__values,0);
  321. assert (len >= 3);
  322. delta = GFC_DESCRIPTOR_STRIDE(__values,0);
  323. if (delta == 0)
  324. delta = 1;
  325. vptr = __values->base_addr;
  326. for (i = 0; i < 3; i++, vptr += delta)
  327. *vptr = x[i];
  328. }
  329. extern void idate_i8 (gfc_array_i8 *);
  330. export_proto(idate_i8);
  331. void
  332. idate_i8 (gfc_array_i8 *__values)
  333. {
  334. int x[3], i;
  335. index_type len, delta;
  336. GFC_INTEGER_8 *vptr;
  337. /* Call helper function. */
  338. idate0(x);
  339. /* Copy the value into the array. */
  340. len = GFC_DESCRIPTOR_EXTENT(__values,0);
  341. assert (len >= 3);
  342. delta = GFC_DESCRIPTOR_STRIDE(__values,0);
  343. if (delta == 0)
  344. delta = 1;
  345. vptr = __values->base_addr;
  346. for (i = 0; i < 3; i++, vptr += delta)
  347. *vptr = x[i];
  348. }
  349. /* GMTIME(STIME, TARRAY) - Non-standard
  350. Description: Given a system time value STime, fills TArray with values
  351. extracted from it appropriate to the GMT time zone using gmtime_r(3).
  352. The array elements are as follows:
  353. 1. Seconds after the minute, range 0-59 or 0-61 to allow for leap seconds
  354. 2. Minutes after the hour, range 0-59
  355. 3. Hours past midnight, range 0-23
  356. 4. Day of month, range 1-31
  357. 5. Number of months since January, range 0-11
  358. 6. Years since 1900
  359. 7. Number of days since Sunday, range 0-6
  360. 8. Days since January 1, range 0-365
  361. 9. Daylight savings indicator: positive if daylight savings is in effect,
  362. zero if not, and negative if the information isn't available. */
  363. static void
  364. gmtime_0 (const time_t * t, int x[9])
  365. {
  366. struct tm lt;
  367. gmtime_r (t, &lt);
  368. x[0] = lt.tm_sec;
  369. x[1] = lt.tm_min;
  370. x[2] = lt.tm_hour;
  371. x[3] = lt.tm_mday;
  372. x[4] = lt.tm_mon;
  373. x[5] = lt.tm_year;
  374. x[6] = lt.tm_wday;
  375. x[7] = lt.tm_yday;
  376. x[8] = lt.tm_isdst;
  377. }
  378. extern void gmtime_i4 (GFC_INTEGER_4 *, gfc_array_i4 *);
  379. export_proto(gmtime_i4);
  380. void
  381. gmtime_i4 (GFC_INTEGER_4 * t, gfc_array_i4 * tarray)
  382. {
  383. int x[9], i;
  384. index_type len, delta;
  385. GFC_INTEGER_4 *vptr;
  386. time_t tt;
  387. /* Call helper function. */
  388. tt = (time_t) *t;
  389. gmtime_0(&tt, x);
  390. /* Copy the values into the array. */
  391. len = GFC_DESCRIPTOR_EXTENT(tarray,0);
  392. assert (len >= 9);
  393. delta = GFC_DESCRIPTOR_STRIDE(tarray,0);
  394. if (delta == 0)
  395. delta = 1;
  396. vptr = tarray->base_addr;
  397. for (i = 0; i < 9; i++, vptr += delta)
  398. *vptr = x[i];
  399. }
  400. extern void gmtime_i8 (GFC_INTEGER_8 *, gfc_array_i8 *);
  401. export_proto(gmtime_i8);
  402. void
  403. gmtime_i8 (GFC_INTEGER_8 * t, gfc_array_i8 * tarray)
  404. {
  405. int x[9], i;
  406. index_type len, delta;
  407. GFC_INTEGER_8 *vptr;
  408. time_t tt;
  409. /* Call helper function. */
  410. tt = (time_t) *t;
  411. gmtime_0(&tt, x);
  412. /* Copy the values into the array. */
  413. len = GFC_DESCRIPTOR_EXTENT(tarray,0);
  414. assert (len >= 9);
  415. delta = GFC_DESCRIPTOR_STRIDE(tarray,0);
  416. if (delta == 0)
  417. delta = 1;
  418. vptr = tarray->base_addr;
  419. for (i = 0; i < 9; i++, vptr += delta)
  420. *vptr = x[i];
  421. }
  422. /* LTIME(STIME, TARRAY) - Non-standard
  423. Description: Given a system time value STime, fills TArray with values
  424. extracted from it appropriate to the local time zone using localtime_r(3).
  425. The array elements are as follows:
  426. 1. Seconds after the minute, range 0-59 or 0-61 to allow for leap seconds
  427. 2. Minutes after the hour, range 0-59
  428. 3. Hours past midnight, range 0-23
  429. 4. Day of month, range 1-31
  430. 5. Number of months since January, range 0-11
  431. 6. Years since 1900
  432. 7. Number of days since Sunday, range 0-6
  433. 8. Days since January 1, range 0-365
  434. 9. Daylight savings indicator: positive if daylight savings is in effect,
  435. zero if not, and negative if the information isn't available. */
  436. static void
  437. ltime_0 (const time_t * t, int x[9])
  438. {
  439. struct tm lt;
  440. localtime_r (t, &lt);
  441. x[0] = lt.tm_sec;
  442. x[1] = lt.tm_min;
  443. x[2] = lt.tm_hour;
  444. x[3] = lt.tm_mday;
  445. x[4] = lt.tm_mon;
  446. x[5] = lt.tm_year;
  447. x[6] = lt.tm_wday;
  448. x[7] = lt.tm_yday;
  449. x[8] = lt.tm_isdst;
  450. }
  451. extern void ltime_i4 (GFC_INTEGER_4 *, gfc_array_i4 *);
  452. export_proto(ltime_i4);
  453. void
  454. ltime_i4 (GFC_INTEGER_4 * t, gfc_array_i4 * tarray)
  455. {
  456. int x[9], i;
  457. index_type len, delta;
  458. GFC_INTEGER_4 *vptr;
  459. time_t tt;
  460. /* Call helper function. */
  461. tt = (time_t) *t;
  462. ltime_0(&tt, x);
  463. /* Copy the values into the array. */
  464. len = GFC_DESCRIPTOR_EXTENT(tarray,0);
  465. assert (len >= 9);
  466. delta = GFC_DESCRIPTOR_STRIDE(tarray,0);
  467. if (delta == 0)
  468. delta = 1;
  469. vptr = tarray->base_addr;
  470. for (i = 0; i < 9; i++, vptr += delta)
  471. *vptr = x[i];
  472. }
  473. extern void ltime_i8 (GFC_INTEGER_8 *, gfc_array_i8 *);
  474. export_proto(ltime_i8);
  475. void
  476. ltime_i8 (GFC_INTEGER_8 * t, gfc_array_i8 * tarray)
  477. {
  478. int x[9], i;
  479. index_type len, delta;
  480. GFC_INTEGER_8 *vptr;
  481. time_t tt;
  482. /* Call helper function. */
  483. tt = (time_t) * t;
  484. ltime_0(&tt, x);
  485. /* Copy the values into the array. */
  486. len = GFC_DESCRIPTOR_EXTENT(tarray,0);
  487. assert (len >= 9);
  488. delta = GFC_DESCRIPTOR_STRIDE(tarray,0);
  489. if (delta == 0)
  490. delta = 1;
  491. vptr = tarray->base_addr;
  492. for (i = 0; i < 9; i++, vptr += delta)
  493. *vptr = x[i];
  494. }