Added timezone functions

pull/64/head
parent cea74b3d89
commit 0660ea8351

@ -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)

@ -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;i<size;i++){
if (countries_file[i][0]=='#') continue;
yon_char_remove_last_symbol(countries_file[i],'\n');
int parsed_size;
config_str parsed = yon_char_parse(countries_file[i],&parsed_size,"\t");
if (size<2) continue;
g_hash_table_insert(timezone_countries,yon_char_new(parsed[0]),yon_char_new(parsed[1]));
yon_char_parsed_free(parsed,parsed_size);
}
yon_char_parsed_free(countries_file,size);
}
int yon_timezone_init(){
yon_timezone_check{
return 0;
} else {
GHashTable *timezone_countries = g_hash_table_new_full(g_str_hash, g_str_equal,free,free);
_yon_timezone_countries_setup(timezone_countries);
__yon_timezone = g_hash_table_new_full(g_str_hash,g_str_equal,free,free);
int size;
config_str timezone_file = yon_file_open(timezone_path,&size);
for (int i=0;i<size;i++){
if (timezone_file[i][0]=='#') continue;
yon_char_remove_last_symbol(timezone_file[i],'\n');
int parsed_size;
config_str parsed = yon_char_parse(timezone_file[i],&parsed_size,"\t");
if (parsed_size<3) continue;
yon_timezone_struct *timezone_info = malloc(sizeof(yon_timezone_struct));
memset(timezone_info,0,sizeof(yon_timezone_struct));
char *city = strstr(parsed[2],"/");
if (city){
city++;
}
timezone_info->country_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;
}

@ -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
Loading…
Cancel
Save