// ********************************************************************** // // Copyright (c) 2003 - 2004 // ZeroC, Inc. // North Palm Beach, FL, USA // // All Rights Reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #include #ifdef _WIN32 # include # include #else # include #endif using namespace IceUtil; Time::Time() : _usec(0) { } Time IceUtil::Time::now() { #ifdef WIN32 struct _timeb tb; _ftime(&tb); return Time(tb.time * static_cast(1000000) + tb.millitm * static_cast(1000)); #else struct timeval tv; gettimeofday(&tv, 0); return Time(tv.tv_sec * static_cast(1000000) + tv.tv_usec); #endif } Time IceUtil::Time::seconds(Int64 t) { return Time(t * static_cast(1000000)); } Time IceUtil::Time::milliSeconds(Int64 t) { return Time(t * static_cast(1000)); } Time IceUtil::Time::microSeconds(Int64 t) { return Time(t); } IceUtil::Time::operator timeval() const { timeval tv; tv.tv_sec = static_cast(_usec / 1000000); tv.tv_usec = static_cast(_usec % 1000000); return tv; } IceUtil::Time::operator double() const { return _usec / 1000000.0L; } Int64 IceUtil::Time::toSeconds() const { return _usec / 1000000; } Int64 IceUtil::Time::toMilliSeconds() const { return _usec / 1000; } Int64 IceUtil::Time::toMicroSeconds() const { return _usec; } std::string IceUtil::Time::toString() const { time_t time = static_cast(_usec / 1000000); struct tm* t; #ifdef _WIN32 t = localtime(&time); #else struct tm tr; localtime_r(&time, &tr); t = &tr; #endif char buf[32]; strftime(buf, sizeof(buf), "%x %H:%M:%S", t); std::ostringstream os; os << buf << ":"; os.fill('0'); os.width(3); os << static_cast(_usec % 1000000 / 1000); return os.str(); } Time::Time(Int64 usec) : _usec(usec) { }