📄 strftime.c
字号:
9999 99 99 0999 09 99 0099 00 99 0001 00 01 0000 00 00 -001 -0 01 -099 -0 99 -999 -9 99 -1000 -10 00 -10000 -100 00 -2147481748 -21474817 48 Be careful of both overflow and sign adjustment due to the asymmetric range of years. */ int neg = tim_p->tm_year < -YEAR_BASE; int century = tim_p->tm_year >= 0 ? tim_p->tm_year / 100 + YEAR_BASE / 100 : abs (tim_p->tm_year + YEAR_BASE) / 100; count += snprintf (&s[count], maxsize - count, "%s%.*d", neg ? "-" : "", 2 - neg, century); if (count >= maxsize) return 0; } break; case 'd': case 'e': if (count < maxsize - 2) { sprintf (&s[count], *format == 'd' ? "%.2d" : "%2d", tim_p->tm_mday); count += 2; } else return 0; break; case 'D': case 'x': /* %m/%d/%y */ if (count < maxsize - 8) { sprintf (&s[count], "%.2d/%.2d/%.2d", tim_p->tm_mon + 1, tim_p->tm_mday, tim_p->tm_year >= 0 ? tim_p->tm_year % 100 : abs (tim_p->tm_year + YEAR_BASE) % 100); count += 8; } else return 0; break; case 'F': { /* Length is not known because of %C%y, so recurse. */ size_t adjust = strftime (&s[count], maxsize - count, "%C%y-%m-%d", tim_p); if (adjust > 0) count += adjust; else return 0; } break; case 'g': if (count < maxsize - 2) { /* 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; sprintf (&s[count], "%.2d", ((year + adjust) % 100 + 100) % 100); count += 2; } else return 0; break; case '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 neg = 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) neg = adjust = 1; else if (adjust > 0 && neg) adjust = -1; year += adjust; if (year == -1) { year = 99; --century; } else if (year == 100) { year = 0; ++century; } count += snprintf (&s[count], maxsize - count, "%s%.*d%.2d", neg ? "-" : "", 2 - neg, century, year); if (count >= maxsize) return 0; } break; case 'H': case 'k': if (count < maxsize - 2) { sprintf (&s[count], *format == 'k' ? "%2d" : "%.2d", tim_p->tm_hour); count += 2; } else return 0; break; case 'I': case 'l': if (count < maxsize - 2) { if (tim_p->tm_hour == 0 || tim_p->tm_hour == 12) { s[count++] = '1'; s[count++] = '2'; } else { sprintf (&s[count], *format == 'I' ? "%.2d" : "%2d", tim_p->tm_hour % 12); count += 2; } } else return 0; break; case 'j': if (count < maxsize - 3) { sprintf (&s[count], "%.3d", tim_p->tm_yday + 1); count += 3; } else return 0; break; case 'm': if (count < maxsize - 2) { sprintf (&s[count], "%.2d", tim_p->tm_mon + 1); count += 2; } else return 0; break; case 'M': if (count < maxsize - 2) { sprintf (&s[count], "%.2d", tim_p->tm_min); count += 2; } else return 0; break; case 'n': if (count < maxsize - 1) s[count++] = '\n'; else return 0; break; case 'p': if (count < maxsize - 2) { if (tim_p->tm_hour < 12) s[count++] = 'A'; else s[count++] = 'P'; s[count++] = 'M'; } else return 0; break; case 'r': if (count < maxsize - 11) { if (tim_p->tm_hour == 0 || tim_p->tm_hour == 12) { s[count++] = '1'; s[count++] = '2'; } else { sprintf (&s[count], "%.2d", tim_p->tm_hour % 12); count += 2; } s[count++] = ':'; sprintf (&s[count], "%.2d", tim_p->tm_min); count += 2; s[count++] = ':'; sprintf (&s[count], "%.2d", tim_p->tm_sec); count += 2; s[count++] = ' '; if (tim_p->tm_hour < 12) s[count++] = 'A'; else s[count++] = 'P'; s[count++] = 'M'; } else return 0; break; case 'R': if (count < maxsize - 5) { sprintf (&s[count], "%.2d:%.2d", tim_p->tm_hour, tim_p->tm_min); count += 5; } else return 0; break; case 'S': if (count < maxsize - 2) { sprintf (&s[count], "%.2d", tim_p->tm_sec); count += 2; } else return 0; break; case 't': if (count < maxsize - 1) s[count++] = '\t'; else return 0; break; case 'T': case 'X': if (count < maxsize - 8) { sprintf (&s[count], "%.2d:%.2d:%.2d", tim_p->tm_hour, tim_p->tm_min, tim_p->tm_sec); count += 8; } else return 0; break; case 'u': if (count < maxsize - 1) { if (tim_p->tm_wday == 0) s[count++] = '7'; else s[count++] = '0' + tim_p->tm_wday; } else return 0; break; case 'U': if (count < maxsize - 2) { sprintf (&s[count], "%.2d", (tim_p->tm_yday + 7 - tim_p->tm_wday) / 7); count += 2; } else return 0; break; case 'V': if (count < maxsize - 2) { 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))))); sprintf (&s[count], "%.2d", week); count += 2; } else return 0; break; case 'w': if (count < maxsize - 1) s[count++] = '0' + tim_p->tm_wday; else return 0; break; case 'W': if (count < maxsize - 2) { int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6; sprintf (&s[count], "%.2d", (tim_p->tm_yday + 7 - wday) / 7); count += 2; } else return 0; break; case 'y': if (count < maxsize - 2) { /* Be careful of both overflow and negative years, thanks to the asymmetric range of years. */ int year = tim_p->tm_year >= 0 ? tim_p->tm_year % 100 : abs (tim_p->tm_year + YEAR_BASE) % 100; sprintf (&s[count], "%.2d", year); count += 2; } else return 0; break; case 'Y': { /* Length is not known because of %C%y, so recurse. */ size_t adjust = strftime (&s[count], maxsize - count, "%C%y", tim_p); if (adjust > 0) count += adjust; else return 0; } break; case 'z': if (tim_p->tm_isdst >= 0) { if (count < maxsize - 5) { long offset; __tzinfo_type *tz = __gettzinfo (); TZ_LOCK; /* 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; TZ_UNLOCK; sprintf (&s[count], "%+03ld%.2ld", offset / SECSPERHOUR, labs (offset / SECSPERMIN) % 60L); count += 5; } else return 0; } break; case 'Z': if (tim_p->tm_isdst >= 0) { int size; TZ_LOCK; size = strlen(_tzname[tim_p->tm_isdst > 0]); for (i = 0; i < size; i++) { if (count < maxsize - 1) s[count++] = _tzname[tim_p->tm_isdst > 0][i]; else { TZ_UNLOCK; return 0; } } TZ_UNLOCK; } break; case '%': if (count < maxsize - 1) s[count++] = '%'; else return 0; break; } if (*format) format++; else break; } if (maxsize) s[count] = '\0'; return count;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -