root / mblock / src / lib / mb_time.cc @ 42d9c6f4
History | View | Annotate | Download (2 kB)
| 1 | /* -*- c++ -*- */
|
|---|---|
| 2 | /*
|
| 3 | * Copyright 2007 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 <mb_time.h> |
| 26 | #include <omnithread.h> |
| 27 | #include <math.h> |
| 28 | #include <assert.h> |
| 29 | |
| 30 | |
| 31 | mb_time::mb_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 | mb_time |
| 39 | mb_time::time(const mb_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 mb_time(abs_sec, abs_nsec);
|
| 47 | } |
| 48 | |
| 49 | |
| 50 | mb_time |
| 51 | operator+(const mb_time &x, const mb_time &y) |
| 52 | {
|
| 53 | mb_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 | mb_time |
| 62 | operator-(const mb_time &x, const mb_time &y) |
| 63 | {
|
| 64 | // assert(!(x < y));
|
| 65 | |
| 66 | mb_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 | mb_time |
| 75 | operator+(const mb_time &x, double y) |
| 76 | {
|
| 77 | return x + mb_time(y);
|
| 78 | } |
| 79 | |
| 80 | mb_time |
| 81 | operator-(const mb_time &x, double y) |
| 82 | {
|
| 83 | return x - mb_time(y);
|
| 84 | } |