List the zoneinfo directory
All checks were successful
Build / build (push) Successful in 20s

This commit is contained in:
Eugen Wissner 2024-05-31 17:01:04 +02:00
parent ed136d3b9a
commit c6036e4d2d
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 43 additions and 2 deletions

View File

@ -85,8 +85,9 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
} }
else if (g_strcmp0(method_name, "ListTimezones") == 0) else if (g_strcmp0(method_name, "ListTimezones") == 0)
{ {
auto return_value = Glib::Variant<std::vector<Glib::ustring>>::create({}); auto return_tuple = Glib::Variant<std::vector<Glib::ustring>>::create_tuple({
auto return_tuple = Glib::Variant<std::vector<Glib::ustring>>::create_tuple({ return_value }); dlackware::timedate::list_timezones()
});
g_dbus_method_invocation_return_value(invocation, return_tuple.gobj()); g_dbus_method_invocation_return_value(invocation, return_tuple.gobj());
} }
@ -165,6 +166,39 @@ int main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
namespace dlackware::timedate
{
void list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator)
{
Glib::FileTest is_directory = Glib::FILE_TEST_IS_DIR;
Glib::FileTest is_regular = Glib::FILE_TEST_IS_REGULAR;
auto zoneinfo_path = std::filesystem::path(zoneinfo_database) / prefix;
auto zoneinfo_directory = Glib::Dir(zoneinfo_path);
for (auto zoneinfo_entry : zoneinfo_directory)
{
auto entry_path = zoneinfo_path / zoneinfo_entry;
if (Glib::file_test(entry_path, is_directory))
{
list_timezones(prefix + zoneinfo_entry + "/", accumulator);
}
else if (Glib::file_test(entry_path, is_regular) && !entry_path.has_extension()
&& zoneinfo_entry != "leapseconds")
{
accumulator.push_back(prefix + zoneinfo_entry);
}
}
}
Glib::Variant<std::vector<Glib::ustring>> list_timezones()
{
std::vector<Glib::ustring> result;
list_timezones("", result);
return Glib::Variant<std::vector<Glib::ustring>>::create(result);
}
}
gchar *slack_get_timezone () { gchar *slack_get_timezone () {
gchar *zone_copied_from, *zone; gchar *zone_copied_from, *zone;

View File

@ -59,6 +59,13 @@
constexpr gint32 USEC_PER_SEC = 1000000ULL; constexpr gint32 USEC_PER_SEC = 1000000ULL;
constexpr gint32 NSEC_PER_USEC = 1000ULL; constexpr gint32 NSEC_PER_USEC = 1000ULL;
namespace dlackware::timedate
{
constexpr const char *zoneinfo_database = "/usr/share/zoneinfo";
Glib::Variant<std::vector<Glib::ustring>> list_timezones();
}
// Returns the system time zone // Returns the system time zone
gchar *slack_get_timezone (); gchar *slack_get_timezone ();