#include "ostream.h"
#include "str.h"
#include "seq-range-array.h"
+#include "time-util.h"
#include "imap-resp-code.h"
#include "imap-quote.h"
#include "imap-seqset.h"
unsigned int count;
uint32_t id, id_min, id_max;
const char *ok_reply;
+ int time_msecs;
bool tryagain, minmax, lost_data;
if (cmd->cancel) {
if (gettimeofday(&end_time, NULL) < 0)
memset(&end_time, 0, sizeof(end_time));
- end_time.tv_sec -= ctx->start_time.tv_sec;
- end_time.tv_usec -= ctx->start_time.tv_usec;
- if (end_time.tv_usec < 0) {
- end_time.tv_sec--;
- end_time.tv_usec += 1000000;
- }
+
+ time_msecs = timeval_diff_msecs(&end_time, &ctx->start_time);
sync_flags = MAILBOX_SYNC_FLAG_FAST;
if (!cmd->uid || ctx->have_seqsets)
ok_reply = t_strdup_printf("OK %s%s completed (%d.%03d secs).",
lost_data ? "["IMAP_RESP_CODE_EXPUNGEISSUED"] " : "",
!ctx->sorting ? "Search" : "Sort",
- (int)end_time.tv_sec, (int)(end_time.tv_usec/1000));
+ time_msecs/1000, time_msecs%1000);
return cmd_sync(cmd, sync_flags, 0, ok_reply);
}
#include "istream.h"
#include "utc-offset.h"
#include "str.h"
+#include "time-util.h"
#include "message-address.h"
#include "message-date.h"
#include "message-search.h"
!ctx->mail_ctx.progress_hidden) {
percentage = ctx->mail_ctx.progress_cur * 100.0 /
ctx->mail_ctx.progress_max;
- msecs = (ioloop_timeval.tv_sec -
- ctx->search_start_time.tv_sec) * 1000 +
- (ioloop_timeval.tv_usec -
- ctx->search_start_time.tv_usec) / 1000;
+ msecs = timeval_diff_msecs(&ioloop_timeval,
+ &ctx->search_start_time);
secs = (msecs / (percentage / 100.0) - msecs) / 1000;
T_BEGIN {
{
struct timeval now;
unsigned long long guess_cost;
- long usecs;
+ long long usecs;
bool ret;
if (ctx->cost < ctx->next_time_check_cost)
if (gettimeofday(&now, NULL) < 0)
i_fatal("gettimeofday() failed: %m");
- usecs = (now.tv_sec - ctx->last_nonblock_timeval.tv_sec) * 1000000 +
- (now.tv_usec - ctx->last_nonblock_timeval.tv_usec);
- if (usecs < 0 || now.tv_sec < 0) {
+
+ usecs = timeval_diff_usecs(&now, &ctx->last_nonblock_timeval);
+ if (usecs < 0) {
/* clock moved backwards. */
ctx->last_nonblock_timeval = now;
ctx->next_time_check_cost = SEARCH_INITIAL_MAX_COST;
#include "ioloop.h"
#include "array.h"
#include "str.h"
+#include "time-util.h"
#include "hostpid.h"
#include "maildir-storage.h"
#include "maildir-keywords.h"
struct timeval tv;
/* use secs + usecs to guarantee uniqueness within this process. */
- if (ioloop_timeval.tv_sec > last_tv.tv_sec ||
- (ioloop_timeval.tv_sec == last_tv.tv_sec &&
- ioloop_timeval.tv_usec > last_tv.tv_usec))
+ if (timeval_cmp(&ioloop_timeval, &last_tv) > 0)
tv = ioloop_timeval;
else {
tv = last_tv;
str-sanitize.c \
strescape.c \
strfuncs.c \
+ time-util.c \
unix-socket-create.c \
unlink-directory.c \
unlink-old-files.c \
str-sanitize.h \
strescape.h \
strfuncs.h \
+ time-util.h \
unix-socket-create.h \
unlink-directory.h \
unlink-old-files.h \
/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
#include "lib.h"
+#include "time-util.h"
#include "ioloop-internal.h"
#include <unistd.h>
static int timeout_cmp(const void *p1, const void *p2)
{
const struct timeout *to1 = p1, *to2 = p2;
- int diff;
- diff = to1->next_run.tv_sec - to2->next_run.tv_sec;
- if (diff == 0)
- diff = to1->next_run.tv_usec - to2->next_run.tv_usec;
- return diff;
+ return timeval_cmp(&to1->next_run, &to2->next_run);
}
static void io_loop_default_time_moved(time_t old_time, time_t new_time)
--- /dev/null
+/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "time-util.h"
+
+#include <sys/time.h>
+
+int timeval_cmp(const struct timeval *tv1, const struct timeval *tv2)
+{
+ if (tv1->tv_sec < tv2->tv_sec)
+ return -1;
+ if (tv1->tv_sec > tv2->tv_sec)
+ return 1;
+ if (tv1->tv_usec < tv2->tv_usec)
+ return -1;
+ if (tv1->tv_usec > tv2->tv_usec)
+ return 1;
+ return 0;
+}
+
+int timeval_diff_msecs(const struct timeval *tv1, const struct timeval *tv2)
+{
+ time_t secs;
+ int usecs;
+
+ secs = tv1->tv_sec - tv2->tv_sec;
+ usecs = tv1->tv_usec - tv2->tv_usec;
+ if (usecs < 0) {
+ secs++;
+ usecs += 1000000;
+ }
+ return (secs * 1000) + (usecs/1000);
+}
+
+long long timeval_diff_usecs(const struct timeval *tv1,
+ const struct timeval *tv2)
+{
+ time_t secs;
+ int usecs;
+
+ secs = tv1->tv_sec - tv2->tv_sec;
+ usecs = tv1->tv_usec - tv2->tv_usec;
+ if (usecs < 0) {
+ secs++;
+ usecs += 1000000;
+ }
+ return ((long long)secs * 1000000ULL) + usecs;
+}
--- /dev/null
+#ifndef TIME_UTIL_H
+#define TIME_UTIL_H
+
+/* Returns -1 if tv1<tv2, 1 if tv1>tv2, 0 if they're equal. */
+int timeval_cmp(const struct timeval *tv1, const struct timeval *tv2);
+/* Returns tv1-tv2 in milliseconds. */
+int timeval_diff_msecs(const struct timeval *tv1, const struct timeval *tv2);
+/* Returns tv1-tv2 in microseconds. */
+long long timeval_diff_usecs(const struct timeval *tv1,
+ const struct timeval *tv2);
+
+#endif
#include "array.h"
#include "file-lock.h"
#include "istream.h"
+#include "time-util.h"
#include "unichar.h"
#include "squat-trie.h"
#include "squat-uidlist.h"
cputime = (double)(clock_end - clock_start) / CLOCKS_PER_SEC;
fprintf(stderr, "\n - Index time: %.2f CPU seconds, "
"%.2f real seconds (%.02fMB/CPUs)\n", cputime,
- (tv_end.tv_sec - tv_start.tv_sec) +
- (tv_end.tv_usec - tv_start.tv_usec)/1000000.0,
+ timeval_diff_msecs(&tv_end, &tv_start)/1000.0,
input->v_offset / cputime / (1024*1024));
if (stat(trie_path, &trie_st) < 0)