summaryrefslogtreecommitdiffstats
blob: d81f31ba2ed37c517bfaf7255bb6d2dbe6946aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2001-2003
// William E. Kempf
// Copyright (C) 2009 Anthony Williams
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// boostinspect:nounnamed

#include <boost/assert.hpp>

namespace {
const int MILLISECONDS_PER_SECOND = 1000;
const int NANOSECONDS_PER_SECOND = 1000000000;
const int NANOSECONDS_PER_MILLISECOND = 1000000;

const int MICROSECONDS_PER_SECOND = 1000000;
const int NANOSECONDS_PER_MICROSECOND = 1000;

#if defined BOOST_THREAD_USES_DATETIME
inline void to_time(int milliseconds, boost::xtime& xt)
{
    int res = 0;
    res = boost::xtime_get(&xt, boost::TIME_UTC_);
    BOOST_ASSERT(res == boost::TIME_UTC_); (void)res;

    xt.sec += (milliseconds / MILLISECONDS_PER_SECOND);
    xt.nsec += ((milliseconds % MILLISECONDS_PER_SECOND) *
        NANOSECONDS_PER_MILLISECOND);

    if (xt.nsec >= NANOSECONDS_PER_SECOND)
    {
        ++xt.sec;
        xt.nsec -= NANOSECONDS_PER_SECOND;
    }
}
#endif
#if defined(BOOST_HAS_PTHREADS)
#if defined BOOST_THREAD_USES_DATETIME
inline void to_timespec(const boost::xtime& xt, timespec& ts)
{
    ts.tv_sec = static_cast<int>(xt.sec);
    ts.tv_nsec = static_cast<int>(xt.nsec);
    if(ts.tv_nsec >= NANOSECONDS_PER_SECOND)
    {
        ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
        ts.tv_nsec %= NANOSECONDS_PER_SECOND;
    }
}
#endif
inline void to_time(int milliseconds, timespec& ts)
{
#if defined BOOST_THREAD_USES_DATETIME
    boost::xtime xt;
    to_time(milliseconds, xt);
    to_timespec(xt, ts);
#else
    ts.tv_sec += (milliseconds / MILLISECONDS_PER_SECOND);
    ts.tv_nsec += ((milliseconds % MILLISECONDS_PER_SECOND) *
        NANOSECONDS_PER_MILLISECOND);

    if (ts.tv_nsec >= NANOSECONDS_PER_SECOND)
    {
        ++ts.tv_sec;
        ts.tv_nsec -= NANOSECONDS_PER_SECOND;
    }
#endif
}

#if defined BOOST_THREAD_USES_DATETIME
inline void to_timespec_duration(const boost::xtime& xt, timespec& ts)
{
    boost::xtime cur;
    int res = 0;
    res = boost::xtime_get(&cur, boost::TIME_UTC_);
    BOOST_ASSERT(res == boost::TIME_UTC_); (void)res;

    if (boost::xtime_cmp(xt, cur) <= 0)
    {
        ts.tv_sec = 0;
        ts.tv_nsec = 0;
    }
    else
    {
        ts.tv_sec = xt.sec - cur.sec;
        ts.tv_nsec = xt.nsec - cur.nsec;

        if( ts.tv_nsec < 0 )
        {
            ts.tv_sec -= 1;
            ts.tv_nsec += NANOSECONDS_PER_SECOND;
        }
        if(ts.tv_nsec >= NANOSECONDS_PER_SECOND)
        {
            ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
            ts.tv_nsec %= NANOSECONDS_PER_SECOND;
        }
    }
}
#endif
#endif

#if defined BOOST_THREAD_USES_DATETIME
inline void to_duration(boost::xtime xt, int& milliseconds)
{
    boost::xtime cur;
    int res = 0;
    res = boost::xtime_get(&cur, boost::TIME_UTC_);
    BOOST_ASSERT(res == boost::TIME_UTC_); (void)res;

    if (boost::xtime_cmp(xt, cur) <= 0)
        milliseconds = 0;
    else
    {
        if (cur.nsec > xt.nsec)
        {
            xt.nsec += NANOSECONDS_PER_SECOND;
            --xt.sec;
        }
        milliseconds = (int)((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) +
            (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) /
                NANOSECONDS_PER_MILLISECOND);
    }
}

inline void to_microduration(boost::xtime xt, int& microseconds)
{
    boost::xtime cur;
    int res = 0;
    res = boost::xtime_get(&cur, boost::TIME_UTC_);
    BOOST_ASSERT(res == boost::TIME_UTC_); (void)res;

    if (boost::xtime_cmp(xt, cur) <= 0)
        microseconds = 0;
    else
    {
        if (cur.nsec > xt.nsec)
        {
            xt.nsec += NANOSECONDS_PER_SECOND;
            --xt.sec;
        }
        microseconds = (int)((xt.sec - cur.sec) * MICROSECONDS_PER_SECOND) +
            (((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MICROSECOND/2)) /
                NANOSECONDS_PER_MICROSECOND);
    }
}
#endif
}

// Change Log:
//    1 Jun 01  Initial creation.