From 0660ea8351b7ce35af918c2c8d86315159e5f83d Mon Sep 17 00:00:00 2001 From: Ivan Dmitrievich Yartsev Date: Thu, 5 Feb 2026 18:45:40 +0600 Subject: [PATCH] Added timezone functions --- source/CMakeLists.txt | 1 + source/libublsettings-gtk3-timezone.c | 147 ++++++++++++++++++++++++++ source/libublsettings-gtk3.h | 41 +++++++ 3 files changed, 189 insertions(+) create mode 100644 source/libublsettings-gtk3-timezone.c diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index f11641c..7c50daf 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -47,6 +47,7 @@ add_library(${PROJECT_NAME} SHARED ${PROJECT_NAME}-vte.c ${PROJECT_NAME}-webkit.c ${PROJECT_NAME}-window-config.c + ${PROJECT_NAME}-timezone.c ${PROJECT_NAME}.c ${PROJECT_NAME}.h) diff --git a/source/libublsettings-gtk3-timezone.c b/source/libublsettings-gtk3-timezone.c new file mode 100644 index 0000000..6e72d89 --- /dev/null +++ b/source/libublsettings-gtk3-timezone.c @@ -0,0 +1,147 @@ +#include "libublsettings-gtk3.h" + +GHashTable *__yon_timezone = NULL; + +#define timezone_path "/usr/share/zoneinfo/zone.tab" +#define timezone_countries_path "/usr/share/zoneinfo/iso3166.tab" + +#define yon_timezone_check if (__yon_timezone&&g_hash_table_size(__yon_timezone)) + +typedef struct { + char *country_code; + char *timezone; + char *comments; + char *city; + char *country; +} yon_timezone_struct; + +void _yon_timezone_countries_setup(GHashTable *timezone_countries){ + g_hash_table_remove_all(timezone_countries); + int size; + config_str countries_file = yon_file_open(timezone_countries_path,&size); + for (int i=0;icountry_code = yon_char_new(parsed[0]); + timezone_info->timezone = yon_char_new(parsed[2]); + timezone_info->city = yon_char_new(city); + timezone_info->country = yon_char_new(g_hash_table_lookup(timezone_countries,parsed[0])); + if (parsed_size>3){ + timezone_info->comments = yon_char_new(parsed[3]); + } + g_hash_table_insert(__yon_timezone,yon_char_new(parsed[2]),timezone_info); + + yon_char_parsed_free(parsed,parsed_size); + } + yon_char_parsed_free(timezone_file,size); + g_hash_table_unref(timezone_countries); + } + + return 1; +} + +void yon_timezone_unref(){ + yon_timezone_check{ + g_hash_table_unref(__yon_timezone); + __yon_timezone = NULL; + } +} + +config_str yon_timezone_get_all(size_t *size){ + (*size) = 0; + yon_timezone_check{ + config_str timezones = (config_str)g_hash_table_get_keys_as_array(__yon_timezone,(guint*)size); + qsort(timezones,*size,sizeof(char*),(__compar_fn_t)yon_char_parsed_compare); + return yon_char_parsed_copy(timezones,*size); + } + return NULL; +} + + +char *yon_timezone_get_country_code(const char *timezone){ + yon_timezone_check{ + yon_timezone_struct *timezone_info = g_hash_table_lookup(__yon_timezone,timezone); + if (timezone_info){ + return yon_char_new(timezone_info->country_code); + } + } + return NULL; +} + +char *yon_timezone_get_comments(const char *timezone){ + yon_timezone_check{ + yon_timezone_struct *timezone_info = g_hash_table_lookup(__yon_timezone,timezone); + if (timezone_info){ + return yon_char_new(timezone_info->comments); + } + } + return NULL; +} + +char *yon_timezone_get_city(const char *timezone){ + yon_timezone_check{ + yon_timezone_struct *timezone_info = g_hash_table_lookup(__yon_timezone,timezone); + if (timezone_info){ + return yon_char_new(timezone_info->city); + } + } + return NULL; +} + +char *yon_timezone_get_country(const char *timezone){ + yon_timezone_check{ + yon_timezone_struct *timezone_info = g_hash_table_lookup(__yon_timezone,timezone); + if (timezone_info){ + return yon_char_new(timezone_info->country); + } + } + return NULL; +} + +#define get_utc_command(tz) yon_char_unite("TZ='",tz,"' date +%z",NULL); + +char *yon_timezone_get_utc(const char *timezone){ + char *command = get_utc_command(timezone); + int size; + config_str ret = yon_config_load(command,&size); + if (size>0){ + yon_char_remove_last_symbol(ret[0],'\n'); + char *temp = yon_char_new(ret[0]); + yon_char_parsed_free(ret,size); + return temp; + } + return NULL; +} \ No newline at end of file diff --git a/source/libublsettings-gtk3.h b/source/libublsettings-gtk3.h index a4725bd..81439df 100644 --- a/source/libublsettings-gtk3.h +++ b/source/libublsettings-gtk3.h @@ -668,4 +668,45 @@ config_str yon_apps_get_by_categories(config_str categories, int categories_size /// @brief Block scroll element changing for GtkComboBox /// @param target - GtkComboBox to block void yon_gtk_combo_box_block_scroll(GtkComboBox *target); + +//timezone section + +/// @brief Init timezone system +/// @return 1 if timezone system has been successfully initialised, 0 if failed +int yon_timezone_init(); + + +/// @brief Finalize timezone system and free all memory; +void yon_timezone_unref(); + +/// @brief Get all timezones list +/// @param size pointer for list size; +/// @return New allocated char* array with timezones +config_str yon_timezone_get_all(size_t *size); + +/// @brief Get a country code for timezone +/// @param timezone Requested timezone; +/// @return A newly allocated char string with country code or NULL +char *yon_timezone_get_country_code(const char *timezone); + +/// @brief Get a comments for timezone +/// @param timezone Requested timezone; +/// @return A newly allocated char string with comments or NULL +char *yon_timezone_get_comments(const char *timezone); + +/// @brief Get a city for timezone +/// @param timezone Requested timezone; +/// @return A newly allocated char string with city or NULL +char *yon_timezone_get_city(const char *timezone); + +/// @brief Get a country for timezone +/// @param timezone Requested timezone; +/// @return A newly allocated char string with country or NULL +char *yon_timezone_get_country(const char *timezone); + +/// @brief Get UTC modifier for timezone +/// @param timezone Requested timezone; +/// @return A newly allocated char string with UTC modifier or NULL +char *yon_timezone_get_utc(const char *timezone); + #endif \ No newline at end of file