forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtxtime.c
148 lines (117 loc) · 2.59 KB
/
dmtxtime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2008, 2009 Mike Laughton. All rights reserved.
* Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact:
* Vadim A. Misbakh-Soloviov <[email protected]>
* Mike Laughton <[email protected]>
*
* \file dmtxtime.c
* \brief Time handling
*/
#define DMTX_USEC_PER_SEC 1000000
#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
#include <sys/time.h>
#include <time.h>
#define DMTX_TIME_PREC_USEC 1
/**
* \brief GETTIMEOFDAY version
* \return Time now
*/
extern DmtxTime
dmtxTimeNow(void)
{
DmtxPassFail err;
struct timeval tv;
DmtxTime tNow;
err = gettimeofday(&tv, NULL);
if(err != 0)
; /* XXX handle error better here */
tNow.sec = tv.tv_sec;
tNow.usec = tv.tv_usec;
return tNow;
}
#elif defined(_MSC_VER)
#include <Windows.h>
#define DMTX_TIME_PREC_USEC 1
/**
* \brief MICROSOFT VC++ version
* \return Time now
*/
extern DmtxTime
dmtxTimeNow(void)
{
FILETIME ft;
unsigned __int64 tm;
DmtxTime tNow;
GetSystemTimeAsFileTime(&ft);
tm = ft.dwHighDateTime;
tm <<= 32;
tm |= ft.dwLowDateTime;
tm /= 10;
tNow.sec = tm / 1000000UL;
tNow.usec = tm % 1000000UL;
return tNow;
}
#else
#include <time.h>
#define DMTX_TIME_PREC_USEC 1000000
/**
* \brief Generic 1 second resolution version
* \return Time now
*/
extern DmtxTime
dmtxTimeNow(void)
{
time_t s;
DmtxTime tNow;
s = time(NULL);
if(errno != 0)
; /* XXX handle error better here */
tNow.sec = s;
tNow.usec = 0;
return tNow;
}
#endif
/**
* \brief Add milliseconds to time t
* \param t
* \param msec
* \return Adjusted time
*/
extern DmtxTime
dmtxTimeAdd(DmtxTime t, long msec)
{
int usec;
usec = msec * 1000;
/* Ensure that time difference will register on local system */
if(usec > 0 && usec < DMTX_TIME_PREC_USEC)
usec = DMTX_TIME_PREC_USEC;
/* Add time */
t.sec += usec/DMTX_USEC_PER_SEC;
t.usec += usec%DMTX_USEC_PER_SEC;
/* Roll extra usecs into secs */
while(t.usec >= DMTX_USEC_PER_SEC) {
t.sec++;
t.usec -= DMTX_USEC_PER_SEC;
}
return t;
}
/**
* \brief Determine whether the received timeout has been exceeded
* \param timeout
* \return 1 (true) | 0 (false)
*/
extern int
dmtxTimeExceeded(DmtxTime timeout)
{
DmtxTime now;
now = dmtxTimeNow();
return (now.sec > timeout.sec || (now.sec == timeout.sec && now.usec > timeout.usec));
}
#undef DMTX_TIME_PREC_USEC
#undef DMTX_USEC_PER_SEC