How to get a date from a year and iso week # in C
The secret lies with Jan 4. Jan 4 is always in week 1 of the ISO year.
Note: Monday is the first day of the ISO Monday to Sunday week.
Find the day-of-the-week for Jan 4. Then find days since the previous Monday -
that day starts the ISO year. Add 7 for each week (offset by 1).
[Edit] Code re-worked Jun 30, 2020 as @@JamesK.Lowden correctly pointed out code was using day-of-the-week 0-6 when it should have been 1-7. That did not affect OP's goal of the year/week number.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int year, month, day;
} ymd;
typedef struct {
int year, week, dow; // ISO week-date: year, week 1-52,53, day-of-the-week 1-7
} ISO_week_date;
int ISO_week_date_to_ymd(ymd *y, const ISO_week_date *x) {
// Set to noon, Jan 4 of the year to avoid daylight saving time issues
// Jan 4 is always in the 1st week of the year
struct tm tm = {.tm_year = x->year - 1900, .tm_mon = 0, .tm_mday = 4,
.tm_hour = 12};
// Use mktime() to find the day-of-the week
if (mktime(&tm) == -1) {
return -1;
}
// Sunday to Jan 4
int DaysSinceSunday = tm.tm_wday; // .tm_wday is days since Sunday
// Monday to Jan 4
int DaysSinceMonday = (DaysSinceSunday + (7 - 1)) % 7;
tm.tm_mday += (x->dow - 1) + (x->week - 1) * 7 - DaysSinceMonday;
if (mktime(&tm) == -1) {
return -1;
}
y->year = tm.tm_year + 1900;
y->month = tm.tm_mon + 1;
y->day = tm.tm_mday;
return 0;
}
Test
char* dow_name(ymd date) {
struct tm tm = {.tm_year = date.year - 1900, .tm_mon = date.month - 1,
.tm_mday = date.day, .tm_hour = 12};
mktime(&tm);
static char name[4];
sprintf(name, "%.3s", asctime(&tm));
return name;
}
void test(int year, int week, int dow) {
ISO_week_date week_date = {.year = year, .week = week, .dow = dow};
ymd date;
if (ISO_week_date_to_ymd(&date, &week_date)) {
fprintf(stderr, "Oops\n");
exit(EXIT_FAILURE);
}
printf("%04d-W%02d-%d --> %s %04d-%02d-%02d\n", year, week, dow,
dow_name(date), date.year, date.month, date.day);
}
int main(void) {
for (int year = 2012; year <= 2018; year++) {
test(year, 1, 1);
test(year, 52, 7); // Some ISO week-numbering years have 53 weeks
puts("");
}
for (int dow = 1; dow <= 7; dow++) {
test(2020, 27, dow);
}
}
Output
2012-W01-1 --> Mon 2012-01-02
2012-W52-7 --> Sun 2012-12-30
2013-W01-1 --> Mon 2012-12-31
2013-W52-7 --> Sun 2013-12-29
2014-W01-1 --> Mon 2013-12-30
2014-W52-7 --> Sun 2014-12-28
2015-W01-1 --> Mon 2014-12-29
2015-W52-7 --> Sun 2015-12-27
2016-W01-1 --> Mon 2016-01-04
2016-W52-7 --> Sun 2017-01-01
2017-W01-1 --> Mon 2017-01-02
2017-W52-7 --> Sun 2017-12-31
2018-W01-1 --> Mon 2018-01-01
2018-W52-7 --> Sun 2018-12-30
2020-W27-1 --> Mon 2020-06-29
2020-W27-2 --> Tue 2020-06-30
2020-W27-3 --> Wed 2020-07-01
2020-W27-4 --> Thu 2020-07-02
2020-W27-5 --> Fri 2020-07-03
2020-W27-6 --> Sat 2020-07-04
2020-W27-7 --> Sun 2020-07-05
Notes:
Code uses midday to cope with adjustments that may occur due to daylight savings time. I am especially concerned with tm.tm_mday += ....
that may cross a daylight adjustment. Alternatively setting .tm_isdst = 0
may be sufficient.