Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created Aug 26, 2012
Embed
What would you like to do?
#import "stdio.h"
#import "time.h"
#import "string.h"
// make now
int main() {
time_t now;
int hour;
char ampm[2];
struct tm * local;
time(&now);
local = localtime(&now);
if (local->tm_hour > 12) {
hour = local->tm_hour - 12;
strncpy(ampm, "pm", 2);
} else {
hour = local->tm_hour;
strncpy(ampm, "am", 2);
}
int seconds_since_midnight = (local->tm_hour * 3600) +
(local->tm_min * 60) +
(local->tm_sec);
int day_seconds = 24 * 60 * 60;
int day_percent = (seconds_since_midnight * 100) / day_seconds;
int week_percent = ((local->tm_wday * day_seconds) +
(seconds_since_midnight) * 100) / (7 * day_seconds);
int year_percent = (((local->tm_yday * day_seconds) +
seconds_since_midnight) * 100) / (365 * day_seconds);
printf("%d:%02d%s d%d%% w%d%% y%d%%\n", hour, local->tm_min, ampm,
day_percent, week_percent, year_percent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment