X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fdate.c;h=f1719289088a9d19d68190a9d9c4502395a8d08c;hb=f7e9b46c69f454a1976b5ff471cc907756c01a7c;hp=965f15a4a6c4e8a4e8e3481e2828bc1e25f7eab4;hpb=c4a0c84c3e569bac79c82cb588d8fd600b234c0b;p=lackey diff --git a/src/date.c b/src/date.c index 965f15a..f171928 100644 --- a/src/date.c +++ b/src/date.c @@ -1,16 +1,16 @@ /* * Copyright (C) 2012 Andy Spencer - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -36,11 +36,6 @@ void date_init(void) YEAR = tm->tm_year+1900; MONTH = tm->tm_mon; DAY = tm->tm_mday-1; - - // Testing */ - YEAR = 2009; - MONTH = MAY; - DAY = 1; } /* Time functions */ @@ -111,6 +106,48 @@ void add_months(year_t *year, month_t *month, int months) *month = total % 12; } +stamp_t get_time(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}); +} + +int get_mins(date_t *start, date_t *end) +{ + return (get_time(end)-get_time(start))/60; +} + +int compare(date_t *a, date_t *b) +{ + int rval = a->year < b->year ? -1 : a->year > b->year ? 1 : + a->month < b->month ? -1 : a->month > b->month ? 1 : + a->day < b->day ? -1 : a->day > b->day ? 1 : + a->hour < b->hour ? -1 : a->hour > b->hour ? 1 : + a->min < b->min ? -1 : a->min > b->min ? 1 : 0; + return rval; +} + +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; +} + +int all_day(date_t *start, date_t *end) +{ + date_t test = *start; + add_days(&test.year, &test.month, &test.day, 1); + return compare(&test, end) <= 0; +} + +int no_date(date_t *date) +{ + return date->year == 0; +} + /* Debug functions */ const char *month_to_str(month_t month) {