Represent time as double instead of struct timespec to simplify the
[pachi/derm.git] / timeinfo.c
blob5a24fd693cb589f30c0ff8d7d21fb8506796352e
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <time.h>
8 #define DEBUG
10 #include "debug.h"
11 #include "timeinfo.h"
14 bool
15 time_parse(struct time_info *ti, char *s)
17 switch (s[0]) {
18 case '_': ti->period = TT_TOTAL; s++; break;
19 default: ti->period = TT_MOVE; break;
21 switch (s[0]) {
22 case '=':
23 ti->dim = TD_GAMES;
24 ti->len.games = atoi(++s);
25 break;
26 default:
27 if (!isdigit(s[0]))
28 return false;
29 ti->dim = TD_WALLTIME;
30 ti->len.t.recommended_time = atof(s);
31 break;
33 return true;
36 /* Returns the current time. */
37 double
38 time_now(void)
40 struct timespec now;
41 clock_gettime(CLOCK_REALTIME, &now);
42 return now.tv_sec + now.tv_nsec/1000000000.0;
45 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
46 void
47 time_sleep(double interval)
49 struct timespec ts;
50 double sec;
51 ts.tv_nsec = (int)(modf(interval, &sec)*1000000000.0);
52 ts.tv_sec = (int)sec;
53 nanosleep(&ts, NULL); /* ignore error if interval was < 0 */