#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include <unistd.h>
#include <time.h>
#include <glib.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "log.h"
#include "time_utils.h"
Go to the source code of this file.
Data Structures | |
struct | tzhead |
struct | MemoryStruct |
Defines | |
#define | MINUTES(x) 60*x |
#define | HOURS(x) 60*60*x |
#define | DATE_STRING_LENGTH 64 |
#define | TZ_FILES_DIR "/usr/share/zoneinfo/Etc/" |
#define | DEFAULT_TIMEZONE_OFFSET 0 |
#define | DEFAULT_TIMEZONE_NAME "GMT+00:00" |
#define | DST_FLAG "DST" |
Functions | |
static void * | myrealloc (void *ptr, size_t size) |
static size_t | writer (void *ptr, size_t size, size_t nmemb, void *data) |
time_t | get_server_time () |
gchar * | get_timezone () |
gboolean | set_timezone (const char *tzname, gboolean is_dst) |
gint | get_timezone_index (const char *tzname, gboolean *dst) |
void | set_time_utc (time_t utc_time) |
gboolean | set_time_from_server (void) |
#define DATE_STRING_LENGTH 64 |
Definition at line 42 of file time_utils.c.
#define DEFAULT_TIMEZONE_NAME "GMT+00:00" |
Definition at line 47 of file time_utils.c.
Referenced by get_timezone_index().
#define DEFAULT_TIMEZONE_OFFSET 0 |
Definition at line 46 of file time_utils.c.
#define DST_FLAG "DST" |
Definition at line 48 of file time_utils.c.
Referenced by get_timezone_index(), and set_timezone().
#define HOURS | ( | x | ) | 60*60*x |
Definition at line 40 of file time_utils.c.
#define MINUTES | ( | x | ) | 60*x |
Copyright (C) 2009 iRex Technologies B.V. All rights reserved.
Definition at line 39 of file time_utils.c.
#define TZ_FILES_DIR "/usr/share/zoneinfo/Etc/" |
Definition at line 44 of file time_utils.c.
Referenced by set_timezone().
time_t get_server_time | ( | ) |
Definition at line 94 of file time_utils.c.
References LOGPRINTF, MemoryStruct::memory, MemoryStruct::size, and writer().
Referenced by set_time_from_server().
00095 { 00096 LOGPRINTF("entry"); 00097 00098 MemoryStruct chunk; 00099 chunk.memory = NULL; 00100 chunk.size = 0; 00101 00102 curl_global_init(CURL_GLOBAL_ALL); 00103 CURL *curl_handle = curl_easy_init(); 00104 00105 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://time.irexnet.com/epoch"); 00106 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer); 00107 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); 00108 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 00109 00110 curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_LIMIT, 1); 00111 curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_TIME, 30); 00112 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 30); 00113 00114 curl_easy_perform(curl_handle); 00115 curl_easy_cleanup(curl_handle); 00116 00117 time_t now = 0; 00118 if (chunk.memory != NULL) now = atoi(chunk.memory); 00119 00120 if (chunk.memory) free(chunk.memory); 00121 curl_global_cleanup(); 00122 00123 return now; 00124 }
gchar* get_timezone | ( | void | ) |
Definition at line 128 of file time_utils.c.
References ERRORPRINTF, LOGPRINTF, and TZ_FILE.
Referenced by init_widgets().
00129 { 00130 LOGPRINTF("entry"); 00131 00132 char actual_tzfile[256]; 00133 memset(actual_tzfile, 0, sizeof(actual_tzfile)); 00134 00135 // get timezone reference 00136 if (readlink(TZ_FILE, actual_tzfile, sizeof(actual_tzfile)) == -1) 00137 { 00138 ERRORPRINTF("Can't dereference symlink %s, errno [%d][%s]", TZ_FILE, errno, strerror(errno)); 00139 return NULL; 00140 } 00141 00142 // check if it exists 00143 if (!g_file_test(actual_tzfile, G_FILE_TEST_IS_REGULAR)) 00144 { 00145 ERRORPRINTF("Can't find symlink target %s", actual_tzfile); 00146 return NULL; 00147 } 00148 00149 return g_path_get_basename(actual_tzfile); 00150 }
gint get_timezone_index | ( | const char * | tzname, | |
gboolean * | dst | |||
) |
Definition at line 178 of file time_utils.c.
References DEFAULT_TIMEZONE_NAME, DST_FLAG, LOGPRINTF, timezones, TOTAL_TIMEZONES, and WARNPRINTF.
Referenced by init_widgets().
00179 { 00180 LOGPRINTF("entry"); 00181 00182 gboolean is_dst; 00183 gint tzindex = -1; 00184 00185 if (tzname == NULL) 00186 { 00187 tzname = DEFAULT_TIMEZONE_NAME; 00188 WARNPRINTF("No timezone given, use default [%s]", tzname); 00189 } 00190 00191 int tzlength = strlen(tzname); 00192 is_dst = g_str_has_suffix(tzname, DST_FLAG); 00193 if (is_dst) 00194 { 00195 // ignore DST suffix 00196 tzlength -= strlen(DST_FLAG); 00197 } 00198 00199 int i; 00200 for (i=0; i<TOTAL_TIMEZONES; i++) 00201 { 00202 if (strncmp(tzname, timezones[i].tzname, tzlength) == 0) 00203 { 00204 tzindex = i; 00205 break; 00206 } 00207 } 00208 00209 if (tzindex == -1) 00210 { 00211 WARNPRINTF("Can't find zoneinfo for [%s]", tzname); 00212 tzindex = 12; // default to GMT 00213 is_dst = FALSE; 00214 } 00215 00216 if (dst) 00217 { 00218 *dst = is_dst; 00219 } 00220 00221 return tzindex; 00222 }
static void* myrealloc | ( | void * | ptr, | |
size_t | size | |||
) | [static] |
Definition at line 71 of file time_utils.c.
Referenced by writer().
gboolean set_time_from_server | ( | void | ) |
Definition at line 238 of file time_utils.c.
References ERRORPRINTF, get_server_time(), LOGPRINTF, and set_time_utc().
00239 { 00240 LOGPRINTF("entry"); 00241 00242 time_t utc_time = get_server_time(); 00243 if (utc_time == 0) { 00244 ERRORPRINTF("Failed to get server time"); 00245 return FALSE; 00246 } 00247 00248 set_time_utc(utc_time); 00249 return TRUE; 00250 }
void set_time_utc | ( | time_t | utc_time | ) |
Definition at line 225 of file time_utils.c.
References LOGPRINTF.
Referenced by set_time_from_server().
00226 { 00227 LOGPRINTF("entry"); 00228 00229 struct timeval tp; 00230 gettimeofday(&tp, NULL); 00231 tp.tv_sec = utc_time; 00232 settimeofday(&tp, NULL); 00233 00234 system("/sbin/hwclock -w -u"); 00235 }
gboolean set_timezone | ( | const char * | tzname, | |
gboolean | is_dst | |||
) |
Definition at line 153 of file time_utils.c.
References DST_FLAG, ERRORPRINTF, LOGPRINTF, TZ_FILE, and TZ_FILES_DIR.
Referenced by datetime_base_save_datetime().
00154 { 00155 LOGPRINTF("entry"); 00156 00157 char actual_tzfile[256]; 00158 00159 snprintf(actual_tzfile, 256, "%s%s%s", TZ_FILES_DIR, tzname, is_dst ? DST_FLAG : ""); 00160 LOGPRINTF("symlink %s to %s", actual_tzfile, TZ_FILE); 00161 if (g_file_test(TZ_FILE, G_FILE_TEST_EXISTS) && (unlink(TZ_FILE) != 0)) 00162 { 00163 ERRORPRINTF("Can't remove %s", TZ_FILE); 00164 return FALSE; 00165 } 00166 00167 if (symlink(actual_tzfile, TZ_FILE) != 0) 00168 { 00169 ERRORPRINTF("Can't create symlink from %s to %s", actual_tzfile, TZ_FILE); 00170 return FALSE; 00171 } 00172 00173 return TRUE; 00174 }
static size_t writer | ( | void * | ptr, | |
size_t | size, | |||
size_t | nmemb, | |||
void * | data | |||
) | [static] |
Definition at line 78 of file time_utils.c.
References MemoryStruct::memory, myrealloc(), and MemoryStruct::size.
Referenced by get_server_time().
00079 { 00080 size_t realsize = size * nmemb; 00081 MemoryStruct *mem = (MemoryStruct *)data; 00082 00083 mem->memory = (char*)myrealloc(mem->memory, mem->size + realsize + 1); 00084 if (mem->memory) 00085 { 00086 memcpy(&(mem->memory[mem->size]), ptr, realsize); 00087 mem->size += realsize; 00088 mem->memory[mem->size] = 0; 00089 } 00090 return realsize; 00091 }