Imported Upstream version 3.2.2
[debian/gnuradio] / omnithread / omni_time.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <gnuradio/omni_time.h>
26 #include <gnuradio/omnithread.h>
27 #include <math.h>
28 #include <assert.h>
29
30
31 omni_time::omni_time(double real_secs)
32 {
33   double floor_secs = floor(real_secs);
34   d_secs = (long) floor_secs;
35   d_nsecs = (long) ((real_secs - floor_secs) * 1e9);      // always positive
36 }
37
38 omni_time
39 omni_time::time(const omni_time &delta_t)
40 {
41   unsigned long abs_sec, abs_nsec;
42   unsigned long rel_sec  = delta_t.d_secs;
43   unsigned long rel_nsec = delta_t.d_nsecs;
44   
45   omni_thread::get_time(&abs_sec, &abs_nsec, rel_sec, rel_nsec);
46   return omni_time(abs_sec, abs_nsec);
47 }
48
49
50 omni_time
51 operator+(const omni_time &x, const omni_time &y)
52 {
53   omni_time r(x.d_secs + y.d_secs, x.d_nsecs + y.d_nsecs);
54   while (r.d_nsecs >= 1000000000){
55     r.d_nsecs -= 1000000000;
56     r.d_secs++;
57   }
58   return r;
59 }
60
61 omni_time
62 operator-(const omni_time &x, const omni_time &y)
63 {
64   // assert(!(x < y));
65
66   omni_time r(x.d_secs - y.d_secs, x.d_nsecs - y.d_nsecs);
67   while (r.d_nsecs < 0){
68     r.d_nsecs += 1000000000;
69     r.d_secs--;
70   }
71   return r;
72 }
73
74 omni_time
75 operator+(const omni_time &x, double y)
76 {
77   return x + omni_time(y);
78 }
79
80 omni_time
81 operator-(const omni_time &x, double y)
82 {
83   return x - omni_time(y);
84 }