ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/Common/timing.cpp
Revision: 37
Committed: 2008-03-05T02:29:38-08:00 (17 years, 4 months ago) by douglas
File size: 1168 byte(s)
Log Message:
Grr! This is going to need a refactor.

File Contents

# User Rev Content
1 douglas 3 // Truck Computer Dooom!
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7 douglas 37 #include "posix.hpp"
8     #include "timing.hpp"
9    
10     namespace Timing
11     {
12    
13     Time::Time(const ::timeval &time)
14     {
15     TIMEVAL_TO_TIMESPEC(&time, &this->time);
16     }
17    
18     Time::Time(time_t seconds, long nanoseconds)
19     {
20     time.tv_sec = seconds;
21     time.tv_nsec = nanoseconds;
22     }
23    
24     Time &Time::operator +=(time_t seconds)
25     {
26     time.tv_sec += seconds;
27    
28     return *this;
29     }
30    
31     Time &Time::operator +=(long nanoseconds)
32     {
33     nanoseconds += time.tv_nsec;
34    
35     time.tv_sec += nanoseconds / 1000000000;
36     time.tv_nsec = nanoseconds % 1000000000;
37    
38     return *this;
39     }
40    
41     Time &Time::operator -=(time_t seconds)
42     {
43     time.tv_sec -= seconds;
44    
45     return *this;
46     }
47    
48     Time &Time::operator -=(long nanoseconds)
49     {
50     nanoseconds = time.tv_nsec - nanoseconds;
51    
52     if (nanoseconds < 0)
53     {
54     nanoseconds = (1000000000 - nanoseconds);
55    
56     time.tv_sec -= nanoseconds / 1000000000;
57     time.tv_nsec = 1000000000 - nanoseconds % 1000000000;
58     }
59     else
60     time.tv_nsec = nanoseconds;
61    
62     return *this;
63     }
64    
65     Time GetTimeOfDay()
66     {
67     ::timeval now;
68    
69     ::gettimeofday(&now, NULL);
70    
71     return now;
72     }
73    
74     void NanoSleep(const Time &time)
75     {
76     ::timespec time_(time);
77    
78     Posix::SysCall(::nanosleep, &time_, &time_);
79     }
80    
81     }

Properties

Name Value
svn:keywords Id