| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "proton/timestamp.hpp" |
| 21 |
|
| 22 |
#include <proton/error.hpp> |
| 23 |
#include <proton/proactor.h> |
| 24 |
#include <proton/types.h> |
| 25 |
|
| 26 |
#include <iostream> |
| 27 |
|
| 28 |
// Can't use std::chrono since we still support C++03, sigh. |
| 29 |
|
| 30 |
#ifdef WIN32 |
| 31 |
#include <windows.h> |
| 32 |
|
| 33 |
#else |
| 34 |
#include <sys/time.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
namespace proton { |
| 38 |
|
| 39 |
#ifdef WIN32 |
| 40 |
|
| 41 |
timestamp timestamp::now() { |
| 42 |
FILETIME now; |
| 43 |
GetSystemTimeAsFileTime(&now); |
| 44 |
ULARGE_INTEGER t; |
| 45 |
t.u.HighPart = now.dwHighDateTime; |
| 46 |
t.u.LowPart = now.dwLowDateTime; |
| 47 |
// Convert to milliseconds and adjust base epoch |
| 48 |
return timestamp(t.QuadPart / 10000 - 11644473600000); |
| 49 |
} |
| 50 |
|
| 51 |
#else |
| 52 |
|
| 53 |
timestamp timestamp::now() { |
| 54 |
struct timeval now; |
| 55 |
if (::gettimeofday(&now, NULL)) throw proton::error("gettimeofday failed"); |
| 56 |
return timestamp(int64_t(now.tv_sec) * 1000 + (now.tv_usec / 1000)); |
| 57 |
} |
| 58 |
|
| 59 |
#endif |
| 60 |
|
| 61 |
std::ostream& operator<<(std::ostream& o, timestamp ts) { return o << ts.milliseconds(); } |
| 62 |
|
| 63 |
} |