era.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Helper functions used by strftime/strptime to handle locale-specific "eras".
  2. Copyright (C) 1995-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 "../locale/localeinfo.h"
  16. #include <libc-lock.h>
  17. #include <stdlib.h>
  18. #include <wchar.h>
  19. #include <string.h>
  20. #include <stdint.h>
  21. /* Some of the functions here must not be used while setlocale is called. */
  22. __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
  23. #define CURRENT(item) (current->values[_NL_ITEM_INDEX (item)].string)
  24. #define CURRENT_WORD(item) (current->values[_NL_ITEM_INDEX (item)].word)
  25. #define ERA_DATE_CMP(a, b) \
  26. (a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1] \
  27. || (a[1] == b[1] && a[2] <= b[2]))))
  28. /* Look up the era information in CURRENT's locale strings and
  29. cache it in CURRENT->private. */
  30. static struct lc_time_data *
  31. _nl_init_era_entries (struct __locale_data *current)
  32. {
  33. size_t cnt;
  34. struct lc_time_data *data;
  35. /* Avoid touching CURRENT if there is no data at all, for _nl_C_LC_TIME. */
  36. if (CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES) == 0)
  37. return NULL;
  38. data = current->private;
  39. if (data != NULL && atomic_load_acquire (&data->era_initialized))
  40. return data;
  41. __libc_rwlock_wrlock (__libc_setlocale_lock);
  42. data = current->private;
  43. if (data == NULL)
  44. {
  45. data = calloc (sizeof *data, 1);
  46. if (data == NULL)
  47. goto out;
  48. current->private = data;
  49. }
  50. if (! data->era_initialized)
  51. {
  52. size_t new_num_eras = CURRENT_WORD (_NL_TIME_ERA_NUM_ENTRIES);
  53. if (new_num_eras == 0)
  54. {
  55. if (data->eras != NULL)
  56. {
  57. free (data->eras);
  58. data->eras = NULL;
  59. }
  60. }
  61. else
  62. {
  63. struct era_entry *new_eras = data->eras;
  64. if (data->num_eras != new_num_eras)
  65. new_eras =
  66. (struct era_entry *) realloc (data->eras,
  67. new_num_eras
  68. * sizeof (struct era_entry));
  69. if (new_eras == NULL)
  70. {
  71. free (data->eras);
  72. data->num_eras = 0;
  73. data->eras = NULL;
  74. }
  75. else
  76. {
  77. const char *ptr = CURRENT (_NL_TIME_ERA_ENTRIES);
  78. data->num_eras = new_num_eras;
  79. data->eras = new_eras;
  80. for (cnt = 0; cnt < new_num_eras; ++cnt)
  81. {
  82. const char *base_ptr = ptr;
  83. memcpy ((void *) (new_eras + cnt), (const void *) ptr,
  84. sizeof (uint32_t) * 8);
  85. if (ERA_DATE_CMP(new_eras[cnt].start_date,
  86. new_eras[cnt].stop_date))
  87. if (new_eras[cnt].direction == (uint32_t) '+')
  88. new_eras[cnt].absolute_direction = 1;
  89. else
  90. new_eras[cnt].absolute_direction = -1;
  91. else
  92. if (new_eras[cnt].direction == (uint32_t) '+')
  93. new_eras[cnt].absolute_direction = -1;
  94. else
  95. new_eras[cnt].absolute_direction = 1;
  96. /* Skip numeric values. */
  97. ptr += sizeof (uint32_t) * 8;
  98. /* Set and skip era name. */
  99. new_eras[cnt].era_name = ptr;
  100. ptr = strchr (ptr, '\0') + 1;
  101. /* Set and skip era format. */
  102. new_eras[cnt].era_format = ptr;
  103. ptr = strchr (ptr, '\0') + 1;
  104. ptr += 3 - (((ptr - (const char *) base_ptr) + 3) & 3);
  105. /* Set and skip wide era name. */
  106. new_eras[cnt].era_wname = (wchar_t *) ptr;
  107. ptr = (char *) (__wcschr ((wchar_t *) ptr, L'\0') + 1);
  108. /* Set and skip wide era format. */
  109. new_eras[cnt].era_wformat = (wchar_t *) ptr;
  110. ptr = (char *) (__wcschr ((wchar_t *) ptr, L'\0') + 1);
  111. }
  112. }
  113. }
  114. atomic_store_release (&data->era_initialized, 1);
  115. }
  116. out:
  117. __libc_rwlock_unlock (__libc_setlocale_lock);
  118. return data;
  119. }
  120. struct era_entry *
  121. _nl_get_era_entry (const struct tm *tp, struct __locale_data *current)
  122. {
  123. struct lc_time_data *data = _nl_init_era_entries (current);
  124. if (data != NULL)
  125. {
  126. /* Now compare date with the available eras. */
  127. const int32_t tdate[3] = { tp->tm_year, tp->tm_mon, tp->tm_mday };
  128. size_t cnt;
  129. for (cnt = 0; cnt < data->num_eras; ++cnt)
  130. if ((ERA_DATE_CMP (data->eras[cnt].start_date, tdate)
  131. && ERA_DATE_CMP (tdate, data->eras[cnt].stop_date))
  132. || (ERA_DATE_CMP (data->eras[cnt].stop_date, tdate)
  133. && ERA_DATE_CMP (tdate, data->eras[cnt].start_date)))
  134. return &data->eras[cnt];
  135. }
  136. return NULL;
  137. }
  138. struct era_entry *
  139. _nl_select_era_entry (int cnt, struct __locale_data *current)
  140. {
  141. struct lc_time_data *data = _nl_init_era_entries (current);
  142. return data == NULL ? NULL : &data->eras[cnt];
  143. }