]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_ddate.c
Fix configuration paths in error message
[i3/i3status] / src / print_ddate.c
index be6776adbd1f55166e5bc73af1e12d00ee42e176..0401a3f366d22c177fd9daf3ee88d4b1a98877b3 100644 (file)
@@ -4,6 +4,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "i3status.h"
+
 /* define fixed output-Strings */
 char *season_long[5] = {
         "Chaos",
@@ -143,12 +145,12 @@ int format_output(char *format, struct disc_time *dt) {
                                         printf("St. Tib's Day");
                                 } else {
                                         /* ...or parse the substring between %{ and %} ... */
-                                        *i = '\0';
+                                        *tibs_end = '\0';
                                         if (!format_output(i + 2, dt)) return 0;
-                                        *i = '%';
+                                        *tibs_end = '%';
                                 }
                                 /* ...and continue with the rest */
-                                i = tibs_end + 2;
+                                i = tibs_end;
                                 break;
                         case '}':
                                 i++;
@@ -164,49 +166,46 @@ int format_output(char *format, struct disc_time *dt) {
 }
 
 /* Get the current date and convert it to discordian */
-struct disc_time *get_ddate() {
-        time_t current_time = time(NULL);
+struct disc_time *get_ddate(struct tm *current_tm) {
+        static struct disc_time dt;
 
-        if (current_time == (time_t) -1) {
+        if (current_tm == NULL)
                 return NULL;
-        }
-
-        struct tm *current_tm = localtime(&current_time);
-
-        if (current_tm == NULL) {
-                return NULL;
-        }
 
         /* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
            year in gregorian calendar */
         int is_leap_year = !(current_tm->tm_year % 4) &&
                             (!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
 
-        struct disc_time *dt = malloc(sizeof(dt));
         if (is_leap_year && current_tm->tm_yday == 59) {
                 /* On St. Tibs Day we don't have to define a date */
-                dt->st_tibs_day = 1;
+                dt.st_tibs_day = 1;
         } else {
-                dt->st_tibs_day = 0;
-                dt->season_day = current_tm->tm_yday % 73;
+                dt.st_tibs_day = 0;
+                dt.season_day = current_tm->tm_yday % 73;
                 if (is_leap_year && current_tm->tm_yday > 59) {
-                        dt->week_day = (current_tm->tm_yday - 1) % 5;
+                        dt.week_day = (current_tm->tm_yday - 1) % 5;
                 } else {
-                        dt->week_day = current_tm->tm_yday % 5;
+                        dt.week_day = current_tm->tm_yday % 5;
                 }
         }
-        dt->year = current_tm->tm_year + 3066;
-        dt->season = current_tm->tm_yday / 73;
-        return dt;
+        dt.year = current_tm->tm_year + 3066;
+        dt.season = current_tm->tm_yday / 73;
+        return &dt;
 }
 
-void print_ddate(const char *format) {
-        struct disc_time *dt = get_ddate();
-        if (dt == NULL) {
+void print_ddate(const char *format, struct tm *current_tm) {
+        static char *form = NULL;
+        struct disc_time *dt;
+        if ((dt = get_ddate(current_tm)) == NULL)
                 return;
-        }
-        char *form = strdup(format);
+        if (form == NULL)
+                if ((form = malloc(strlen(format) + 1)) == NULL)
+                        return;
+        if (output_format == O_I3BAR)
+                printf("{\"name\":\"ddate\", \"full_text\":\"");
+        strcpy(form, format);
         format_output(form, dt);
-        free(dt);
-        free(form);
+        if (output_format == O_I3BAR)
+                printf("\"}");
 }