date_and_time.c 16 KB

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