7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen#ifndef TIME_UTIL_H
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen#define TIME_UTIL_H
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen
0531ee2f23b4562eb6c212593680c820fa9e828bTimo Sirainen#include <sys/time.h> /* for struct timeval */
0531ee2f23b4562eb6c212593680c820fa9e828bTimo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen/* Returns -1 if tv1<tv2, 1 if tv1>tv2, 0 if they're equal. */
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainenint timeval_cmp(const struct timeval *tv1, const struct timeval *tv2);
e28b88ee83b47dc2257140600f491482704c7b79Stephan Bosch/* Same as timeval_cmp, but tv->usecs must differ by at least usec_margin */
e28b88ee83b47dc2257140600f491482704c7b79Stephan Boschint timeval_cmp_margin(const struct timeval *tv1, const struct timeval *tv2,
e28b88ee83b47dc2257140600f491482704c7b79Stephan Bosch unsigned int usec_margin);
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen/* Returns tv1-tv2 in milliseconds. */
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainenint timeval_diff_msecs(const struct timeval *tv1, const struct timeval *tv2);
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen/* Returns tv1-tv2 in microseconds. */
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainenlong long timeval_diff_usecs(const struct timeval *tv1,
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen const struct timeval *tv2);
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Boschstatic inline void
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Boschtimeval_add_msecs(struct timeval *tv, unsigned int msecs)
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch{
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_sec += msecs / 1000;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_usec += (msecs % 1000) * 1000;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch if (tv->tv_usec >= 1000000) {
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_sec++;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_usec -= 1000000;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch }
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch}
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Boschstatic inline void
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Boschtimeval_sub_msecs(struct timeval *tv, unsigned int msecs)
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch{
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_sec -= msecs / 1000;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_usec -= (msecs % 1000) * 1000;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch if (tv->tv_usec < 0) {
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_sec--;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch tv->tv_usec += 1000000;
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch }
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch}
2753f93e5d9cd5fbd773540de3772d182fcced5eStephan Bosch
27a33f6aaa3d02d88c020315ae613e0c4910e950Timo Sirainen/* Convert t to local time and return timestamp on that day at 00:00:00. */
27a33f6aaa3d02d88c020315ae613e0c4910e950Timo Sirainentime_t time_to_local_day_start(time_t t);
27a33f6aaa3d02d88c020315ae613e0c4910e950Timo Sirainen
9983cbb4cef92d68ffde87dcc91913cc2fed3da5Martti Rannanjärvi/* Wrappers to strftime() */
9983cbb4cef92d68ffde87dcc91913cc2fed3da5Martti Rannanjärviconst char *t_strftime(const char *fmt, const struct tm *tm) ATTR_STRFTIME(1);
c3c07d6527ad28c8546cbbf84c257d178e23c184Timo Sirainenconst char *t_strflocaltime(const char *fmt, time_t t) ATTR_STRFTIME(1);
9983cbb4cef92d68ffde87dcc91913cc2fed3da5Martti Rannanjärviconst char *t_strfgmtime(const char *fmt, time_t t) ATTR_STRFTIME(1);
c3c07d6527ad28c8546cbbf84c257d178e23c184Timo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen#endif