// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// Time represents an absolute point in time, internally represented as
-// microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC)
-// (See http://crbug.com/14734). System-dependent clock interface routines are
-// defined in time_PLATFORM.cc.
+// Time represents an absolute point in coordinated universal time (UTC),
+// internally represented as microseconds (s/1,000,000) since the Windows epoch
+// (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent
+// clock interface routines are defined in time_PLATFORM.cc.
//
// TimeDelta represents a duration of time, internally represented in
// microseconds.
//
// These classes are represented as only a 64-bit value, so they can be
// efficiently passed by value.
+//
+// Definitions of operator<< are provided to make these types work with
+// DCHECK_EQ() and other log macros. For human-readable formatting, see
+// "base/i18n/time_formatting.h".
#ifndef BASE_TIME_TIME_H_
#define BASE_TIME_TIME_H_
#include <time.h>
+#include <iosfwd>
+
#include "base/base_export.h"
#include "base/basictypes.h"
#include "build/build_config.h"
static TimeDelta FromMinutes(int minutes);
static TimeDelta FromSeconds(int64 secs);
static TimeDelta FromMilliseconds(int64 ms);
+ static TimeDelta FromSecondsD(double secs);
+ static TimeDelta FromMillisecondsD(double ms);
static TimeDelta FromMicroseconds(int64 us);
#if defined(OS_WIN)
static TimeDelta FromQPCValue(LONGLONG qpc_value);
return TimeDelta(a * td.delta_);
}
+// For logging use only.
+BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta);
+
// Time -----------------------------------------------------------------------
-// Represents a wall clock time.
+// Represents a wall clock time in UTC.
class BASE_EXPORT Time {
public:
static const int64 kMillisecondsPerSecond = 1000;
static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
kMicrosecondsPerSecond;
+ // The representation of Jan 1, 1970 UTC in microseconds since the
+ // platform-dependent epoch.
+ static const int64 kTimeTToMicrosecondsOffset;
+
#if !defined(OS_WIN)
// On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
// the Posix delta of 1970. This is used for migrating between the old
// this global header and put in the platform-specific ones when we remove the
// migration code.
static const int64 kWindowsEpochDeltaMicroseconds;
+#else
+ // To avoid overflow in QPC to Microseconds calculations, since we multiply
+ // by kMicrosecondsPerSecond, then the QPC value should not exceed
+ // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply.
+ static const int64 kQPCOverflowThreshold = 0x8637BD05AF7;
#endif
// Represents an exploded time that can be formatted nicely. This is kind of
// treat it as static across all windows versions.
static const int kMinLowResolutionThresholdMs = 16;
- // Enable or disable Windows high resolution timer. If the high resolution
- // timer is not enabled, calls to ActivateHighResolutionTimer will fail.
- // When disabling the high resolution timer, this function will not cause
- // the high resolution timer to be deactivated, but will prevent future
- // activations.
- // Must be called from the main thread.
- // For more details see comments in time_win.cc.
+ // Enable or disable Windows high resolution timer.
static void EnableHighResolutionTimer(bool enable);
// Activates or deactivates the high resolution timer based on the |activate|
bool is_local,
Time* parsed_time);
- // The representation of Jan 1, 1970 UTC in microseconds since the
- // platform-dependent epoch.
- static const int64 kTimeTToMicrosecondsOffset;
-
-#if defined(OS_WIN)
- // Indicates whether fast timers are usable right now. For instance,
- // when using battery power, we might elect to prevent high speed timers
- // which would draw more power.
- static bool high_resolution_timer_enabled_;
- // Count of activations on the high resolution timer. Only use in tests
- // which are single threaded.
- static int high_resolution_timer_activated_;
-#endif
-
// Time in microseconds in UTC.
int64 us_;
};
}
// static
+inline TimeDelta TimeDelta::FromSecondsD(double secs) {
+ // Preserve max to prevent overflow.
+ if (secs == std::numeric_limits<double>::infinity())
+ return Max();
+ return TimeDelta(static_cast<int64>(secs * Time::kMicrosecondsPerSecond));
+}
+
+// static
+inline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
+ // Preserve max to prevent overflow.
+ if (ms == std::numeric_limits<double>::infinity())
+ return Max();
+ return TimeDelta(static_cast<int64>(ms * Time::kMicrosecondsPerMillisecond));
+}
+
+// static
inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
// Preserve max to prevent overflow.
if (us == std::numeric_limits<int64>::max())
return Time(t.us_ + delta_);
}
+// For logging use only.
+BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time);
+
// TimeTicks ------------------------------------------------------------------
class BASE_EXPORT TimeTicks {
public:
+ // We define this even without OS_CHROMEOS for seccomp sandbox testing.
+#if defined(OS_LINUX)
+ // Force definition of the system trace clock; it is a chromeos-only api
+ // at the moment and surfacing it in the right place requires mucking
+ // with glibc et al.
+ static const clockid_t kClockSystemTrace = 11;
+#endif
+
TimeTicks() : ticks_(0) {
}
// This is only for testing.
static bool IsHighResClockWorking();
- // Enable high resolution time for TimeTicks::Now(). This function will
- // test for the availability of a working implementation of
- // QueryPerformanceCounter(). If one is not available, this function does
- // nothing and the resolution of Now() remains 1ms. Otherwise, all future
- // calls to TimeTicks::Now() will have the higher resolution provided by QPC.
- // Returns true if high resolution time was successfully enabled.
- static bool SetNowIsHighResNowIfSupported();
-
// Returns a time value that is NOT rollover protected.
static TimeTicks UnprotectedNow();
#endif
return TimeTicks(t.ticks_ + delta_);
}
+// For logging use only.
+BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
+
} // namespace base
#endif // BASE_TIME_TIME_H_