diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 84 |
1 files changed, 52 insertions, 32 deletions
@@ -17,16 +17,21 @@ * along with @. If not, see <http://www.gnu.org/licenses/>. */ +#include "config.h" + +#ifdef ENABLE_NLS +#include <locale.h> +#endif + #include <errno.h> -#include <inttypes.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> -#include "config.h" #include "datetime.h" +#include "i18n.h" #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG #include <getopt.h> @@ -60,22 +65,23 @@ static const struct option LONGOPTS_[] = { static void _print_usage(FILE *stream, const char *program_name) { - fprintf(stream, "Usage: %s [date]time\n", program_name); + fprintf(stream, _("Usage: %s [date]time\n"), program_name); } static void _print_help(const char *program_name) { _print_usage(stdout, program_name); - puts("Options:"); #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG - puts("\t-F, --list-formats List all supported date and time formats"); - puts("\t-h, --help Show this help information"); - puts("\t-V, --version Show version information"); + puts(_("Options:\n" + " -F, --list-formats List all supported date and time formats\n" + " -h, --help Show this help information\n" + " -V, --version Show version information")); #else - puts("\t-F List all supported date and time formats"); - puts("\t-h Show this help information"); - puts("\t-V Show version information"); + puts(_("Options:\n" + " -F List all supported date and time formats\n" + " -h Show this help information\n" + " -V Show version information")); #endif } @@ -111,31 +117,31 @@ _list_formats(const struct tm *tm) char *buf; size_t i; - puts("Time formats:"); + puts(_("Time formats:")); i = 0; while (datetime_strftime_time(tm, &out, &buf_sz, &buf, &i) > 0) { if (out[0] == '\0') { continue; } - printf(" * %s\n", out); + printf(_(" * %s\n"), out); } - puts("Date formats:"); + puts(_("Date formats:")); i = 0; while (datetime_strftime_date(tm, &out, &buf_sz, &buf, &i) > 0) { if (out[0] == '\0') { continue; } - printf(" * %s\n", out); + printf(_(" * %s\n"), out); } - puts("Additional formats:"); + puts(_("Additional formats:")); i = 0; while (datetime_strftime_misc(tm, &out, &buf_sz, &buf, &i) > 0) { if (out[0] == '\0') { continue; } - printf(" * %s\n", out); + printf(_(" * %s\n"), out); } } @@ -143,13 +149,12 @@ static void _print_version(void) { printf("@ (atsign) %s%s\n", PACKAGE_VERSION, PACKAGE_VERSION_GIT); - puts("Copyright (C) 2021-2022 P. J. McDermott"); - puts("License GPLv3+: GNU GPL version 3 or later " - "<http://gnu.org/licenses/gpl.html>."); - puts("This is free software: you are free to change and redistribute " - "it."); - puts("There is NO WARRANTY, to the extent permitted by law.\n"); - printf("Please report bugs to <%s>.\n", PACKAGE_BUGREPORT); + puts(_("Copyright (C) 2021, 2022 P. J. McDermott\n" + "License GPLv3+: GNU GPL version 3 or later " + "<http://gnu.org/licenses/gpl.html>.\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law.\n")); + printf(_("Please report bugs to <%s>.\n"), PACKAGE_BUGREPORT); } static char * @@ -168,7 +173,7 @@ _concat_args(int argc, char * const argv[]) buf = calloc(buf_l, sizeof(*buf)); if (buf == NULL) { - fprintf(stderr, "Failed to allocate buffer: %s\n", + fprintf(stderr, _("Failed to allocate buffer: %s\n"), strerror(errno)); return NULL; } @@ -196,9 +201,19 @@ main(int argc, char * const argv[]) char *buf; time_t now; struct tm *now_tm; + struct tm arg_tm; time_t arg; double dif; +#ifdef ENABLE_NLS + bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); +#ifdef HAVE_BIND_TEXTDOMAIN_CODESET + bind_textdomain_codeset(PACKAGE, "UTF-8"); +#endif + textdomain(PACKAGE); + setlocale(LC_ALL, ""); +#endif + #if defined(ENABLE_TESTS) && ENABLE_TESTS dbg = false; #endif @@ -243,11 +258,11 @@ main(int argc, char * const argv[]) default: _print_usage(stderr, argv[0]); #if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG - fprintf(stderr, "Try '%s --help' for more " - "information.\n", argv[0]); + fprintf(stderr, _("Try '%s --help' for more " + "information.\n"), argv[0]); #else - fprintf(stderr, "Try '%s -h' for more " - "information.\n", argv[0]); + fprintf(stderr, _("Try '%s -h' for more " + "information.\n"), argv[0]); #endif return EXIT_FAILURE; } @@ -265,7 +280,7 @@ main(int argc, char * const argv[]) return EXIT_FAILURE; } - if (datetime_parse(now_tm, buf, &arg) < 0) { + if (datetime_parse(now_tm, buf, &arg_tm, &arg) < 0) { free(buf); return EXIT_FAILURE; } @@ -273,8 +288,13 @@ main(int argc, char * const argv[]) #if defined(ENABLE_TESTS) && ENABLE_TESTS if (dbg == true) { - dif = datetime_diff_epoch(arg); - printf("%" PRId64 "\n", (int64_t) dif); + printf("%04d-%02d-%02d %02d:%02d:%02d\n", + arg_tm.tm_year + 1900, + arg_tm.tm_mon + 1, + arg_tm.tm_mday, + arg_tm.tm_hour, + arg_tm.tm_min, + arg_tm.tm_sec); return EXIT_SUCCESS; } #endif @@ -282,7 +302,7 @@ main(int argc, char * const argv[]) now = time(NULL); dif = difftime(arg, now); if (dif >= 1000 * 24 * 60 * 60) { - fputs("Date too far in the future\n", stderr); + fputs(_("Date too far in the future\n"), stderr); return EXIT_FAILURE; } |