Rewrite slack_get_is_localtime with ifstreams

This commit is contained in:
Eugen Wissner 2024-05-15 22:16:36 +02:00
parent 33819d2eb9
commit b8bb1c78ae
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0

View File

@ -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;
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
is_localtime = FALSE;
while (fh.good())
{
fh.getline(time_str.data(), time_str.size());
fh.clear(fh.rdstate() & (~std::ios_base::failbit));
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;
if (std::strncmp(time_str.data(), "localtime", time_str.size()) == 0)
{
return true;
}
}
fclose (fh);
return is_localtime;
return false;
}
gboolean slack_get_ntp()