]> Pileus Git - lackey/blobdiff - src/date.c
Add some special formatting
[lackey] / src / date.c
index ec71579d359380fe9054ad2dbfba6811437b6661..c7e5cbde8da081cfb2f810e85618072d0fff92dc 100644 (file)
@@ -36,6 +36,11 @@ void date_init(void)
        YEAR  = tm->tm_year+1900;
        MONTH = tm->tm_mon;
        DAY   = tm->tm_mday-1;
+
+       /* Testing */
+       //YEAR  = 2008;
+       //MONTH = OCT;
+       //DAY   = 21;
 }
 
 /* Time functions */
@@ -106,6 +111,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)
 {