| 1 |
// -*- C++ -*- Time-stamp: <2011-05-02 12:27:16 ptr> |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2002, 2006-2011 |
| 5 |
* Petr Ovtchenkov |
| 6 |
* |
| 7 |
* This material is provided "as is", with absolutely no warranty expressed |
| 8 |
* or implied. Any use is at your own risk. |
| 9 |
* |
| 10 |
* Permission to use or copy this software for any purpose is hereby granted |
| 11 |
* without fee, provided the above notices are retained on all copies. |
| 12 |
* Permission to modify the code and to distribute modified code is granted, |
| 13 |
* provided the above notices are retained, and a notice that the code was |
| 14 |
* modified is included with the above copyright notice. |
| 15 |
* |
| 16 |
* Derived from original chrono.cc of 'complement' project |
| 17 |
* [http://complement.sourceforge.net] |
| 18 |
* to make it close to JTC1/SC22/WG21 C++ 0x working draft |
| 19 |
* [http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2011/n3291.pdf] |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "stlport_prefix.h" |
| 23 |
|
| 24 |
#include <chrono> |
| 25 |
|
| 26 |
_STLP_BEGIN_NAMESPACE |
| 27 |
|
| 28 |
namespace chrono { |
| 29 |
|
| 30 |
const bool system_clock::is_steady = false; |
| 31 |
|
| 32 |
system_clock::time_point system_clock::now() /* nothrow */ throw() |
| 33 |
{ |
| 34 |
//timeval tv; |
| 35 |
//::gettimeofday( &tv, 0 ); |
| 36 |
//return system_time( tv.tv_sec * nanoseconds::ticks_per_second + tv.tv_usec * 1000LL, system_time::_adopt_t() ); |
| 37 |
|
| 38 |
#ifdef __unix |
| 39 |
::timespec tv; |
| 40 |
clock_gettime( CLOCK_REALTIME, &tv ); |
| 41 |
|
| 42 |
return time_point( duration( tv.tv_nsec + tv.tv_sec * period::den ) /* seconds(tv.tv_sec) + nanoseconds(tv.tv_nsec) */ ); |
| 43 |
#elif defined( WIN32 ) |
| 44 |
union { |
| 45 |
FILETIME ft; // 100 ns intervals since Jan 1 1601 (UTC) |
| 46 |
__int64 t; |
| 47 |
} ft; |
| 48 |
GetSystemTimeAsFileTime( &ft.ft ); |
| 49 |
|
| 50 |
return time_point( duration( ft.t - 11644473600LL) ); // 60 * 60 * 24 * 134774, 1970 - 1601 |
| 51 |
#else |
| 52 |
# error "You should implement OS-dependent precise clock" |
| 53 |
#endif |
| 54 |
} |
| 55 |
|
| 56 |
time_t system_clock::to_time_t( const system_clock::time_point& t ) /* noexcept */ throw() |
| 57 |
{ |
| 58 |
#ifdef __unix |
| 59 |
return t.time_since_epoch().count() / period::den; |
| 60 |
#elif defined( WIN32 ) |
| 61 |
#else |
| 62 |
# error "You should implement OS-dependent precise clock" |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
system_clock::time_point system_clock::from_time_t( time_t t ) /* noexcept */ throw() |
| 67 |
{ |
| 68 |
#ifdef __unix |
| 69 |
return time_point( duration_cast<duration>( seconds(t) ) ); |
| 70 |
#elif defined( WIN32 ) |
| 71 |
#else |
| 72 |
# error "You should implement OS-dependent precise clock" |
| 73 |
#endif |
| 74 |
} |
| 75 |
|
| 76 |
const bool steady_clock::is_steady = true; |
| 77 |
|
| 78 |
steady_clock::time_point steady_clock::now() /* nothrow */ throw() |
| 79 |
{ |
| 80 |
#ifdef __unix |
| 81 |
::timespec tv; |
| 82 |
clock_gettime( CLOCK_MONOTONIC, &tv ); |
| 83 |
|
| 84 |
return time_point( duration( tv.tv_nsec + tv.tv_sec * period::den ) /* seconds(tv.tv_sec) + nanoseconds(tv.tv_nsec) */ ); |
| 85 |
#elif defined( WIN32 ) |
| 86 |
#else |
| 87 |
# error "You should implement OS-dependent precise clock" |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
} // namespace chrono |
| 92 |
|
| 93 |
_STLP_END_NAMESPACE |