How to create an array of dates between a start and end date in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    int year;
    int month;
    int day;
} Date;

time_t make_time_t(int year, int month, int day) {
    struct tm t = {0};
    t.tm_year = year - 1900;
    t.tm_mon  = month - 1;
    t.tm_mday = day;
    
    return mktime(&t);
}

Date from_time_t(time_t t) {
    struct tm *tm = localtime(&t);
    Date d;
    d.year  = tm->tm_year + 1900;
    d.month = tm->tm_mon + 1;
    d.day   = tm->tm_mday;
    
    return d;
}

int main(void) {
    // YYYY MM DD
    int y1 = 2026, m1 = 1, d1 = 3;
    int y2 = 2026, m2 = 1, d2 = 12;

    time_t start = make_time_t(y1, m1, d1);
    time_t end   = make_time_t(y2, m2, d2);

    if (start > end) {
        fprintf(stderr, "Start date must be <= end date\n");
        return 1;
    }

    // Count days
    size_t count = 0;
    for (time_t t = start; t <= end; t += 24 * 3600)
        count++;

    // Allocate array
    Date *dates = malloc(count * sizeof(Date));
    if (!dates) {
        perror("malloc");
        return 1;
    }

    // Fill array
    size_t idx = 0;
    for (time_t t = start; t <= end; t += 24 * 3600)
        dates[idx++] = from_time_t(t);

    // Print results
    printf("Generated %zu dates:\n", count);
    for (size_t i = 0; i < count; i++)
        printf("%04d-%02d-%02d\n", dates[i].year, dates[i].month, dates[i].day);

    free(dates);
    
    return 0;
}


/*
run:

Generated 10 dates:
2026-01-03
2026-01-04
2026-01-05
2026-01-06
2026-01-07
2026-01-08
2026-01-09
2026-01-10
2026-01-11
2026-01-12

*/

 



answered Jan 30 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    int year;
    int month;
    int day;
} Date;

/* Convert Y-M-D to time_t */
time_t make_time_t(int year, int month, int day) {
    struct tm t = {0};
    t.tm_year = year - 1900;
    t.tm_mon  = month - 1;
    t.tm_mday = day;
    return mktime(&t);
}

/* Convert time_t to Date */
Date from_time_t(time_t t) {
    struct tm *tm = localtime(&t);
    Date d;
    d.year  = tm->tm_year + 1900;
    d.month = tm->tm_mon + 1;
    d.day   = tm->tm_mday;
    return d;
}

/* Print a date */
void print_date(Date d) {
    printf("%04d-%02d-%02d\n", d.year, d.month, d.day);
}

/* Compute days between two dates */
int days_between(Date a, Date b) {
    time_t ta = make_time_t(a.year, a.month, a.day);
    time_t tb = make_time_t(b.year, b.month, b.day);
    double diff = difftime(tb, ta);
    return (int)(diff / (24 * 3600));
}

/* Add N days to a date */
Date add_days(Date d, int n) {
    time_t t = make_time_t(d.year, d.month, d.day);
    t += (time_t)n * 24 * 3600;
    return from_time_t(t);
}

/* Generate an array of dates from start to end (inclusive) */
Date* generate_dates(Date start, Date end, size_t *out_count) {
    time_t t_start = make_time_t(start.year, start.month, start.day);
    time_t t_end   = make_time_t(end.year, end.month, end.day);

    if (t_start > t_end) {
        *out_count = 0;
        return NULL;
    }

    size_t count = 0;
    for (time_t t = t_start; t <= t_end; t += 24 * 3600)
        count++;

    Date *arr = malloc(count * sizeof(Date));
    if (!arr) return NULL;

    size_t idx = 0;
    for (time_t t = t_start; t <= t_end; t += 24 * 3600)
        arr[idx++] = from_time_t(t);

    *out_count = count;
    return arr;
}

int main(void) {
    Date start = {2026, 1, 3};
    Date end   = {2026, 1, 12};

    size_t count = 0;
    Date *dates = generate_dates(start, end, &count);

    if (!dates) {
        fprintf(stderr, "Failed to generate dates\n");
        return 1;
    }

    printf("Generated %zu dates:\n", count);
    for (size_t i = 0; i < count; i++)
        print_date(dates[i]);

    free(dates);

    return 0;
}



/*
run:

Generated 10 dates:
2026-01-03
2026-01-04
2026-01-05
2026-01-06
2026-01-07
2026-01-08
2026-01-09
2026-01-10
2026-01-11
2026-01-12

*/

 



answered Jan 30 by avibootz
...