getdate.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Convert a string representation of time to a time value.
  2. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <limits.h>
  16. #include <stdio.h>
  17. #include <stdio_ext.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <ctype.h>
  25. #define TM_YEAR_BASE 1900
  26. /* Prototypes for local functions. */
  27. static int first_wday (int year, int mon, int wday);
  28. static int check_mday (int year, int mon, int mday);
  29. /* Set to one of the following values to indicate an error.
  30. 1 the DATEMSK environment variable is null or undefined,
  31. 2 the template file cannot be opened for reading,
  32. 3 failed to get file status information,
  33. 4 the template file is not a regular file,
  34. 5 an error is encountered while reading the template file,
  35. 6 memory allication failed (not enough memory available),
  36. 7 there is no line in the template that matches the input,
  37. 8 invalid input specification Example: February 31 or a time is
  38. specified that can not be represented in a time_t (representing
  39. the time in seconds since 00:00:00 UTC, January 1, 1970) */
  40. int getdate_err;
  41. /* Returns the first weekday WDAY of month MON in the year YEAR. */
  42. static int
  43. first_wday (int year, int mon, int wday)
  44. {
  45. struct tm tm;
  46. if (wday == INT_MIN)
  47. return 1;
  48. memset (&tm, 0, sizeof (struct tm));
  49. tm.tm_year = year;
  50. tm.tm_mon = mon;
  51. tm.tm_mday = 1;
  52. mktime (&tm);
  53. return (1 + (wday - tm.tm_wday + 7) % 7);
  54. }
  55. /* Returns 1 if MDAY is a valid day of the month in month MON of year
  56. YEAR, and 0 if it is not. */
  57. static int
  58. check_mday (int year, int mon, int mday)
  59. {
  60. switch (mon)
  61. {
  62. case 0:
  63. case 2:
  64. case 4:
  65. case 6:
  66. case 7:
  67. case 9:
  68. case 11:
  69. if (mday >= 1 && mday <= 31)
  70. return 1;
  71. break;
  72. case 3:
  73. case 5:
  74. case 8:
  75. case 10:
  76. if (mday >= 1 && mday <= 30)
  77. return 1;
  78. break;
  79. case 1:
  80. if (mday >= 1 && mday <= (__isleap (year) ? 29 : 28))
  81. return 1;
  82. break;
  83. }
  84. return 0;
  85. }
  86. int
  87. __getdate_r (const char *string, struct tm *tp)
  88. {
  89. FILE *fp;
  90. char *line;
  91. size_t len;
  92. char *datemsk;
  93. char *result = NULL;
  94. __time64_t timer;
  95. struct tm tm;
  96. struct __stat64_t64 st;
  97. bool mday_ok = false;
  98. bool found = false;
  99. datemsk = getenv ("DATEMSK");
  100. if (datemsk == NULL || *datemsk == '\0')
  101. return 1;
  102. if (__stat64_time64 (datemsk, &st) < 0)
  103. return 3;
  104. if (!S_ISREG (st.st_mode))
  105. return 4;
  106. if (__access (datemsk, R_OK) < 0)
  107. return 2;
  108. /* Open the template file. */
  109. fp = fopen (datemsk, "rce");
  110. if (fp == NULL)
  111. return 2;
  112. /* No threads reading this stream. */
  113. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  114. /* Skip leading whitespace. */
  115. while (isspace (*string))
  116. string++;
  117. size_t inlen, oldlen;
  118. oldlen = inlen = strlen (string);
  119. /* Skip trailing whitespace. */
  120. while (inlen > 0 && isspace (string[inlen - 1]))
  121. inlen--;
  122. char *instr = NULL;
  123. if (inlen < oldlen)
  124. {
  125. instr = __strndup(string, inlen);
  126. if (instr == NULL)
  127. {
  128. fclose(fp);
  129. return 6;
  130. }
  131. string = instr;
  132. }
  133. line = NULL;
  134. len = 0;
  135. do
  136. {
  137. ssize_t n;
  138. n = __getline (&line, &len, fp);
  139. if (n < 0)
  140. break;
  141. if (line[n - 1] == '\n')
  142. line[n - 1] = '\0';
  143. /* Do the conversion. */
  144. tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
  145. tp->tm_hour = tp->tm_sec = tp->tm_min = INT_MIN;
  146. tp->tm_isdst = -1;
  147. tp->tm_gmtoff = 0;
  148. tp->tm_zone = NULL;
  149. result = strptime (string, line, tp);
  150. if ((found = (result && *result == '\0')))
  151. break;
  152. }
  153. while (!__feof_unlocked (fp));
  154. free (instr);
  155. /* Free the buffer. */
  156. free (line);
  157. /* Check for errors. */
  158. if (__ferror_unlocked (fp))
  159. {
  160. fclose (fp);
  161. return 5;
  162. }
  163. /* Close template file. */
  164. fclose (fp);
  165. if (!found)
  166. return 7;
  167. /* Get current time. */
  168. timer = time64_now ();
  169. __localtime64_r (&timer, &tm);
  170. /* If only the weekday is given, today is assumed if the given day
  171. is equal to the current day and next week if it is less. */
  172. if (tp->tm_wday >= 0 && tp->tm_wday <= 6 && tp->tm_year == INT_MIN
  173. && tp->tm_mon == INT_MIN && tp->tm_mday == INT_MIN)
  174. {
  175. tp->tm_year = tm.tm_year;
  176. tp->tm_mon = tm.tm_mon;
  177. tp->tm_mday = tm.tm_mday + (tp->tm_wday - tm.tm_wday + 7) % 7;
  178. mday_ok = true;
  179. }
  180. /* If only the month is given, the current month is assumed if the
  181. given month is equal to the current month and next year if it is
  182. less and no year is given (the first day of month is assumed if
  183. no day is given. */
  184. if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday == INT_MIN)
  185. {
  186. if (tp->tm_year == INT_MIN)
  187. tp->tm_year = tm.tm_year + (((tp->tm_mon - tm.tm_mon) < 0) ? 1 : 0);
  188. tp->tm_mday = first_wday (tp->tm_year, tp->tm_mon, tp->tm_wday);
  189. mday_ok = true;
  190. }
  191. /* If no hour, minute and second are given the current hour, minute
  192. and second are assumed. */
  193. if (tp->tm_hour == INT_MIN && tp->tm_min == INT_MIN && tp->tm_sec == INT_MIN)
  194. {
  195. tp->tm_hour = tm.tm_hour;
  196. tp->tm_min = tm.tm_min;
  197. tp->tm_sec = tm.tm_sec;
  198. }
  199. /* Fill in the gaps. */
  200. if (tp->tm_hour == INT_MIN)
  201. tp->tm_hour = 0;
  202. if (tp->tm_min == INT_MIN)
  203. tp->tm_min = 0;
  204. if (tp->tm_sec == INT_MIN)
  205. tp->tm_sec = 0;
  206. /* If no date is given, today is assumed if the given hour is
  207. greater than the current hour and tomorrow is assumed if
  208. it is less. */
  209. if (tp->tm_hour >= 0 && tp->tm_hour <= 23
  210. && tp->tm_mon == INT_MIN
  211. && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
  212. {
  213. tp->tm_mon = tm.tm_mon;
  214. tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
  215. mday_ok = 1;
  216. }
  217. /* More fillers. */
  218. if (tp->tm_year == INT_MIN)
  219. tp->tm_year = tm.tm_year;
  220. if (tp->tm_mon == INT_MIN)
  221. tp->tm_mon = tm.tm_mon;
  222. /* Check if the day of month is within range, and if the time can be
  223. represented in a time_t. We make use of the fact that the mktime
  224. call normalizes the struct tm. */
  225. if ((!mday_ok && !check_mday (TM_YEAR_BASE + tp->tm_year, tp->tm_mon,
  226. tp->tm_mday))
  227. || __mktime64 (tp) == (time_t) -1)
  228. return 8;
  229. return 0;
  230. }
  231. weak_alias (__getdate_r, getdate_r)
  232. libc_hidden_def (__getdate_r)
  233. struct tm *
  234. getdate (const char *string)
  235. {
  236. /* Buffer returned by getdate. */
  237. static struct tm tmbuf;
  238. int errval = __getdate_r (string, &tmbuf);
  239. if (errval != 0)
  240. {
  241. getdate_err = errval;
  242. return NULL;
  243. }
  244. return &tmbuf;
  245. }