diff options
| author | Eugen Wissner <belka@caraus.de> | 2024-05-15 22:16:36 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2024-05-15 22:16:57 +0200 |
| commit | b8bb1c78ae85498cf4ed987721670612437ada2e (patch) | |
| tree | ccc967e43967a3073d5cc0e7e27f3d8d2e760980 | |
| parent | 33819d2eb9b09bde8b72027adb5476eae68e1c74 (diff) | |
| download | slack-timedate-b8bb1c78ae85498cf4ed987721670612437ada2e.tar.gz | |
Rewrite slack_get_is_localtime with ifstreams
| -rw-r--r-- | slack-timedate.cpp | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/slack-timedate.cpp b/slack-timedate.cpp index 607736f..3f8b2d8 100644 --- a/slack-timedate.cpp +++ b/slack-timedate.cpp @@ -16,7 +16,9 @@ * */ +#include <cstring> #include <filesystem> +#include <fstream> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -215,24 +217,26 @@ gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) return clock_settime (CLOCK_REALTIME, &ts) == 0; } -gboolean slack_get_is_localtime () { - FILE *fh; - char time_str[10]; // "localtime" is longer than "UTC" and has 9 letters + \0 - gboolean is_localtime; - - is_localtime = FALSE; +gboolean slack_get_is_localtime() +{ + std::ifstream fh{ "/etc/hardwareclock", std::ios::in }; + if (!fh.is_open()) + { + return false; + } + std::array<char, 10> time_str; // "localtime" is longer than "UTC" and has 9 letters + \0 - fh = fopen ("/etc/hardwareclock", "r"); - if (fh == NULL) return FALSE; - while (fgets (time_str, 10, fh)) { - if (!g_strcmp0 (time_str, "localtime")) { - is_localtime = TRUE; - break; - } - } + while (fh.good()) + { + fh.getline(time_str.data(), time_str.size()); + fh.clear(fh.rdstate() & (~std::ios_base::failbit)); - fclose (fh); - return is_localtime; + if (std::strncmp(time_str.data(), "localtime", time_str.size()) == 0) + { + return true; + } + } + return false; } gboolean slack_get_ntp() |
