Skip to content

Instantly share code, notes, and snippets.

@datafatmunger
Last active April 7, 2017 13:21
Show Gist options
  • Select an option

  • Save datafatmunger/eac04bd9c86fbc4cdb988287ddac340a to your computer and use it in GitHub Desktop.

Select an option

Save datafatmunger/eac04bd9c86fbc4cdb988287ddac340a to your computer and use it in GitHub Desktop.
JBG's Amsterdam Life in C
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
struct Task {
char start[8];
char end[8];
char name[20];
int fail;
};
void print_task(char *name, int len, int offset, int fail) {
printf("%20s |", name);
for(int i = 0; i < offset/2; i++) {
printf(" ");
}
for(int i = 0; i < len/2; i++) {
if(fail)
printf("x");
else
printf("#");
}
printf("\n");
}
void print_header(int sm, int sy, int em, int ey) {
int c = 0;
int im = sm;
int iy = sy;
printf("%20s |", "");
while(iy - 2 != ey) {
printf("%d |", iy + 1900);
iy++;
}
printf("\n");
}
int len_months(sm, sy, em, ey) {
int c = 0;
int im = sm;
int iy = sy;
while(iy != ey || em != im) {
while((iy != ey && im < 12) || (iy == ey && em != im)) {
c++, im++;
}
if(iy != ey) {
im = 0;
iy++;
}
}
return c;
}
void parse_dates(struct Task t, struct tm *s, struct tm *e) {
if(strptime(t.start, "%b %Y", s) == NULL) {
fprintf(stderr, "Error: Start time parse failed for %s", t.name);
exit(1);
}
if(strptime(t.end, "%b %Y", e) == NULL) {
fprintf(stderr, "Error: End time parse failed for %s", t.name);
exit(1);
}
}
int main(int argc, char **argv) {
struct Task tasks[] = {
{"Sep 2009", "May 2011", "Whatser", 1},
{"Feb 2011", "Apr 2017", "Hackers & Founders", 0},
{"Jul 2011", "Apr 2017", "HIP*T", 0},
{"Aug 2011", "Jul 2013", "Ideedock", 1},
{"Dec 2011", "Jan 2017", "HearUsHere", 0},
{"Mar 2013", "Apr 2017", "H&F Building", 0},
{"Jul 2013", "Apr 2017", "Hackers & Designers", 0},
{"Dec 2013", "Jan 2016", "DuoDisco", 0},
{"Jan 2015", "Mar 2016", "Verifeye", 0},
{"Aug 2014", "Jul 2016", "Curious Monkeys", 1},
{"Sep 2015", "Sep 2016", "BLACK", 1}
};
int num_tasks = sizeof(tasks)/sizeof(tasks[0]);
struct Task l = tasks[num_tasks - 1];
struct tm fs;
struct tm ls;
struct tm le;
if(strptime("Jan 2009", "%b %Y", &fs) == NULL) {
fprintf(stderr, "Error: Start time parse failed.");
exit(1);
}
parse_dates(l, &ls, &le);
print_header(fs.tm_mon, fs.tm_year, le.tm_mon, le.tm_year);
for(int i = 0; i < num_tasks; i++) {
struct Task t = tasks[i];
struct tm st;
struct tm et;
parse_dates(t, &st, &et);
int task_len = len_months(
st.tm_mon, st.tm_year,
et.tm_mon, et.tm_year);
int offset = len_months(
fs.tm_mon, fs.tm_year,
st.tm_mon, st.tm_year);
print_task(t.name, task_len, offset, t.fail);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment