]> Pileus Git - lackey/blobdiff - src/date.c
Add simple notification daemon mode
[lackey] / src / date.c
index 22104967bc7c214738794926ffa6214bdfd1a920..182d5aa2b58e2a216cc1c9a9576bc60d76b7d4ff 100644 (file)
 
 /* Time Keeping Bugs Abound! */
 
+#define _POSIX_C_SOURCE 200112L
+
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <time.h>
 
+#include "util.h"
+#include "conf.h"
 #include "date.h"
 
 /* Global data */
-year_t  YEAR;
-month_t MONTH;
-day_t   DAY;
+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);
 
-       YEAR  = tm->tm_year+1900;
-       MONTH = tm->tm_mon;
-       DAY   = tm->tm_mday-1;
+       /* Sync selection */
+       SEL.year  = NOW.year;
+       SEL.month = NOW.month;
+       SEL.day   = NOW.day;
+}
 
-       /* Testing */
-       //YEAR  = 2008;
-       //MONTH = OCT;
-       //DAY   = 21;
+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);
+}
+
+void date_sync(void)
+{
+       NOW = get_date(0);
 }
 
 /* Time functions */
@@ -92,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)
@@ -111,19 +124,44 @@ 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 get_secs(date_t *start, date_t *end)
+{
+       return (get_stamp(*end)-get_stamp(*start));
 }
 
 int compare(date_t *a, date_t *b)
@@ -136,6 +174,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;
@@ -193,8 +238,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);