libdmtx 0.7.8.7
libdmtx is a software library that enables programs to read and write Data Matrix barcodes of the modern ECC200 variety.
Loading...
Searching...
No Matches
dmtxtime.c
Go to the documentation of this file.
1
17#include <errno.h>
18
19#include "dmtx.h"
20
21#define DMTX_USEC_PER_SEC 1000000
22
23#if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY)
24
25# include <sys/time.h>
26# include <time.h>
27# define DMTX_TIME_PREC_USEC 1
28
33extern DmtxTime dmtxTimeNow(void)
34{
35 DmtxPassFail err;
36 struct timeval tv;
37 DmtxTime tNow;
38
39 err = gettimeofday(&tv, NULL);
40 if (err != 0)
41 ; /* XXX handle error better here */
42
43 tNow.sec = tv.tv_sec;
44 tNow.usec = tv.tv_usec;
45
46 return tNow;
47}
48
49#elif defined(_MSC_VER)
50
51# include <Windows.h>
52# define DMTX_TIME_PREC_USEC 1
53
58extern DmtxTime dmtxTimeNow(void)
59{
60 FILETIME ft;
61 unsigned __int64 tm;
62 DmtxTime tNow;
63
64 GetSystemTimeAsFileTime(&ft);
65
66 tm = ft.dwHighDateTime;
67 tm <<= 32;
68 tm |= ft.dwLowDateTime;
69 tm /= 10;
70
71 tNow.sec = tm / 1000000UL;
72 tNow.usec = tm % 1000000UL;
73
74 return tNow;
75}
76
77#else
78
79# include <time.h>
80# define DMTX_TIME_PREC_USEC 1000000
81
87{
88 time_t s;
89 DmtxTime tNow;
90
91 s = time(NULL);
92 if (errno != 0) {
93 ; /* XXX handle error better here */
94 }
95
96 tNow.sec = s;
97 tNow.usec = 0;
98
99 return tNow;
100}
101
102#endif
103
110extern DmtxTime dmtxTimeAdd(DmtxTime t, long msec)
111{
112 long usec;
113
114 usec = msec * 1000;
115
116 /* Ensure that time difference will register on local system */
117 if (usec > 0 && usec < DMTX_TIME_PREC_USEC) {
118 usec = DMTX_TIME_PREC_USEC;
119 }
120
121 /* Add time */
122 t.sec += usec / DMTX_USEC_PER_SEC;
123 t.usec += usec % DMTX_USEC_PER_SEC;
124
125 /* Roll extra usecs into secs */
126 while (t.usec >= DMTX_USEC_PER_SEC) {
127 t.sec++;
128 t.usec -= DMTX_USEC_PER_SEC;
129 }
130
131 return t;
132}
133
139extern int dmtxTimeExceeded(DmtxTime timeout)
140{
141 DmtxTime now;
142
143 now = dmtxTimeNow();
144
145 return (now.sec > timeout.sec || (now.sec == timeout.sec && now.usec > timeout.usec));
146}
147
148#undef DMTX_TIME_PREC_USEC
149#undef DMTX_USEC_PER_SEC
libdmtx - Data Matrix Encoding/Decoding Library Copyright 2008, 2009 Mike Laughton.
#define DmtxPassFail
Definition dmtx.h:42
DmtxTime dmtxTimeNow(void)
Generic 1 second resolution version.
Definition dmtxtime.c:86
#define DMTX_TIME_PREC_USEC
Definition dmtxtime.c:80
int dmtxTimeExceeded(DmtxTime timeout)
Determine whether the received timeout has been exceeded.
Definition dmtxtime.c:139
DmtxTime dmtxTimeAdd(DmtxTime t, long msec)
Add milliseconds to time t.
Definition dmtxtime.c:110
#define DMTX_USEC_PER_SEC
Definition dmtxtime.c:21
DmtxTime.