1 /*
2  * Theme include for time.
3  *
4  * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /** * @file
20  *
21  * Time-related functionality.
22  */
23 
24 #ifndef _USUAL_TIME_H_
25 #define _USUAL_TIME_H_
26 
27 #include <usual/base.h>
28 
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32 
33 #ifdef _WIN32
34 #include <winsock2.h>
35 #endif
36 
37 #include <time.h>
38 
39 /** Type to hold microseconds. */
40 typedef uint64_t usec_t;
41 
42 /** How many microseconds in a second. */
43 #define USEC ((usec_t)1000000)
44 
45 /** Convert usec timestamp to ISO timestamp with millisecond precision: YYYY-mm-dd hh:mm:ss.SSS TZ */
46 char *format_time_ms(usec_t time, char *dest, unsigned destlen);
47 /** Convert usec timestamp to ISO timestamp with second precision: YYYY-mm-dd hh:mm:ss TZ */
48 char *format_time_s(usec_t time, char *dest, unsigned destlen);
49 
50 /** Query system time */
51 usec_t get_time_usec(void);
52 
53 /** Query cached system time */
54 usec_t get_cached_time(void);
55 /** Forget cached system time, next call will fill it. */
56 void reset_time_cache(void);
57 
58 #ifdef WIN32
59 
60 
61 #ifndef HAVE_GETTIMEOFDAY
62 #define gettimeofday(t,z) usual_gettimeofday(t,z)
63 
64 /** Compat: gettimeofday() */
65 int gettimeofday(struct timeval * tp, void * tzp);
66 
67 #endif
68 
69 
70 #ifndef HAVE_LOCALTIME_R
71 #define localtime_r(t,r) usual_localtime_r(t,r)
72 
73 /** Compat: localtime_r() */
74 struct tm *localtime_r(const time_t *tp, struct tm *result);
75 
76 #endif
77 
78 
79 #ifndef HAVE_USLEEP
80 #define usleep(x) usual_usleep(x)
81 
82 /** Compat: usleep() */
usleep(long usec)83 static inline void usleep(long usec) { Sleep(usec / 1000); }
84 
85 #endif
86 
87 #ifndef HAVE_GETRUSAGE
88 #define getrusage(w,r) usual_getrusage(w,r)
89 
90 #define RUSAGE_SELF 0
91 
92 /** Compat: rusage for win32 */
93 struct rusage {
94 	struct timeval ru_utime;
95 	struct timeval ru_stime;
96 };
97 
98 /** Compat: getrusage() for win32 */
99 int getrusage(int who, struct rusage *r_usage);
100 
101 #endif
102 
103 #endif
104 
105 #ifndef HAVE_TIMEGM
106 #define timegm(tm) usual_timegm(tm)
107 
108 /** Compat: timegm() */
109 time_t timegm(struct tm *tm);
110 
111 #endif
112 
113 #endif
114