summaryrefslogtreecommitdiff
path: root/src/config_time.c
blob: a480a60b1111bc685a278589698f933db01708dd (plain)
    1 /**
    2  * I3cstat prints a configurable status bar for the i3 window manager
    3  * Copyright (C) 2022 Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #include "config_time.h"
   19 
   20 void config_time_init(struct node* n) {
   21   n->data = malloc(sizeof(struct config_time));
   22   n->type = CTYPE_TIME;
   23   n->loadfunc = &config_time_load;
   24   n->loadkey = &load_time_key;
   25 
   26   if(getenv("LANG") != NULL) {
   27     strcpy(( (struct config_time*) n->data)->locale, getenv("LANG"));
   28   } else {
   29     strcpy(( (struct config_time*) n->data)->locale, "POSIX");
   30   }
   31 
   32   strcpy(((struct config_time*) n->data)->fmt, "%T");
   33 }
   34 
   35 
   36 void load_time_key(struct node* n, char* key, char* val) {
   37   if(strcmp(key, "tz") == 0)
   38     strcpy(((struct config_time*) n->data)->tz, val);
   39   else if(strcmp(key, "fmt") == 0)
   40     strcpy(((struct config_time*) n->data)->fmt, val);
   41   else if(strcmp(key, "locale") == 0)
   42     strcpy(((struct config_time*) n->data)->locale, val);
   43   else
   44     printf("ERROR: Unknown time key %s\n", key);
   45 }
   46 
   47 
   48 int config_time_load(struct node* n) {
   49   time_t timep;
   50   struct tm* info;
   51 
   52   setenv("TZ", ((struct config_time*)n->data)->tz, 1);
   53   setlocale(LC_ALL, ((struct config_time*)n->data)->locale);
   54 
   55   time(&timep);
   56   info = localtime(&timep);
   57 
   58   strftime(n->text, 256, ((struct config_time*)n->data)->fmt, info);
   59 
   60   strcpy(n->color, C_DGREY);
   61   strcpy(n->label_color, C_LGREY);
   62 
   63   return 0;
   64 }

Generated by cgit