用 C 获取 ISO8601 周数
When "%V"
with strftime()
不可用或有问题,代码可以定向计算 ISO 8601 周。
ISO 8601一年中的几周从星期一开始。
当一个人想要找到ISO 8601 周年,通常还需要相应的“年”。
一年中的第一周,即第 1 周,是从星期一开始,一月份至少有 4 天的第一周 - 或者如下面的代码所示,一年中的第一个星期四位于第一周。
12 月 31 日有可能是明年的第一周。
1 月 1 日可能是上一年的第 52/53 周。
#include <time.h>
// return 1 on failure, 0 on success
int tm_YearWeek(const struct tm *tmptr, int *year, int *week) {
// work with local copy
struct tm tm = *tmptr;
// fully populate the yday and wday fields.
if (mktime(&tm) == -1) {
return 1;
}
// Find day-of-the-week: 0 to 6.
// Week starts on Monday per ISO 8601
// 0 <= DayOfTheWeek <= 6, Monday, Tuesday ... Sunday
int DayOfTheWeek = (tm.tm_wday + (7 - 1)) % 7;
// Offset the month day to the Monday of the week.
tm.tm_mday -= DayOfTheWeek;
// Offset the month day to the mid-week (Thursday) of the week, 3 days later.
tm.tm_mday += 3;
// Re-evaluate tm_year and tm_yday (local time)
if (mktime(&tm) == -1) {
return 1;
}
*year = tm.tm_year + 1900;
// Convert yday to week of the year, stating with 1.
*week = tm.tm_yday / 7 + 1;
return 0;
}
Example
int main() {
struct tm tm = { 0 };
tm.tm_year = 2008 - 1900;
tm.tm_mon = 12 - 1;
tm.tm_mday = 31;
tm.tm_isdst = -1;
int y = 0, w = 0;
int err = tm_YearWeek(&tm, &y, &w);
printf("Err:%d Year:%d Week:%d %02d%02d\n", err, y, w, y%100, w);
return 0;
}
2008 年 12 月 31 日的输出为 2009 年第 1 周或0901。这是根据上面的讨论所预期的,并且可以解释 OP 对 OP 代码未明确表达的关注。
Err:0 Year:2009 Week:1 0901