From 9f5c2bda3a42c64f7708bda1ba800671f1bb6a87 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Sat, 16 Jul 2022 18:06:29 -0600 Subject: Support new locale key for time and date Since different locales support different date formats, this allows specification of the locale for a given time/date instance, rather than setting the locale for the entire program instance and affecting all other instances. Note that this probably will not have much of an effect unless the default `fmt` value is overridden (try `%c` for fun). --- src/config_time.c | 10 ++++++++++ src/config_time.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/config_time.c b/src/config_time.c index 39c8427..a480a60 100644 --- a/src/config_time.c +++ b/src/config_time.c @@ -23,6 +23,12 @@ void config_time_init(struct node* n) { n->loadfunc = &config_time_load; n->loadkey = &load_time_key; + if(getenv("LANG") != NULL) { + strcpy(( (struct config_time*) n->data)->locale, getenv("LANG")); + } else { + strcpy(( (struct config_time*) n->data)->locale, "POSIX"); + } + strcpy(((struct config_time*) n->data)->fmt, "%T"); } @@ -32,6 +38,8 @@ void load_time_key(struct node* n, char* key, char* val) { strcpy(((struct config_time*) n->data)->tz, val); else if(strcmp(key, "fmt") == 0) strcpy(((struct config_time*) n->data)->fmt, val); + else if(strcmp(key, "locale") == 0) + strcpy(((struct config_time*) n->data)->locale, val); else printf("ERROR: Unknown time key %s\n", key); } @@ -42,6 +50,8 @@ int config_time_load(struct node* n) { struct tm* info; setenv("TZ", ((struct config_time*)n->data)->tz, 1); + setlocale(LC_ALL, ((struct config_time*)n->data)->locale); + time(&timep); info = localtime(&timep); diff --git a/src/config_time.h b/src/config_time.h index e8d1ea1..8d07b63 100644 --- a/src/config_time.h +++ b/src/config_time.h @@ -24,12 +24,14 @@ #include #include #include +#include #include "common.h" #include "config_node.h" struct config_time { char tz[256]; char fmt[128]; + char locale[64]; }; void config_time_init(struct node*); -- cgit v1.2.3