annotate future.c @ 229:28a56e4c06a4

Improvements to log file handling
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 27 Oct 2014 17:48:22 +0100
parents b955e35c07ae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 /* welcome in the 2012 */
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2
81
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
3 /* time functions */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
4 #include <windows.h>
208
b955e35c07ae Fix includes
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
5 #include <ansi_c.h>
81
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
6
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
7 #include "future.h"
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
8
16
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 /* an implementation of the C99 `round` function based on `floor` and `ceil` */
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 double round(double x)
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 {
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12 if (x >= 0.0) {
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13 double y = floor(x);
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 if (x - y >= 0.5)
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 y += 1.0;
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16 return y;
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 } else {
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 double y = ceil(x);
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19 if (x - y >= 0.5)
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
20 y -= 1.0;
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 return y;
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
22 }
9d57d1fcbcd5 Implementation of the C99 round() function
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23 }
81
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
24
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
25
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
26 /* implement high resolution time service */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
27 int gettimeofday(struct timeval *tp, struct timezone *tzp)
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
28 {
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
29 /* difference between 1970-01-01 and 1601-01-01
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
30 in 100-nanoseconds intervals */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
31 const unsigned __int64 shift = 116444736000000000ULL;
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
32 union {
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
33 FILETIME ft;
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
34 unsigned __int64 integer; /* 100-nanoseconds since 1601-01-01 */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
35 } now;
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
36
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
37 /* retrive system time in UTC. the resolution is limited to the resolution
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
38 of the system timer, which is typically in the range of 10 milliseconds
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
39 to 16 milliseconds */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
40 GetSystemTimeAsFileTime(&now.ft);
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
41
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
42 /* shift to epoch and truncate to microseconds */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
43 now.integer = (now.integer - shift) / 10LL;
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
44
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
45 tp->tv_usec = (long)(now.integer % 1000000LL);
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
46 tp->tv_sec = (long)(now.integer / 1000000LL);
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
47
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
48 /* FIXME CVI redefines the epoch as seconds since 1900-01-01 ! */
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
49 tp->tv_sec += 2208988800;
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
50
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
51 return 0;
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
52 }
be814b934eca Implement gettimeofday()
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 16
diff changeset
53