X-Git-Url: http://pileus.org/git/?p=lackey;a=blobdiff_plain;f=src%2Fdate.c;h=4371dffde91570abfde169baa12f65efc4dc62ae;hp=9bcf81663906629c95bbf32890ae13026c05946e;hb=39c440444ff38c0e1702209c67cf018b92c71882;hpb=296fd1bb5f87b1961e98c7ea4c224219012f7161 diff --git a/src/date.c b/src/date.c index 9bcf816..4371dff 100644 --- a/src/date.c +++ b/src/date.c @@ -17,23 +17,42 @@ /* Time Keeping Bugs Abound! */ +#define _POSIX_C_SOURCE 200112L + #include +#include +#include #include +#include "util.h" +#include "conf.h" #include "date.h" /* Global data */ +date_t NOW; date_t SEL; /* Initialize */ void date_init(void) { - time_t sec = time(NULL); - struct tm *tm = localtime(&sec); + /* Sync current time */ + NOW = get_date(0); + + /* Sync selection */ + SEL.year = NOW.year; + SEL.month = NOW.month; + SEL.day = NOW.day; +} + +void date_config(const char *group, const char *name, const char *key, const char *value) +{ + if (match(group, "date") && match(key, "timezone") && value) + setenv("TZ", get_string(value), 1); +} - SEL.year = tm->tm_year+1900; - SEL.month = tm->tm_mon; - SEL.day = tm->tm_mday-1; +void date_sync(void) +{ + NOW = get_date(0); } /* Time functions */ @@ -85,16 +104,17 @@ wday_t start_of_month(year_t year, month_t month) void add_days(year_t *year, month_t *month, day_t *day, int days) { - time_t time = mktime(&(struct tm){ - .tm_year = *year-1900, - .tm_mon = *month, - .tm_mday = *day+1, - .tm_hour = 12}); - time += days*24*60*60; - struct tm *tm = localtime(&time); - *year = tm->tm_year+1900; - *month = tm->tm_mon; - *day = tm->tm_mday-1; + date_t date = { + .year = *year, + .month = *month, + .day = *day, + .hour = 12 + }; + stamp_t stamp = get_stamp(date); + date_t then = get_date(stamp + days*24*60*60); + *year = then.year; + *month = then.month; + *day = then.day; } void add_months(year_t *year, month_t *month, int months) @@ -104,19 +124,39 @@ void add_months(year_t *year, month_t *month, int months) *month = total % 12; } -stamp_t get_time(date_t *date) +/* Date functions */ +date_t get_date(stamp_t stamp) +{ + time_t t = stamp ? stamp : time(NULL); + struct tm *tm = localtime(&t); + date_t date = { + .year = tm->tm_year+1900, + .month = tm->tm_mon, + .day = tm->tm_mday-1, + .hour = tm->tm_hour, + .min = tm->tm_min, + .sec = tm->tm_sec, + }; + return date; +} + +stamp_t get_stamp(date_t date) { - return mktime(&(struct tm){ - .tm_year = date->year-1900, - .tm_mon = date->month, - .tm_mday = date->day+1, - .tm_hour = date->hour, - .tm_min = date->min}); + struct tm tm = { + .tm_year = date.year-1900, + .tm_mon = date.month, + .tm_mday = date.day+1, + .tm_hour = date.hour, + .tm_min = date.min, + .tm_sec = date.sec, + }; + time_t t = mktime(&tm); + return (stamp_t)t; } int get_mins(date_t *start, date_t *end) { - return (get_time(end)-get_time(start))/60; + return (get_stamp(*end)-get_stamp(*start))/60; } int compare(date_t *a, date_t *b) @@ -129,6 +169,13 @@ int compare(date_t *a, date_t *b) return rval; } +int same_day(date_t *a, date_t *b) +{ + return a->year == b->year && + a->month == b->month && + a->day == b->day; +} + int before(date_t *start, int year, int month, int day, int hour, int min) { return compare(start, &(date_t){year, month, day, hour, min}) < 0; @@ -186,8 +233,21 @@ const char *day_to_string(wday_t day) /* Test functions */ void date_test(void) { + setenv("TZ", "US/Central", 1); + + time_t timet = time(NULL); + date_t date = get_date(timet); + stamp_t stamp = get_stamp(date); + + printf("Time\n"); + printf(" time %ld\n", timet); + printf(" stamp %lld\n", stamp); + printf(" date %04d-%02d-%02d %02d:%02d %02ds\n", + date.year, date.month, date.day, + date.hour, date.min, date.sec); + printf("Info\n"); - printf(" Year Month Start Weeks Days\n"); + printf(" Year Month Start Weeks Days\n"); for (int y = 2012; y <= 2012; y++) for (int m = JAN; m <= DEC; m++) { printf(" %-5d", y);