Skip to content

Instantly share code, notes, and snippets.

@nothings
Created November 24, 2016 08:54
Show Gist options
  • Select an option

  • Save nothings/4bda6d696cf2e729f8e58d499eb9e8b1 to your computer and use it in GitHub Desktop.

Select an option

Save nothings/4bda6d696cf2e729f8e58d499eb9e8b1 to your computer and use it in GitHub Desktop.
#define STB_DEFINE
#include "stb.h"
int get_seconds(char *timestamp)
{
int sec;
struct tm t;
// sscanf was very slow
t.tm_year = atoi(timestamp+ 0);
t.tm_mon = atoi(timestamp+ 5);
t.tm_mday = atoi(timestamp+ 8);
t.tm_hour = atoi(timestamp+11);
t.tm_min = atoi(timestamp+14);
t.tm_sec = atoi(timestamp+17);
--t.tm_mon; // month format mismatch
t.tm_year -= 1900;
t.tm_isdst = 0; // bogus!
sec = mktime(&t); // this will probably be slow as heck
assert(sec != -1);
return sec;
}
int main(int argc, char **argv)
{
stb_sdict *d = stb_sdict_new(1);
void *p;
int i=0, len;
char *s = stb_file("c:/x/data.txt", &len);
for (i=0; i < len; i += 20+20+8+2) {
int timesofar=0;
int t0 = get_seconds(s+i);
int t1 = get_seconds(s+i+20);
char *id = s + i + 40;
id[8] = '\0';
timesofar = (int) stb_sdict_get(d, id);
timesofar += (t1-t0);
stb_sdict_set(d, id, (void *) timesofar);
}
free(s);
// does the output need to be sorted? I don't know C# so can't tell
stb_sdict_for(d,i,s,p) {
printf("%s %d\n", s, (int) p);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment