1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
|
/* NOTE: This file defines both strftime() and wcsftime(). Take care when
* making changes. See also wcsftime.c, and note the (small) overlap in the
* manual description, taking care to edit both as needed. */
/*
* strftime.c
* Original Author: G. Haley
* Additions from: Eric Blake, Corinna Vinschen
* Changes to allow dual use as wcstime, also: Craig Howland
*
* Places characters into the array pointed to by s as controlled by the string
* pointed to by format. If the total number of resulting characters including
* the terminating null character is not more than maxsize, returns the number
* of characters placed into the array pointed to by s (not including the
* terminating null character); otherwise zero is returned and the contents of
* the array indeterminate.
*/
/*
FUNCTION
<<strftime>>, <<strftime_l>>---convert date and time to a formatted string
INDEX
strftime
INDEX
strftime_l
SYNOPSIS
#include <time.h>
size_t strftime(char *restrict <[s]>, size_t <[maxsize]>,
const char *restrict <[format]>,
const struct tm *restrict <[timp]>);
size_t strftime_l(char *restrict <[s]>, size_t <[maxsize]>,
const char *restrict <[format]>,
const struct tm *restrict <[timp]>,
locale_t <[locale]>);
DESCRIPTION
<<strftime>> converts a <<struct tm>> representation of the time (at
<[timp]>) into a null-terminated string, starting at <[s]> and occupying
no more than <[maxsize]> characters.
<<strftime_l>> is like <<strftime>> but creates a string in a format
as expected in locale <[locale]>. If <[locale]> is LC_GLOBAL_LOCALE or
not a valid locale object, the behaviour is undefined.
You control the format of the output using the string at <[format]>.
<<*<[format]>>> can contain two kinds of specifications: text to be
copied literally into the formatted string, and time conversion
specifications. Time conversion specifications are two- and
three-character sequences beginning with `<<%>>' (use `<<%%>>' to
include a percent sign in the output). Each defined conversion
specification selects only the specified field(s) of calendar time
data from <<*<[timp]>>>, and converts it to a string in one of the
following ways:
o+
o %a
The abbreviated weekday name according to the current locale. [tm_wday]
o %A
The full weekday name according to the current locale.
In the default "C" locale, one of `<<Sunday>>', `<<Monday>>', `<<Tuesday>>',
`<<Wednesday>>', `<<Thursday>>', `<<Friday>>', `<<Saturday>>'. [tm_wday]
o %b
The abbreviated month name according to the current locale. [tm_mon]
o %B
The full month name according to the current locale.
In the default "C" locale, one of `<<January>>', `<<February>>',
`<<March>>', `<<April>>', `<<May>>', `<<June>>', `<<July>>',
`<<August>>', `<<September>>', `<<October>>', `<<November>>',
`<<December>>'. [tm_mon]
o %c
The preferred date and time representation for the current locale.
[tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday]
o %C
The century, that is, the year divided by 100 then truncated. For
4-digit years, the result is zero-padded and exactly two characters;
but for other years, there may a negative sign or more digits. In
this way, `<<%C%y>>' is equivalent to `<<%Y>>'. [tm_year]
o %d
The day of the month, formatted with two digits (from `<<01>>' to
`<<31>>'). [tm_mday]
o %D
A string representing the date, in the form `<<"%m/%d/%y">>'.
[tm_mday, tm_mon, tm_year]
o %e
The day of the month, formatted with leading space if single digit
(from `<<1>>' to `<<31>>'). [tm_mday]
o %E<<x>>
In some locales, the E modifier selects alternative representations of
certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>.
o %F
A string representing the ISO 8601:2000 date format, in the form
`<<"%Y-%m-%d">>'. [tm_mday, tm_mon, tm_year]
o %g
The last two digits of the week-based year, see specifier %G (from
`<<00>>' to `<<99>>'). [tm_year, tm_wday, tm_yday]
o %G
The week-based year. In the ISO 8601:2000 calendar, week 1 of the year
includes January 4th, and begin on Mondays. Therefore, if January 1st,
2nd, or 3rd falls on a Sunday, that day and earlier belong to the last
week of the previous year; and if December 29th, 30th, or 31st falls
on Monday, that day and later belong to week 1 of the next year. For
consistency with %Y, it always has at least four characters.
Example: "%G" for Saturday 2nd January 1999 gives "1998", and for
Tuesday 30th December 1997 gives "1998". [tm_year, tm_wday, tm_yday]
o %h
Synonym for "%b". [tm_mon]
o %H
The hour (on a 24-hour clock), formatted with two digits (from
`<<00>>' to `<<23>>'). [tm_hour]
o %I
The hour (on a 12-hour clock), formatted with two digits (from
`<<01>>' to `<<12>>'). [tm_hour]
o %j
The count of days in the year, formatted with three digits
(from `<<001>>' to `<<366>>'). [tm_yday]
o %k
The hour (on a 24-hour clock), formatted with leading space if single
digit (from `<<0>>' to `<<23>>'). Non-POSIX extension (c.p. %I). [tm_hour]
o %l
The hour (on a 12-hour clock), formatted with leading space if single
digit (from `<<1>>' to `<<12>>'). Non-POSIX extension (c.p. %H). [tm_hour]
o %m
The month number, formatted with two digits (from `<<01>>' to `<<12>>').
[tm_mon]
o %M
The minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min]
o %n
A newline character (`<<\n>>').
o %O<<x>>
In some locales, the O modifier selects alternative digit characters
for certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>.
o %p
Either `<<AM>>' or `<<PM>>' as appropriate, or the corresponding strings for
the current locale. [tm_hour]
o %P
Same as '<<%p>>', but in lowercase. This is a GNU extension. [tm_hour]
o %q
Quarter of the year (from `<<1>>' to `<<4>>'), with January starting
the first quarter. This is a GNU extension. [tm_mon]
o %r
Replaced by the time in a.m. and p.m. notation. In the "C" locale this
is equivalent to "%I:%M:%S %p". In locales which don't define a.m./p.m.
notations, the result is an empty string. [tm_sec, tm_min, tm_hour]
o %R
The 24-hour time, to the minute. Equivalent to "%H:%M". [tm_min, tm_hour]
o %s
The time elapsed, in seconds, since the start of the Unix epoch at
1970-01-01 00:00:00 UTC.
o %S
The second, formatted with two digits (from `<<00>>' to `<<60>>'). The
value 60 accounts for the occasional leap second. [tm_sec]
o %t
A tab character (`<<\t>>').
o %T
The 24-hour time, to the second. Equivalent to "%H:%M:%S". [tm_sec,
tm_min, tm_hour]
o %u
The weekday as a number, 1-based from Monday (from `<<1>>' to
`<<7>>'). [tm_wday]
o %U
The week number, where weeks start on Sunday, week 1 contains the first
Sunday in a year, and earlier days are in week 0. Formatted with two
digits (from `<<00>>' to `<<53>>'). See also <<%W>>. [tm_wday, tm_yday]
o %V
The week number, where weeks start on Monday, week 1 contains January 4th,
and earlier days are in the previous year. Formatted with two digits
(from `<<01>>' to `<<53>>'). See also <<%G>>. [tm_year, tm_wday, tm_yday]
o %v
A string representing the BSD/OSX/Ruby VMS/Oracle date format, in the form
"%e-%b-%Y". Non-POSIX extension. [tm_mday, tm_mon, tm_year]
o %w
The weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>').
[tm_wday]
o %W
The week number, where weeks start on Monday, week 1 contains the first
Monday in a year, and earlier days are in week 0. Formatted with two
digits (from `<<00>>' to `<<53>>'). [tm_wday, tm_yday]
o %x
Replaced by the preferred date representation in the current locale.
In the "C" locale this is equivalent to "%m/%d/%y".
[tm_mon, tm_mday, tm_year]
o %X
Replaced by the preferred time representation in the current locale.
In the "C" locale this is equivalent to "%H:%M:%S". [tm_sec, tm_min, tm_hour]
o %y
The last two digits of the year (from `<<00>>' to `<<99>>'). [tm_year]
(Implementation interpretation: always positive, even for negative years.)
o %Y
The full year, equivalent to <<%C%y>>. It will always have at least four
characters, but may have more. The year is accurate even when tm_year
added to the offset of 1900 overflows an int. [tm_year]
o %z
The offset from UTC. The format consists of a sign (negative is west of
Greewich), two characters for hour, then two characters for minutes
(-hhmm or +hhmm). If tm_isdst is negative, the offset is unknown and no
output is generated; if it is zero, the offset is the standard offset for
the current time zone; and if it is positive, the offset is the daylight
savings offset for the current timezone. The offset is determined from
the TZ environment variable, as if by calling tzset(). [tm_isdst]
o %Z
The current time zone abbreviation. If tm_isdst is negative, no output
is generated. Otherwise, the time zone abbreviation is based on the TZ
environment variable, as if by calling tzset(). [tm_isdst]
o %%
A single character, `<<%>>'.
o-
RETURNS
When the formatted time takes up no more than <[maxsize]> characters,
the result is the length of the formatted string. Otherwise, if the
formatting operation was abandoned due to lack of room, the result is
<<0>>, and the string starting at <[s]> corresponds to just those
parts of <<*<[format]>>> that could be completely filled in within the
<[maxsize]> limit.
PORTABILITY
ANSI C requires <<strftime>>, but does not specify the contents of
<<*<[s]>>> when the formatted string would require more than
<[maxsize]> characters. Unrecognized specifiers and fields of
<<timp>> that are out of range cause undefined results. Since some
formats expand to 0 bytes, it is wise to set <<*<[s]>>> to a nonzero
value beforehand to distinguish between failure and an empty string.
This implementation does not support <<s>> being NULL, nor overlapping
<<s>> and <<format>>.
<<strftime_l>> is POSIX-1.2008.
<<strftime>> and <<strftime_l>> require no supporting OS subroutines.
BUGS
(NOT Cygwin:) <<strftime>> ignores the LC_TIME category of the current
locale, hard-coding the "C" locale settings.
*/
#define _GNU_SOURCE
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <wctype.h>
#include <wchar.h>
#include "local.h"
#include "locale_private.h"
#include "stdio_private.h"
#undef TOLOWER
/* Defines to make the file dual use for either strftime() or wcsftime().
* To get wcsftime, define MAKE_WCSFTIME.
* To get strftime, do not define MAKE_WCSFTIME.
* Names are kept friendly to strftime() usage. The biggest ugliness is the
* use of the CQ() macro to make either regular character constants and
* string literals or wide-character constants and wide-character-string
* literals, as appropriate. */
#if !defined(MAKE_WCSFTIME)
#define CHAR char /* string type basis */
#define CQ(a) a /* character constant qualifier */
#define t_snprintf __d_snprintf /* char equivalent function name */
#define t_strncmp strncmp /* char equivalent function name */
#define SFLG /* %s flag (null for normal char) */
#define _ctloc(x) (ctloclen = strlen(ctloc = x))
#define TOLOWER(c) tolower((int)(unsigned char)(c))
#define STRTOUL(c, p, b) strtoul((c), (p), (b))
#define STRCPY(a, b) strcpy((a), (b))
#define STRCHR(a, b) strchr((a), (b))
#define STRLEN(a) strlen(a)
#else
#define strftime wcsftime /* Alternate function name */
#define strftime_l wcsftime_l /* Alternate function name */
#define CHAR wchar_t /* string type basis */
#define CQ(a) L##a /* character constant qualifier */
#define t_snprintf __d_swprintf /* wide-char equivalent function name */
#define t_strncmp wcsncmp /* wide-char equivalent function name */
#define TOLOWER(c) towlower((wint_t)(c))
#define STRTOUL(c, p, b) wcstoul((c), (p), (b))
#define STRCPY(a, b) wcscpy((a), (b))
#define STRCHR(a, b) wcschr((a), (b))
#define STRLEN(a) wcslen(a)
#define SFLG "l" /* %s flag (l for wide char) */
#ifdef WTIME_WDAY
#define _ctloc(x) (ctloclen = wcslen(ctloc = W##x))
#else
#define CTLOCBUFLEN 256 /* Arbitrary big buffer size */
static const wchar_t *
__ctloc(wchar_t *buf, const char *elem, size_t *len_ret)
{
buf[CTLOCBUFLEN - 1] = L'\0';
*len_ret = mbstowcs(buf, elem, CTLOCBUFLEN - 1);
if (*len_ret == (size_t)-1)
*len_ret = 0;
return buf;
}
#define _ctloc(x) (ctloc = __ctloc(ctlocbuf, x, &ctloclen))
#endif
#endif /* MAKE_WCSFTIME */
#define CHECK_LENGTH() \
if (len < 0 || (count += len) >= maxsize) \
return 0
/* Enforce the coding assumptions that YEAR_BASE is positive. (%C, %Y, etc.) */
#if YEAR_BASE < 0
#error "YEAR_BASE < 0"
#endif
/* Using the tm_year, tm_wday, and tm_yday components of TIM_P, return
-1, 0, or 1 as the adjustment to add to the year for the ISO week
numbering used in "%g%G%V", avoiding overflow. */
static int
iso_year_adjust(const struct tm *tim_p)
{
/* Account for fact that tm_year==0 is year 1900. */
int leap = isleap(tim_p->tm_year + (YEAR_BASE - (tim_p->tm_year < 0 ? 0 : 2000)));
/* Pack the yday, wday, and leap year into a single int since there are so
many disparate cases. */
#define PACK(yd, wd, lp) (((yd) << 4) + (wd << 1) + (lp))
switch (PACK(tim_p->tm_yday, tim_p->tm_wday, leap)) {
case PACK(0, 5, 0): /* Jan 1 is Fri, not leap. */
case PACK(0, 6, 0): /* Jan 1 is Sat, not leap. */
case PACK(0, 0, 0): /* Jan 1 is Sun, not leap. */
case PACK(0, 5, 1): /* Jan 1 is Fri, leap year. */
case PACK(0, 6, 1): /* Jan 1 is Sat, leap year. */
case PACK(0, 0, 1): /* Jan 1 is Sun, leap year. */
case PACK(1, 6, 0): /* Jan 2 is Sat, not leap. */
case PACK(1, 0, 0): /* Jan 2 is Sun, not leap. */
case PACK(1, 6, 1): /* Jan 2 is Sat, leap year. */
case PACK(1, 0, 1): /* Jan 2 is Sun, leap year. */
case PACK(2, 0, 0): /* Jan 3 is Sun, not leap. */
case PACK(2, 0, 1): /* Jan 3 is Sun, leap year. */
return -1; /* Belongs to last week of previous year. */
case PACK(362, 1, 0): /* Dec 29 is Mon, not leap. */
case PACK(363, 1, 1): /* Dec 29 is Mon, leap year. */
case PACK(363, 1, 0): /* Dec 30 is Mon, not leap. */
case PACK(363, 2, 0): /* Dec 30 is Tue, not leap. */
case PACK(364, 1, 1): /* Dec 30 is Mon, leap year. */
case PACK(364, 2, 1): /* Dec 30 is Tue, leap year. */
case PACK(364, 1, 0): /* Dec 31 is Mon, not leap. */
case PACK(364, 2, 0): /* Dec 31 is Tue, not leap. */
case PACK(364, 3, 0): /* Dec 31 is Wed, not leap. */
case PACK(365, 1, 1): /* Dec 31 is Mon, leap year. */
case PACK(365, 2, 1): /* Dec 31 is Tue, leap year. */
case PACK(365, 3, 1): /* Dec 31 is Wed, leap year. */
return 1; /* Belongs to first week of next year. */
}
return 0; /* Belongs to specified year. */
#undef PACK
}
#ifdef _WANT_C99_TIME_FORMATS
typedef struct {
int year;
CHAR *era_C;
CHAR *era_Y;
} era_info_t;
static era_info_t *
#if defined(MAKE_WCSFTIME) && defined(WTIME_ERA)
get_era_info(const struct tm *tim_p, const wchar_t *era)
#else
get_era_info(const struct tm *tim_p, const char *era)
#endif
{
#if defined(MAKE_WCSFTIME) && defined(WTIME_ERA)
wchar_t *c;
const wchar_t *dir;
#define ERA_STRCHR(a, b) wcschr((a), (b))
#define ERA_STRNCPY(a, b, c) wcsncpy((a), (b), (c))
#define ERA_STRTOL(a, b, c) wcstol((a), (b), (c))
#else
char *c;
const char *dir;
#define ERA_STRCHR(a, b) strchr((a), (b))
#define ERA_STRNCPY(a, b, c) strncpy((a), (b), (c))
#define ERA_STRTOL(a, b, c) strtol((a), (b), (c))
#endif
long offset;
struct tm stm, etm;
era_info_t *ei;
ei = (era_info_t *)calloc(1, sizeof(era_info_t));
if (!ei)
return NULL;
stm.tm_isdst = etm.tm_isdst = 0;
while (era) {
dir = era;
era += 2;
offset = ERA_STRTOL(era, &c, 10);
era = c + 1;
stm.tm_year = ERA_STRTOL(era, &c, 10) - YEAR_BASE;
/* Adjust offset for negative gregorian dates. */
if (stm.tm_year <= -YEAR_BASE)
++stm.tm_year;
stm.tm_mon = ERA_STRTOL(c + 1, &c, 10) - 1;
stm.tm_mday = ERA_STRTOL(c + 1, &c, 10);
stm.tm_hour = stm.tm_min = stm.tm_sec = 0;
era = c + 1;
if (era[0] == '-' && era[1] == '*') {
etm = stm;
stm.tm_year = INT_MIN;
stm.tm_mon = stm.tm_mday = stm.tm_hour = stm.tm_min = stm.tm_sec = 0;
era += 3;
} else if (era[0] == '+' && era[1] == '*') {
etm.tm_year = INT_MAX;
etm.tm_mon = 11;
etm.tm_mday = 31;
etm.tm_hour = 23;
etm.tm_min = etm.tm_sec = 59;
era += 3;
} else {
etm.tm_year = ERA_STRTOL(era, &c, 10) - YEAR_BASE;
/* Adjust offset for negative gregorian dates. */
if (etm.tm_year <= -YEAR_BASE)
++etm.tm_year;
etm.tm_mon = ERA_STRTOL(c + 1, &c, 10) - 1;
etm.tm_mday = ERA_STRTOL(c + 1, &c, 10);
etm.tm_mday = 31;
etm.tm_hour = 23;
etm.tm_min = etm.tm_sec = 59;
era = c + 1;
}
if ((tim_p->tm_year > stm.tm_year
|| (tim_p->tm_year == stm.tm_year
&& (tim_p->tm_mon > stm.tm_mon
|| (tim_p->tm_mon == stm.tm_mon && tim_p->tm_mday >= stm.tm_mday))))
&& (tim_p->tm_year < etm.tm_year
|| (tim_p->tm_year == etm.tm_year
&& (tim_p->tm_mon < etm.tm_mon
|| (tim_p->tm_mon == etm.tm_mon && tim_p->tm_mday <= etm.tm_mday))))) {
/* Gotcha */
size_t len;
/* year */
if (*dir == '+' && stm.tm_year != INT_MIN)
ei->year = tim_p->tm_year - stm.tm_year + offset;
else
ei->year = etm.tm_year - tim_p->tm_year + offset;
/* era_C */
c = ERA_STRCHR(era, ':');
#if defined(MAKE_WCSFTIME) && !defined(WTIME_ERA)
len = mbsnrtowcs(NULL, &era, c - era, 0, NULL);
if (len == (size_t)-1) {
free(ei);
return NULL;
}
#else
len = c - era;
#endif
ei->era_C = (CHAR *)malloc((len + 1) * sizeof(CHAR));
if (!ei->era_C) {
free(ei);
return NULL;
}
#if defined(MAKE_WCSFTIME) && !defined(WTIME_ERA)
len = mbsnrtowcs(ei->era_C, &era, c - era, len + 1, NULL);
#else
ERA_STRNCPY(ei->era_C, era, len);
era += len;
#endif
ei->era_C[len] = CQ('\0');
/* era_Y */
++era;
c = ERA_STRCHR(era, ';');
if (!c)
c = ERA_STRCHR(era, '\0');
#if defined(MAKE_WCSFTIME) && !defined(WTIME_ERA)
len = mbsnrtowcs(NULL, &era, c - era, 0, NULL);
if (len == (size_t)-1) {
free(ei->era_C);
free(ei);
return NULL;
}
#else
len = c - era;
#endif
ei->era_Y = (CHAR *)malloc((len + 1) * sizeof(CHAR));
if (!ei->era_Y) {
free(ei->era_C);
free(ei);
return NULL;
}
#if defined(MAKE_WCSFTIME) && !defined(WTIME_ERA)
len = mbsnrtowcs(ei->era_Y, &era, c - era, len + 1, NULL);
#else
ERA_STRNCPY(ei->era_Y, era, len);
era += len;
#endif
ei->era_Y[len] = CQ('\0');
return ei;
} else
era = ERA_STRCHR(era, ';');
if (era)
++era;
}
return NULL;
}
static void
free_era_info(era_info_t *ei)
{
free(ei->era_C);
free(ei->era_Y);
free(ei);
}
typedef struct {
size_t num;
CHAR **digit;
CHAR *buffer;
} alt_digits_t;
static alt_digits_t *
#if defined(MAKE_WCSFTIME) && defined(WTIME_ALT_DIGITS)
get_alt_digits(const wchar_t *alt_digits)
#else
get_alt_digits(const char *alt_digits)
#endif
{
alt_digits_t *adi;
#if defined(MAKE_WCSFTIME) && defined(WTIME_ALT_DIGITS)
const wchar_t *a, *e;
#define ALT_STRCHR(a, b) wcschr((a), (b))
#define ALT_STRCPY(a, b) wcscpy((a), (b))
#define ALT_STRLEN(a) wcslen(a)
#else
const char *a, *e;
#define ALT_STRCHR(a, b) strchr((a), (b))
#define ALT_STRCPY(a, b) strcpy((a), (b))
#define ALT_STRLEN(a) strlen(a)
#endif
CHAR *aa, *ae;
size_t len;
adi = (alt_digits_t *)calloc(1, sizeof(alt_digits_t));
if (!adi)
return NULL;
/* Compute number of alt_digits. */
adi->num = 1;
for (a = alt_digits; (e = ALT_STRCHR(a, ';')) != NULL; a = e + 1)
++adi->num;
/* Allocate the `digit' array, which is an array of `num' pointers into
`buffer'. */
adi->digit = (CHAR **)calloc(adi->num, sizeof(CHAR *));
if (!adi->digit) {
free(adi);
return NULL;
}
/* Compute memory required for `buffer'. */
#if defined(MAKE_WCSFTIME) && !defined(WTIME_ALT_DIGITS)
len = mbstowcs(NULL, alt_digits, 0);
if (len == (size_t)-1) {
free(adi->digit);
free(adi);
return NULL;
}
#else
len = ALT_STRLEN(alt_digits);
#endif
/* Allocate it. */
adi->buffer = (CHAR *)malloc((len + 1) * sizeof(CHAR));
if (!adi->buffer) {
free(adi->digit);
free(adi);
return NULL;
}
/* Store digits in it. */
#if defined(MAKE_WCSFTIME) && !defined(WTIME_ALT_DIGITS)
mbstowcs(adi->buffer, alt_digits, len + 1);
#else
ALT_STRCPY(adi->buffer, alt_digits);
#endif
/* Store the pointers into `buffer' into the appropriate `digit' slot. */
for (len = 0, aa = adi->buffer; (ae = STRCHR(aa, CQ(';'))) != NULL; ++len, aa = ae + 1) {
*ae = '\0';
adi->digit[len] = aa;
}
adi->digit[len] = aa;
return adi;
}
static void
free_alt_digits(alt_digits_t *adi)
{
free(adi->digit);
free(adi->buffer);
free(adi);
}
/* Return 0 if no alt_digit is available for a number.
Return -1 if buffer size isn't sufficient to hold alternative digit.
Return length of new digit otherwise. */
static int
conv_to_alt_digits(CHAR *buf, size_t bufsiz, unsigned num, alt_digits_t *adi)
{
if (num < adi->num) {
size_t len = STRLEN(adi->digit[num]);
if (bufsiz < len)
return -1;
STRCPY(buf, adi->digit[num]);
return (int)len;
}
return 0;
}
static size_t
__strftime(CHAR *s, size_t maxsize, const CHAR *format, const struct tm *tim_p, locale_t locale,
era_info_t **era_info, alt_digits_t **alt_digits)
#else /* !_WANT_C99_TIME_FORMATS */
static size_t
__strftime(CHAR *s, size_t maxsize, const CHAR *format, const struct tm *tim_p, locale_t locale)
#define __strftime(s, m, f, t, l, e, a) __strftime((s), (m), (f), (t), (l))
#endif /* !_WANT_C99_TIME_FORMATS */
{
size_t count = 0;
int len = 0;
const CHAR *ctloc;
#if defined(MAKE_WCSFTIME) && !defined(WTIME_WDAY)
CHAR ctlocbuf[CTLOCBUFLEN];
#endif
size_t i, ctloclen;
CHAR alt;
CHAR pad;
unsigned long width;
int tzset_called = 0;
for (;;) {
while (*format && *format != CQ('%')) {
if (count < maxsize - 1)
s[count++] = *format++;
else
return 0;
}
if (*format == CQ('\0'))
break;
format++;
pad = '\0';
width = 0;
/* POSIX-1.2008 feature: '0' and '+' modifiers require 0-padding with
slightly different semantics. */
if (*format == CQ('0') || *format == CQ('+'))
pad = *format++;
/* POSIX-1.2008 feature: A minimum field width can be specified. */
if (*format >= CQ('1') && *format <= CQ('9')) {
CHAR *fp;
width = STRTOUL(format, &fp, 10);
format = fp;
}
alt = CQ('\0');
if (*format == CQ('E')) {
alt = *format++;
#ifdef _WANT_C99_TIME_FORMATS
#if defined(MAKE_WCSFTIME) && defined(TIME_WERA)
if (!*era_info && *TIME_WERA)
*era_info = get_era_info(tim_p, TIME_WERA);
#else
if (!*era_info && *TIME_ERA)
*era_info = get_era_info(tim_p, TIME_ERA);
#endif
#endif /* _WANT_C99_TIME_FORMATS */
} else if (*format == CQ('O')) {
alt = *format++;
#ifdef _WANT_C99_TIME_FORMATS
#if defined(MAKE_WCSFTIME) && defined(TIME_WALT_DIGITS)
if (!*alt_digits && *TIME_WALT_DIGITS)
*alt_digits = get_alt_digits(TIME_WALT_DIGITS);
#else
if (!*alt_digits && *TIME_ALT_DIGITS)
*alt_digits = get_alt_digits(TIME_ALT_DIGITS);
#endif
#endif /* _WANT_C99_TIME_FORMATS */
}
switch (*format) {
case CQ('a'):
_ctloc(TIME_WDAY[tim_p->tm_wday]);
for (i = 0; i < ctloclen; i++) {
if (count < maxsize - 1)
s[count++] = ctloc[i];
else
return 0;
}
break;
case CQ('A'):
_ctloc(TIME_WEEKDAY[tim_p->tm_wday]);
for (i = 0; i < ctloclen; i++) {
if (count < maxsize - 1)
s[count++] = ctloc[i];
else
return 0;
}
break;
case CQ('b'):
case CQ('h'):
_ctloc(TIME_MON[tim_p->tm_mon]);
for (i = 0; i < ctloclen; i++) {
if (count < maxsize - 1)
s[count++] = ctloc[i];
else
return 0;
}
break;
case CQ('B'):
_ctloc(TIME_MONTH[tim_p->tm_mon]);
for (i = 0; i < ctloclen; i++) {
if (count < maxsize - 1)
s[count++] = ctloc[i];
else
return 0;
}
break;
case CQ('c'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == 'E' && *era_info && *TIME_ERA_D_T_FMT)
_ctloc(TIME_ERA_E_T_FMT);
else
#endif /* _WANT_C99_TIME_FORMATS */
_ctloc(TIME_C_FMT);
goto recurse;
case CQ('r'):
_ctloc(TIME_AMPM_FMT);
goto recurse;
case CQ('x'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == 'E' && *era_info && *TIME_ERA_D_FMT)
_ctloc(TIME_ERA_D_FMT);
else
#endif /* _WANT_C99_TIME_FORMATS */
_ctloc(TIME_X_FMT);
goto recurse;
case CQ('X'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == 'E' && *era_info && *TIME_ERA_T_FMT)
_ctloc(TIME_ERA_T_FMT) else
#endif /* _WANT_C99_TIME_FORMATS */
_ctloc(TIME_UX_FMT);
recurse:
if (*ctloc) {
/* Recurse to avoid need to replicate %Y formation. */
len = __strftime(&s[count], maxsize - count, ctloc, tim_p, locale, era_info,
alt_digits);
if (len > 0)
count += len;
else
return 0;
}
break;
case CQ('C'): {
/* Examples of (tm_year + YEAR_BASE) that show how %Y == %C%y
with 32-bit int.
%Y %C %y
2147485547 21474855 47
10000 100 00
9999 99 99
0999 9 99
0099 0 99
0001 0 01
0000 0 00
-001 -1 99
-099 -1 01
-999 -10 01
-1000 -11 00
-10000 -101 00
-2147481748 -21474818 52
Be careful of both overflow and sign adjustment due to the
asymmetric range of years.
*/
#ifdef _WANT_C99_TIME_FORMATS
if (alt == 'E' && *era_info)
len = t_snprintf(&s[count], maxsize - count, CQ("%" SFLG "s"), (*era_info)->era_C);
else
#endif /* _WANT_C99_TIME_FORMATS */
{
char *pos = "";
int year = tim_p->tm_year + YEAR_BASE;
int century = year / 100 - (year % 100 < 0);
if (pad) /* '0' or '+' */
{
if (century >= 100 && pad == CQ('+'))
pos = "+";
}
if (width < 1)
width = 1;
len = t_snprintf(&s[count], maxsize - count, CQ("%s%.*d"), pos, (int)width,
century);
}
CHECK_LENGTH();
} break;
case CQ('d'):
case CQ('e'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == CQ('O') && *alt_digits) {
if (tim_p->tm_mday < 10) {
if (*format == CQ('d')) {
if (maxsize - count < 2)
return 0;
len = conv_to_alt_digits(&s[count], maxsize - count, 0, *alt_digits);
CHECK_LENGTH();
}
if (*format == CQ('e') || len == 0)
s[count++] = CQ(' ');
}
len = conv_to_alt_digits(&s[count], maxsize - count, tim_p->tm_mday, *alt_digits);
CHECK_LENGTH();
if (len > 0)
break;
}
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count,
*format == CQ('d') ? CQ("%.2d") : CQ("%2d"), tim_p->tm_mday);
CHECK_LENGTH();
break;
case CQ('D'):
/* %m/%d/%y */
{
len = __strftime(&s[count], maxsize - count, CQ("%m/%d/%y"), tim_p, locale,
era_info, alt_digits);
if (len > 0)
count += len;
else
return 0;
}
break;
case CQ('F'): { /* %F is equivalent to "%Y-%m-%d", flags and width can change
that. Recurse to avoid need to replicate %Y formation. */
len = __strftime(&s[count], maxsize - count, CQ("%Y-%m-%d"), tim_p, locale, era_info,
alt_digits);
if (len > 0)
count += len;
else
return 0;
} break;
case CQ('g'):
/* Be careful of both overflow and negative years, thanks to
the asymmetric range of years. */
{
int adjust = iso_year_adjust(tim_p);
int year = tim_p->tm_year >= 0 ? tim_p->tm_year % 100
: abs(tim_p->tm_year + YEAR_BASE) % 100;
if (adjust < 0 && tim_p->tm_year <= -YEAR_BASE)
adjust = 1;
else if (adjust > 0 && tim_p->tm_year < -YEAR_BASE)
adjust = -1;
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"),
((year + adjust) % 100 + 100) % 100);
CHECK_LENGTH();
}
break;
case CQ('G'): {
/* See the comments for 'C' and 'Y'; this is a variable length
field. Although there is no requirement for a minimum number
of digits, we use 4 for consistency with 'Y'. */
int sign = tim_p->tm_year < -YEAR_BASE;
int adjust = iso_year_adjust(tim_p);
int century = tim_p->tm_year >= 0 ? tim_p->tm_year / 100 + YEAR_BASE / 100
: abs(tim_p->tm_year + YEAR_BASE) / 100;
int year = tim_p->tm_year >= 0 ? tim_p->tm_year % 100
: abs(tim_p->tm_year + YEAR_BASE) % 100;
if (adjust < 0 && tim_p->tm_year <= -YEAR_BASE)
sign = adjust = 1;
else if (adjust > 0 && sign)
adjust = -1;
year += adjust;
if (year == -1) {
year = 99;
--century;
} else if (year == 100) {
year = 0;
++century;
}
CHAR fmtbuf[10], *fmt = fmtbuf;
/* int potentially overflows, so use unsigned instead. */
unsigned p_year = century * 100 + year;
if (sign)
*fmt++ = CQ('-');
else if (pad == CQ('+') && p_year >= 10000) {
*fmt++ = CQ('+');
sign = 1;
}
if (width && sign)
--width;
*fmt++ = CQ('%');
if (pad)
*fmt++ = CQ('0');
STRCPY(fmt, CQ(".*u"));
len = t_snprintf(&s[count], maxsize - count, fmtbuf, (int)width, p_year);
if (len < 0 || (count += len) >= maxsize)
return 0;
} break;
case CQ('H'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == CQ('O') && *alt_digits) {
len = conv_to_alt_digits(&s[count], maxsize - count, tim_p->tm_hour, *alt_digits);
CHECK_LENGTH();
if (len > 0)
break;
}
#endif /* _WANT_C99_TIME_FORMATS */
__fallthrough;
case CQ('k'): /* newlib extension */
len = t_snprintf(&s[count], maxsize - count,
*format == CQ('k') ? CQ("%2d") : CQ("%.2d"), tim_p->tm_hour);
CHECK_LENGTH();
break;
case CQ('l'): /* newlib extension */
if (alt == CQ('O'))
alt = CQ('\0');
__fallthrough;
case CQ('I'): {
register int h12;
h12 = (tim_p->tm_hour == 0 || tim_p->tm_hour == 12) ? 12 : tim_p->tm_hour % 12;
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len = conv_to_alt_digits(&s[count], maxsize - count, h12, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count,
*format == CQ('I') ? CQ("%.2d") : CQ("%2d"), h12);
CHECK_LENGTH();
} break;
case CQ('j'):
len = t_snprintf(&s[count], maxsize - count, CQ("%.3d"), tim_p->tm_yday + 1);
CHECK_LENGTH();
break;
case CQ('m'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len = conv_to_alt_digits(&s[count], maxsize - count, tim_p->tm_mon + 1,
*alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"), tim_p->tm_mon + 1);
CHECK_LENGTH();
break;
case CQ('M'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len
= conv_to_alt_digits(&s[count], maxsize - count, tim_p->tm_min, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"), tim_p->tm_min);
CHECK_LENGTH();
break;
case CQ('n'):
if (count < maxsize - 1)
s[count++] = CQ('\n');
else
return 0;
break;
case CQ('p'):
case CQ('P'):
_ctloc(TIME_AM_PM[tim_p->tm_hour < 12 ? 0 : 1]);
for (i = 0; i < ctloclen; i++) {
if (count < maxsize - 1)
s[count++] = (*format == CQ('P') ? (CHAR)TOLOWER(ctloc[i]) : ctloc[i]);
else
return 0;
}
break;
case CQ('q'): /* GNU quarter year */
len = t_snprintf(&s[count], maxsize - count, CQ("%.1d"), tim_p->tm_mon / 3 + 1);
CHECK_LENGTH();
break;
case CQ('R'):
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d:%.2d"), tim_p->tm_hour,
tim_p->tm_min);
CHECK_LENGTH();
break;
case CQ('s'):
/*
* From:
* The Open Group Base Specifications Issue 7
* IEEE Std 1003.1, 2013 Edition
* Copyright (c) 2001-2013 The IEEE and The Open Group
* XBD Base Definitions
* 4. General Concepts
* 4.15 Seconds Since the Epoch
* A value that approximates the number of seconds that have elapsed since the
* Epoch. A Coordinated Universal Time name (specified in terms of seconds
* (tm_sec), minutes (tm_min), hours (tm_hour), days since January 1 of the year
* (tm_yday), and calendar year minus 1900 (tm_year)) is related to a time
* represented as seconds since the Epoch, according to the expression below.
* If the year is <1970 or the value is negative, the relationship is undefined.
* If the year is >=1970 and the value is non-negative, the value is related to a
* Coordinated Universal Time name according to the C-language expression, where
* tm_sec, tm_min, tm_hour, tm_yday, and tm_year are all integer types:
* tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
* (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
* ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
* OR
* ((((tm_year-69)/4 - (tm_year-1)/100 + (tm_year+299)/400 +
* (tm_year-70)*365 + tm_yday)*24 + tm_hour)*60 + tm_min)*60 + tm_sec
*/
/* modified from %z case by hoisting offset outside if block and initializing */
{
long offset = 0; /* offset < 0 => W of GMT, > 0 => E of GMT:
subtract to get UTC */
if (tim_p->tm_isdst >= 0) {
TZ_LOCK;
if (!tzset_called) {
_tzset_unlocked();
tzset_called = 1;
}
#if defined(__TM_GMTOFF)
offset = tim_p->__TM_GMTOFF;
#else
__tzinfo_type *tz = __gettzinfo();
/* The sign of this is exactly opposite the envvar TZ. We
could directly use the global _timezone for tm_isdst==0,
but have to use __tzrule for daylight savings. */
offset = -tz->__tzrule[tim_p->tm_isdst > 0].offset;
#endif
TZ_UNLOCK;
}
len = t_snprintf(&s[count], maxsize - count, CQ("%lld"),
(((((long long)tim_p->tm_year - 69) / 4
- (tim_p->tm_year - 1) / 100 + (tim_p->tm_year + 299) / 400
+ (tim_p->tm_year - 70) * 365 + tim_p->tm_yday)
* 24
+ tim_p->tm_hour)
* 60
+ tim_p->tm_min)
* 60
+ tim_p->tm_sec - offset);
CHECK_LENGTH();
}
break;
case CQ('S'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len
= conv_to_alt_digits(&s[count], maxsize - count, tim_p->tm_sec, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"), tim_p->tm_sec);
CHECK_LENGTH();
break;
case CQ('t'):
if (count < maxsize - 1)
s[count++] = CQ('\t');
else
return 0;
break;
case CQ('T'):
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d:%.2d:%.2d"), tim_p->tm_hour,
tim_p->tm_min, tim_p->tm_sec);
CHECK_LENGTH();
break;
case CQ('u'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == CQ('O') && *alt_digits) {
len = conv_to_alt_digits(&s[count], maxsize - count,
tim_p->tm_wday == 0 ? 7 : tim_p->tm_wday, *alt_digits);
CHECK_LENGTH();
if (len > 0)
break;
}
#endif /* _WANT_C99_TIME_FORMATS */
if (count < maxsize - 1) {
if (tim_p->tm_wday == 0)
s[count++] = CQ('7');
else
s[count++] = CQ('0') + tim_p->tm_wday;
} else
return 0;
break;
case CQ('U'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len
= conv_to_alt_digits(&s[count], maxsize - count,
(tim_p->tm_yday + 7 - tim_p->tm_wday) / 7, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"),
(tim_p->tm_yday + 7 - tim_p->tm_wday) / 7);
CHECK_LENGTH();
break;
case CQ('V'): {
int adjust = iso_year_adjust(tim_p);
int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
int week = (tim_p->tm_yday + 10 - wday) / 7;
if (adjust > 0)
week = 1;
else if (adjust < 0)
/* Previous year has 53 weeks if current year starts on
Fri, and also if current year starts on Sat and
previous year was leap year. */
week = 52
+ (4 >= (wday - tim_p->tm_yday
- isleap(tim_p->tm_year
+ (YEAR_BASE - 1 - (tim_p->tm_year < 0 ? 0 : 2000)))));
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len = conv_to_alt_digits(&s[count], maxsize - count, week, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"), week);
CHECK_LENGTH();
} break;
case CQ('v'): /* BSD/OSX/Ruby extension VMS/Oracle date format
from Arnold Robbins strftime version 3.0 */
{ /* %v is equivalent to "%e-%b-%Y", flags and width can change year
format. Recurse to avoid need to replicate %b and %Y formation. */
CHAR fmtbuf[32], *fmt = fmtbuf;
STRCPY(fmt, CQ("%e-%b-%"));
fmt += STRLEN(fmt);
if (pad) /* '0' or '+' */
*fmt++ = pad;
else
*fmt++ = '+';
if (!pad)
width = 10;
if (width < 6)
width = 6;
width -= 6;
if (width) {
len = t_snprintf(fmt, fmtbuf + 32 - fmt, CQ("%lu"), width);
if (len > 0)
fmt += len;
}
STRCPY(fmt, CQ("Y"));
len = __strftime(&s[count], maxsize - count, fmtbuf, tim_p, locale, era_info,
alt_digits);
if (len > 0)
count += len;
else
return 0;
} break;
case CQ('w'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == CQ('O') && *alt_digits) {
len = conv_to_alt_digits(&s[count], maxsize - count, tim_p->tm_wday, *alt_digits);
CHECK_LENGTH();
if (len > 0)
break;
}
#endif /* _WANT_C99_TIME_FORMATS */
if (count < maxsize - 1)
s[count++] = CQ('0') + tim_p->tm_wday;
else
return 0;
break;
case CQ('W'): {
int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
wday = (tim_p->tm_yday + 7 - wday) / 7;
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len = conv_to_alt_digits(&s[count], maxsize - count, wday, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"), wday);
CHECK_LENGTH();
} break;
case CQ('y'): {
#ifdef _WANT_C99_TIME_FORMATS
if (alt == 'E' && *era_info)
len = t_snprintf(&s[count], maxsize - count, CQ("%d"), (*era_info)->year);
else
#endif /* _WANT_C99_TIME_FORMATS */
{
/* Be careful of both overflow and negative years, thanks to
the asymmetric range of years. */
int year = tim_p->tm_year % 100;
if (year < 0)
year += 100;
#ifdef _WANT_C99_TIME_FORMATS
if (alt != CQ('O') || !*alt_digits
|| !(len = conv_to_alt_digits(&s[count], maxsize - count, year, *alt_digits)))
#endif /* _WANT_C99_TIME_FORMATS */
len = t_snprintf(&s[count], maxsize - count, CQ("%.2d"), year);
}
CHECK_LENGTH();
} break;
case CQ('Y'):
#ifdef _WANT_C99_TIME_FORMATS
if (alt == 'E' && *era_info) {
ctloc = (*era_info)->era_Y;
goto recurse;
} else
#endif /* _WANT_C99_TIME_FORMATS */
{
CHAR fmtbuf[10], *fmt = fmtbuf;
int sign = tim_p->tm_year < -YEAR_BASE;
/* int potentially overflows, so use unsigned instead. */
register unsigned year = (unsigned)tim_p->tm_year + (unsigned)YEAR_BASE;
if (sign) {
*fmt++ = CQ('-');
year = UINT_MAX - year + 1;
} else if (pad == CQ('+') && year >= 10000) {
*fmt++ = CQ('+');
sign = 1;
}
if (width && sign)
--width;
*fmt++ = CQ('%');
if (pad)
*fmt++ = CQ('0');
STRCPY(fmt, CQ(".*u"));
len = t_snprintf(&s[count], maxsize - count, fmtbuf, (int)width, year);
CHECK_LENGTH();
}
break;
case CQ('z'):
if (tim_p->tm_isdst >= 0) {
long offset;
TZ_LOCK;
if (!tzset_called) {
_tzset_unlocked();
tzset_called = 1;
}
#if defined(__TM_GMTOFF)
offset = tim_p->__TM_GMTOFF;
#else
__tzinfo_type *tz = __gettzinfo();
/* The sign of this is exactly opposite the envvar TZ. We
could directly use the global _timezone for tm_isdst==0,
but have to use __tzrule for daylight savings. */
offset = -tz->__tzrule[tim_p->tm_isdst > 0].offset;
#endif
TZ_UNLOCK;
len = t_snprintf(&s[count], maxsize - count, CQ("%+03ld%.2ld"),
offset / SECSPERHOUR, labs(offset / SECSPERMIN) % 60L);
CHECK_LENGTH();
}
break;
case CQ('Z'):
if (tim_p->tm_isdst >= 0) {
size_t size;
const char *tznam = NULL;
TZ_LOCK;
if (!tzset_called) {
_tzset_unlocked();
tzset_called = 1;
}
#if defined(__TM_ZONE)
tznam = tim_p->__TM_ZONE;
#endif
if (!tznam)
tznam = tzname[tim_p->tm_isdst > 0];
/* Note that in case of wcsftime this loop only works for
timezone abbreviations using the portable codeset (aka ASCII).
This seems to be the case, but if that ever changes, this
loop needs revisiting. */
size = strlen(tznam);
for (i = 0; i < size; i++) {
if (count < maxsize - 1)
s[count++] = tznam[i];
else {
TZ_UNLOCK;
return 0;
}
}
TZ_UNLOCK;
}
break;
case CQ('%'):
if (count < maxsize - 1)
s[count++] = CQ('%');
else
return 0;
break;
default:
return 0;
}
if (*format)
format++;
else
break;
}
if (maxsize)
s[count] = CQ('\0');
return count;
}
size_t
strftime(CHAR * __restrict s, size_t maxsize, const CHAR * __restrict format,
const struct tm * __restrict tim_p)
{
#ifdef _WANT_C99_TIME_FORMATS
era_info_t *era_info = NULL;
alt_digits_t *alt_digits = NULL;
size_t ret
= __strftime(s, maxsize, format, tim_p, __get_current_locale(), &era_info, &alt_digits);
if (era_info)
free_era_info(era_info);
if (alt_digits)
free_alt_digits(alt_digits);
return ret;
#else /* !_WANT_C99_TIME_FORMATS */
return __strftime(s, maxsize, format, tim_p, __get_current_locale(), NULL, NULL);
#endif /* !_WANT_C99_TIME_FORMATS */
}
size_t
strftime_l(CHAR * __restrict s, size_t maxsize, const CHAR *__restrict format,
const struct tm * __restrict tim_p, locale_t locale)
{
#ifdef _WANT_C99_TIME_FORMATS
era_info_t *era_info = NULL;
alt_digits_t *alt_digits = NULL;
size_t ret = __strftime(s, maxsize, format, tim_p, locale, &era_info, &alt_digits);
if (era_info)
free_era_info(era_info);
if (alt_digits)
free_alt_digits(alt_digits);
return ret;
#else /* !_WANT_C99_TIME_FORMATS */
return __strftime(s, maxsize, format, tim_p, locale, NULL, NULL);
#endif /* !_WANT_C99_TIME_FORMATS */
}
|