Last active
December 31, 2016 05:04
-
-
Save mniip/64cc641acb5e9906c8c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <curses.h> | |
void quit() | |
{ | |
endwin(); | |
exit(EXIT_SUCCESS); | |
} | |
void sighandler(int sig) | |
{ | |
quit(); | |
} | |
int digits[13][7] = | |
{ | |
{ 0, 0, 0, 0, 0,14,14}, | |
{ 0, 0, 0, 0, 0, 0, 0}, | |
{14,17,17,17,17,17,14}, | |
{ 4, 6, 5, 4, 4, 4,31}, | |
{14,17, 8, 4, 2, 1,31}, | |
{14,17,16,14,16,17,14}, | |
{17,17,17,31,16,16,16}, | |
{31, 1, 1,15,16,17,14}, | |
{14,17, 1,15,17,17,14}, | |
{31,16,16, 8, 4, 4, 4}, | |
{14,17,17,14,17,17,14}, | |
{14,17,17,30,16,17,14}, | |
{ 0,14,14, 0,14,14, 0} | |
}; | |
void drawdigit(int y, int x, char c) | |
{ | |
int *digit = digits[c - '.']; | |
int i, j; | |
for(i = 0; i < 7; i++) | |
{ | |
int v = digit[i]; | |
for(j = 0; j < 5; j++) | |
if(v & (1 << j)) | |
{ | |
attron(COLOR_PAIR(1)); | |
mvaddch(y + i, x + j, ' '); | |
attroff(COLOR_PAIR(1)); | |
} | |
else | |
mvaddch(y + i, x + j, ' '); | |
} | |
} | |
struct timespec now; | |
struct tm last; | |
int newsecond; | |
void updatetime() | |
{ | |
newsecond = 0; | |
time_t old = now.tv_sec; | |
clock_gettime(CLOCK_REALTIME, &now); | |
if(now.tv_sec != old) | |
{ | |
memcpy(&last, gmtime(&now.tv_sec), sizeof(struct tm)); | |
newsecond = 1; | |
} | |
} | |
void drawtime() | |
{ | |
int d = 30; | |
int y = 18; | |
if(newsecond) | |
{ | |
drawdigit(y, d + 1, last.tm_hour / 10 + '0'); | |
drawdigit(y, d + 7, last.tm_hour % 10 + '0'); | |
drawdigit(y, d + 13, ':'); | |
drawdigit(y, d + 19, last.tm_min / 10 + '0'); | |
drawdigit(y, d + 25, last.tm_min % 10 + '0'); | |
drawdigit(y, d + 31, ':'); | |
drawdigit(y, d + 37, last.tm_sec / 10 + '0'); | |
drawdigit(y, d + 43, last.tm_sec % 10 + '0'); | |
drawdigit(y, d + 49, '.'); | |
} | |
drawdigit(y, d + 55, now.tv_nsec / 100000000 + '0'); | |
drawdigit(y, d + 61, now.tv_nsec / 10000000 % 10 + '0'); | |
drawdigit(y, d + 67, now.tv_nsec / 1000000 % 10 + '0'); | |
} | |
time_t NEW_YEAR = 1483228800; | |
int zones[] = {840, 825, 780, 765, 720, 690, 660, 630, 600, 570, 540, 510, 480, 420, 390, 360, 345, 330, 300, 270, 240, 210, 180, 120, 60, 0, -60, -120, -180, -210, -240, -300, -360, -420, -480, -540, -570, -600, -660, -720}; | |
int nzones = sizeof(zones) / sizeof(*zones); | |
int drawzone(int y, int x, int offt) | |
{ | |
if(newsecond) | |
{ | |
move(y, x); | |
if(now.tv_sec + offt * 60 >= NEW_YEAR) | |
{ | |
attron(COLOR_PAIR(2)); | |
printw("UTC%c%02d%02d | ------------", offt >= 0 ? '+' : '-', abs(offt) / 60, abs(offt) % 60); | |
attroff(COLOR_PAIR(2)); | |
} | |
else | |
{ | |
attron(COLOR_PAIR(3)); | |
printw("UTC%c%02d%02d | ", offt >= 0 ? '+' : '-', abs(offt) / 60, abs(offt) % 60); | |
time_t left_sec = NEW_YEAR - now.tv_sec - offt * 60 - 1; | |
if(left_sec >= 3600) | |
printw("%2ld:%02ld:%02ld.%03ld", left_sec / 3600, left_sec / 60 % 60, left_sec % 60, (1000000000 - now.tv_nsec) / 1000000 % 1000); | |
else if(left_sec >= 60) | |
printw(" %2ld:%02ld.%03ld", left_sec / 60 % 60, left_sec % 60, (1000000000 - now.tv_nsec) / 1000000 % 1000); | |
else | |
printw(" %2ld.%03ld", left_sec % 60, (1000000000 - now.tv_nsec) / 1000000 % 1000); | |
attroff(COLOR_PAIR(3)); | |
} | |
} | |
else | |
{ | |
if(now.tv_sec + offt * 60 < NEW_YEAR) | |
{ | |
attron(COLOR_PAIR(3)); | |
move(y, x + 20); | |
printw("%03ld", (1000000000 - now.tv_nsec) / 1000000 % 1000); | |
attroff(COLOR_PAIR(3)); | |
} | |
} | |
} | |
int main() | |
{ | |
initscr(); | |
noecho(); | |
start_color(); | |
init_pair(1, COLOR_BLACK, COLOR_WHITE); | |
init_pair(2, COLOR_RED, COLOR_BLACK); | |
init_pair(3, COLOR_GREEN, COLOR_BLACK); | |
signal(SIGINT, sighandler); | |
while(1) | |
{ | |
updatetime(); | |
drawtime(); | |
int i; | |
for(i = 0; i < nzones; i++) | |
drawzone(i + 1, 1, zones[i]); | |
refresh(); | |
usleep(500); | |
} | |
pause(); | |
quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on line 150, I find the below to be sufficient, and less taxing on CPU:
usleep(50000);