1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:13:01 +01:00

Use time_t instead of uint64_t for timestamps

Also call time() directly from get_unix_time() instead of manually updating the time val.
This commit is contained in:
Jfreegman 2016-09-22 18:31:52 -04:00
parent c24e1bd2b8
commit 2194b9e259
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
13 changed files with 26 additions and 39 deletions

View File

@ -101,7 +101,7 @@ struct Node {
static struct DHT_Nodes { static struct DHT_Nodes {
struct Node list[MAX_NODES]; struct Node list[MAX_NODES];
size_t count; size_t count;
uint64_t last_updated; time_t last_updated;
} Nodes; } Nodes;
@ -574,7 +574,7 @@ static void DHT_bootstrap(Tox *m)
/* Manages connection to the Tox DHT network. */ /* Manages connection to the Tox DHT network. */
void do_tox_connection(Tox *m) void do_tox_connection(Tox *m)
{ {
static uint64_t last_bootstrap_time = 0; static time_t last_bootstrap_time = 0;
bool connected = tox_self_get_connection_status(m) != TOX_CONNECTION_NONE; bool connected = tox_self_get_connection_status(m) != TOX_CONNECTION_NONE;
if (!connected && timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL)) { if (!connected && timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL)) {

View File

@ -825,7 +825,7 @@ static void draw_infobox(ToxWindow *self)
if (x2 < INFOBOX_WIDTH || y2 < INFOBOX_HEIGHT) if (x2 < INFOBOX_WIDTH || y2 < INFOBOX_HEIGHT)
return; return;
uint64_t curtime = get_unix_time(); time_t curtime = get_unix_time();
/* update elapsed time string once per second */ /* update elapsed time string once per second */
if (curtime > infobox->lastupdate) if (curtime > infobox->lastupdate)

View File

@ -61,8 +61,8 @@ struct FileTransfer {
size_t index; size_t index;
uint64_t file_size; uint64_t file_size;
uint64_t position; uint64_t position;
uint64_t last_line_progress; /* The last time we updated the progress bar */ time_t last_line_progress; /* The last time we updated the progress bar */
uint64_t last_keep_alive; /* The last time we sent or received data */ time_t last_keep_alive; /* The last time we sent or received data */
uint32_t line_id; uint32_t line_id;
uint8_t file_id[TOX_FILE_ID_LENGTH]; uint8_t file_id[TOX_FILE_ID_LENGTH];
}; };

View File

@ -314,7 +314,7 @@ static void sort_blocklist_index(void)
qsort(Blocked.index, Blocked.num_blocked, sizeof(uint32_t), index_name_cmp_block); qsort(Blocked.index, Blocked.num_blocked, sizeof(uint32_t), index_name_cmp_block);
} }
static void update_friend_last_online(uint32_t num, uint64_t timestamp) static void update_friend_last_online(uint32_t num, time_t timestamp)
{ {
Friends.list[num].last_online.last_on = timestamp; Friends.list[num].last_online.last_on = timestamp;
Friends.list[num].last_online.tm = *localtime((const time_t*)&timestamp); Friends.list[num].last_online.tm = *localtime((const time_t*)&timestamp);
@ -439,7 +439,7 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *m, uint32_t num, bool sort)
fprintf(stderr, "tox_friend_get_public_key failed (error %d)\n", pkerr); fprintf(stderr, "tox_friend_get_public_key failed (error %d)\n", pkerr);
TOX_ERR_FRIEND_GET_LAST_ONLINE loerr; TOX_ERR_FRIEND_GET_LAST_ONLINE loerr;
uint64_t t = tox_friend_get_last_online(m, num, &loerr); time_t t = tox_friend_get_last_online(m, num, &loerr);
if (loerr != TOX_ERR_FRIEND_GET_LAST_ONLINE_OK) if (loerr != TOX_ERR_FRIEND_GET_LAST_ONLINE_OK)
t = 0; t = 0;
@ -910,7 +910,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
return; return;
} }
uint64_t cur_time = time(NULL); time_t cur_time = get_unix_time();
struct tm cur_loc_tm = *localtime((const time_t *) &cur_time); struct tm cur_loc_tm = *localtime((const time_t *) &cur_time);
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
@ -1047,7 +1047,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattroff(self->window, COLOR_PAIR(BLUE)); wattroff(self->window, COLOR_PAIR(BLUE));
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
uint64_t last_seen = Friends.list[f].last_online.last_on; time_t last_seen = Friends.list[f].last_online.last_on;
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
if (last_seen != 0) { if (last_seen != 0) {

View File

@ -387,7 +387,7 @@ struct group_add_thrd {
ToxWindow *self; ToxWindow *self;
int peernum; int peernum;
int groupnum; int groupnum;
uint64_t timestamp; time_t timestamp;
pthread_t tid; pthread_t tid;
pthread_attr_t attr; pthread_attr_t attr;
}; };

View File

@ -64,7 +64,7 @@ typedef struct {
uint8_t type; uint8_t type;
int num_peers; int num_peers;
int side_pos; /* current position of the sidebar - used for scrolling up and down */ int side_pos; /* current position of the sidebar - used for scrolling up and down */
uint64_t start_time; time_t start_time;
uint8_t *peer_names; uint8_t *peer_names;
uint8_t *oldpeer_names; uint8_t *oldpeer_names;
uint16_t *peer_name_lengths; uint16_t *peer_name_lengths;

View File

@ -50,7 +50,7 @@ struct line_info {
char name1[TOXIC_MAX_NAME_LENGTH + 1]; char name1[TOXIC_MAX_NAME_LENGTH + 1];
char name2[TOXIC_MAX_NAME_LENGTH + 1]; char name2[TOXIC_MAX_NAME_LENGTH + 1];
char msg[MAX_LINE_INFO_MSG_SIZE]; char msg[MAX_LINE_INFO_MSG_SIZE];
uint64_t timestamp; time_t timestamp;
uint8_t type; uint8_t type;
uint8_t bold; uint8_t bold;
uint8_t colour; uint8_t colour;

View File

@ -25,7 +25,7 @@
struct chatlog { struct chatlog {
FILE *file; FILE *file;
uint64_t lastwrite; time_t lastwrite;
char path[MAX_STR_SIZE]; char path[MAX_STR_SIZE];
bool log_on; /* specific to current chat window */ bool log_on; /* specific to current chat window */
}; };

View File

@ -29,7 +29,7 @@ struct cqueue_msg {
int line_id; int line_id;
uint8_t type; uint8_t type;
uint32_t receipt; uint32_t receipt;
uint64_t last_send_try; time_t last_send_try;
struct cqueue_msg *next; struct cqueue_msg *next;
struct cqueue_msg *prev; struct cqueue_msg *prev;
}; };

View File

@ -39,8 +39,6 @@
extern ToxWindow *prompt; extern ToxWindow *prompt;
extern struct user_settings *user_settings; extern struct user_settings *user_settings;
static uint64_t current_unix_time;
void hst_to_net(uint8_t *num, uint16_t numbytes) void hst_to_net(uint8_t *num, uint16_t numbytes)
{ {
#ifndef WORDS_BIGENDIAN #ifndef WORDS_BIGENDIAN
@ -56,19 +54,13 @@ void hst_to_net(uint8_t *num, uint16_t numbytes)
return; return;
} }
/* Note: The time functions are not thread safe */ time_t get_unix_time(void)
void update_unix_time(void)
{ {
current_unix_time = (uint64_t) time(NULL); return time(NULL);
}
uint64_t get_unix_time(void)
{
return current_unix_time;
} }
/* Returns 1 if connection has timed out, 0 otherwise */ /* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t timeout) int timed_out(time_t timestamp, time_t timeout)
{ {
return timestamp + timeout <= get_unix_time(); return timestamp + timeout <= get_unix_time();
} }
@ -77,7 +69,7 @@ int timed_out(uint64_t timestamp, uint64_t timeout)
struct tm *get_time(void) struct tm *get_time(void)
{ {
struct tm *timeinfo; struct tm *timeinfo;
uint64_t t = get_unix_time(); time_t t = get_unix_time();
timeinfo = localtime((const time_t*) &t); timeinfo = localtime((const time_t*) &t);
return timeinfo; return timeinfo;
} }
@ -95,7 +87,7 @@ void get_time_str(char *buf, int bufsize)
} }
/* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */ /* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */
void get_elapsed_time_str(char *buf, int bufsize, uint64_t secs) void get_elapsed_time_str(char *buf, int bufsize, time_t secs)
{ {
if (!secs) if (!secs)
return; return;

View File

@ -61,20 +61,17 @@ int hex_string_to_bytes(char *buf, int size, const char *keystr);
int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_t output_size); int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_t output_size);
/* get the current unix time (not thread safe) */ /* get the current unix time (not thread safe) */
uint64_t get_unix_time(void); time_t get_unix_time(void);
/* Puts the current time in buf in the format of [HH:mm:ss] (not thread safe) */ /* Puts the current time in buf in the format of [HH:mm:ss] (not thread safe) */
void get_time_str(char *buf, int bufsize); void get_time_str(char *buf, int bufsize);
/* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */ /* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */
void get_elapsed_time_str(char *buf, int bufsize, uint64_t secs); void get_elapsed_time_str(char *buf, int bufsize, time_t secs);
/* get the current local time (not thread safe) */ /* get the current local time (not thread safe) */
struct tm *get_time(void); struct tm *get_time(void);
/* updates current unix time (should be run once per do_toxic loop) */
void update_unix_time(void);
/* Returns 1 if the string is empty, 0 otherwise */ /* Returns 1 if the string is empty, 0 otherwise */
int string_is_empty(const char *string); int string_is_empty(const char *string);
@ -91,7 +88,7 @@ int wcs_to_mbs_buf(char *buf, const wchar_t *string, size_t n);
int mbs_to_wcs_buf(wchar_t *buf, const char *string, size_t n); int mbs_to_wcs_buf(wchar_t *buf, const char *string, size_t n);
/* Returns 1 if connection has timed out, 0 otherwise */ /* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t timeout); int timed_out(time_t timestamp, time_t timeout);
/* Colours the window tab according to type. Beeps if is_beep is true */ /* Colours the window tab according to type. Beeps if is_beep is true */
void alert_window(ToxWindow *self, int type, bool is_beep); void alert_window(ToxWindow *self, int type, bool is_beep);

View File

@ -733,7 +733,6 @@ static Tox *load_toxic(char *data_path)
static void do_toxic(Tox *m) static void do_toxic(Tox *m)
{ {
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
update_unix_time();
if (arg_opts.no_connect) { if (arg_opts.no_connect) {
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
@ -1091,7 +1090,6 @@ void DnD_callback(const char* asdv, DropType dt)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
update_unix_time();
parse_args(argc, argv); parse_args(argc, argv);
/* Use the -b flag to enable stderr */ /* Use the -b flag to enable stderr */
@ -1212,12 +1210,12 @@ int main(int argc, char **argv)
snprintf(avatarstr, sizeof(avatarstr), "/avatar \"%s\"", user_settings->avatar_path); snprintf(avatarstr, sizeof(avatarstr), "/avatar \"%s\"", user_settings->avatar_path);
execute(prompt->chatwin->history, prompt, m, avatarstr, GLOBAL_COMMAND_MODE); execute(prompt->chatwin->history, prompt, m, avatarstr, GLOBAL_COMMAND_MODE);
uint64_t last_save = (uint64_t) time(NULL); time_t last_save = get_unix_time();
while (true) { while (true) {
do_toxic(m); do_toxic(m);
uint64_t cur_time = get_unix_time(); time_t cur_time = get_unix_time();
if (timed_out(last_save, AUTOSAVE_FREQ)) { if (timed_out(last_save, AUTOSAVE_FREQ)) {
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);

View File

@ -204,8 +204,8 @@ struct infobox {
bool hide; bool hide;
bool active; bool active;
uint64_t lastupdate; time_t lastupdate;
uint64_t starttime; time_t starttime;
char timestr[TIME_STR_SIZE]; char timestr[TIME_STR_SIZE];
WINDOW *win; WINDOW *win;