Question

.ics (iCalender) files contain start and end times as such:

DTSTART:20131023T190000
DTEND:20131023T220000

I want to use that information to construct date and time strings as follows:

"September 23, 2013 (Fri)",
"7:00 pm to 10:00 pm"

I looked around the time library and found strftime(char *s, size_t max, const char *format, const struct tm *tm) which could easily suit my needs if I could figure out how to construct the last parameter from the data I have.

Was it helpful?

Solution

Here's a rough and ready solution that'll do the trick. I've left out reading the strings from the file, which sounds like you can do. Error-checking has also mostly been left out, since this uses hardcoded data.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>


void parse_time(char * ics_string, struct tm * dest_tm) {
    char year[5], month[3], day[3], hour[3], minute[3], second[3];

    sscanf(ics_string, "%4s%2s%2s%*1s%2s%2s%2s",
            year, month, day, hour, minute, second);

    dest_tm->tm_year = strtol(year, NULL, 10) - 1900;
    dest_tm->tm_mon = strtol(month, NULL, 10) - 1;
    dest_tm->tm_mday = strtol(day, NULL, 10);
    dest_tm->tm_hour = strtol(hour, NULL, 10);
    dest_tm->tm_min = strtol(minute, NULL, 10);
    dest_tm->tm_sec = strtol(second, NULL, 10);
    dest_tm->tm_isdst = -1;

    if ( mktime(dest_tm) < 0 ) {
        fprintf(stderr, "Couldn't get local time.\n");
        exit(EXIT_FAILURE);
    }
}


bool same_day(struct tm * first, struct tm * second) {
    if ( (first->tm_year == second->tm_year) &&
         (first->tm_mon == second->tm_mon) &&
         (first->tm_mday == second->tm_mday) ) {
        return true;
    } else {
        return false;
    }
}


void calc_diff_string(char * buffer, size_t len,
        struct tm * start_tm, struct tm * end_tm) {
    char start_buffer[len];
    char end_buffer[len];

    strftime(start_buffer, len, "%B %d, %Y (%a), %I:%M %p", start_tm);

    if ( same_day(start_tm, end_tm) ) {
        strftime(end_buffer, len, "%I:%M %p", end_tm);
    } else {
        strftime(end_buffer, len, "%B %d, %Y (%a), %I:%M %p", end_tm);
    }

    sprintf(buffer, "%s to %s", start_buffer, end_buffer);
}


void print_time_diff(char * start_time, char * end_time) {
    struct tm start_tm, end_tm;
    char buffer[1024];

    parse_time(start_time, &start_tm);
    parse_time(end_time, &end_tm);

    calc_diff_string(buffer, sizeof(buffer), &start_tm, &end_tm);
    puts(buffer);
}


int main(void) {
    print_time_diff("20131023T190000", "20131023T220000");
    print_time_diff("20131023T190000", "20131024T220000");

    return EXIT_SUCCESS;
}

which outputs:

paul@local:~/src/c/scratch/timestrip$ ./timestrip
October 23, 2013 (Wed), 07:00 PM to 10:00 PM
October 23, 2013 (Wed), 07:00 PM to October 24, 2013 (Thu), 10:00 PM
paul@local:~/src/c/scratch/timestrip$

Brief commentary:

  • parse_time() takes a string in your format (excluding the DTSTART: and DTEND: prefixes) and populates a struct tm with the corresponding time.
  • same_day() returns true if the two times are on the same day, so you get the short-form message in the output.
  • calc_diff_string() calculates your output string.
  • print_time_diff() glues them all together and outputs your string.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top