Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/thread/src/timeconv.inl @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 3.5 KB
Line 
1// Copyright (C) 2001-2003
2// William E. Kempf
3//
4// Permission to use, copy, modify, distribute and sell this software
5// and its documentation for any purpose is hereby granted without fee,
6// provided that the above copyright notice appear in all copies and
7// that both that copyright notice and this permission notice appear
8// in supporting documentation.  William E. Kempf makes no representations
9// about the suitability of this software for any purpose.
10// It is provided "as is" without express or implied warranty.
11
12namespace {
13const int MILLISECONDS_PER_SECOND = 1000;
14const int NANOSECONDS_PER_SECOND = 1000000000;
15const int NANOSECONDS_PER_MILLISECOND = 1000000;
16
17const int MICROSECONDS_PER_SECOND = 1000000;
18const int NANOSECONDS_PER_MICROSECOND = 1000;
19
20inline void to_time(int milliseconds, boost::xtime& xt)
21{
22    int res = 0;
23    res = boost::xtime_get(&xt, boost::TIME_UTC);
24    assert(res == boost::TIME_UTC);
25
26    xt.sec += (milliseconds / MILLISECONDS_PER_SECOND);
27    xt.nsec += ((milliseconds % MILLISECONDS_PER_SECOND) *
28        NANOSECONDS_PER_MILLISECOND);
29
30    if (xt.nsec >= NANOSECONDS_PER_SECOND)
31    {
32        ++xt.sec;
33        xt.nsec -= NANOSECONDS_PER_SECOND;
34    }
35}
36
37#if defined(BOOST_HAS_PTHREADS)
38inline void to_timespec(const boost::xtime& xt, timespec& ts)
39{
40    ts.tv_sec = static_cast<int>(xt.sec);
41    ts.tv_nsec = static_cast<int>(xt.nsec);
42    if(ts.tv_nsec >= NANOSECONDS_PER_SECOND)
43    {
44        ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
45        ts.tv_nsec %= NANOSECONDS_PER_SECOND;
46    }
47}
48
49inline void to_time(int milliseconds, timespec& ts)
50{
51    boost::xtime xt;
52    to_time(milliseconds, xt);
53    to_timespec(xt, ts);
54}
55
56inline void to_timespec_duration(const boost::xtime& xt, timespec& ts)
57{
58    boost::xtime cur;
59    int res = 0;
60    res = boost::xtime_get(&cur, boost::TIME_UTC);
61    assert(res == boost::TIME_UTC);
62
63    if (boost::xtime_cmp(xt, cur) <= 0)
64    {
65        ts.tv_sec = 0;
66        ts.tv_nsec = 0;
67    }
68    else
69    {
70        ts.tv_sec = xt.sec - cur.sec;
71        ts.tv_nsec = xt.nsec - cur.nsec;
72
73        if( ts.tv_nsec < 0 )
74        {
75            ts.tv_sec -= 1;
76            ts.tv_nsec += NANOSECONDS_PER_SECOND;
77        }
78        if(ts.tv_nsec >= NANOSECONDS_PER_SECOND)
79        {
80            ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
81            ts.tv_nsec %= NANOSECONDS_PER_SECOND;
82        }
83    }
84}
85#endif
86
87inline void to_duration(boost::xtime xt, int& milliseconds)
88{
89    boost::xtime cur;
90    int res = 0;
91    res = boost::xtime_get(&cur, boost::TIME_UTC);
92    assert(res == boost::TIME_UTC);
93
94    if (boost::xtime_cmp(xt, cur) <= 0)
95        milliseconds = 0;
96    else
97    {
98        if (cur.nsec > xt.nsec)
99        {
100            xt.nsec += NANOSECONDS_PER_SECOND;
101            --xt.sec;
102        }
103        milliseconds = (int)((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) +
104            (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) /
105                NANOSECONDS_PER_MILLISECOND);
106    }
107}
108
109inline void to_microduration(const boost::xtime& xt, int& microseconds)
110{
111    boost::xtime cur;
112    int res = 0;
113    res = boost::xtime_get(&cur, boost::TIME_UTC);
114    assert(res == boost::TIME_UTC);
115
116    if (boost::xtime_get(&cur, boost::TIME_UTC) <= 0)
117        microseconds = 0;
118    else
119    {
120        microseconds = (int)((xt.sec - cur.sec) * MICROSECONDS_PER_SECOND) +
121            (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MICROSECOND/2)) /
122                NANOSECONDS_PER_MICROSECOND);
123    }
124}
125}
126
127// Change Log:
128//    1 Jun 01  Initial creation.
Note: See TracBrowser for help on using the repository browser.