add new sigc++2 directory
[ardour.git] / libs / glibmm2 / glib / glibmm / timeval.cc
1 // -*- c++ -*-
2 /* $Id: timeval.cc 2 2003-01-07 16:59:16Z murrayc $ */
3
4 /* timeval.cc
5  *
6  * Copyright (C) 2002 The gtkmm Development Team
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <glib/gmain.h>
24 #include <glib.h>
25 #include <glib/gtimer.h>
26
27 #include <glibmm/timeval.h>
28
29
30 namespace Glib
31 {
32
33 void TimeVal::assign_current_time()
34 {
35   g_get_current_time(this);
36 }
37
38 void TimeVal::add(const TimeVal& rhs)
39 {
40   g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
41   g_return_if_fail(rhs.tv_usec >= 0 && rhs.tv_usec < G_USEC_PER_SEC);
42
43   tv_usec += rhs.tv_usec;
44
45   if(tv_usec >= G_USEC_PER_SEC)
46   {
47     tv_usec -= G_USEC_PER_SEC;
48     ++tv_sec;
49   }
50
51   tv_sec += rhs.tv_sec;
52 }
53
54 void TimeVal::subtract(const TimeVal& rhs)
55 {
56   g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
57   g_return_if_fail(rhs.tv_usec >= 0 && rhs.tv_usec < G_USEC_PER_SEC);
58
59   tv_usec -= rhs.tv_usec;
60
61   if(tv_usec < 0)
62   {
63     tv_usec += G_USEC_PER_SEC;
64     --tv_sec;
65   }
66
67   tv_sec -= rhs.tv_sec;
68 }
69
70 void TimeVal::add_seconds(long seconds)
71 {
72   g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
73
74   tv_sec += seconds;
75 }
76
77 void TimeVal::subtract_seconds(long seconds)
78 {
79   g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
80
81   tv_sec -= seconds;
82 }
83
84 void TimeVal::add_milliseconds(long milliseconds)
85 {
86   g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
87
88   tv_usec += (milliseconds % 1000) * 1000;
89
90   if(tv_usec < 0)
91   {
92     tv_usec += G_USEC_PER_SEC;
93     --tv_sec;
94   }
95   else if(tv_usec >= G_USEC_PER_SEC)
96   {
97     tv_usec -= G_USEC_PER_SEC;
98     ++tv_sec;
99   }
100
101   tv_sec += milliseconds / 1000;
102 }
103
104 void TimeVal::subtract_milliseconds(long milliseconds)
105 {
106   add_milliseconds(-1 * milliseconds);
107 }
108
109 void TimeVal::add_microseconds(long microseconds)
110
111   g_time_val_add(this, microseconds);
112 }
113
114 void TimeVal::subtract_microseconds(long microseconds)
115 {
116   g_time_val_add(this, -1 * microseconds);
117 }
118
119 } // namespace Glib
120