mkale.com

Code Prettify

By October 27, 2011 3:26PM [link]

I installed the prettify module from google code so I can write about code here. Let's test it out! Just for fun, here are a few time helper functions that I use:

#include <time.h>

time_t midnightOnDay(time_t t) {
    struct tm theday;
    localtime_r(&t, &theday);
    theday.tm_hour = 0;
    theday.tm_min = 0;
    theday.tm_sec = 0;
    return mktime(&theday);
}

time_t todayMidnight() {
    return midnightOnDay(time(0));
}

bool areSameDay(time_t t1, time_t t2) {
    struct tm tm1, tm2;
    localtime_r(&t1, &tm1);
    localtime_r(&t2, &tm2);
    return ((tm1.tm_mday == tm2.tm_mday) && (tm1.tm_mon == tm2.tm_mon) && (tm1.tm_year == tm2.tm_year));
}

int daysBetween(time_t t1, time_t t2) {
    // we'll be rounding down fractional days, so set t1 to midnight and then do division
    time_t newt1 = midnightOnDay(t1);
    return ((t2 - newt1) / TWENTYFOURHOURS_SECONDS);
}

Let's see how this works!

Update November 4th, 3pm

I disabled the module temporarily while I work out the formatting.

History | Blog | mkale.com