summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/sys/windows/Time.cpp
blob: 4169ef1f0ac98956d2c3e442d5808e8f7d5e66c3 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "qpid/sys/Time.h"
#include <cmath>
#include <ostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread_time.hpp>
#include <windows.h>
#include <time.h>

using namespace boost::posix_time;

namespace {

// High-res timing support. This will display times since program start,
// more or less. Keep track of the start value and the conversion factor to
// seconds.
bool timeInitialized = false;
LARGE_INTEGER start_hpc;
double hpc_freq = 1.0;

double start_time;

/// Static constant to remove time skew between FILETIME and POSIX
/// time.  POSIX and Win32 use different epochs (Jan. 1, 1970 v.s.
/// Jan. 1, 1601).  The following constant defines the difference
/// in 100ns ticks.
const DWORDLONG FILETIME_to_timval_skew = 0x19db1ded53e8000;

}

namespace qpid {
namespace sys {

AbsTime::AbsTime(const AbsTime& t, const Duration& d) {
    if (d == Duration::max()) {
        timepoint = ptime(max_date_time);
    }
    else {
        time_duration td = microseconds(d.nanosecs / 1000);
        timepoint = t.timepoint + td;
    }
}

AbsTime AbsTime::FarFuture() {
    AbsTime ff;
    ptime maxd(max_date_time);
    ff.timepoint = maxd;
    return ff;
}

AbsTime AbsTime::Zero() {
    AbsTime time_epoch;
    time_epoch.timepoint = boost::posix_time::from_time_t(0);
    return time_epoch;
}

AbsTime AbsTime::epoch() {
    AbsTime time_epoch;
    time_epoch.timepoint = boost::posix_time::from_time_t(0);
    return time_epoch;
}

AbsTime AbsTime::now() {
    AbsTime time_now;
    time_now.timepoint = boost::get_system_time();
    return time_now;
}

Duration Duration::FromEpoch() {
    time_duration d = boost::get_system_time() - boost::posix_time::from_time_t(0);
    return d.total_nanoseconds();
}

Duration::Duration(const AbsTime& start, const AbsTime& finish) {
    time_duration d = finish.timepoint - start.timepoint;
    nanosecs = d.total_nanoseconds();
}

std::ostream& operator<<(std::ostream& o, const Duration& d) {
    if (d >= TIME_SEC) return o << (double(d)/TIME_SEC) << "s";
    if (d >= TIME_MSEC) return o << (double(d)/TIME_MSEC) << "ms";
    if (d >= TIME_USEC) return o << (double(d)/TIME_USEC) << "us";
    return o << int64_t(d) << "ns";
}

std::istream& operator>>(std::istream& i, Duration& d) {
    // Don't throw, let the istream throw if it's configured to do so.
    double number;
    i >> number;
    if (i.fail()) return i;

    if (i.eof() || std::isspace(i.peek())) // No suffix
        d = number*TIME_SEC;
    else {
        std::string suffix;
        i >> suffix;
        if (i.fail()) return i;
        if (suffix.compare("s") == 0) d = number*TIME_SEC;
        else if (suffix.compare("ms") == 0) d = number*TIME_MSEC;
        else if (suffix.compare("us") == 0) d = number*TIME_USEC;
        else if (suffix.compare("ns") == 0) d = number*TIME_NSEC;
        else i.setstate(std::ios::failbit);
    }
    return i;
}

std::ostream& operator<<(std::ostream& o, const AbsTime& t) {
    std::string time_string = to_simple_string(t.timepoint);
    return o << time_string;
}


void sleep(int secs) {
    ::Sleep(secs * 1000);
}

void usleep(uint64_t usecs) {
    DWORD msecs = usecs / 1000;
    if (msecs == 0)
        msecs = 1;
    ::Sleep(msecs);
}

void outputFormattedNow(std::ostream& o) {
    ::time_t rawtime;
    ::tm timeinfo;
    char time_string[100];

    ::time( &rawtime );
#ifdef _MSC_VER
    ::localtime_s(&timeinfo, &rawtime);
#else
    timeinfo = *(::localtime(&rawtime));
#endif
    ::strftime(time_string, 100,
               "%Y-%m-%d %H:%M:%S",
               &timeinfo);
    o << time_string << " ";
}

void outputHiresNow(std::ostream& o) {
    ::time_t tv_sec;
    ::tm timeinfo;
    char time_string[100];

    if (!timeInitialized) {
        // To start, get the current time from FILETIME which includes
        // sub-second resolution. However, since FILETIME is updated a bit
        // "bumpy" every 15 msec or so, future time displays will be the
        // starting FILETIME plus a delta based on the high-resolution
        // performance counter.
        FILETIME file_time;
        ULARGE_INTEGER start_usec;
        ::GetSystemTimeAsFileTime(&file_time);   // This is in 100ns units
        start_usec.LowPart = file_time.dwLowDateTime;
        start_usec.HighPart = file_time.dwHighDateTime;
        start_usec.QuadPart -= FILETIME_to_timval_skew;
        start_usec.QuadPart /= 10;   // Convert 100ns to usec
        tv_sec = (time_t)(start_usec.QuadPart / (1000 * 1000));
        long tv_usec = (long)(start_usec.QuadPart % (1000 * 1000));
        start_time = static_cast<double>(tv_sec);
        start_time += tv_usec / 1000000.0;

        start_hpc.QuadPart = 0;
        LARGE_INTEGER iFreq;
        iFreq.QuadPart = 1;
        QueryPerformanceCounter(&start_hpc);
        QueryPerformanceFrequency(&iFreq);
        hpc_freq = static_cast<double>(iFreq.QuadPart);
        timeInitialized = true;
    }
    LARGE_INTEGER hpc_now;
    hpc_now.QuadPart = 0;
    QueryPerformanceCounter(&hpc_now);
    hpc_now.QuadPart -= start_hpc.QuadPart;
    if (hpc_now.QuadPart < 0)
        hpc_now.QuadPart = 0;
    double now = static_cast<double>(hpc_now.QuadPart);
    now /= hpc_freq;                 // now is seconds after this
    double fnow = start_time + now;
    double usec, sec;
    usec = modf(fnow, &sec);
    tv_sec = static_cast<time_t>(sec);
#ifdef _MSC_VER
    ::localtime_s(&timeinfo, &tv_sec);
#else
    timeinfo = *(::localtime(&tv_sec));
#endif
    ::strftime(time_string, 100,
               "%Y-%m-%d %H:%M:%S",
               &timeinfo);
    // No way to set "max field width" to cleanly output the double usec so
    // convert it back to integral number of usecs and print that.
    unsigned long i_usec = usec * 1000 * 1000;
    o << time_string << "." << std::setw(6) << std::setfill('0') << i_usec << " ";
}
}}