2

All of the examples I can find for this online seem to use languages which have facilities that have no obvious analogy in C. I need a solution that will work in plain C, without depending any any external libraries beyond the standard ones.

Given a year number and iso week number, I want to know how to calculate what the actual date (day, month, and year) is for those values.

EDIT: Thank you for the answers so far, but I think that it may not clear what I was originally trying to ask. I an not talking about weeks since January 1 of a given year, I am talking specifically about iso week numbers. For example, iso week 1 of 2019 starts on December 31, 2018... iso weeks always start on a monday, and the year number for its start date can sometimes be off from the actual calendar year by 1.

markt1964
  • 2,638
  • 2
  • 22
  • 54
  • Use `localtime`, `localtime_r`, `gmtime` or `gmtime_r` from `time.h` – ikegami Jan 12 '18 at 22:09
  • 1
    More specifically, you can probably get pretty far by filling in a `struct tm` with `tm_year` containing the year you want (minus 1900, of course), `tm_mon` containing 0, and `tm_mday` containing 7 times the week number, plus the day of the week. Then call `mktime()`. After you do so, `tm_mon`, `tm_mday`, and `tm_wday` should contain the correct values for the date. – Steve Summit Jan 12 '18 at 22:18
  • @SteveSummit: so basically running [this answer](https://stackoverflow.com/q/42568215/2564301) in reverse. – Jongware Jan 12 '18 at 22:40
  • There is useful information available in [How do I calculate the week number given a date?](https://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date) . My answer includes code (it is actually SPL — stored procedure language (for Informix) — but you can think of it as a pseudo-code. It is not dreadfully difficult to follow (though it can be painful to write the code so it works). – Jonathan Leffler Jan 13 '18 at 01:15
  • There's a related question asked a day or two ago — [C week of year with variable start and anchor day](https://stackoverflow.com/questions/48215997/c-week-of-year-with-variable-start-and-anchor-days). It is more about going date to week than the converse, and you probably aren't after the flexibility that it asks for. – Jonathan Leffler Jan 13 '18 at 01:22

1 Answers1

3

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • While very helpful, I am not sure the code is correct for ISO format. Both the Wikipedia examples and https://www.tondering.dk/claus/cal/iso8601.php indicate the ISO day in this format is 1-7, not 0-6 as shown. Today is June 30: 2020-W27-2. That is also what the GNU date utility shows for today. This program converts 2020-W27-2 to 1 July 2020. – James K. Lowden Jun 30 '20 at 22:04