From 81e3a36c830463d97ff4f5937a670206cc26e193 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 6 Jan 2021 15:24:01 -0800 Subject: [PATCH] format: Move epoch into struct fmt_settings. --- src/data/format.c | 21 +++++++++++++++++++++ src/data/format.h | 2 ++ src/data/settings.c | 18 ++---------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/data/format.c b/src/data/format.c index 8aa16e65ff..d0b87a503f 100644 --- a/src/data/format.c +++ b/src/data/format.c @@ -20,6 +20,7 @@ #include #include +#include #include #include "data/identifier.h" @@ -65,6 +66,7 @@ fmt_settings_uninit (struct fmt_settings *settings) void fmt_settings_copy (struct fmt_settings *new, const struct fmt_settings *old) { + new->epoch = old->epoch; new->decimal = old->decimal; for (int i = 0; i < FMT_N_CCS; i++) new->ccs[i] = fmt_number_style_clone (old->ccs[i]); @@ -147,6 +149,25 @@ fmt_settings_get_style (const struct fmt_settings *settings, } } +static int +default_epoch (void) +{ + static int epoch = 0; + if (!epoch) + { + time_t t = time (0); + struct tm *tm = localtime (&t); + epoch = (tm != NULL ? tm->tm_year + 1900 : 2000) - 69; + } + return epoch; +} + +int +fmt_settings_get_epoch (const struct fmt_settings *settings) +{ + return !settings->epoch ? default_epoch () : settings->epoch; +} + void fmt_settings_set_cc (struct fmt_settings *settings, enum fmt_type type, struct fmt_number_style *style) diff --git a/src/data/format.h b/src/data/format.h index 3637b95d0c..8cc1b8718a 100644 --- a/src/data/format.h +++ b/src/data/format.h @@ -180,6 +180,7 @@ int fmt_neg_affix_width (const struct fmt_number_style *); struct fmt_settings { + int epoch; /* 0 for default epoch. */ char decimal; /* '.' or ','. */ struct fmt_number_style *ccs[FMT_N_CCS]; /* CCA through CCE. */ }; @@ -191,6 +192,7 @@ void fmt_settings_copy (struct fmt_settings *, const struct fmt_settings *); const struct fmt_number_style *fmt_settings_get_style ( const struct fmt_settings *, enum fmt_type); +int fmt_settings_get_epoch (const struct fmt_settings *); void fmt_settings_set_cc (struct fmt_settings *, enum fmt_type, struct fmt_number_style *); diff --git a/src/data/settings.c b/src/data/settings.c index 37b609dcfb..776cd51957 100644 --- a/src/data/settings.c +++ b/src/data/settings.c @@ -20,7 +20,6 @@ #include #include -#include #include "data/case.h" #include "data/format.h" @@ -53,7 +52,6 @@ struct settings int viewwidth; bool safer_mode; bool include; - int epoch; bool route_errors_to_terminal; bool route_errors_to_listing; bool scompress; @@ -89,7 +87,6 @@ static struct settings the_settings = { 79, /* viewwidth */ false, /* safer_mode */ true, /* include */ - -1, /* epoch */ true, /* route_errors_to_terminal */ true, /* route_errors_to_listing */ true, /* scompress */ @@ -129,7 +126,6 @@ static struct settings the_settings = { void settings_init (void) { - settings_set_epoch (-1); settings_set_decimal_char (get_system_decimal ()); } @@ -297,24 +293,14 @@ settings_set_include (bool include) int settings_get_epoch (void) { - assert (the_settings.epoch >= 0); - - return the_settings.epoch; + return fmt_settings_get_epoch (&the_settings.styles); } /* Sets the year that starts the epoch. */ void settings_set_epoch (int epoch) { - if (epoch < 0) - { - time_t t = time (0); - struct tm *tm = localtime (&t); - epoch = (tm != NULL ? tm->tm_year + 1900 : 2000) - 69; - } - - the_settings.epoch = epoch; - assert (the_settings.epoch >= 0); + the_settings.styles.epoch = epoch; } /* Compress system files by default? */ -- 2.30.2