get time at the beginning of the loop

This commit is contained in:
Michael Stapelberg 2011-04-21 20:49:22 +02:00
parent 686b8798aa
commit 4fa8a4e0ab
3 changed files with 11 additions and 11 deletions

View File

@ -322,6 +322,10 @@ int main(int argc, char *argv[]) {
int interval = cfg_getint(cfg_general, "interval"); int interval = cfg_getint(cfg_general, "interval");
while (1) { while (1) {
time_t current_time = time(NULL);
struct tm *current_tm = NULL;
if (current_time != (time_t) -1)
current_tm = localtime(&current_time);
for (j = 0; j < cfg_size(cfg, "order"); j++) { for (j = 0; j < cfg_size(cfg, "order"); j++) {
if (j > 0) if (j > 0)
print_seperator(); print_seperator();
@ -350,7 +354,7 @@ int main(int argc, char *argv[]) {
print_load(cfg_getstr(sec, "format")); print_load(cfg_getstr(sec, "format"));
CASE_SEC("time") CASE_SEC("time")
print_time(cfg_getstr(sec, "format")); print_time(cfg_getstr(sec, "format"), current_tm);
CASE_SEC("ddate") CASE_SEC("ddate")
print_ddate(cfg_getstr(sec, "format")); print_ddate(cfg_getstr(sec, "format"));
@ -371,9 +375,9 @@ int main(int argc, char *argv[]) {
* we dont use sleep(interval) but we sleep until the next * we dont use sleep(interval) but we sleep until the next
* second (with microsecond precision) plus (interval-1) * second (with microsecond precision) plus (interval-1)
* seconds. */ * seconds. */
struct timeval current_time; struct timeval current_timeval;
gettimeofday(&current_time, NULL); gettimeofday(&current_timeval, NULL);
struct timespec ts = {interval - 1, (10e5 - current_time.tv_usec) * 1000}; struct timespec ts = {interval - 1, (10e5 - current_timeval.tv_usec) * 1000};
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
} }
} }

View File

@ -5,6 +5,7 @@ enum { O_DZEN2, O_XMOBAR, O_NONE } output_format;
#include <stdbool.h> #include <stdbool.h>
#include <confuse.h> #include <confuse.h>
#include <time.h>
#define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0) #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
#define max(a, b) ((a) > (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b))
@ -61,7 +62,7 @@ char *endcolor() __attribute__ ((pure));
void print_ipv6_info(const char *format_up, const char *format_down); void print_ipv6_info(const char *format_up, const char *format_down);
void print_disk_info(const char *path, const char *format); void print_disk_info(const char *path, const char *format);
void print_battery_info(int number, const char *format, bool last_full_capacity); void print_battery_info(int number, const char *format, bool last_full_capacity);
void print_time(const char *format); void print_time(const char *format, struct tm *current_tm);
void print_ddate(const char *format); void print_ddate(const char *format);
const char *get_ip_addr(); const char *get_ip_addr();
void print_wireless_info(const char *interface, const char *format_up, const char *format_down); void print_wireless_info(const char *interface, const char *format_up, const char *format_down);

View File

@ -3,14 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void print_time(const char *format) { void print_time(const char *format, struct tm *current_tm) {
static char part[512]; static char part[512];
/* Get date & time */ /* Get date & time */
time_t current_time = time(NULL);
if (current_time == (time_t) -1) {
return;
}
struct tm *current_tm = localtime(&current_time);
if (current_tm == NULL) { if (current_tm == NULL) {
return; return;
} }