X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Ftime%2Fstrftime.c;h=592b214d0ff6ac90c2a9b761303a944837b5b01f;hp=b60197df28a19c2dee757dfdaa01f1119d9f5223;hb=HEAD;hpb=062446a85db9b58b4900df82e17cb8c07ec50453 diff --git a/src/time/strftime.c b/src/time/strftime.c index b60197df..592b214d 100644 --- a/src/time/strftime.c +++ b/src/time/strftime.c @@ -1,13 +1,14 @@ #include #include #include +#include #include #include -#include "__time.h" +#include "libc.h" // FIXME: integer overflows -const char *__langinfo(nl_item); +const char *__nl_langinfo_l(nl_item, locale_t); static int is_leap(int y) { @@ -17,7 +18,33 @@ static int is_leap(int y) return !(y%4) && ((y%100) || !(y%400)); } -size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm) +static int week_num(const struct tm *tm) +{ + int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7; + /* If 1 Jan is just 1-3 days past Monday, + * the previous week is also in this year. */ + if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2) + val++; + if (!val) { + val = 52; + /* If 31 December of prev year a Thursday, + * or Friday of a leap year, then the + * prev year has 53 weeks. */ + int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7; + if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1))) + val++; + } else if (val == 53) { + /* If 1 January is not a Thursday, and not + * a Wednesday of a leap year, then this + * year has only 52 weeks. */ + int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7; + if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year))) + val = 1; + } + return val; +} + +size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc) { nl_item item; int val; @@ -67,14 +94,15 @@ do_fmt: fmt = "%Y-%m-%d"; goto recu_strftime; case 'g': - // FIXME - val = 0; //week_based_year(tm)%100; - fmt = "%02d"; - goto number; case 'G': - // FIXME - val = 0; //week_based_year(tm); fmt = "%04d"; + val = tm->tm_year + 1900; + if (tm->tm_yday < 3 && week_num(tm) != 1) val--; + else if (tm->tm_yday > 360 && week_num(tm) == 1) val++; + if (*f=='g') { + fmt = "%02d"; + val %= 100; + } goto number; case 'H': val = tm->tm_hour; @@ -133,27 +161,7 @@ do_fmt: fmt = "%02d"; goto number; case 'V': - val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7; - /* If 1 Jan is just 1-3 days past Monday, - * the previous week is also in this year. */ - if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2) - val++; - if (!val) { - val = 52; - /* If 31 December of prev year a Thursday, - * or Friday of a leap year, then the - * prev year has 53 weeks. */ - int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7; - if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1))) - val++; - } else if (val == 53) { - /* If 1 January is not a Thursday, and not - * a Wednesday of a leap year, then this - * year has only 52 weeks. */ - int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7; - if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year))) - val = 1; - } + val = week_num(tm); fmt = "%02d"; goto number; case 'w': @@ -175,14 +183,11 @@ do_fmt: fmt = "%04d"; goto number; case 'z': - if (tm->tm_isdst < 0) continue; - val = -__timezone - (tm->tm_isdst ? __dst_offset : 0); + val = -tm->__tm_gmtoff; l += snprintf(s+l, n-l, "%+.2d%.2d", val/3600, abs(val%3600)/60); continue; case 'Z': - if (tm->tm_isdst < 0 || !__tzname[0] || !__tzname[0][0]) - continue; - l += snprintf(s+l, n-l, "%s", __tzname[!!tm->tm_isdst]); + l += snprintf(s+l, n-l, "%s", tm->__tm_zone); continue; default: return 0; @@ -195,14 +200,21 @@ number: l += snprintf(s+l, n-l, fmt, val); continue; nl_strcat: - l += snprintf(s+l, n-l, "%s", __langinfo(item)); + l += snprintf(s+l, n-l, "%s", __nl_langinfo_l(item, loc)); continue; nl_strftime: - fmt = __langinfo(item); + fmt = __nl_langinfo_l(item, loc); recu_strftime: - l += strftime(s+l, n-l, fmt, tm); + l += __strftime_l(s+l, n-l, fmt, tm, loc); } if (l >= n) return 0; s[l] = 0; return l; } + +size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm) +{ + return __strftime_l(s, n, f, tm, LC_GLOBAL_LOCALE); +} + +weak_alias(__strftime_l, strftime_l);